This topic describes how to build a LAMP stack (also known as LAMP) on an ECS instance. 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.

Prerequisites

An Alibaba Cloud account is created. To create an Alibaba Cloud account, go to the account registration page.

Background information

An ECS instance with the following configurations is used in the examples in this topic:
  • Instance type: ecs.c6.large
  • Operating system: CentOS 7.2 64-bit
  • Network type: VPC
  • IP address: a public IP address
The following software versions are used in the examples in this topic. Choose appropriate software versions when you build a LAMP stack.
  • Apache 2.4.43
  • MySQL 5.6.24
  • PHP 7.0.32
  • phpMyAdmin 4.0.10.20

This topic is intended for individual users who are familiar with the Linux operating system, but new to using Alibaba Cloud ECS to build websites.

This topic describes how to manually build a LAMP stack. You can also purchase a LAMP image on Alibaba Cloud Marketplace and create an ECS instance from the image to build websites.

Procedure

Step 1. Prepare the compilation environment

  1. Create an instance by using the provided wizard.
  2. Connect to a Linux instance from the console.
  3. Run the cat /etc/redhat-release command to check the system version.
    Check the system version
  4. Disable the firewall.
    1. Run the systemctl status firewalld command to check the status of the firewall.
      Check the status of the firewall
      • If the firewall is in the inactive state, the firewall is disabled.
      • If the firewall is in the active state, the firewall is enabled. In this example, the firewall is in the active state. Therefore, you must disable the firewall.
    2. Disable the firewall. Skip this step if the firewall is already disabled.
      • To temporarily disable the firewall, run the systemctl stop firewalld command.
        Note After you run this command, the firewall is temporarily disabled. It will enter the active state when you restart the Linux operating system.
      • To permanently disable the firewall, run the systemctl disable firewalld command.
        Note You can re-enable the firewall after it is disabled. For more information, visit the firewalld website.
  5. Disable Security-Enhanced Linux (SELinux).
    1. Run the getenforce command to check the status of SELinux.
      Check the status of SELinux
      • If SELinux is in the Disabled state, SELinux is disabled.
      • If SELinux is in the Enforcing state, SELinux is enabled. In this example, SELinux is in the Enforcing state. Therefore, you must disable SELinux.
    2. Disable SELinux. Skip this step if SELinux is already disabled.
      • To temporarily disable SELinux, run the setenforce 0 command.
        Note After you run this command, SELinux is temporarily disabled. It will enter the Enforcing state when you restart the Linux operating system.
      • To permanently disable SELinux, run the vi /etc/selinux/config command to edit the SELinux configuration file. Press the Enter key. Move the pointer to the SELINUX=enforcing line and press the I key to switch to the edit mode. Change the line to SELINUX=disabled and press the Esc key. Enter :wq and press the Enter key to save and close the SELinux configuration file.
        Note You can re-enable SELinux after it is disabled. For more information, see SELinux documentation.
    3. Restart the system to apply the settings.
  6. Add an inbound rule to the security group of the ECS instance to allow traffic on the required port. For more information, see Add security group rules.

