Step-by-Step Guide to Installing LNMP (Linux, Nginx, MySQL, PHP) on CentOS

This tutorial provides a comprehensive, step-by-step procedure for setting up a complete LNMP stack on CentOS, covering environment preparation, removal of conflicting packages, compilation and installation of MySQL 5.6.17, PHP 5.6.32, Nginx 1.8.0, and their integration with detailed configuration and firewall adjustments.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Step-by-Step Guide to Installing LNMP (Linux, Nginx, MySQL, PHP) on CentOS

Environment description : The target platform is Linux (CentOS 6.9 or 7.3) with Nginx 1.8.0, MySQL 5.6.17, and PHP 5.6.32. Installation directories are /usr/local/mysql, /usr/local/php, and /usr/local/nginx respectively.

Preparation work :

Create a source directory: mkdir -p /usr/local/src/ Remove any existing Apache, MySQL, or PHP packages:

rpm -e httpd
rpm -e mysql
rpm -e php
yum -y remove httpd
yum -y remove mysql
yum -y remove php

Search and force‑remove leftover Apache packages using rpm -qa http* and rpm -e --nodeps <package_name>.

Disable SELinux if it interferes:

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

(permanent, requires reboot) or setenforce 0 (temporary).

Install required build tools:

yum -y install make gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel gettext gettext-devel gmp-devel pspell-devel unzip libcap lsof

Installing MySQL 5.6.17 :

Create MySQL group and user (no login, no home):

groupadd mysql
useradd -s /sbin/nologin -g mysql -M mysql

Download and compile CMake (required for MySQL 5.6+):

wget http://www.cmake.org/files/v2.8/cmake-2.8.12.2.tar.gz
tar zxvf cmake-2.8.12.2.tar.gz
cd cmake-2.8.12.2
./configure
make && make install

Download MySQL source and compile with CMake options:

wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.17.tar.gz
tar zxvf mysql-5.6.17.tar.gz
cd mysql-5.6.17
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_USER=mysql \
-DWITH_DEBUG=0 \
-DWITH_SSL=system
make && make install

Configure my.cnf: rename any existing /etc/my.cnf to /etc/my.cnf.bak, copy my-default.cnf to /etc/my.cnf, then initialize the data directory:

/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

Copy the init script and enable service:

cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig mysqld on
service mysqld start

Set root password and adjust PATH: vim /etc/profile (add PATH=/usr/local/mysql/bin:$PATH and export PATH) then source /etc/profile.

/usr/local/mysql/bin/mysqladmin -uroot -p password 'your_password'

Optionally adjust firewall to allow port 3306:

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

Installing PHP 5.6.32 :

Install required extensions: yum install libmcrypt libmcrypt-devel mcrypt mhash (if missing, enable EPEL repository first).

Download, extract, and compile PHP:

wget http://cn2.php.net/distributions/php-5.6.32.tar.gz
tar zxvf php-5.6.32.tar.gz
cd php-5.6.32
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
make && make install

Configure PHP‑FPM:

mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp php.ini-production /usr/local/php/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
groupadd www
useradd -s /sbin/nologin -g www -M www
/etc/init.d/php-fpm start
ln -s /usr/local/php/bin/php /usr/bin/php

Installing Nginx 1.8.0 :

Create nginx user/group and required directories:

groupadd -r nginx
useradd -s /sbin/nologin -g nginx -M nginx
mkdir -pv /var/tmp/nginx/client/

Download and compile Nginx:

wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/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/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--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 \
--with-pcre
make && make install

Create an init script (see source) and enable it:

chmod +x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
systemctl restart nginx.service

(or /etc/init.d/nginx start)

Integrating Nginx with PHP :

Edit /usr/local/nginx/conf/nginx.conf to set user and worker processes, then add server block:

user nginx nginx;
location / {
root /home/www;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /home/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

Reload Nginx configuration ( /etc/init.d/nginx reload or service nginx restart).

Create a test index.php in the web root:

<?php
echo 'Hello,PHP';
phpinfo();
?>

Access it via a browser to verify that Nginx correctly forwards PHP requests.

Finally, open HTTP port 80 in the firewall (similar to the MySQL port command) and the LNMP environment on CentOS is ready for use.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxmysqlPHPInstallationCentOSLNMP
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.