This topic describes how to manually build an LNMP environment on an ECS instance that runs the CentOS 6 operating system. LNMP represents a combination of Linux, NGINX, MySQL, and PHP.

Prerequisites

  • You must have registered an Alibaba Cloud account. If not, create a new Alibaba Cloud account first.
  • An inbound rule is added to a security group of the ECS instance to allow traffic on port 80. If port 80 is not enabled, enable the port first. For more information, see Add security group rules.
    Direction Authorization policy Protocol type Port range Priority Authorization type Authorized object
    Inbound Allow HTTP (80) 80/80 1 IPv4 CIDR block The public IP addresses of clients to be allowed to access the LNMP environment. Separate multiple IP addresses with commas (,).

    A value of 0.0.0.0/0 indicates that all IP addresses are allowed to access the LNMP environment.

Background information

This topic is intended for individual users who are familiar with Linux, but new to web development on Alibaba Cloud ECS instances.

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

The procedure described in this topic is applicable to the following instance configurations and software versions:

  • Instance type: ecs.c6.large
  • Operating system: public images running CentOS 6.8 32-bit
    Note If you are using a 32-bit operating system, do not select instance types that have a memory capacity greater than 4 GiB.
  • NGINX: nginx-1.10.2
  • MySQL: MySQL 5.6.24
  • PHP: PHP 5.6.23
  • Network type: VPC
  • IP address: public IP address

The commands and parameters used in this topic may vary based on your software version.

Procedure

Perform the following steps to build an LNMP environment on an ECS instance:

Step 1: Prepare the compilation environment

  1. Create an ECS instance.
  2. Connect to the Linux instance.
  3. Run the cat /etc/redhat-release command to check the system version.Check the system version

Step 2: Install and configure NGINX

  1. Run the following commands in sequence to add a user to run the NGINX service process:
    groupadd -r nginx
    useradd -r -g nginx  nginx
  2. Download the source code package, and decompress and compile the package.
    1. Run the following command to download the source code package:
      wget http://nginx.org/download/nginx-1.10.2.tar.gz
    2. Run the following command to decompress the source code package:
      tar xvf nginx-1.10.2.tar.gz -C /usr/local/src
    3. Run the following commands in sequence to install compilation tools:
      yum groupinstall "Development tools"
      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:
      cd /usr/local/src/nginx-1.10.2
    5. Run the following commands in sequence to compile the source code:
      ./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
      make && make install
    6. Run the following command to create a directory:
      mkdir -p /var/tmp/nginx/client
  3. Add the SysV startup script.
    1. Run the vi /etc/init.d/nginx command to open the SysV startup script file.
    2. Press the I key 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.
  4. Run the following command to grant executable permissions to the script:
    chmod a+x /etc/init.d/ossfs
  5. Run the following commands in sequence to add NGINX to the service management list and enable it to run at system startup:
    chkconfig --add nginx
    chkconfig  nginx on
  6. Run the following command to start NGINX:
    service nginx start
  7. 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 target instance, 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 response indicates that NGINX is installed.nginx_welcome

Step 3: Install and configure MySQL

  1. Run the following commands in sequence to prepare the compilation environment:
    yum groupinstall "Server Platform Development"  "Development tools" -y
    yum install cmake -y
  2. Create a directory to store MySQL data.
    1. Run the mkdir /mnt/data command to create a directory to store MySQL data.
    2. Run the groupadd -r mysql command to create a user group named mysql.
    3. Run the useradd -r -g mysql -s /sbin/nologin mysql command to create a user named mysql.
    4. Run the id mysql command to check whether the user is created.
    5. Run the chown -R mysql:mysql /mnt/data command to change the group and user of the MySQL data directory to mysql.
  3. Download the latest stable version of the source code package, and decompress and compile it.
    1. Run one of the following commands to download the source code package:
      • 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. Run the following command to decompress the source code package:
      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 package:
      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 \
      make && make install
  4. Configure MySQL.
    1. Run the following command to change the group and user of the installation directory to mysql:
      chown -R mysql:mysql /usr/local/mysql/
    2. Run the following commands in sequence to initialize the database:
      cd /usr/local/mysql
      /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 under the /etc directory. You must change the file name to another one such as /etc/my.cnf.bak. Otherwise, the file will interfere with the configuration of MySQL and cause MySQL to fail to start.
    3. Run the following commands in sequence to copy the configuration file of MySQL:
      cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
      cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
    4. Run the following command to grant executable permissions to the startup script of MySQL:
      chmod a+x /etc/init.d/ossfs
    5. Run the following commands in sequence to add MySQL to the service management list and enable it to run at system startup:
      chkconfig --add mysqld
      chkconfig mysqld  on 
    6. 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
    7. Run the following commands in sequence to set the PATH environment variable:
      echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh      
      source /etc/profile.d/mysql.sh
  5. Run the following command to start MySQL:
    service mysqld start 
  6. Run the following command to connect to the MySQL database for testing:
    mysql -h 127.0.0.1

Step 4: Install PHP-FPM

As a web server, NGINX does not directly call or parse external programs when it receives requests. It must use FastCGI to call such programs. However, in case of a PHP request, NGINX will transfer the request to a PHP interpreter, and return 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, which can effectively control the memory and processes and smoothly reload PHP configurations.

  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. Download the latest stable version of the source code package, and decompress and compile it.
    1. Run the following command to download the source code package:
      wget http://cn2.php.net/get/php-5.6.23.tar.bz2/from/this/mirror
      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 run the rm -rf mirror command to download the GA version of the PHP source code package again.
    2. Run the following commands in sequence to decompress the source code package:
      cp mirror php-5.6.23.tar.bz2
      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 package:
      ./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
      make && make install
  3. Configure PHP.
    1. Run the following commands in sequence to add the PHP and PHP-FPM configuration files:
      cp /usr/local/src/php-5.6.23/php.ini-production /etc/php.ini
      cd /usr/local/php/etc/
      cp php-fpm.conf.default php-fpm.conf
      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:
      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 executable permissions to the PHP-FPM startup script:
      chmod +x /etc/init.d/php-fpm
    4. Run the following commands in sequence to add PHP-FPM to the service management list and enable it to run at system startup:
      chkconfig --add php-fpm     
      chkconfig --list php-fpm    
      chkconfig php-fpm on
  4. Run the following command to start PHP-FPM:
    service php-fpm start
  5. Add the NGINX support for FastCGI.
    1. Run the following command to back up the default NGINX configuration file:
      cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
    2. Run the following command to add the NGINX configuration file:
      cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf
    3. Run the vi /etc/nginx/nginx.conf command to open the NGINX configuration file.
    4. Press the I key, and add a homepage in the PHP format to the supported homepages.
      Example:
      location / {
        root   /usr/local/nginx/html;
        index  index.php index.html index.htm;
      }
    5. Delete the annotation character 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 service nginx reload command to reload the NGINX configuration file.
  7. Modify the index.php file.
    1. Run the vi /usr/local/nginx/html/index.php command to open the index.php file.
    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 5: 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 target instance, 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 response indicates that the LNMP environment is built.lnmp_success