Step 2. Install Apache

  1. Run the following commands to install the dependency packages:
    1. yum groupinstall "Development Tools" -y
    2. yum install libtool -y
    3. yum install expat-devel pcre pcre-devel openssl-devel -y
  2. Run the following commands to download and decompress the Apache, APR, and APR-util source code packages:
    Note The versions of the source code packages are continuously upgraded. You can check the available versions of the source code packages on the httpd source code package and APR source code package pages, and replace the versions of the source code packages in the command with the versions to be installed.
    1. wget https://mirrors.aliyun.com/apache/httpd/httpd-<version of the Apache source code package>.tar.gz

      In this example, the version of the Apache source code package is 2.4.43. You can run the following command to download the package:

      wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.43.tar.gz
    2. wget https://mirrors.aliyun.com/apache/apr/apr-<version of the APR source code package>.tar.gz

      In this example, the version of the APR source code package is 1.6.5. You can run the following command to download the package:

      wget https://mirrors.aliyun.com/apache/apr/apr-1.6.5.tar.gz
    3. wget https://mirrors.aliyun.com/apache/apr/apr-util-<version of the APR-util source code package>.tar.gz

      In this example, the version of the APR-util source code package is 1.6.1. You can run the following command to download the package:

      wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
    4. tar xvf httpd-<version of the Apache source code package>.tar.gz -C /usr/local/src

      In this example, the version of the Apache source code package is 2.4.43. You can run the following command to decompress the package:

      tar xvf httpd-2.4.43.tar.gz -C /usr/local/src
    5. tar xvf apr-<version of the APR source code package>.tar.gz -C /usr/local/src

      In this example, the version of the APR source code package is 1.6.5. You can run the following command to decompress the package:

      tar xvf apr-1.6.5.tar.gz -C /usr/local/src
    6. tar xvf apr-util-<version of the APR-util source code package>.tar.gz -C /usr/local/src

      In this example, the version of the APR-util source code package is 1.6.1. You can run the following command to decompress the package:

      tar xvf apr-util-1.6.1.tar.gz -C /usr/local/src
  3. Run the following commands to move the APR and APR-util folders to the Apache srclib folder:
    1. cd /usr/local/src
    2. mv apr-<version of the APR source code package> httpd-<version of the Apache source code package>/srclib/apr

      In this example, the version of the APR source code package is 1.6.5 and the version of the Apache source code package is 2.4.43. You can run the following command to move the APR folder:

      mv apr-1.6.5 httpd-2.4.43/srclib/apr
    3. mv apr-util-<version of the APR-util source code package> httpd-<version of the Apache source code package>/srclib/apr-util

      In this example, the version of the APR-util source code package is 1.6.1 and the version of the Apache source code package is 2.4.43. You can run the following command to move the APR-util folder:

      mv apr-util-1.6.1 httpd-2.4.43/srclib/apr-util
  4. Run the following commands to compile the source code:
    1. cd /usr/local/src/httpd-<version of the source code package>

      In this example, the version of the Apache source code package is 2.4.43. You can run the following command to access the directory where the Apache source code is located:

      cd /usr/local/src/httpd-2.4.43
    2. ./buildconf
    3. ./configure --prefix=/usr/local/apache2 \
      --enable-ssl \
      --enable-so \
      --with-mpm=event \
      --with-included-apr \
      --enable-cgi \
      --enable-rewrite \
      --enable-mods-shared=most \
      --enable-mpms-shared=all
    4. make && make install
  5. Run the following commands to set the PATH environment variable:
    1. echo "export PATH=$PATH:/usr/local/apache2/bin" > /etc/profile.d/httpd.sh
    2. source /etc/profile.d/httpd.sh
  6. You can run the httpd -v command to check the Apache version number.
    http-v
  7. Add the Apache configuration file.
    1. Run the vi /usr/lib/systemd/system/httpd.service command to open the Apache configuration file.
    2. Press the I key and add the following content to the file:
      [Unit] 
      Description=The Apache HTTP Server 
      After=network.target 
      
      [Service] 
      Type=forking 
      ExecStart=/usr/local/apache2/bin/apachectl -k start 
      ExecReload=/usr/local/apache2/bin/apachectl -k graceful 
      ExecStop=/usr/local/apache2/bin/apachectl -k graceful-stop 
      PIDFile=/usr/local/apache2/logs/httpd.pid 
      PrivateTmp=false
      
      [Install] 
      WantedBy=multi-user.target
    3. Press the Esc key, enter :wq, and then press the Enter key to save and close the file.
  8. Run the following commands to start Apache and set it to run at startup:
    1. systemctl start httpd
    2. systemctl enable httpd
  9. Check the installation result.
    1. Log on to the ECS console.
    2. In the left-side navigation pane, choose Instances & Images > Instances.
    3. On the Instances page, find the instance you created and copy its public IP address from the IP address column.
    4. Enter http://<Public IP address of the ECS instance> in the address bar of your browser and press the Enter key.
      If the following page is displayed, Apache is started.The default welcome page

