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.
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-develAdd 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/</code>
<code>./configure --user=nginx --group=nginx --prefix=/usr/local/nginx \
--with-http_stub_status_module --with-http_ssl_module</code>
<code>make -j 4</code>
<code>make installCopy 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</code>
<code>ln -s /usr/local/nginx/sbin/nginx /usr/sbinConfigure Nginx to work with PHP (excerpt of nginx.conf):
user nginx;</code>
<code>worker_processes 8;</code>
<code>events { worker_connections 1024; }</code>
<code>http {</code>
<code> include mime.types;</code>
<code> default_type application/octet-stream;</code>
<code> sendfile on;</code>
<code> keepalive_timeout 65;</code>
<code> server {</code>
<code> listen 80;</code>
<code> server_name localhost;</code>
<code> location / {</code>
<code> root html;</code>
<code> index index.html index.htm;</code>
<code> }</code>
<code> location ~ \.(php|php5)?$ {</code>
<code> root /usr/local/nginx/html;</code>
<code> fastcgi_pass 127.0.0.1:9000;</code>
<code> fastcgi_index index.php;</code>
<code> fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;</code>
<code> include fastcgi_params;</code>
<code> }</code>
<code> error_page 500 502 503 504 /50x.html;</code>
<code> location = /50x.html { root html; }</code>
<code> }</code>
<code>}2. Install and Deploy MySQL
Remove any existing MySQL packages:
rpm -e mysql --nodeps</code>
<code>rpm -e mysql-server --nodepsInstall 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/</code>
<code>cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all</code>
<code>make -j 4</code>
<code>make installPost‑installation configuration:
chown -R mysql:mysql /usr/local/mysql/ # set ownership</code>
<code>mv /etc/my.cnf /etc/my.cnf.old</code>
<code>cp support-files/my-medium.cnf /etc/my.cnf</code>
<code>/usr/local/mysql/scripts/mysql_install_db --user=mysql \
--basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/</code>
<code>echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile # add to PATH</code>
<code>. /etc/profile # reload profile</code>
<code>cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld</code>
<code>chmod +x /etc/init.d/mysqld</code>
<code>chkconfig --add mysqld3. Install PHP
Install required development libraries:
yum -y install zlib-devel libxml2-devel libjpeg-turbo-devel freetype-devel \
libpng-devel gd-devel libcurl-develExtract, configure, compile and install PHP with many extensions (including FPM):
tar zxf php-$PVERSION.tar.gz -C /usr/src/</code>
<code>./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</code>
<code>make -j 4</code>
<code>make installCopy the production php.ini and set up PHP‑FPM configuration:
cp /usr/src/php-$PVERSION/php.ini-production /usr/local/php/php.ini</code>
<code>cd /usr/local/php/etc/</code>
<code>cp php-fpm.conf.default php-fpm.conf4. Start Services
Start Nginx and open port 80 in the firewall:
nginx</code>
<code>firewall-cmd --zone=public --add-port=80/tcp --permanent</code>
<code>firewall-cmd --reload</code>
<code>netstat -anlpt | grep nginxStart MySQL and enable it at boot:
service mysqld start</code>
<code>chkconfig mysqld on</code>
<code>netstat -anlpt | grep mysqldStart PHP‑FPM:
/usr/local/php/sbin/php-fpm</code>
<code>netstat -anlpt | grep php-fpm5. Test the LNMP Stack
Create a simple PHP test page:
echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/index.phpOpen 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
