Manually deploy a LAMP stack

更新时间:
复制 MD 格式

Install and configure Linux, Apache, MySQL, and PHP on an ECS instance to host web applications.

Note

You can use Terraform Explorer to automatically build a LAMP environment.

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 security group rules allow traffic on ports 22 and 80 for the ECS instance.

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

Deploy a LAMP stack

Alibaba Cloud Linux 3 or CentOS 8

  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

    On Alibaba Cloud Linux 3, install compat-openssl10 for compatibility with earlier 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 initial root password.

      • On Alibaba Cloud Linux 3:

        echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
      • On CentOS 8, the root user has no initial password.

    2. Set a new MySQL root password. Replace <oldpwd> with the initial password and <newpwd> with your new password. On CentOS 8, leave <oldpwd> empty.

      Important

      The password must be at least 8 characters long and include uppercase, lowercase, digits, and special characters.

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

    Note

    This example uses PHP 8.4. For other versions, change the module name (e.g., php:remi-8.1 for PHP 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 sed -i "s/\$releasever/8/g" /etc/yum.repos.d/remi-modular.repo /etc/yum.repos.d/remi-safe.repo /etc/yum.repos.d/remi.repo
    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 PHP-FPM listening address.

      sudo grep 'listen =' /etc/php-fpm.d/www.conf
    2. Create /etc/httpd/conf.d/php-fpm.conf with php-fpm forwarding rules.

      Important

      If PHP-FPM listens on 127.0.0.1:9000, set SetHandler 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.

      sudo systemctl restart httpd
    4. Create test.php in /var/www/html/. Replace <username> and <password> with your MySQL credentials.

      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. Open http://<Public-IP-Address-of-ECS-instance>/test.php in a browser. If success appears, the LAMP stack is working.

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 initial root password.

      echo $(PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log); PASSWORD=${PASSWORD##* }; echo $PASSWORD)
    2. Set a new MySQL root password. Replace <oldpwd> with the initial password and <newpwd> with your new password.

      Important

      The password must be at least 8 characters long and include uppercase, lowercase, digits, and special characters.

      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 sed -i "s/\$releasever/7/g" /etc/yum.repos.d/remi-modular.repo /etc/yum.repos.d/remi-safe.repo /etc/yum.repos.d/remi.repo
    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 PHP-FPM listening address.

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

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

    2. Create /etc/httpd/conf.d/php-fpm.conf with php-fpm forwarding rules.

      Important

      If PHP-FPM listens on a socket file, change proxy:fcgi://127.0.0.1:9000 to proxy:unix:<path>;. Replace <path> with the socket file path.

      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.

      sudo systemctl restart httpd
    4. Create test.php in /var/www/html/. Replace <username> and <password> with your MySQL credentials.

      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. Open http://<Public-IP-Address-of-ECS-instance>/test.php in a browser. If success appears, the LAMP stack is working.

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. Install the MySQL server.

    sudo apt update -y && sudo apt install -y mysql-server
  3. Set the root password and authentication plugin. Replace <newpwd> with your password.

    Important

    By default, root uses the auth_socket plugin and has no password. Press Enter when prompted.

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

    Note

    Run sudo apt search php to list available PHP versions. For example, for PHP 8.1: sudo apt install -y php8.1 php8.1-fpm php8.1-mysql.

    # 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 PHP-FPM listening address. Replace <version> with your PHP version, such as 8.4.

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

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

    2. Create /etc/apache2/conf-available/php-fpm.conf with PHP forwarding rules. Replace <listen> with the PHP-FPM listening address.

      • If PHP-FPM listens on a socket file, replace <listen> with unix:<path>; and <path> with the socket file path.

        Important

        Socket files require read/write permissions. Run sudo chmod 666 <path> to grant them.

      • If PHP-FPM listens on 127.0.0.1:9000, 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 to enable the configuration.

      sudo ln -s /etc/apache2/conf-available/php-fpm.conf /etc/apache2/conf-enabled/
    4. Enable the proxy_fcgi and setenvif modules, then activate the PHP-FPM configuration.

      sudo a2enmod proxy_fcgi setenvif
      sudo a2enconf php8.4-fpm
    5. Restart Apache.

      sudo systemctl restart apache2
    6. Create test.php in /var/www/html/. Replace <username> and <password> with your MySQL credentials.

      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. Open http://<Public-IP-Address-of-ECS-instance>/test.php in a browser. If success appears, the LAMP stack is working.

FAQ

Why can't I access the test.php page?

Possible causes:

Port 80 may not be open in the security group, the system firewall may be enabled, or port 80 may be occupied by another service.

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 for remote access. Deploy MySQL on a Linux instance.