Step 3. Install MySQL

  1. Prepare the compilation environment and a directory to store MySQL data.
    1. Run the following commands to prepare the compilation environment:
      1. yum install ncurses-devel bison gnutls-devel -y
      2. yum install cmake -y
    2. Run the following commands to prepare a directory to store MySQL data and add a user and user group:
      1. cd
      2. mkdir /mnt/data
      3. groupadd -r mysql
      4. useradd -r -g mysql -s /sbin/nologin mysql
      5. id mysql
    3. Run the following command to change the owner and group of the data directory:
      chown -R mysql:mysql /mnt/data
  2. Run the following commands to download, decompress, and compile the general availability (GA) version of the source code:
    Note If an exception occurs to the official MySQL download link, we recommend that you download the MySQL source code package from a third-party link on your ECS instance. Alternatively, you can download the package from the MySQL official website or a third-party channel, and then use a remote connection tool such as PuTTY to upload the package to your ECS instance.
    1. Use one of the following methods to download the source code package. In this example, the version of the MySQL source code package is 5.6.24.

      • wget https://dev.mysql.com/get/Downloads/mysql-5.6.24.tar.gz
      • wget https://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.24.tar.gz
    2. tar xvf mysql-5.6.24.tar.gz -C  /usr/local/src
    3. cd /usr/local/src/mysql-5.6.24
    4. cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
      -DMYSQL_DATADIR=/mnt/data \
      -DSYSCONFDIR=/etc \
      -DWITH_INNOBASE_STORAGE_ENGINE=1 \
      -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
      -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
      -DWITH_READLINE=1 \
      -DWITH_SSL=system \
      -DWITH_ZLIB=system \
      -DWITH_LIBWRAP=0 \
      -DMYSQL_TCP_PORT=3306 \
      -DDEFAULT_CHARSET=utf8 \
      -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
      -DDEFAULT_COLLATION=utf8_general_ci \
      -DWITH_SYSTEMD=1 \
      -DINSTALL_SYSTEMD_UNITDIR=/usr/lib/systemd/system 
    5. make && make install
  3. Configure MySQL.
    1. Run the following command to change the group of the installation directory to mysql:
      chown -R mysql:mysql /usr/local/mysql/
    2. Run the following commands to initialize the database and copy the configuration file:
      1. cd /usr/local/mysql
      2. /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mnt/data/
      3. mv /etc/my.cnf /etc/my.cnf.bak
      4. cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
    3. Run the following command to change the installation and data storage paths:
      echo -e "basedir = /usr/local/mysql\ndatadir = /mnt/data\n" >> /etc/my.cnf
    4. Modify the MySQL configuration file.
      1. Run the vi /usr/lib/systemd/system/mysql.service command to open the MySQL configuration file.
      2. Press the I key and add the following content to the file:
        [Unit]
        Description=MySQL Community Server
        After=network.target
        After=syslog.target
        
        [Install]
        WantedBy=multi-user.target
        Alias=mysql.service
        
        [Service]
        User=mysql
        Group=mysql
        PermissionsStartOnly=true
        ExecStart=/usr/local/mysql/bin/mysqld
        TimeoutSec=600
        Restart=always
        PrivateTmp=false
      3. Press the Esc key, enter :wq, and then press the Enter key to save and close the file.
  4. Run the following commands to set the PATH environment variable:
    1. echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
    2. source /etc/profile.d/mysql.sh
  5. Run the following commands to start MySQL and set it to run at startup:
    1. systemctl start mysql
    2. systemctl enable mysql
  6. Change the MySQL root password. Run the following command and follow the on-screen instructions to set the password:
    mysqladmin -u root password
    If an error related to mysql.sock is reported when you attempt to connect to MySQL, the following problems may occur:
    • MySQL is not running properly. You can run the service mysqld status command to check whether MySQL is running.
    • The configured path to the mysql.sock file is incorrect. You must open the /etc/my.cnf file, and change the mysql.sock file path specified in the socket value to the actual mysql.sock file path. Then, restart MySQL.
  7. Run the following command to log on to the MySQL database:
    mysql -uroot -p
    Test the database
  8. Run the \q command to log off from the MySQL database.

