更新时间:2026-07-08 GMT+08:00
分享

手工搭建LAMP环境(CentOS 8)

简介

LAMP环境是常用的Web开发环境之一,其中LAMP分别代表Linux、Apache、MySQL和PHP。

本文介绍如何使用弹性云服务器的Linux实例手工搭建LAMP平台的web环境,具体操作以CentOS 8.0 64位操作系统为例。

前提条件

  • 实例内存大于等于4 GiB。
  • 弹性云服务器已绑定弹性公网IP。具体步骤请参见绑定弹性公网IP
  • 弹性云服务器所在安全组添加了如下表所示的安全组规则,具体步骤参见为安全组添加安全组规则
    表1 安全组规则

    方向

    优先级

    策略

    类型

    协议端口

    源地址

    入方向

    1

    允许

    IPv4

    TCP: 80

    0.0.0.0/0

资源规划

表2 资源和成本规划

资源

资源说明

成本说明

弹性云服务器

  • 计费模式:按需计费
  • 可用区:可用区1
  • 规格:ac9.large.2
  • 镜像:CentOS 8.0 64bit
  • 系统盘:40GiB
  • 弹性公网IP:现在购买
  • 线路:全动态BGP
  • 公网带宽:按流量计费
  • 带宽大小:5 Mbit/s

ECS涉及以下几项费用:

  • 云服务器
  • 云硬盘
  • 弹性公网IP

具体的计费方式及标准请参考计费模式概述

Apache

是一个开放源码的Web服务器。

免费

MySQL

是一款开源的关系数据库软件。

免费

PHP

是一款开源软件,用于Web开发。

免费

CentOS部署LAMP环境操作流程

ECS实例基于CentOS部署操作步骤如下:

  1. 安装Apache
  2. 安装MySQL
  3. 安装PHP
  4. 浏览器访问测试。

操作步骤

  1. 登录弹性云服务器。

    具体操作请参见Linux ECS登录方式概述

  2. 安装Apache。

    1. 执行以下命令,安装Apache。
      sudo dnf install -y httpd
    2. 执行以下命令,设置开机自启。
      sudo systemctl enable httpd
    3. 执行以下命令,启动Apache服务器。
      sudo systemctl start httpd
    4. 执行以下命令,验证Apache服务器是否安装成功。
      httpd -v
    5. 回显如下版本信息,表示Apache安装成功。

    6. 使用浏览器访问 “http://服务器IP地址”,显示如下页面,说明Apache安装成功。

  3. 安装MySQL。

    1. 执行以下命令,安装MySQL。
      sudo dnf -y install @mysql
    2. 执行以下命令,查看MySQL版本。
      mysql -V

      回显如下版本信息,表示MySQL安装成功。

    3. 依次执行以下命令,启动MySQL服务并设置开机自启。
      systemctl start mysqld
      systemctl enable mysqld
    4. 执行以下命令,查看MySQL运行状态。
      systemctl status mysqld.service

      回显如下信息,表示MySQL运行中。

    5. 执行以下命令,并按照回显提示信息进行操作,加固MySQL。
      mysql_secure_installation
      Securing the MySQL server deployment.
      
      Connecting to MySQL using a blank password.
      
      VALIDATE PASSWORD COMPONENT can be used to test passwords
      and improve security. It checks the strength of password
      and allows the users to set only those passwords which are
      secure enough. Would you like to setup VALIDATE PASSWORD component?
      
      Press y|Y for Yes, ant other key for No: Y    #设置密码验证策略,输入Y
      
      There are three levels of password validation policy:
      
      LOW    Length >= 8
      MEDIUM Length >= 8, numeric, mixed case, and special characters
      STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file
      
      Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2       #选择密码验证策略,输入2
      Please set the password for root here.
      
      New password:  #设置新的root用户密码
      
      Re-enter new password:   #再次输入密码
      
      Estimated strength of the password: 100
      Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y  #确认使用已设置的密码,输入Y
      By default, a MySQL installation has an anonymous user,
      allowing anyone to log into MySQL without having to have
      a user account created for them. This is intended only for
      testing, and to make the installation go a bit smoother.
      You should remove them before moving into a production
      environment.
      
      Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y   #是否删除匿名用户,输入Y
      Success.
      
      
      Normally, root should only be allowed to connect from
      'localhost'. This ensures that someone cannot guess at
      the root password from the network.
      
      Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y   #禁止root远程登录,输入Y
      Success.
      
      By default, MySQL comes with a database named 'test' that
      anyone can access. This is also intended only for testing,
      and should be removed before moving into a production
      environment.
      
      
      Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y   #是否删除test库和对它的访问权限,输入Y
       - Dropping test database...
      Success.
      
       - Removing privileges on test database...
      Success.
      
      Reloading the privilege tables will ensure that all changes
      made so far will take effect immediately.
      
      Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y   #是否重新加载授权表,输入Y
      Success.
      
      All done!

  4. 安装PHP。

    1. 执行以下命令,安装并更新epel源。
      sudo dnf -y install epel-release
      sudo dnf -y update epel-release
    2. 执行以下命令,启动PHP服务并设置开机自启动。
      systemctl start php-fpm
      systemctl enable php-fpm
    3. 执行以下命令,安装PHP以及对应模块。
      sudo dnf -y install php php-curl php-dom php-exif php-fileinfo php-fpm php-gd php-hash php-json php-mbstring php-mysqli php-openssl php-pcre php-xml libsodium
    4. 执行以下命令,验证PHP的安装版本。
      php -v
    5. 回显如下图所示,表示PHP安装成功。

  5. 浏览器访问测试。

    1. 在/var/www/html/目录下创建“info.php”的测试页面。
      vim /var/www/html/info.php
    2. i键进入编辑模式。
    3. 修改打开的“info.php”文件,将如下内容写入文件。
      <?
        php  phpinfo(); 
      ?>
    4. 按Esc键退出编辑模式,并输入:wq保存后退出。
    5. 使用浏览器访问“http://服务器IP地址/info.php”,显示如下页面,说明环境搭建成功。

后续操作

完成LAMP环境搭建后,您可以参考以下文档进行网站或应用搭建。

常见问题

  1. 无法通过弹性公网IP访问info.php页面

    可能原因:安全组未放行80端口、系统防火墙开启、80端口被占用。

    处理方法:请参考云服务器端口不通怎样排查?进行排查。

  2. 如何设置MySQL远程访问

    创建非root用户远程登录MySQL的账号,并允许远程主机使用该账号访问MySQL。具体操作,请参见Linux系统中部署MySQL数据库

相关文档