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 inbound rule is added to a security group of the ECS instance to allow traffic
on port 80. For more information, see Add security group rules.
- 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: Prepare the compilation environment
- Create a CentOS 6 instance.
- Connect to the CentOS 6 instance.
- Run the
cat /etc/redhat-release
command to check the operating system version. 
Step 2: Install and configure NGINX
- Run the following commands in sequence to add a user to run the NGINX service process:
groupadd -r nginx
useradd -r -g nginx nginx
- Download the source code package and then decompress and compile the package.
- Run the following command to download the source code package:
wget http://nginx.org/download/nginx-1.10.2.tar.gz
- Run the following command to decompress the source code package:
tar xvf nginx-1.10.2.tar.gz -C /usr/local/src
- 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
- Run the following command to go to the directory of the NGINX source code package:
cd /usr/local/src/nginx-1.10.2
- Run the following commands 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
- Run the following command to create a directory:
mkdir -p /var/tmp/nginx/client
- Add the SysV startup script.
- Run the
vi /etc/init.d/nginx
command to open the SysV startup script file.
- 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
- Press the Esc key, enter :wq, and then press the Enter key to save and close the SysV startup script file.
- Run the following command to grant execute permissions on the script:
chmod +x /etc/init.d/nginx
- Run the following commands in sequence to add NGINX to the list of system services
and enable NGINX to start on system startup:
chkconfig --add nginx
chkconfig nginx on
- Run the following command to start the NGINX service:
- Test whether NGINX is installed.
- Log on to the ECS console.
- In the left-side navigation pane, choose .
- On the Instances page, find the instance that you created and copy its public IP address from the
IP Address column.
- In the browser address bar, enter the IP address and press the Enter key.
The following page indicates that NGINX is installed.

Step 3: Install and configure MySQL
- Run the following commands in sequence to prepare the compilation environment:
yum groupinstall "Server Platform Development" "Development tools" -y
yum install cmake -y
- Create a directory to store MySQL data.
- Run the
mkdir /mnt/data
command to create a directory to store MySQL data.
- Run the
groupadd -r mysql
command to create a user group named mysql
.
- Run the
useradd -r -g mysql -s /sbin/nologin mysql
command to create a user named mysql
.
- Run the
id mysql
command to check whether the user is created.
- Run the
chown -R mysql:mysql /mnt/data
command to change the group and user of the MySQL data directory to mysql
.
- Download the latest stable version of the source code package and then decompress
and compile it.
- Run one of the following commands to download the source code package:
- Run the following command to decompress the source code package:
tar xvf mysql-5.6.24.tar.gz -C /usr/local/src
- Run the following command to go to the directory of the MySQL source code package:
cd /usr/local/src/mysql-5.6.24
- 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
- Configure MySQL.
- Run the following command to change the group and user of the MySQL installation directory
to mysql:
chown -R mysql:mysql /usr/local/mysql/
- 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 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.
- 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
- Run the following command to grant execute permissions on the startup script of MySQL:
chmod +x /etc/init.d/mysqld
- Run the following commands in sequence to add MySQL to the list of system services
and enable MySQL to start on system startup:
chkconfig --add mysqld
chkconfig mysqld on
- Run the following command to change the installation and data storage paths in the
configuration file:
echo -e "basedir = /usr/local/mysql\ndatadir = /mnt/data\n" >> /etc/my.cnf
- Run the following commands 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
- Run the following command to start the MySQL service:
- Run the following command to connect to the MySQL database for testing:
Step 4: 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.
- Run the following command to install the dependency:
yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel
- Download the latest stable version of the source code package, and decompress and
compile it.
- 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
- 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
- Run the following command to go to the directory of the PHP source code package:
cd /usr/local/src/php-5.6.23
- 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
- Configure PHP.
- 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
- 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
- Run the following command to grant execute permissions on the PHP-FPM startup script:
chmod +x /etc/init.d/php-fpm
- Run the following commands in sequence to add PHP-FPM to the list of system services
and enable PHP-FPM to start on system startup:
chkconfig --add php-fpm
chkconfig --list php-fpm
chkconfig php-fpm on
- Run the following command to start PHP-FPM:
- Add the NGINX support for FastCGI.
- Run the following command to back up the default NGINX configuration file:
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
- Run the following command to add the NGINX configuration file:
cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf
- Run the
vi /etc/nginx/nginx.conf
command to open the NGINX configuration file.
- 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;
}
- 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;
}
- Change
root html;
to root /usr/local/nginx/html;
.
- Change
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
to fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
.
- Press the Esc key, enter :wq, and then press the Enter key to save and close the NGINX configuration file.
- Run the
service nginx reload
command to reload the NGINX configuration file.
- Modify the index.php file.
- Run the
vi /usr/local/nginx/html/index.php
command to open the index.php file.
- 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();
?>
- 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
- Log on to the ECS console.
- In the left-side navigation pane, choose .
- 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.
- In the browser address bar, enter the IP address and press the Enter key.
The following page indicates that the LNMP environment is built.
