All Products
Search
Document Center

Elastic Compute Service:Build a LAMP stack on an Alibaba Cloud Linux or CentOS 7.x instance

Last Updated:May 23, 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. In most cases, LAMP stacks are used to build websites. LAMP has extensive community support and a wealth of resources and is suitable for developing, deploying, and maintaining web applications of various sizes. LAMP stacks are highly flexible and allow you to modify and customize servers. You can configure LAMP stack components based on your business requirements to maximize performance and security. This topic describes how to build a LAMP stack on an Elastic Compute Service (ECS) instance that runs Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 7.x.

Prerequisites

An ECS instance that is used to build a LAMP stack is created. For more information, see Create an instance on the Custom Launch tab.

The ECS instance meets the following requirements:

  • The ECS 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 ECS instance runs Alibaba Cloud Linux 2, Alibaba Cloud Linux 3, or CentOS 7.x.

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

Step 1: Install Apache

  1. Connect to the ECS instance on which you want to build a LAMP stack.

    For more information, see Connect to a Linux instance by using a password or key.

  2. Run the following command to install Apache and its extension package:

    sudo yum -y install httpd httpd-manual mod_ssl mod_perl
  3. Run the following command to view the version of Apache:

    httpd -v

    The following command output indicates that Apache is installed:

    httpd -v

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

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

    sudo systemctl status httpd

    The following command output indicates that Apache is started.

    image.png

Step 2: Install and configure MySQL

Install MySQL

  1. (Optional) If the ECS instance runs the Alibaba Cloud Linux 3 operating system, you must install MySQL dependency packages.

    sudo yum install -y compat-openssl10
  2. Run the following command to update the Yellowdog Updater Modified (YUM) repository:

    sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm
  3. Run the following command to install MySQL:

    sudo yum -y install mysql-community-server
  4. Run the following command to view the version of MySQL:

    mysql -V

    The following command output indicates that MySQL is installed.

    image

  5. Run the following command to start MySQL:

    sudo systemctl start mysqld
  6. Run the following commands in sequence to configure MySQL to automatically start on system startup:

    sudo systemctl enable mysqld
    sudo systemctl daemon-reload

Configure MySQL

  1. Run the following command to view the /var/log/mysqld.log file and obtain and record the initial password of the root user:

    sudo grep 'temporary password' /var/log/mysqld.log

    The following command output indicates that ARQTRy3+**** is the initial password of the root user. You can use this initial password when you reset the password of the root user.

    2021-11-10T07:01:26.595215Z 1 [Note] A temporary password is generated for root@localhost: ARQTRy3+****
  2. Run the following command to configure the security settings of MySQL:

    sudo mysql_secure_installation
    1. Enter the initial password of 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.

      Securing the MySQL server deployment.
      
      Enter password for user root: # Enter the initial password that you obtained in the previous step.
    2. Configure a new password for MySQL.

      The existing password for the user account root has expired. Please set a new password.
      
      New password: # Enter a new password. The password must be 8 to 30 characters in length and contain uppercase letters, lowercase letters, digits, and any of the following special characters: ( ) ` ~ ! @ # $ % ^ & * - + = | { } [ ] : ; ' < > , . ? /
      
      Re-enter new password: # Re-enter the new password. 
      The 'validate_password' plugin is installed on the server.
      The subsequent steps will run with the existing configuration
      of the plugin.
      Using existing password for root.
      
      Estimated strength of the password: 100 # The strength of the new password is displayed in the command output. 
      Change the password for root ? (Press y|Y for Yes, any other key for No) :Y # Enter Y to confirm the new password. 
      
      # After the new password is configured, verify the password. 
      New password: # Enter the new password. 
      
      Re-enter new password: # Re-enter the new password. 
      
      Estimated strength of the password: 100
      Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :Y # Enter Y to use the new password.
    3. Enter Y to delete the anonymous users.

      Remove anonymous users? (Press y|Y for Yes, any other key for No) :Y
      Success.
    4. Enter Y to deny remote access by the root user.

      Disallow root login remotely? (Press y|Y for Yes, any other key for No) :Y
      Success.
    5. Enter Y to delete the test database and the access permissions on the database.

      Remove test database and access to it? (Press y|Y for Yes, any other key for No) :Y
       - Dropping test database...
      Success.
      
       - Removing privileges on test database...
      Success.
    6. Enter Y to reload privilege tables.

      Reload privilege tables now? (Press y|Y for Yes, any other key for No) :Y
      Success.
      
      All done!

For more information, see the official MySQL documentation.

Step 3: Install and configure PHP

Install PHP

  1. Install PHP.

    Alibaba Cloud Linux 3/2

    1. (Optional) If the ECS instance runs the Alibaba Cloud Linux 3 operating system, you must install PHP dependency packages.

      sudo yum install -y compat-openssl10
    2. Run the following command to update the YUM repository:

      sudo rpm -Uvh https://mirrors.aliyun.com/remi/enterprise/remi-release-7.rpm
    3. Run the following command to enable the PHP 8.0 repository:

      sudo yum-config-manager --enable remi-php80
    4. Run the following command to install PHP:

      sudo yum install -y php php-cli php-fpm php-common php-mysqlnd php-gd php-mbstring

    CentOS 7.x

    1. Update the YUM repositories.

      1. Run the following commands to install the Extra Packages for Enterprise Linux (EPEL) repository and Remi repository:

        sudo yum install -y epel-release
        sudo rpm -Uvh https://mirrors.aliyun.com/remi/enterprise/remi-release-7.rpm
      2. Run the following command to enable the PHP 8.0 repository:

        sudo yum-config-manager --enable remi-php80
    2. Run the following command to install PHP:

      sudo yum install -y php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json
  2. Run the following command to view the version of PHP:

    php -v

    The following command output indicates that PHP is installed:

    image

Modify the Apache configuration file to add support for PHP

  1. Run the following command to back up the Apache configuration file:

    sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak
  2. Modify the Apache configuration file to add support for PHP.

    Important

    If you do not add support for PHP, PHP pages cannot be displayed when you access the pages by using a web browser.

    1. Run the following command to open the Apache configuration file:

      sudo vim /etc/httpd/conf/httpd.conf
    2. Press the I key to enter Insert mode.

    3. Add the following configuration information:

      <FilesMatch \.php$>
          SetHandler "proxy:fcgi://127.0.0.1:9000"
      </FilesMatch>
    4. Press the Esc key, enter :wq, and then press the Enter key to save and close the configuration file.

    5. Run the following command to restart PHP FastCGI Process Manager (PHP-FPM):

      sudo systemctl restart php-fpm
  3. Run the following command to create a test file in the root directory of the Apache website:

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

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

    The page shown in the following figure indicates that PHP is installed.

    image

  6. After you confirm that the LAMP stack is built, we recommend that you run the following command to delete the phpinfo.php 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