LAMP is an acronym of the names of its original four components: the Linux operating system, Apache HTTP Server, MySQL relational database management system, and PHP programming language. LAMP stacks are commonly used to build websites. This topic describes how to build a LAMP stack on an Elastic Compute Service (ECS) instance that runs Ubuntu.

Prerequisites

  • An ECS instance is created and has a public IP address assigned. For more information, see Create an instance by using the wizard.
    In this topic, an ECS instance that has the following configurations is used. We recommend that you use the same operating system version as that used in the example to prevent command compatibility issues between operating system versions.
    • Instance type: ecs.c6.large
    • Operating system: Ubuntu 20.04 64-bit public image
    • Network type: Virtual Private Cloud (VPC)
    • IP address: a public IP address
  • An inbound rule is added to a security group of the ECS instance to allow traffic on ports 22 and 80. For more information, see Add a security group rule.

Background information

This topic is intended for individual users who are familiar with Linux operating systems but new to using Alibaba Cloud ECS to build websites. The following software versions are used in the sample procedure. Operations may vary based on your instance type and software versions.
  • Apache: 2.4.41
  • MySQL: 8.0.29
  • PHP: 7.4.3

Step 1: Make preparations

  1. Connect to the ECS instance on which you want to build a LAMP stack.
    For more information, see Connection methods.
  2. Disable the firewall on the instance operating system.
    1. Run the following command to check the state of the firewall:
      sudo ufw status
      • If the firewall is disabled and in the inactive state, Status: inactive is displayed.
      • If the firewall is enabled and in the active state, Status: active is displayed.
    2. Optional:Disable the firewall.
      If the firewall is enabled, run the following command to disable the firewall and prevent the firewall from starting on instance startup:
      sudo ufw disable
      Note If you want to re-enable the firewall after it is disabled and start the firewall on instance startup, run the sudo ufw enable command.

Step 2: Install Apache

  1. Run the following command to update the Ubuntu software packages:
    sudo apt update
  2. Run the following command to install Apache:
    sudo apt-get -y install apache2
  3. Run the following command to check the version of Apache:
    apache2 -v
    A command output similar to the following one indicates that Apache is installed and its version is 2.4.41.
    Server version: Apache/2.4.41 (Ubuntu)
    Server built:   2022-01-05T14:49:56
  4. Run the following command to start Apache and configure Apache to start on system startup:
    sudo systemctl start apache2
  5. On your Windows computer or another host that can access the Internet, use a browser to access <Public IP address of the ECS instance>.
    If Apache is working normally, the following default Apache homepage is displayed. Apache homepage

Step 3: Install and configure MySQL

  1. Install MySQL.
    1. Run the following command to install MySQL:
      sudo apt -y install mysql-server
    2. Run the following command to check the version of MySQL:
      sudo mysql -V
      A command output similar to the following one indicates that MySQL is installed and its version is 8.0.29.
      mysql  Ver 8.0.29-0ubuntu0.20.04.3 for Linux on x86_64 ((Ubuntu))
  2. Run the following command to start MySQL:
    sudo systemctl start mysql
  3. Configure MySQL.
    1. Run the following command to access MySQL:
      sudo mysql
    2. Run the following command to set the password for the root user:
      ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'mynewpassword';
      In this example, the password is Mysql@1234. Sample command:
      ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'Mysql@1234';
    3. Run the following command to exit the MySQL database:
      exit;
    4. Run the following command to configure the security settings of MySQL:
      sudo mysql_secure_installation
    5. Follow the command line instructions to configure the following settings in sequence.
      1. Enter the password of the root user. In this example, Mysql@1234 is entered.
        root@iZbp19jsi7s0g7m4zgc****:~# sudo mysql_secure_installation
        
        Securing the MySQL server deployment.
        
        Enter password for user root: 
        Note When you enter a password, no command output is returned to maximize data security. You only need to enter the correct password and then press the Enter key.
      2. Enter Y to change the password of the root user.
        Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y
      3. Enter the new password of the root user.
        New password:
        
        Re-enter new password:
        
        Estimated strength of the password: 100
      4. Enter Y to use the new password that you set.
        Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
      5. Enter Y to delete the anonymous user account that comes with 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
      6. Enter Y to deny remote access by the root user to MySQL.
        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
      7. Enter Y to delete the database named 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) : 
      8. Enter Y to reload privilege tables.
        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
        When the configuration is complete, All done! is displayed in the command output.
  4. Check whether you can log on to the MySQL database.
    1. Run the following command to log on to the MySQL database:
      sudo mysql -uroot -p
    2. At the Enter password: prompt, enter the password that you set for the MySQL database.
      Note When you enter a password, no command output is returned to maximize data security. You need only to enter the correct password and then press the Enter key.
      The following example command output indicates that you are logged on to the MySQL database.
      root@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. Run the following command to exit the MySQL database:
      exit;

Step 4: Install PHP

  1. Run the following command to install PHP:
    sudo apt -y install php-fpm
    sudo apt -y install libapache2-mod-php
  2. Run the following command to check the version of PHP:
    sudo php -v
    A command output similar to the following indicates that PHP is installed and its version is 7.4.3.
    PHP 7.4.3 (cli) (built: Nov 25 2021 23:16:22) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
        with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Step 5: Create and access a test webpage

  1. In the root directory of the Apache website, create a test webpage.
    1. Run the following command to view the path information of the root directory of the Apache website:
      sudo cat /etc/apache2/sites-available/000-default.conf
      The DocumentRoot /var/www/html line in the command output indicates that the website root directory is /var/www/html.
    2. Run the following command to create a test webpage in the website root directory and add the phpinfo() functions to the webpage.
      The phpinfo() function is used to show all configuration information of PHP.
      sudo echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
  2. Run the following command to restart Apache:
    sudo systemctl restart apache2
  3. On your Windows computer or another host that can access the Internet, use a browser to access http://<Public IP address of the ECS instance>/phpinfo.php.
    The following page shows PHP settings and indicates that the LAMP stack is built. Test PHP webpage

What to do next

After the LAMP stack is built, we recommend that you delete the phpinfo.php test file to prevent data leaks.
rm -rf <Website root directory> /phpinfo.php
In this example, /var/www/html is used as the website root directory. Run the following command to delete the test file:
rm -rf /var/www/html/phpinfo.php