All Products
Search
Document Center

Elastic Compute Service:Manually deploy a LAMP stack

Last Updated:Feb 13, 2025

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. This topic describes how to manually deploy a LAMP stack on an Elastic Compute Service (ECS) instance.

Prerequisites

  • A public IP address is automatically assigned to the ECS instance. Alternatively, an elastic IP address (EIP) is associated with the ECS instance. For instructions on how to enable public bandwidth, see Enable public bandwidth.

  • Inbound rules are added to a security group of the ECS instance to open ports 22 and 80. For information about how to add a security group rule, see Add a security group rule.

  • The ECS instance has at least 4 GiB of memory.

Deploy a LAMP stack

Alibaba Cloud Linux 3 or CentOS 8

Important

All content is removed from the default CentOS 8 repository at the following address: http://mirror.centos.org/centos/8/. If you continue using the default CentOS 8 repository on Alibaba Cloud, an error is reported. You must change CentOS 8 repository addresses. For more information, see Change CentOS 8 repository addresses.

  1. Install Apache.

    # Install the Apache server.
    sudo dnf install -y httpd
    # Automatically start the Apache server on instance startup.
    sudo systemctl enable httpd
    # Start the Apache server.
    sudo systemctl start httpd
  2. Install MySQL.

    Note

    If your instance runs Alibaba Cloud Linux 3, you must install compat-openssl10, which is compatible with earlier versions of OpenSSL libraries.

    sudo yum install -y compat-openssl10
    # Add an official MySQL repository.
    sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el8-1.noarch.rpm
    # Install MySQL.
    sudo dnf install -y mysql-server
    # Start MySQL and configure MySQL to automatically start on system startup.
    sudo systemctl start mysqld
    sudo systemctl enable mysqld
    1. Query the default initial password of the root user.

      • If your instance runs Alibaba Cloud Linux 3, run the following command:

        echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
      • If your instance runs CentOS 8, the root user does not have an initial password.

    2. Specify a new password for the root user of MySQL. In the following command, replace <oldpwd> with the initial password and <newpwd> with the new password. If your instance runs CentOS 8, replace <oldpwd> with an empty string and press the Enter key to skip to the next line when you are prompted to enter a password.

      Important

      The password must be at least eight characters in length, and contain at least one uppercase letter, one lowercase letter, one digit, and one special character.

      sudo mysqladmin -uroot -p'<oldpwd>' password '<newpwd>'
  3. Install PHP.

    Note

    In this example, PHP 8.4 is used. If you require another PHP version, change the module name based on the PHP version that you want to install. For example, if you want to install PHP 8.1, change the module name to php:remi-8.1.

    # Specify the remi repository and enable the php:remi-8.4 module.
    sudo rpm -Uvh http://mirrors.cloud.aliyuncs.com/remi/enterprise/remi-release-8.rpm  --nodeps
    sudo dnf install -y yum-utils && sudo dnf module enable -y php:remi-8.4
    # Install PHP, PHP FastCGI Process Manager (PHP-FPM), and the MySQL extension.
    sudo dnf install -y php php-fpm php-mysqlnd
    # Start PHP-FPM and configure PHP-FPM to automatically start on system startup.
    sudo systemctl start php-fpm
    sudo systemctl enable php-fpm
  4. Verify the LAMP stack.

    1. Query the default listening address of PHP-FPM in the configuration file.

      sudo grep 'listen =' /etc/php-fpm.d/www.conf
    2. Create the /etc/httpd/conf.d/php-fpm.conf configuration file and configure php-fpm rules in the file.

      Important

      If the listening address of PHP-FPM is 127.0.0.1:9000, set the SetHandler parameter to proxy:fcgi://127.0.0.1:9000.

      sudo tee /etc/httpd/conf.d/php-fpm.conf <<-'EOF'
      <FilesMatch \.php$>
          SetHandler "proxy:unix:/run/php-fpm/www.sock;"
      </FilesMatch>
      EOF
    3. Restart httpd for the configuration to take effect.

      sudo systemctl restart httpd
    4. Create the test.php file in the /var/www/html/ directory and add the following content to the file. Replace <username> with the MySQL username and <password> with the corresponding password.

      sudo tee /var/www/html/test.php <<-'EOF'
      <?php
      $servername = "localhost";
      $username = "<username>";
      $password = "<password>";
      
      $conn = new mysqli($servername, $username, $password);
      
      if ($conn->connect_error) {
      die("fail: " . $conn->connect_error);
      }
      echo "success\n";
      ?>
      EOF
    5. Enter http://<Public IP address of the ECS instance>/test.php in the address bar of a web browser on your on-premises machine. If success is returned, you are connected to MySQL by using the PHP proxy.

