Backend Development 8 min read

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

This tutorial provides a comprehensive, command‑by‑command walkthrough for installing, compiling, configuring, and starting Nginx, MySQL, and PHP on a Linux server, culminating in a functional LNMP stack test page.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Step-by-Step Guide to Installing and Configuring LNMP (Nginx, MySQL, PHP) on Linux

Below is a complete, command‑line guide for setting up the LNMP (Linux‑Nginx‑MySQL‑PHP) stack on a fresh Linux system.

1. Install and Deploy Nginx

Prepare the environment and install required development libraries:

yum -y install pcre-devel zlib-devel openssl openssl-devel

Add a dedicated nginx user:

useradd -s /sbin/nologin nginx -M

Extract the source, configure, compile and install:

tar zxf nginx-$NVERSION.tar.gz -C /usr/src/
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx \
    --with-http_stub_status_module --with-http_ssl_module
make -j 4
make install

Copy the default configuration and create a symbolic link for the nginx binary:

cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.default
ln -s /usr/local/nginx/sbin/nginx /usr/sbin

Configure Nginx to work with PHP (excerpt of nginx.conf ):

user  nginx;
worker_processes  8;
events { worker_connections 1024; }
http {
include       mime.types;
default_type  application/octet-stream;
sendfile        on;
keepalive_timeout  65;
server {
listen       80;
server_name  localhost;
location / {
root   html;
index  index.html index.htm;
}
location ~ \.(php|php5)?$ {
root           /usr/local/nginx/html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
include        fastcgi_params;
}
error_page   500 502 503 504  /50x.html;
location = /50x.html { root   html; }
}
}

2. Install and Deploy MySQL

Remove any existing MySQL packages:

rpm -e mysql --nodeps
rpm -e mysql-server --nodeps

Install build tools:

yum -y install cmake ncurses-devel

Add a dedicated mysql user:

useradd -s /sbin/nologin -M mysql

Extract, configure, compile and install MySQL:

tar zxf mysql-$MVERSION.tar.gz -C /usr/src/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
      -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 \
      -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all
make -j 4
make install

Post‑installation configuration:

chown -R mysql:mysql /usr/local/mysql/   # set ownership
mv /etc/my.cnf /etc/my.cnf.old
cp support-files/my-medium.cnf /etc/my.cnf
/usr/local/mysql/scripts/mysql_install_db --user=mysql \
      --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile   # add to PATH
. /etc/profile   # reload profile
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld

3. Install PHP

Install required development libraries:

yum -y install zlib-devel libxml2-devel libjpeg-turbo-devel freetype-devel \
    libpng-devel gd-devel libcurl-devel

Extract, configure, compile and install PHP with many extensions (including FPM):

tar zxf php-$PVERSION.tar.gz -C /usr/src/
./configure --prefix=/usr/local/php \
    --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
    --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib \
    --with-libxml-dir=/usr --enable-xml --disable-rpath \
    --enable-bcmath --enable-shmop --enable-sysvsem \
    --enable-inline-optimization --with-curl --enable-mbregex \
    --enable-fpm --enable-mbstring --with-gd --with-gettext \
    --enable-gd-native-ttf --with-openssl --with-mhash \
    --enable-pcntl --enable-sockets --with-xmlrpc \
    --enable-zip --enable-soap --enable-short-tags \
    --enable-static --with-fpm-user=nginx --with-fpm-group=nginx \
    --enable-fpm --enable-ftp --enable-opcache=yes
make -j 4
make install

Copy the production php.ini and set up PHP‑FPM configuration:

cp /usr/src/php-$PVERSION/php.ini-production /usr/local/php/php.ini
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf

4. Start Services

Start Nginx and open port 80 in the firewall:

nginx
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
netstat -anlpt | grep nginx

Start MySQL and enable it at boot:

service mysqld start
chkconfig mysqld on
netstat -anlpt | grep mysqld

Start PHP‑FPM:

/usr/local/php/sbin/php-fpm
netstat -anlpt | grep php-fpm

5. Test the LNMP Stack

Create a simple PHP test page:

echo "
" > /usr/local/nginx/html/index.php

Open a browser and navigate to http://your_server_ip/ to see the PHP information page, confirming that Nginx, MySQL, and PHP are all working together.

Thank you for reading; feel free to leave a comment or share this guide with others.

backendlinuxMySQLphpnginxinstallationLNMP
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

login 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.