This topic describes how to manually build an LNMP environment on an Elastic Compute Service (ECS) instance that runs a CentOS 6 operating system. LNMP is an acronym of the names of its original four open source components: Linux operating system, NGINX web server, MySQL relational database management system, and PHP programming language.

Prerequisites

  • An instance that runs CentOS 6 is created. For more information, see Create an instance by using the wizard.
  • An inbound rule is added to a security group of the ECS instance to allow traffic on port 80. For more information, see Add a security group rule.
  • The CentOS 6 source address is changed. For more information, see Change the CentOS 6 source address.
    Note CentOS 6 has reached its end of life (EOL). The CentOS 6 source has been removed from the http://mirror.centos.org/centos-6/ source address in compliance with the CentOS community rules. You must manually change the CentOS 6 source address to ensure that the YUM repository is available. Otherwise, errors are reported when you run yum commands.

Background information

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

This topic describes how to manually build an LNMP environment. You can also purchase an LNMP image in Alibaba Cloud Marketplace and create an ECS instance from the image to build websites.

The following configurations and software versions are used in the example:

  • Instance type: ecs.c6.large
  • Operating system: a CentOS 6.8 32-bit public image
    Note If you are using a 32-bit operating system, select an instance type with up to 4 GiB of memory.
  • NGINX: nginx 1.10.2
  • MySQL: MySQL 5.6.24
  • PHP: PHP 5.6.23
  • Network type: Virtual Private Cloud (VPC)
  • IP address: a public IP address

If you use software versions different from the preceding ones, you may need to adjust commands and parameter settings.

Step 1: Install and configure NGINX

Important To prevent unexpected risks, we recommend that you perform operations as a regular user, instead of an administrator. If the regular user does not have sudo permissions, grant the permissions to the user. For more information, see the "How do I grant sudo permissions to a regular user?" question in the FAQ section.
  1. Connect to the Linux instance.
  2. Run the following commands in sequence to add a user to run the NGINX service process:
    sudo groupadd -r nginx
    sudo useradd -r -g nginx nginx
  3. Download the source code package and then decompress and compile the package.
    1. Run the following command to download the source code package:
      sudo wget http://nginx.org/download/nginx-1.10.2.tar.gz
    2. Run the following command to decompress the source code package:
      sudo tar xvf nginx-1.10.2.tar.gz -C /usr/local/src
    3. Run the following commands in sequence to install compilation tools:
      sudo yum -y groupinstall "Development tools"
      sudo yum -y install gcc wget gcc-c++ automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel
    4. Run the following command to go to the directory of the NGINX source code package:
      sudo cd /usr/local/src/nginx-1.10.2
    5. Run the following commands in sequence to compile the source code:
      sudo ./configure \
      --prefix=/usr/local/nginx \
      --sbin-path=/usr/sbin/nginx \
      --conf-path=/etc/nginx/nginx.conf \
      --error-log-path=/var/log/nginx/error.log \
      --http-log-path=/var/log/nginx/access.log \
      --pid-path=/var/run/nginx.pid \
      --lock-path=/var/run/nginx.lock \
      --http-client-body-temp-path=/var/tmp/nginx/client \
      --http-proxy-temp-path=/var/tmp/nginx/proxy \
      --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
      --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
      --http-scgi-temp-path=/var/tmp/nginx/scgi \
      --user=nginx \
      --group=nginx \
      --with-pcre \
      --with-http_v2_module \
      --with-http_ssl_module \
      --with-http_realip_module \
      --with-http_addition_module \
      --with-http_sub_module \
      --with-http_dav_module \
      --with-http_flv_module \
      --with-http_mp4_module \
      --with-http_gunzip_module \
      --with-http_gzip_static_module \
      --with-http_random_index_module \
      --with-http_secure_link_module \
      --with-http_stub_status_module \
      --with-http_auth_request_module \
      --with-mail \
      --with-mail_ssl_module \
      --with-file-aio \
      --with-ipv6 \
      --with-http_v2_module \
      --with-threads \
      --with-stream \
      --with-stream_ssl_module
      sudo make && make install
    6. Run the following command to create a directory:
      sudo mkdir -p /var/tmp/nginx/client
  4. Add the SysV startup script.
    1. Run the following command to open the SysV startup script file:
      sudo vim /etc/init.d/nginx
    2. Press the I key to enter the edit mode and add the following content to the script file:
      #!/bin/sh 
      # 
      # nginx - this script starts and stops the nginx daemon 
      # 
      # chkconfig:   - 85 15 
      # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
      #               proxy and IMAP/POP3 proxy server 
      # processname: nginx 
      # config:      /etc/nginx/nginx.conf 
      # config:      /etc/sysconfig/nginx 
      # pidfile:     /var/run/nginx.pid 
      # Source function library. 
      . /etc/rc.d/init.d/functions
      # Source networking configuration. 
      . /etc/sysconfig/network
      # Check that networking is up. 
      [ "$NETWORKING" = "no" ] && exit 0
      nginx="/usr/sbin/nginx"
      prog=$(basename $nginx)
      NGINX_CONF_FILE="/etc/nginx/nginx.conf"
      [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
      lockfile=/var/lock/subsys/nginx
      start() {
          [ -x $nginx ] || exit 5
          [ -f $NGINX_CONF_FILE ] || exit 6
          echo -n $"Starting $prog: " 
          daemon $nginx -c $NGINX_CONF_FILE
          retval=$?
          echo 
          [ $retval -eq 0 ] && touch $lockfile
          return $retval
      }
      stop() {
          echo -n $"Stopping $prog: " 
          killproc $prog -QUIT
          retval=$?
          echo 
          [ $retval -eq 0 ] && rm -f $lockfile
          return $retval
      killall -9 nginx
      }
      restart() {
          configtest || return $?
          stop
          sleep 1
          start
      }
      reload() {
          configtest || return $?
          echo -n $"Reloading $prog: " 
          killproc $nginx -HUP
      RETVAL=$?
          echo 
      }
      force_reload() {
          restart
      }
      configtest() {
      $nginx -t -c $NGINX_CONF_FILE
      }
      rh_status() {
          status $prog
      }
      rh_status_q() {
          rh_status >/dev/null 2>&1
      }
      case "$1" in
          start)
              rh_status_q && exit 0
          $1
              ;;
          stop)
              rh_status_q || exit 0
              $1
              ;;
          restart|configtest)
              $1
              ;;
          reload)
              rh_status_q || exit 7
              $1
              ;;
          force-reload)
              force_reload
              ;;
          status)
              rh_status
              ;;
          condrestart|try-restart)
              rh_status_q || exit 0
                  ;;
          *)
            echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
              exit 2
      esac
    3. Press the Esc key, enter :wq, and then press the Enter key to save and close the SysV startup script file.
  5. Run the following command to grant execute permissions on the script:
    sudo chmod +x /etc/init.d/nginx
  6. Run the following commands in sequence to add NGINX to the list of system services and configure NGINX to start on system startup:
    sudo chkconfig --add nginx
    sudo chkconfig  nginx on
  7. Run the following command to start NGINX:
    sudo service nginx start
  8. Test whether NGINX is installed.
    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 that you created and copy its public IP address from the IP Address column.
    4. In the browser address bar, enter the IP address and press the Enter key.
      The following page indicates that NGINX is installed. nginx_welcome