Alibaba Cloud Linux 2 or CentOS 7

  1. Install Apache.

    # Install the Apache server.
    sudo yum install -y httpd
    # Automatically start the Apache server on instance startup.
    sudo systemctl enable httpd
    # Start the Apache server.
    sudo systemctl start httpd
  2. Install MySQL.

    # Add an official MySQL repository.
    sudo rpm -Uvh https://repo.mysql.com/mysql84-community-release-el7-1.noarch.rpm
    # Install MySQL.
    sudo yum install -y mysql-server
    # Start MySQL and configure MySQL to automatically start on system startup.
    sudo systemctl start mysqld
    sudo systemctl enable mysqld
    1. Query the default initial password of the root user.

      echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
    2. Specify a new password for the root user of MySQL. In the following command, replace <oldpwd> with the initial password and <newpwd> with the new password.

      Important

      The password must be at least eight characters in length, and contain at least one uppercase letter, one lowercase letter, one digit, and one special character.

      sudo mysqladmin -uroot -p'<oldpwd>' password '<newpwd>'
  3. Install PHP.

    # Specify the remi repository and enable the remi-php83 module.
    sudo rpm -Uvh http://mirrors.cloud.aliyuncs.com/remi/enterprise/remi-release-7.rpm  --nodeps
    sudo yum install -y yum-utils && sudo yum-config-manager --enable   remi-php83
    # Install PHP, PHP-FPM, and the MySQL extension.
    sudo yum install -y php php-fpm php-mysqlnd
    # Start PHP-FPM and configure PHP-FPM to automatically start on system startup.
    sudo systemctl start php-fpm
    sudo systemctl enable php-fpm
  4. Verify the LAMP stack.

    1. Query the default listening address of PHP-FPM in the configuration file.

      sudo grep 'listen =' /etc/php-fpm.d/www.conf
      • If the address of a socket file is returned, PHP-FPM listens to the socket file.

      • If 127.0.0.1:9000 is returned, PHP-FPM listens on local port 9000.

    2. Create the /etc/httpd/conf.d/php-fpm.conf configuration file and configure php-fpm rules in the file.

      Important

      If the listening address of PHP-FPM is the address of a socket file, change proxy:fcgi://127.0.0.1:9000 to proxy:unix:<path>;. Replace <path> with the address of your socket file.

      sudo tee /etc/httpd/conf.d/php-fpm.conf <<-'EOF'
      <FilesMatch \.php$>
          SetHandler "proxy:fcgi://127.0.0.1:9000"
      </FilesMatch>
      EOF
    3. Restart httpd for the configuration to take effect.

      sudo systemctl restart httpd
    4. Create the test.php file in the /var/www/html/ directory and add the following content to the file. Replace <username> with the MySQL username and <password> with the corresponding password.

      sudo tee /var/www/html/test.php <<-'EOF'
      <?php
      $servername = "localhost";
      $username = "<username>";
      $password = "<password>";
      
      $conn = new mysqli($servername, $username, $password);
      
      if ($conn->connect_error) {
      die("fail: " . $conn->connect_error);
      }
      echo "success\n";
      ?>
      EOF
    5. Enter http://<Public IP address of the ECS instance>/test.php in the address bar of a web browser on your on-premises machine. If success is returned, you are connected to MySQL by using the PHP proxy.

