This topic describes how to build a LAMP stack on an ECS instance. LAMP is an acronym of the names of its four open-source components: the Linux operating system, Apache HTTP Server, MySQL relational database management system, and PHP programming language.

Prerequisites

You must have an Alibaba Cloud account before you follow the instructions provided in the tutorial. To create an Alibaba Cloud account, click Create an Alibaba Cloud account.

Background information

This example uses an ECS instance with the following configuration:
  • Uses the 64-bit CentOS 7.2 operating system
  • Uses a VPC network
  • Uses the public IP address of the ECS instance
This example chooses the following software versions. When you build a LAMP stack, choose software versions as needed.
  • Apache 2.4.37
  • 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 start the ECS instance to quickly build a website.

Procedure

Step 1. Prepare the compilation environment

Follow these steps to prepare the compilation environment:

  1. Create an instance by using the wizard.
  2. Connect to a Linux instance by using the Management Terminal.
  3. Run the cat /etc/redhat-release command to view the system version.

    View the system version
  4. Disable the firewall.
    1. Run the systemctl status firewalld command to check the firewall status.

      Check the firewall status
      • If the firewall status is inactive, the firewall is disabled.
      • If the firewall status is active, the firewall is enabled. In this example, the firewall is enabled. Therefore, you must disable the firewall.
    2. The firewall must be disabled. If the firewall has already been disabled, skip this step.
      • If you want to temporarily disable the firewall, run the systemctl stop firewalld command.
        Note This command temporarily disables the firewall. After you restart the Linux operating system, the firewall is enabled.
      • If you want to permanently disable the firewall, run the systemctl disable firewalld command.
        Note You can enable the firewall again. For more information, see the firewall site.
  5. Disable SELinux.
    1. Run the getenforce command to check the SELinux status.

      Check the SELinux status
      • If the SELinux status is Disabled, SELinux is disabled.
      • If the SELinux status is Enforcing, SELinux is enabled. In this example, SELinux is enabled. Therefore, you must disable SELinux.
    2. Disable SELinux. If SELinux has already been disabled, skip this step.
      • If you want to temporarily disable SELinux, run the setenforce 0 command.
        Note This command temporarily disables SELinux. After you restart the Linux operating system, SELinux is enabled.
      • If you want to permanently disable SELinux, run the vi /etc/selinux/config command to edit the configuration file of SELinux. Press Enter to run the command, move the cursor to the SELINUX=enforcing row, and press I to edit the configuration file. Change SELINUX=enforcing to SELINUX=disabled, press Esc, enter :wq, and then press Enter to save and close the configuration file.
        Note You can enable SELinux again. For more information, see the SELinux documentation.
    3. Restart the system to apply the settings.
  6. Add an inbound rule to the security group of the ECS instance to open the required port. For more information, see Add security group rules.

Step 2. Install Apache HTTP Server

Follow these steps to install Apache HTTP Server.

  1. Run the following commands to install the dependency package:
    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:
    1. wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.37.tar.gz
    2. wget https://mirrors.aliyun.com/apache/apr/apr-1.6.5.tar.gz
    3. wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz
    4. tar xvf httpd-2.4.37.tar.gz -C /usr/local/src
    5. tar xvf apr-1.6.5.tar.gz -C /usr/local/src
    6. tar xvf apr-util-1.6.1.tar.gz -C /usr/local/src
    Note The source code version is continuously upgraded. You can obtain the installation package path in the httpd source code installation package or the APR source code installation package.
  3. Run the following commands to move the APR and APR-until folders to the Apache srclib folder:
    1. cd /usr/local/src
    2. mv apr-1.6.5 httpd-2.4.37/srclib/apr
    3. mv apr-util-1.6.1 httpd-2.4.37/srclib/apr-util
  4. Run the following commands to compile the source code:
    1. cd /usr/local/src/httpd-2.4.37
    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 view the Apache version number.

    View the Apache version number
  7. Add the Apache configuration file.
    1. Run the vi /usr/lib/systemd/system/httpd.service command to open the configuration file.
    2. Press I and add the following content to the configuration 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 Esc, enter :wq, and then press Enter to save and close the Apache configuration file.
  8. Run the following commands to start Apache HTTP Server and enable Apache HTTP Server to automatically start when the operating system is started.
    1. systemctl start httpd
    2. systemctl enable httpd
  9. Check the installation status.
    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 target instance and copy its public IP address.
    4. Enter http:// The public IP address of the ECS instance into the address bar of your browser, and then press Enter.
      If the following page is displayed, it indicates that Apache HTTP Server has been started.
      The default welcome page

Step 3. Install the MySQL database management system

Follow these steps to install the MySQL database management system:

  1. Run the following commands to prepare the compiling 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.
    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
  4. Run the following commands to download, decompress, and compile the GA version of the source code:
    1. wget https://downloads.mysql.com/archives/get/file/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
  5. Run the following command to change the group of the installation directory to mysql:
    chown -R mysql:mysql /usr/local/mysql/
  6. 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
  7. 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
  8. Modify the MySQL configuration file.
    1. Run the vi /usr/lib/systemd/system/mysql.service command to open the MySQL configuration file.
    2. Press I and enter the following content:
      [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 Esc, enter :wq, and then press Enter to save and close the MySQL configuration file.
  9. 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
  10. Run the following commands to start MySQL and enable it to automatically start when the operating system is started:
    1. systemctl start mysql
    2. systemctl enable mysql
  11. Change the MySQL root password. Run the following command and set the password by following the instructions:
    mysqladmin -u root password
  12. Run the following command to log on to the MySQL database:
    mysql -uroot -p

    Test the database
  13. Run the \q command to log out of MySQL.

Step 4. Install PHP

Follow these steps to 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:
    1. cd
    2. wget http://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 space, terminate PHP extensions that you do not need when you configure PHP to save memory space. For example, you can add --disable-fileinfo 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 I to edit the configuration file.
    1. Find the ServerName parameter and add ServerName localhost:80 to the parameter.

      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 parameter 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
      After you add the content, the configuration is as follows.
      The content is added
    5. Press Esc, enter :wq, and then press Enter 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 I to edit the file. Add the following content to the file:
      <? php
      phpinfo();
      ? >
    3. Press Esc to exit the edit mode. Enter :wq to save and close the index.php file.
    4. Run the following command to restart Apache HTTP Server.
      systemctl restart httpd
  6. Enter http:// The public IP address of the ECS instance into the address bar of your browser and press Enter.
    If the following page is displayed, it indicates that PHP parsing is working properly.
    PHP parsing is working properly

Step 5. Install phpMyAdmin

Follow these steps to 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 command 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 file to the prepared storage directory:
    mv phpMyAdmin-4.0.10.20-all-languages/*  /usr/local/apache2/htdocs/phpmyadmin
  4. Enter http:// The public IP address of the ECS instance/phpmyadmin into the address bar of your browser, and press Enter to go to the logon page of phpMyAdmin.
    If the following page is displayed, it indicates that phpMyAdmin has been installed.
    PhpMyAdmin is installed
  5. Enter the MySQL username and password, and click Go.

    Enter the MySQL username and password