Step 2: Install and configure MySQL

  1. Run the following commands in sequence to prepare the compilation environment:
    sudo yum groupinstall "Server Platform Development"  "Development tools" -y
    sudo yum install cmake -y
  2. Prepare a directory to store MySQL data.
    1. Run the following command to create a directory to store MySQL data:
      sudo mkdir /mnt/data
    2. Run the following command to create a user group named mysql:
      sudo groupadd -r mysql
    3. Run the following command to create a user named mysql:
      sudo useradd -r -g mysql -s /sbin/nologin mysql
    4. Run the following command to check whether the user is created:
      sudo id mysql
    5. Run the following command to change the group and user of the MySQL data directory to mysql:
      sudo chown -R mysql:mysql /mnt/data
  3. Download the latest stable version of the source code package, decompress the package, and then compile the source code.
    1. Run one of the following commands to download the source code package:
      • sudo wget https://dev.mysql.com/get/Downloads/mysql-5.6.24.tar.gz
      • sudo wget https://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.24.tar.gz
    2. Run the following command to decompress the source code package:
      sudo tar xvf mysql-5.6.24.tar.gz -C  /usr/local/src
    3. Run the following command to go to the directory of the MySQL source code package:
      cd /usr/local/src/mysql-5.6.24
    4. Run the following commands in sequence to compile the source code:
      sudo 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 \
      -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
      -DDEFAULT_CHARSET=utf8 \
      -DDEFAULT_COLLATION=utf8_general_ci
      sudo make && make install
  4. Configure MySQL.
    1. Run the following command to change the group and user of the MySQL installation directory to mysql:
      sudo chown -R mysql:mysql /usr/local/mysql/
    2. Run the following commands in sequence to initialize MySQL:
      cd /usr/local/mysql
      sudo /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mnt/data/
      Note After MySQL is installed on CentOS 6.8, a file named my.cnf appears in the /etc directory. You must change the file name. For example, change the file name to /etc/my.cnf.bak. Otherwise, the file interferes with the configuration process of MySQL and causes MySQL to fail to start.
    3. Run the following commands in sequence to copy the configuration file of MySQL:
      sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
      sudo cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
    4. Run the following command to grant execute permissions on the startup script of MySQL:
      sudo chmod +x /etc/init.d/mysqld
    5. Run the following commands in sequence to add MySQL to the list of system services and configure MySQL to start on system startup:
      sudo chkconfig --add mysqld
      sudo chkconfig mysqld  on 
    6. Run the following command to change the installation path and the data storage path of MySQL in the configuration file:
      sudo echo -e "basedir = /usr/local/mysql\ndatadir = /mnt/data\n" >> /etc/my.cnf
    7. Run the following commands to configure the PATH environment variable:
      sudo echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh      
      sudo source /etc/profile.d/mysql.sh
  5. Run the following command to start MySQL:
    sudo service mysqld start 
  6. Run the following command to connect to MySQL for testing:
    sudo mysql -h 127.0.0.1

