edit-icon download-icon

Build LNMP environment under CentOS 6

Last Updated: Feb 07, 2018

This article describes how to build LNMP environment under CentOS on an ECS instance with the basic configuration.

LNMP means:

  • Linux: A family of free and open-source UNIX-like software operating systems (OS).
  • Nginx: A lightweight HTTP and reverse proxy server.
  • MySQL: A relational database management system.
  • PHP: A scripting language that is especially suited for web development.

This method is applicable to individual users who are familiar with Linux, but new to website construction by using Alibaba Cloud ECS.

Follow these steps to build LNMP environment on an ECS instance:

  1. Prepare the compiling environment
  2. Install Nginx
  3. Install MySQL
  4. Install PHP-FPM
  5. Test

Step 1. Prepare the compiling environment

Follow these steps to prepare the compiling environment.

  1. Check the version of the operating system.

    1. # cat /etc/redhat-release
    2. CentOS release 6.5 (Final)

    Note: This article is based on a Linux instance running CentOS 6.5. You may have different OS versions. The same is applicable to the Nginx, MySQL, and PHP versions mentioned in the following paragraphs.

  2. Disable SELINUX: Run the command to modify the configuration file, which will permanently take effect after restarting the service.

    1. # sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config

    Run the command to make the configuration take effect immediately.

    1. # setenforce 0
  3. Add a security rule to accept Internet access to the Web server on the instance. For more information, see Add a security group rule.

Step 2: Install Nginx

Nginx is a small and highly-efficient Web server based on Linux. Follow these steps to install Nginx:

  1. Add a user to run the Nginx service process.

    1. # groupadd -r nginx
    2. # useradd -r -g nginx nginx
  2. Download the source code package, decompress it, and then compile.

    1. # wget http://nginx.org/download/nginx-1.10.2.tar.gz
    2. # tar xvf nginx-1.10.2.tar.gz -C /usr/local/src
    3. # yum groupinstall "Development tools"
    4. # yum -y install gcc wget gcc-c++ automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel
    5. # cd /usr/local/src/nginx-1.10.2
    6. # ./configure \
    7. --prefix=/usr/local/nginx \
    8. --sbin-path=/usr/sbin/nginx \
    9. --conf-path=/etc/nginx/nginx.conf \
    10. --error-log-path=/var/log/nginx/error.log \
    11. --http-log-path=/var/log/nginx/access.log \
    12. --pid-path=/var/run/nginx.pid \
    13. --lock-path=/var/run/nginx.lock \
    14. --http-client-body-temp-path=/var/tmp/nginx/client \
    15. --http-proxy-temp-path=/var/tmp/nginx/proxy \
    16. --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
    17. --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
    18. --http-scgi-temp-path=/var/tmp/nginx/scgi \
    19. --user=nginx \
    20. --group=nginx \
    21. --with-pcre \
    22. --with-http_v2_module \
    23. --with-http_ssl_module \
    24. --with-http_realip_module \
    25. --with-http_addition_module \
    26. --with-http_sub_module \
    27. --with-http_dav_module \
    28. --with-http_flv_module \
    29. --with-http_mp4_module \
    30. --with-http_gunzip_module \
    31. --with-http_gzip_static_module \
    32. --with-http_random_index_module \
    33. --with-http_secure_link_module \
    34. --with-http_stub_status_module \
    35. --with-http_auth_request_module \
    36. --with-mail \
    37. --with-mail_ssl_module \
    38. --with-file-aio \
    39. --with-ipv6 \
    40. --with-http_v2_module \
    41. --with-threads \
    42. --with-stream \
    43. --with-stream_ssl_module
    44. # make && make install
    45. # mkdir -pv /var/tmp/nginx/client
  3. Add a SysV startup script.

    1. # vim /etc/init.d/nginx
    2. #!/bin/sh
    3. #
    4. # nginx - this script starts and stops the nginx daemon
    5. #
    6. # chkconfig: - 85 15
    7. # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
    8. # proxy and IMAP/POP3 proxy server
    9. # processname: nginx
    10. # config: /etc/nginx/nginx.conf
    11. # config: /etc/sysconfig/nginx
    12. # pidfile: /var/run/nginx.pid
    13. # Source function library.
    14. . /etc/rc.d/init.d/functions
    15. # Source networking configuration.
    16. . /etc/sysconfig/network
    17. # Check that networking is up.
    18. [ "$NETWORKING" = "no" ] && exit 0
    19. nginx="/usr/sbin/nginx"
    20. prog=$(basename $nginx)
    21. NGINX_CONF_FILE="/etc/nginx/nginx.conf"
    22. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
    23. lockfile=/var/lock/subsys/nginx
    24. start() {
    25. [ -x $nginx ] || exit 5
    26. [ -f $NGINX_CONF_FILE ] || exit 6
    27. echo -n $"Starting $prog: "
    28. daemon $nginx -c $NGINX_CONF_FILE
    29. retval=$?
    30. echo
    31. [ $retval -eq 0 ] && touch $lockfile
    32. return $retval
    33. }
    34. stop() {
    35. echo -n $"Stopping $prog: "
    36. killproc $prog -QUIT
    37. retval=$?
    38. echo
    39. [ $retval -eq 0 ] && rm -f $lockfile
    40. return $retval
    41. killall -9 nginx
    42. }
    43. restart() {
    44. configtest || return $?
    45. stop
    46. sleep 1
    47. start
    48. }
    49. reload() {
    50. configtest || return $?
    51. echo -n $"Reloading $prog: "
    52. killproc $nginx -HUP
    53. RETVAL=$?
    54. echo
    55. }
    56. force_reload() {
    57. restart
    58. }
    59. configtest() {
    60. $nginx -t -c $NGINX_CONF_FILE
    61. }
    62. rh_status() {
    63. status $prog
    64. }
    65. rh_status_q() {
    66. rh_status >/dev/null 2>&1
    67. }
    68. case "$1" in
    69. start)
    70. rh_status_q && exit 0
    71. $1
    72. ;;
    73. stop)
    74. rh_status_q || exit 0
    75. $1
    76. ;;
    77. restart|configtest)
    78. $1
    79. ;;
    80. reload)
    81. rh_status_q || exit 7
    82. $1
    83. ;;
    84. force-reload)
    85. force_reload
    86. ;;
    87. status)
    88. rh_status
    89. ;;
    90. condrestart|try-restart)
    91. rh_status_q || exit 0
    92. ;;
    93. *)
    94. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
    95. exit 2
    96. esac
  4. Grant the permission to run the script.

    1. # chmod +x /etc/init.d/nginx
  5. Add Nginx to the service management list, and set it to automatically start on startup.

    1. # chkconfig --add nginx
    2. # chkconfig nginx on
  6. Start the service.

    1. # service nginx start
  7. Access the instance by using http://Public IP address. If the following page appears, Nginx is installed successfully.
    Nginx is installed successfully

