All Products
Search
Document Center

Elastic Compute Service:Build a LAMP stack on a Ubuntu instance

Last Updated:Mar 06, 2024

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 that is used to deploy a LAMP stack is created. For more information, see Create an instance on the Custom Launch tab.

The instance meets the following requirements:

  • The instance is associated with an auto-assigned public IP address or an elastic IP address (EIP). For information about how to associate an EIP with an instance, see Associate or disassociate an EIP.

  • The instance runs the Ubuntu 22.04, Ubuntu 20.04, or Ubuntu 18.04 operating system.

  • An inbound rule is added to a security group of the instance to allow traffic on ports 22, 80, and 443. For more information, see Add a security group rule.

Step 1: 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.52.

    image.png

  4. Run the following commands to start Apache and configure Apache to automatically start on system startup:

    sudo systemctl start apache2
    sudo systemctl enable apache2
  5. Run the following command to check the status of Apache:

    sudo systemctl status apache2

    A command output similar to the following one indicates that Apache is started.

    image.png

Step 2: 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:

      mysql -V

      A command output similar to the following one indicates that MySQL is installed and its version is 8.0.35.

      image.png

  2. Run the following command to start MySQL:

    sudo systemctl start mysql
  3. Run the following commands in sequence to configure MySQL to start on system startup:

    sudo systemctl enable mysql
    sudo systemctl daemon-reload
  4. Configure MySQL.

    1. Run the following command to access MySQL:

      sudo mysql
    2. Run the following command to set a 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 MySQL:

      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.

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

        For data security purposes, no output is returned when you enter a password. You need only to enter the correct password and then press the Enter key.

      2. Enter Y to configure a password strength policy.

        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. Specify a password strength value.

        In this example, 2 is used.

        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. 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
      5. Enter the new password of the root user.

        New password:
        
        Re-enter new password:
        
        Estimated strength of the password: 100
      6. 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
      7. 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
      8. 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
      9. 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) : 
      10. 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.

  5. Check whether you can log on to MySQL.

    1. Run the following command to log on to MySQL:

      sudo mysql -uroot -p
    2. At the Enter password: prompt, enter the password that you set for MySQL.

      Note

      For data security purposes, no output is returned when you enter a password. 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 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. Run the following command to exit MySQL:

      exit;

Step 3: Install PHP

  1. Run the following commands in sequence 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:

    php -v

    A command output similar to the following one indicates that PHP is installed and its version is 7.4.3.

    image.png

  3. 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() function to the webpage.

      The phpinfo() function is used to show all configuration information of PHP.

      sudo sh -c 'echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php'
  4. Run the following command to restart Apache:

    sudo systemctl restart apache2
  5. Enter http://<Public IP address of the ECS instance>/phpinfo.php in the address bar of a browser on your computer and press the Enter key.

    A page similar to the following one indicates that PHP is installed.

    image.png

  6. After the LAMP stack is built, we recommend that you delete the phpinfo.php test file to prevent data leaks.

    sudo rm -rf <Website root directory>/phpinfo.php

    In this example, the website root directory /var/www/html is used. Run the following command to delete the test file:

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

(Optional) Step 4: Install phpMyAdmin

phpMyAdmin is a MySQL database management tool that allows you to manage databases by using web interfaces.

  1. Run the following command to install the dependencies required by phpMyAdmin:

    sudo apt install -y php-cli php-mbstring php-mysql unzip 
  2. Run the following command to create a directory to store phpMyAdmin data:

    sudo mkdir -p /var/www/html/phpmyadmin
  3. Run the following commands to download and decompress the phpMyAdmin package.

    1. Run the following commands in sequence to switch to the home directory and download the phpMyAdmin package:

      cd
      wget --no-check-certificate https://files.phpmyadmin.net/phpMyAdmin/4.9.11/phpMyAdmin-4.9.11-all-languages.zip
    2. Run the following commands to download and decompress the phpMyAdmin package:

      sudo unzip phpMyAdmin-4.9.11-all-languages
  4. Run the following command to copy the phpMyAdmin files to the prepared directory:

    sudo mv phpMyAdmin-4.9.11-all-languages/*  /var/www/html/phpmyadmin
  5. Run the following command to restart Apache:

    sudo systemctl restart apache2
  6. Enter http://Public IP address of the ECS instance/phpmyadmin in the address bar of a browser on your computer and press the Enter key to go to the logon page of phpMyAdmin.

  7. Enter the username and password of your MySQL account and click Go.

    输入用户名和密码

    If the following page is displayed, the connection to MySQL is established.MySQL连接成功