Step 3: Install PHP-FPM

NGINX acts as a web server and does not directly call or parse external programs when it receives requests. It must use Fast Common Gateway Interface (FastCGI) to call external programs. However, in case of a PHP request, NGINX transfers the request to a PHP interpreter and returns the result to the client. PHP-FPM is a FastCGI process manager that can parse PHP code. PHP-FPM provides better PHP process management methods to effectively control memory and processes and smoothly reload PHP configurations.

  1. Run the following command to install the dependency:
    sudo yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel
  2. Download the latest stable version of the source code package, decompress the package, and then compile the source code.
    1. Run the following command to download the source code package:
      sudo wget http://cn2.php.net/get/php-5.6.23.tar.bz2/from/this/mirror
    2. Run the following commands in sequence to decompress the source code package:
      sudo cp mirror php-5.6.23.tar.bz2
      sudo tar xvf php-5.6.23.tar.bz2 -C /usr/local/src
    3. Run the following command to go to the directory of the PHP source code package:
      cd /usr/local/src/php-5.6.23
    4. Run the following commands in sequence to compile the source code:
      sudo ./configure --prefix=/usr/local/php \
      --with-config-file-scan-dir=/etc/php.d \
      --with-config-file-path=/etc \
      --with-mysql=/usr/local/mysql \
      --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-mcrypt \
      --with-bz2
      sudo make && make install
  3. Configure PHP.
    1. Run the following commands in sequence to add the PHP and PHP-FPM configuration files:
      sudo cp /usr/local/src/php-5.6.23/php.ini-production /etc/php.ini
      cd /usr/local/php/etc/
      sudo cp php-fpm.conf.default php-fpm.conf
      sudo sed -i 's@;pid = run/php-fpm.pid@pid = /usr/local/php/var/run/php-fpm.pid@' php-fpm.conf
    2. Run the following command to add the PHP-FPM startup script:
      sudo cp /usr/local/src/php-5.6.23/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    3. Run the following command to grant execute permissions on the PHP-FPM startup script:
      sudo chmod +x /etc/init.d/php-fpm
    4. Run the following commands in sequence to add PHP-FPM to the list of system services and configure PHP-FPM to start on system startup:
      sudo chkconfig --add php-fpm     
      sudo chkconfig --list php-fpm    
      sudo chkconfig php-fpm on
  4. Run the following command to start PHP-FPM:
    sudo service php-fpm start
  5. Add the NGINX support for FastCGI.
    1. Run the following command to back up the default NGINX configuration file:
      sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
    2. Run the following command to add the NGINX configuration file:
      sudo cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf
    3. Run the following command to open the NGINX configuration file:
      sudo vim /etc/nginx/nginx.conf
    4. Press the I key and add index.php to the index line to support .php index page files.
      Example:
      location / {
        root   /usr/local/nginx/html;
        index  index.php index.html index.htm;
      }
    5. Delete the annotation symbol in front of the following content:
      location ~ \.php$ {
       root html;
       fastcgi_pass 127.0.0.1:9000;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
       include fastcgi_params;
      }
    6. Change root html; to root /usr/local/nginx/html;.
    7. Change fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; to fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;.
    8. Press the Esc key, enter :wq, and then press the Enter key to save and close the NGINX configuration file.
  6. Run the following command to reload the NGINX configuration file:
    sudo service nginx reload
  7. Modify the index.php file.
    1. Run the following command to open the index.php file:
      sudo vim /usr/local/nginx/html/index.php
    2. Press the I key and enter the following content:
      <?php
      $conn=mysql_connect('127.0.0.1','root','');
      if ($conn){
      echo "LNMP platform connect to mysql is successful!";
      }else{
      echo "LNMP platform connect to mysql is failed!";
      }
      phpinfo();
      ?>
    3. Press the Esc key, enter :wq, and then press the Enter key to save and close the index.php file.

Step 4: Test the connection to the LNMP environment

  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 where the LNMP environment is built and copy the public IP address of the instance from the IP Address column.
  4. In the browser address bar, enter the IP address and press the Enter key.
    The following page indicates that the LNMP environment is built. lnmp_success