Step3. Install MySQL

Follow these steps to install MySQL.

  1. Prepare the compiling environment.

    1. # yum groupinstall "Server Platform Development" "Development tools" -y
    2. # yum install cmake -y
  2. Create a directory to store the data of MySQL.

    1. # mkdir /mnt/data
    2. # groupadd -r mysql
    3. # useradd -r -g mysql -s /sbin/nologin mysql
    4. # id mysql
    5. uid=497(mysql) gid=498(mysql) groups=498(mysql)
  3. Change the owner and group of the data directory.

    1. # chown -R mysql:mysql /mnt/data
  4. Decompress and compile the stable source code package downloaded from MySQL official website. In this article, we use version 5.6.24.

    1. # tar xvf mysql-5.6.24.tar.gz -C /usr/local/src
    2. # cd /usr/local/src/mysql-5.6.24
    3. # cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
    4. -DMYSQL_DATADIR=/mnt/data \
    5. -DSYSCONFDIR=/etc \
    6. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    7. -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
    8. -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
    9. -DWITH_READLINE=1 \
    10. -DWITH_SSL=system \
    11. -DWITH_ZLIB=system \
    12. -DWITH_LIBWRAP=0 \
    13. -DMYSQL_TCP_PORT=3306 \
    14. -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
    15. -DDEFAULT_CHARSET=utf8 \
    16. -DDEFAULT_COLLATION=utf8_general_ci
    17. # make && make install
  5. Change the group of the installation directory to mysql.

    1. # chown -R mysql:mysql /usr/local/mysql/
  6. Initialize the database.

    1. # /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mnt/data/

    Note: After completing the minimum installation of the CentOS 6.5 operating system, a my.cnf file is generated under the /etc directory. You must rename this file. For example, rename it as /etc/my.cnf.bak. Otherwise, this file will interfere with the correct configuration for MySQL source code installation, leading to MySQL start failure.

  7. Copy the configuration file and startup script.

    1. # cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    2. # chmod +x /etc/init.d/mysqld
    3. # cp support-files/my-default.cnf /etc/my.cnf
  8. Set automatic start on startup.

    1. # chkconfig mysqld on
    2. # chkconfig --add mysqld
  9. Modify the installation path and data storage path in the configuration file.

    1. # echo -e "basedir = /usr/local/mysql\ndatadir = /mnt/data\n" >> /etc/my.cnf
  10. 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
  11. Start the service.

    1. # service mysqld start
    2. # mysql -h 127.0.0.1