Step 4. Install PHP

  1. Run the following command to install the dependency package:
    yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel -y
  2. Run the following commands to download, decompress, and compile the GA version of the source code package:
    Note To download the GA version of the PHP source code package, ensure that the network environment is good. If the download fails, run the cd command and then the rm -rf mirror command to download the GA version of the PHP source code package again.
    1. cd
    2. wget https://cn2.php.net/get/php-7.0.32.tar.bz2/from/this/mirror
    3. cp mirror php-7.0.32.tar.bz2
    4. tar xvf php-7.0.32.tar.bz2 -C /usr/local/src
    5. cd /usr/local/src/php-7.0.32
    6. ./configure --prefix=/usr/local/php \
      --with-config-file-scan-dir=/etc/php.d \
      --with-apxs2=/usr/local/apache2/bin/apxs \
      --with-config-file-path=/etc \
      --with-pdo-mysql=mysqlnd \
      --with-mysqli=/usr/local/mysql/bin/mysql_config \
      --enable-mbstring \
      --with-freetype-dir \
      --with-jpeg-dir \
      --with-png-dir \
      --with-zlib \
      --with-libxml-dir=/usr \
      --with-openssl \
      --enable-xml \
      --enable-sockets \
      --enable-fpm \
      --with-bz2
    7. make && make install
    Note If the ECS instance does not have sufficient memory, terminate PHP extensions that are not needed to save memory when you configure PHP. For example, you can add the --disable-fileinfo option to the ./configure command to terminate the fileinfo extension.
  3. Run the following command to copy the PHP configuration file:
    cp php.ini-production /etc/php.ini
  4. Run the vi /usr/local/apache2/conf/httpd.conf command to open the Apache configuration file, and then press the I key to switch to the edit mode.
    1. Find the ServerName parameter and add ServerName localhost:80.
      Add parameter content
    2. Find the Directory parameter. Add a number sign (#) before Require all denied, start a new line, and then add Require all granted.
      Add content
    3. Find DirectoryIndex index.html and replace it with DirectoryIndex index.php index.html.
      Replace the content
    4. Find the following content:
      Find the following content
      Add the following content:
      AddType application/x-httpd-php .php
      AddType application/x-httpd-php-source .phps
      The configurations are as shown in the following figure.Added
    5. Press the Esc key, enter :wq, and press the Enter key to save and close the Apache configuration file.
  5. Add Apache support for PHP parsing.
    1. Run the following command to open the index.php file:
      vi /usr/local/apache2/htdocs/index.php
    2. Press the I key to switch to the edit mode. Add the following content to the file:
      <? php
      phpinfo();
      ? >
    3. Press the Esc key, enter :wq, and then press the Enter key to save and close the index.php file.
    4. Run the following command to restart Apache:
      systemctl restart httpd
  6. Enter http://<Public IP address of the ECS instance> in the address bar of your browser and press the Enter key.
    If the following page is displayed, PHP parsing is working properly.PHP parsing is working properly

Step 5. Install phpMyAdmin

  1. Run the following commands to prepare a directory to store phpMyAdmin data:
    1. cd
    2. mkdir -p /usr/local/apache2/htdocs/phpmyadmin
  2. Run the following commands to download and decompress the phpMyAdmin package:
    1. wget https://files.phpmyadmin.net/phpMyAdmin/4.0.10.20/phpMyAdmin-4.0.10.20-all-languages.zip
    2. unzip phpMyAdmin-4.0.10.20-all-languages.zip
  3. Run the following command to copy the phpMyAdmin files to the prepared directory:
    mv phpMyAdmin-4.0.10.20-all-languages/*  /usr/local/apache2/htdocs/phpmyadmin
  4. Enter http://<Public IP address of the ECS instance>/phpmyadmin in the address bar of your browser and press the Enter key to go to the logon page of phpMyAdmin.
    If the following page is displayed, phpMyAdmin is installed.phpMyAdmin is installed
  5. Enter the MySQL username and password. Click Go.
    Enter the MySQL username and password