Ubuntu 20.04 or later

  1. Install Apache.

    # Install the Apache server.
    sudo apt update -y && sudo apt install -y apache2
    # Automatically start the Apache server on instance startup.
    sudo systemctl enable apache2
    # Start the Apache server.
    sudo systemctl start apache2
  2. Update the software package list and install the MySQL server.

    sudo apt update -y && sudo apt install -y mysql-server
  3. Change the password and identity authentication plug-in used by the root user of the MySQL server. Replace <newpwd> in the following command with the actual password.

    Important

    The default identity authentication plug-in used by the root user is auth_socket. After the command is run, you are prompted to enter a password. Press the Enter key to skip to the next line.

    sudo mysql -uroot -p -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '<newpwd>';" -e "FLUSH PRIVILEGES;"
  4. Install PHP.

    Note

    Run the sudo apt search php command to query all PHP versions that you can install. If you want to install a different PHP version, replace the version number in the following command with the actual version number. For example, if you want to install PHP 8.1, run the sudo apt install -y php8.1 php8.1-fpm php8.1-mysql command.

    # Install the software-properties-common package and add the ppa:ondrej/php Personal Package Archive (PPA) repository.
    sudo apt update && sudo apt install -y software-properties-common && sudo add-apt-repository -y ppa:ondrej/php
    # Install PHP 8.4 and the related components, including PHP-FPM and the MySQL extension.
    sudo apt install -y php8.4 php8.4-fpm php8.4-mysql
  5. Verify the LAMP stack.

    1. Query the default listening address of PHP-FPM in the configuration file. Replace <version> with your actual PHP version. For example, if you use PHP 8.4, replace <version> with 8.4.

      sudo grep '^listen =' /etc/php/<version>/fpm/pool.d/www.conf
      • If the address of a socket file is returned, PHP-FPM listens to the socket file.

      • If 127.0.0.1:9000 is returned, PHP-FPM listens on local port 9000.

    2. Create the /etc/apache2/conf-available/php-fpm.conf configuration file and configure PHP forwarding rules in the file. Replace <listen> with the actual listening address of PHP-FPM.

      • If the address of a socket file is used as the listening address of PHP-FPM, replace <listen> with unix:<path>; and <path> with the address of the socket file.

        Important

        To listen to socket files, your account must have the read and write permissions on the socket files. You can run the sudo chmod 666 <path> command to grant the preceding permissions. Replace <path> with the actual address of a socket file.

      • If 127.0.0.1:9000 is used as the listening address of PHP-FPM, replace <listen> with fcgi://127.0.0.1:9000.

      sudo tee /etc/apache2/conf-available/php-fpm.conf <<-'EOF'
      <FilesMatch \.php$>
          SetHandler "proxy:<listen>"
      </FilesMatch>
      EOF
    3. Create a symbolic link for the configuration file.

      sudo ln -s /etc/apache2/conf-available/php-fpm.conf /etc/apache2/conf-enabled/
    4. Enable the proxy_fcgi and setenvif modules to allow Apache to forward PHP requests to PHP-FPM. Then, enable the configuration file of PHP-FPM.

      sudo a2enmod proxy_fcgi setenvif
      sudo a2enconf php8.4-fpm
    5. Restart httpd for the configuration to take effect.

      sudo systemctl restart apache2
    6. Create the test.php file in the /var/www/html/ directory and add the following content to the file. Replace <username> with the MySQL username and <password> with the corresponding password.

      sudo tee /var/www/html/test.php <<-'EOF'
      <?php
      $servername = "localhost";
      $username = "<username>";
      $password = "<password>";
      
      $conn = new mysqli($servername, $username, $password);
      
      if ($conn->connect_error) {
      die("fail: " . $conn->connect_error);
      }
      echo "success\n";
      ?>
      EOF
    7. Enter http://<Public IP address of the ECS instance>/test.php in the address bar of a web browser on your on-premises machine. If success is returned, you are connected to MySQL by using the PHP proxy.

FAQ

Why am I unable to access the test.php page by using the public IP address of the ECS instance on which the page is hosted?

Possible causes and solutions:

Port 80 is not open in the security groups of the ECS instance, the system firewall is enabled on the ECS instance, or port 80 is used by a different service.

For information about how to troubleshoot the issue based on the preceding causes, see What do I do if I cannot access a service deployed on an instance?

How do I allow remote access to MySQL?

Create a non-root account and allow remote access to MySQL by using the account. For more information, see Deploy MySQL on a Linux instance.