Step 4. Install PHP-FPM

Nginx cannot process PHP. As a Web server, when Nginx receives a request, it does not support directly calling or parsing the external program. It must use FastCGI to call such programs. However, in case of PHP requests, Nginx will transfer the request to a PHP interpreter, and return the result to the client. PHP-FPM is a FastCGI process manager that supports parsing PHP code. PHP-FPM provides better PHP process management methods, which can effectively control the memory and process, and can support smoothly reloading PHP configuration.

Follow these steps to install PHP-FPM.

  1. Install dependency package.

    1. # yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel
  2. Decompress the source code package downloaded from the official website, and then compile and install it.

    1. # tar xvf php-5.6.23.tar.bz2 -C /usr/local/src
    2. # cd /usr/local/src/php-5.6.23
    3. # ./configure --prefix=/usr/local/php \
    4. --with-config-file-scan-dir=/etc/php.d \
    5. --with-config-file-path=/etc \
    6. --with-mysql=/usr/local/mysql \
    7. --with-mysqli=/usr/local/mysql/bin/mysql_config \
    8. --enable-mbstring \
    9. --with-freetype-dir \
    10. --with-jpeg-dir \
    11. --with-png-dir \
    12. --with-zlib \
    13. --with-libxml-dir=/usr \
    14. --with-openssl \
    15. --enable-xml \
    16. --enable-sockets \
    17. --enable-fpm \
    18. --with-mcrypt \
    19. --with-bz2
    20. # make && make install
  3. Add the PHP and PHP-FPM configuration files.

    1. # cp /usr/local/src/php-5.6.23/php.ini-production /etc/php.ini
    2. # cd /usr/local/php/etc/
    3. # cp php-fpm.conf.default php-fpm.conf
    4. # sed -i 's@;pid = run/php-fpm.pid@pid = /usr/local/php/var/run/php-fpm.pid@' php-fpm.conf
  4. Add the PHP-FPM startup script.

    1. # cp /usr/local/src/php-5.6.23/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    2. # chmod +x /etc/init.d/php-fpm
  5. Add PHP-FPM to the service list, and set it to automatically start on startup.

    1. # chkconfig --add php-fpm
    2. # chkconfig --list php-fpm
    3. # chkconfig php-fpm on
  6. Start the service.

    1. # service php-fpm start
  7. Follow these steps to configure Nginx to support fastcgi:

    1. Back up the default configuration file.
      1. # cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak
      2. # cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf
    2. Edit /etc/nginx/nginx.conf:
      • Add a home page in the PHP format into the supported home page formats as shown:
        1. location / {
        2. root /usr/local/nginx/html;
        3. index index.php index.html index.htm;
        4. }
      • Delete comments in front of the following content:
        1. location ~ \.php$ {
        2. root /usr/local/nginx/html;
        3. fastcgi_pass 127.0.0.1:9000;
        4. fastcgi_index index.php;
        5. fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
        6. include fastcgi_params;
        7. }
    3. Reload the Nginx configuration file.
      1. # service nginx reload
  8. Create an index.php test page under /usr/local/nginx/html/, the content of which is shown as follows:

    1. # cat index.php
    2. <?php
    3. $conn=mysql_connect('127.0.0.1','root','');
    4. if ($conn){
    5. echo "LNMP platform connect to mysql is successful!";
    6. }else{
    7. echo "LNMP platform connect to mysql is failed!";
    8. }
    9. phpinfo();
    10. ?>

Step 5. Test

Access the instance by using http://Public IP address/index.php. If the following page appears, LNMP environment is built successfully.
PHP is installed successfully

Thank you! We've received your feedback.