文档

部署LAMP环境(Ubuntu)

更新时间:

LAMP环境是常用的Web开发环境之一,其中LAMP分别代表Linux、Apache、MySQL和PHP。本文介绍如何在Ubuntu操作系统的ECS实例内部署LAMP环境。

准备工作

创建用于部署LAMP环境的ECS实例,具体操作,请参见自定义购买实例

该实例必须满足以下条件:

  • 实例已分配公网IP地址或绑定弹性公网IP(EIP)。具体操作,请参见绑定和解绑弹性公网IP

  • 操作系统:Ubuntu 22.04、Ubuntu 20.04、Ubuntu 18.04。

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

步骤一:安装Apache

  1. 运行以下命令,更新Ubuntu系统内的软件包。

    sudo apt update
  2. 运行以下命令,安装Apache。

    sudo apt-get -y install apache2
  3. 运行以下命令,查看Apache版本。

    apache2 -v

    返回结果类似如下所示,表示Apache已成功安装,Apache的版本为2.4.52。

    image.png
  4. 运行以下命令,启动Apache服务并设置服务开机自启动。

    sudo systemctl start apache2
    sudo systemctl enable apache2
  5. 运行以下命令,查看Apache服务状态。

    sudo systemctl status apache2

    返回结果类似如下所示,表示Apache已启动。

    image.png

步骤二:安装并配置MySQL

  1. 安装MySQL。

    1. 运行以下命令,安装MySQL。

      sudo apt -y install mysql-server
    2. 运行以下命令,查看MySQL版本。

      mysql -V

      返回结果类似如下所示,表示MySQL已成功安装,MySQL的版本为8.0.35。

      image.png
  2. 运行以下命令,启动MySQL服务。

    sudo systemctl start mysql
  3. 依次运行以下命令,设置开机启动MySQL。

    sudo systemctl enable mysql
    sudo systemctl daemon-reload
  4. 配置MySQL。

    1. 运行以下命令,进入MySQL。

      sudo mysql
    2. 运行以下命令,设置root用户密码。

      ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'mynewpassword';

      本示例中密码以Mysql@1234为例,示例命令:

      ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'Mysql@1234';
    3. 运行以下命令,退出MySQL数据库。

      exit;
    4. 运行以下命令,对MySQL进行安全性配置。

      sudo mysql_secure_installation
    5. 根据命令行提示,依次完成以下配置项。

      1. 输入root用户的密码。本示例中输入Mysql@1234

        ecs-user@iZbp19jsi7s0g7m4zgc****:~# sudo mysql_secure_installation
        
        Securing the MySQL server deployment.
        
        Enter password for user root: 
        说明

        在输入密码时,系统为了最大限度的保证数据安全,命令行将不做任何回显。您只需要输入正确的密码信息,然后按Enter键即可。

      2. 输入Y,设置密码验证策略。

        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, any other key for No: Y
      3. 选择密码验证策略。

        本示例输入2。

        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: 0
      4. 输入Y,更改root用户密码。

        Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y
      5. 输入root用户密码。

        New password:
        
        Re-enter new password:
        
        Estimated strength of the password: 100
      6. 输入Y,确认使用已设置的密码。

        Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
      7. 输入Y,删除MySQL自带的匿名用户。

        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
      8. 输入Y,禁止MySQL的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
      9. 输入Y,移除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) : 
      10. 输入Y,重新加载授权表。

        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

        当命令行回显All done!时,表示配置完成。

  5. 测试登录MySQL数据库。

    1. 运行以下命令,登录MySQL数据库。

      sudo mysql -uroot -p
    2. 在命令行回显的Enter password:后输入已设置的数据库密码。

      说明

      在输入密码时,系统为了最大限度的保证数据安全,命令行将不做任何回显。您只需要输入正确的密码信息,然后按Enter键即可。

      成功登录MySQL数据库后,命令行信息如下所示。

      ecs-user@iZbp19jsi7s0g7m4zgc****:~# sudo mysql -uroot -p
      Enter password:
      Welcome to the MySQL monitor.  Commands end with ; or \g.
      Your MySQL connection id is 15
      Server version: 8.0.29-0ubuntu0.20.04.3 (Ubuntu)
      
      Copyright (c) 2000, 2022, Oracle and/or its affiliates.
      
      Oracle is a registered trademark of Oracle Corporation and/or its
      affiliates. Other names may be trademarks of their respective
      owners.
      
      Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
      
      mysql>
    3. 运行以下命令,退出MySQL数据库。

      exit;

步骤三:安装PHP

  1. 依次运行以下命令,安装PHP。

    sudo apt -y install php-fpm
    sudo apt -y install libapache2-mod-php
  2. 运行以下命令,查看PHP版本。

    php -v

    返回结果类似如下所示,表示PHP已成功安装,PHP的版本为7.4.3。

    image.png
  3. 在Apache网站根目录中,创建测试网页。

    1. 运行以下命令,查看Apache网站根目录的路径信息。

      sudo cat /etc/apache2/sites-available/000-default.conf

      命令行返回结果中的DocumentRoot /var/www/html一行,即表示网站根目录为/var/www/html

    2. 运行以下命令,在网站根目录创建测试网页,并把phpinfo()函数添加至网页内容中。

      phpinfo()函数会展示PHP的所有配置信息。

      sudo sh -c 'echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php'
  4. 运行以下命令,重启Apache服务。

    sudo systemctl restart apache2
  5. 在本地机器浏览器地址栏,输入http://<ECS实例公网IP地址>/phpinfo.php并按Enter键。

    显示如下页面表示安装成功。

    image.png
  6. 成功搭建LAMP环境后,建议您删除phpinfo.php测试文件,消除数据泄露风险。

    sudo rm -rf <网站根目录>/phpinfo.php

    本教程中网站根目录为/var/www/html,则需要运行以下命令删除测试文件。

    sudo rm -rf /var/www/html/phpinfo.php

(可选)步骤四:安装phpMyAdmin

phpMyAdmin是一个MySQL数据库管理工具,通过Web接口管理数据库方便快捷。

  1. 运行以下命令,安装phpMyAdmin所需的依赖。

    sudo apt install -y php-cli php-mbstring php-mysql unzip 
  2. 运行以下命令,创建phpMyAdmin数据存放目录。

    sudo mkdir -p /var/www/html/phpmyadmin
  3. 运行以下命令,下载phpMyAdmin压缩包并解压。

    1. 依次运行以下命令,返回系统主目录并下载phpMyAdmin压缩包。

      cd
      wget --no-check-certificate https://files.phpmyadmin.net/phpMyAdmin/4.9.11/phpMyAdmin-4.9.11-all-languages.zip
    2. 依次运行以下命令,解压phpMyAdmin压缩包。

      sudo unzip phpMyAdmin-4.9.11-all-languages
  4. 运行以下命令,将phpMyAdmin文件复制到准备好的数据存放目录。

    sudo mv phpMyAdmin-4.9.11-all-languages/*  /var/www/html/phpmyadmin
  5. 运行以下命令,重启Apache服务。

    sudo systemctl restart apache2
  6. 在本地机器浏览器地址栏,输入http://实例公网 IP/phpmyadmin并按Enter键,访问phpMyAdmin登录页面。

  7. 输入MySQL的用户名和密码,单击执行

    输入用户名和密码

    如果出现以下页面,说明MySQL连接成功。MySQL连接成功

  • 本页导读 (1)
文档反馈