我们已经准备好了,你呢?

2023我们与您携手共赢,为您的企业形象保驾护航!


喜讯:国内、香港、海外云服务器租用特惠活动,2核/4G/10M仅需31元每月,点击抢购>>>

点击这里点击这里进入阿里云ECS官方页面,立即免费体验云服务器ECS>>>

阿里云服务器ECS使用教程-部署LAMP环境(CentOS 7)

本文介绍如何使用云服务器ECS实例搭建LAMP平台,其中LAMP分别代表Linux、Apache、MySQL和PHP。

前提条件

  • 已创建ECS实例并为实例分配公网IP地址。具体操作,请参见创建方式导航。

    本文示例步骤中,使用以下配置的ECS实例。实际操作时,建议您保持操作系统版本一致,避免因操作系统版本问题导致的命令执行报错。

    • 实例规格:ecs.c6.large

    • 操作系统:公共镜像CentOS 7.8 64位

    • 网络类型:专有网络VPC

    • IP地址:公网IP

  • 已在实例安全组的入方向添加安全组规则并放行22、80端口。具体操作请参见添加安全组规则。


背景信息

本文适用于熟悉Linux操作系统,初次使用阿里云进行建站的个人用户。在示例步骤中使用了以下版本的软件。操作时,请您以实际软件版本为准。

  • Apache:2.4.6

  • MySQL:5.7.31

  • PHP:7.0.33

  • phpMyAdmin:4.0.10.20

本文主要说明手动安装LAMP平台的操作步骤,您也可以在云市场购买LAMP镜像直接启动ECS,以便快速建站。



点我去体验


步骤一:准备工作

  1. 使用向导创建实例。

  2. 通过密码或密钥认证登录Linux实例

  3. 运行命令cat /etc/redhat-release查看系统版本。

    CentOS 7.8

  4. 关闭防火墙。

    • 如果您想临时关闭防火墙,运行命令systemctl stop firewalld

      说明

      这只是暂时关闭防火墙,下次重启Linux后,防火墙还会开启。

    • 如果您想永久关闭防火墙,运行命令systemctl disable firewalld

      说明

      如果您想重新开启防火墙,请参见firewalld官网信息。

    • 如果防火墙的状态参数是inactive,则防火墙为关闭状态。

    • 如果防火墙的状态参数是active,则防火墙为开启状态。本示例中防火墙为开启状态,因此需要关闭防火墙。

    1. 运行systemctl status firewalld命令查看当前防火墙的状态。

      查看防火墙状态

    2. 关闭防火墙。如果防火墙为关闭状态,请忽略此步骤。

  5. 关闭SELinux。

    • 如果您想临时关闭SELinux,运行命令setenforce 0

      说明

      这只是暂时关闭SELinux,下次重启Linux后,SELinux还会开启。

    • 如果您想永久关闭SELinux,运行命令vi /etc/selinux/config编辑SELinux配置文件。回车后,把光标移动到SELINUX=enforcing这一行,按i键,将其修改为SELINUX=disabled, 按Esc键,然后输入:wq并回车以保存并关闭SELinux配置文件。 最后重启系统使设置生效。

      说明

      如果您想重新开启SELinux,请参见SELinux的官方文档。

    • 如果SELinux状态参数是Disabled, 则SELinux为关闭状态。

    • 如果SELinux状态参数是Enforcing,则SELinux为开启状态。本示例中SELinux为开启状态,因此需要关闭SELinux。

    1. 运行getenforce命令查看SELinux的当前状态。

      查看SELinux状态

    2. 关闭SELinux。如果SELinux为关闭状态,请忽略此步骤。

步骤二:安装Apache

  1. 运行以下命令安装Apache服务及扩展包。

    yum -y install httpd httpd-manual mod_ssl mod_perl
  2. 运行httpd -v命令可查看Apache的版本号。

    httpd -v

  3. 依次运行以下命令启动Apache服务并设置服务开机自启动。

    1. systemctl start httpd
    2. systemctl enable httpd
  4. 查看安装结果。

    1. 登录ECS管理控制台。

    2. 在左侧导航栏,单击实例与镜像 > 实例

    3. 实例列表中找到正在部署环境的实例,从该实例的IP地址中复制公网IP。

    4. 在本地机器的浏览器地址栏中,输入http://实例公网IP并按Enter键。

      若返回页面如下图所示,说明Apache服务启动成功。httpd

步骤三:安装并配置MySQL

  1. 运行以下命令更新YUM源。

    rpm -Uvh  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
  2. 运行以下命令安装MySQL。

    说明

    如果您使用的操作系统内核版本为el8,可能会提示报错信息No match for argument。您需要先运行命令yum module disable mysql禁用默认的MySQL模块,再安装MySQL。

    yum -y install mysql-community-server --nogpgcheck
  3. 运行以下命令查看MySQL版本号。

    mysql -V

    返回结果如下所示,表示MySQL安装成功。

    mysql  Ver 14.14 Distrib 5.7.31, for Linux (x86_64) using  EditLine wrapper
  4. 运行以下命令启动MySQL。

    systemctl start mysqld
  5. 依次运行以下命令设置开机启动MySQL。

    systemctl enable mysqldsystemctl daemon-reload
  6. 运行以下命令查看MySQL的初始密码。

    grep "password" /var/log/mysqld.log

    返回结果示例如下,本示例中初始密码为+47,uijcojcU

    2020-08-28T03:01:49.848762Z 1 [Note] A temporary password is generated for root@localhost: +47,uijcojcU
  7. 运行以下命令配置MySQL的安全性。

    mysql_secure_installation

    安全性的配置包含以下五个方面:

    1. 重置root账号的密码。

      说明

      请您安全保管root账号的密码信息。

      Enter password for user root: #输入上一步获取的root用户初始密码。
      
      The existing password for the user account root has expired. Please set a new password.New password: #输入新密码,长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
      
      Re-enter new password: #重复输入新密码。
      The 'validate_password' plugin is installed on the server.
      The subsequent steps will run with the existing configurationof the plugin.Using existing password for root.
      
      Estimated strength of the password: 100Change the password for root ? ((Press y|Y for Yes, any other key for No) :Y #按Y,并再次输入上步已设置的密码。New password: #再次输入新密码。
      
      Re-enter new password: #重复输入新密码。
      
      Estimated strength of the password: 100Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :Y #按Y使用新密码。
    2. 输入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 fortesting, 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.
    3. 输入Y禁止root账号远程登录。

      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.
    4. 输入Y删除test库以及对test库的访问权限。

      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.
    5. 输入Y重新加载授权表。

      Reloading t
      推荐:TOP云智能建站优惠活动,仅880元即可搭建一个后台管理五端合一的智能网站(PC网站、手机网站、百度智能小程序、微信小程序、支付宝小程序),独享百度搜索SEO优势资源,让你的网站不仅有颜值有排面,更有排名,可以实实在在为您带来效益,请点击进入TOP云智能建站>>>,或咨询在线客服了解详情。


我们已经准备好了,你呢?

2023我们与您携手共赢,为您的企业形象保驾护航!

在线客服
联系方式

热线电话

131-3501-0006

上班时间

周一到周五

公司电话

130-0743-9476

二维码
线