赛尔校园公共服务平台 Logo
平台使用
阿里云
百度云
移动云
智算服务
教育生态
登录 →
赛尔校园公共服务平台 Logo
平台使用 阿里云 百度云 移动云 智算服务 教育生态
登录
  1. 首页
  2. 阿里云
  3. 轻量应用服务器
  4. 实践教程
  5. 最佳实践
  6. 搭建环境
  7. 搭建LAMP
  8. 部署LAMP环境(Ubuntu)

部署LAMP环境(Ubuntu)

  • 搭建LAMP
  • 发布于 2025-04-16
  • 0 次阅读
文档编辑
文档编辑

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

说明
  • 如果您还没创建轻量应用服务器,建议您直接通过LAMP应用镜像快速搭建LAMP环境。具体操作,请参见搭建LAMP开发环境(通过应用镜像)。

  • 如果您需要在Alibaba Cloud Linux/CentOS 7.x操作系统中部署LAMP环境。具体操作,请参见手动搭建LAMP开发环境(Alibaba Cloud Linux/CentOS 7.x)。

准备工作

创建用于部署LAMP环境的轻量应用服务器实例。具体操作,请参见创建轻量应用服务器。

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

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

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

步骤一:安装Apache

  1. 远程连接需要部署LAMP环境的轻量应用服务器实例。具体操作,请参见远程连接Linux服务器。

    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://<轻量应用服务器实例公网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
相关文章

搭建LAMP开发环境(通过应用镜像) 2025-04-16 17:07

LAMP是搭建Web应用时最常用的环境,LAMP分别表示Linux、Apache、MySQL以及PHP,即在Linux系统中安装Apache、MySQL以及PHP。本文将介绍如何使用轻量应用服务器的LAMP镜像快速搭建开发环境。 背景信息 阿里云的<

手动搭建LAMP开发环境(Alibaba Cloud LinuxCentOS 7.x) 2025-04-16 17:07

LAMP环境是常用的Web开发环境之一,其中LAMP分别代表Linux、Apache、MySQL和PHP。本文介绍如何在Alibaba Cloud Linux 3/2、CentOS 7.x操作系统的轻量应用服务器实例内部署LAMP环境。 说明

部署LAMP环境(Ubuntu) 2025-04-16 17:07

LAMP环境是常用的Web开发环境之一,其中LAMP分别代表Linux、Apache、MySQL和PHP。本文介绍如何在Ubuntu操作系统的轻量应用服务器实例内部署LAMP环境。 说明 </

目录
Copyright © 2025 your company All Rights Reserved. Powered by 赛尔网络.
京ICP备14022346号-15
gongan beian 京公网安备11010802041014号