Website Vidya
DeploymentStep-by-Step Guide

How to Install LEMP on CentOS 7: 5 Easy Steps

LEMP Stack is a group of open source programs that work collectively to enable a server to host dynamic websites or web applications. LEMP represents L inux (operating system), E Nginx (web server), M ySQL (MariaDB database), P HP (scriptin...

RR
Website Vidya TeamLead Systems Educator
Published:
6 min read
How to Install LEMP on CentOS 7: 5 Easy Steps
AI Executive SummaryCORE CIS AI

6 min read

LEMP Stack is a group of open source programs that work collectively to enable a server to host dynamic websites or web applications. LEMP represents L inux (operating system), E Nginx (web server), M ySQL (MariaDB database), P HP (scriptin...

Key Highlights
  • Prerequisites
  • How to Install LEMP on CentOS
  • Conclusion
Grounded in full engineering runbookCORE CIS Ai Engine

LEMP Stack is a group of open source programs that work collectively to enable a server to host dynamic websites or web applications. LEMP represents Linux (operating system), ENginx (web server), MySQL (MariaDB database), PHP (scripting language). It is the most popular alternative to LAMP Stack, which uses Apache instead of Nginx. In this guide, we will learn how to install LEMP on CentOS 7.

Prerequisites

Before we install LEMP on CentOS, update the software packages of your server to the latest version. To do that run the following command:

sudo yum -y update

This guide requires you to have root access of your server. You will require an SSH client like PuTTY (Windows) or terminal shell (Linux, macOS) to connect to your server.

How to Install LEMP on CentOS

Now that Linux (CentOS) is already installed and software packages are updated to the latest version, we will be installing Nginx, MySQL and PHP.

Step #1: Installing Nginx on CentOS 7

Since Nginx is not available by default in the CentOS repository, we need to install EPEL repository on our server. To do that, run:

sudo yum install epel-release -y

Since we are using a sudo command, you will be asked to enter the regular user's password to check permissions to run the command and operations get executed with root privileges.

Now that the Nginx repository is installed, we can move further to install Nginx on CentOS by using yum command:

sudo yum install Nginx

Nginx will prompt you with yes or no option. If you answer yes Nginx will finish the installation while answering no will stop the installation of Nginx.

Nginx should be up and running on your server. You can check the status of your Nginx web server by typing:

sudo systemctl status nginx

If the Nginx has not started yet, you start by typing:

sudo systemctl start nginx

Now, we would want our Nginx to start automatically on system boot. To enable that, run:

sudo systemctl enable nginx

Allow HTTP and HTTPS Traffic

If you have firewall enabled on your server and you want to deploy your website or web application, you would also need to allow HTTP and HTTPS traffic. To allow that, run:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

Now, check whether Nginx is running or not by entering the public IP address of your server in the browser. Your page should look like this:

nginx

If you do not know the IP address of your server, you can type:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

Alternatively, you can type:

curl http://icanhazip.com

Step #2: Installing MySQL (MariaDB)

Now that we have our Nginx web server up and running, it's time to install MySQL (MariaDB).

MariaDB is a community-developed fork of MySQL relational database. Which is commercially supported and intended to remain free and open-source software under GNU General Public Licence.

To install the MariaDB server and support for MySQL/MariaDB-PHP, run:

sudo yum install mariadb-server php-mysql

After MariaDB is installed, enable and start the MariaDB service:

sudo systemctl start mariadb
sudo systemctl enable mariadb

Now we need to secure the MariaDB installation by running:

sudo mysql_secure_installation

The prompt will ask you for the current root password, which you do not have since you just installed the MariaDB. So leave the password field blank and press enter.

The next prompt will ask you whether you want to set a root password or not. Enter Y and follow the instructions.

Now prompt will return a few security questions. Answer Y for the following prompts:

Remove anonymous users? [Y/n]
Disallow root login remotely? [Y/n]
Remove test database and access to it? [Y/n]
Reload privilege tables now? [Y/n]

The last thing we need to do is to enable the MariaDB so that it starts automatically every time a server is rebooted. To do that, run:

sudo systemctl enable mariadb

Step #3: Install PHP

Install the PHP FastCGI processing manager which will process the our web application or website code to display the dynamic content. So we are going to install the php-mysql and php-fpm packages by running the following command:

sudo yum install php php-mysql php-fpm

To ensure that PHP is up and running:

sudo systemctl start php-fpm

Also, enable to start the automatically on server reboot:

sudo systemctl enable php-fpm

Now we need to tell PHP to only accept URIs for files present on the server. This particular action will reduce the vulnerability in the PHP setup where the PHP interpreter can be tricked to run the arbitrary code. To learn more about this vulnerability check out the official Nginx documentation.

sudo sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php.ini

By default, PHP is configured to run under apache user. To configure PHP for Nginx, we need to match the apache user with Nginx user and group.

Now change the user and group variables inside www.conf file:

sudo sed -i 's/user = apache/user = nginx/g' /etc/php-fpm.d/www.conf 
sudo sed -i 's/group = apache/group = nginx/g' /etc/php-fpm.d/www.conf

Alternatively, you can open PHP-FPM configuration to make the appropriate changes:

 nano /etc/php-fpm.d/www.conf

Find and replace the following:

user = apache to user = nginx
group = apache to group = nginx
listen.owner = nobody to listen.owner = nginx
listen.group = nobody to listen.group = nginx

Save the configuration by pressing CTRL + X and start PHP-FPM:

sudo systemctl start php-fpm.service

Now enable PHP-FPM to start automatically on reboot:

systemctl enable php-fpm.service

Step #4: Configure Nginx to Serve PHP Pages

Now that we have all the configuration installed, we need to configure the Nginx to serve PHP pages.

First of all, create a root directory for your website.

For example, if the domain name is example.com:

sudo mkdir -p /var/www/example.com/

You can replace example.com with your domain name.

Once you create the root directory for your website, you need to disable the default Nginx configuration by running:

sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.disabled

Now create a Nginx configuration file for your website:

sudo nano /etc/nginx/conf.d/example.com.conf

Paste the following inside your example.com.conf file:

server {
     listen         80 default_server;
     listen         [::]:80 default_server;
     server_name    example.com www.example.com;
     root           /var/www/example.com;
     index          index.php index.html;
     
     location / {   
       try_files $uri $uri/ =404;
     }

     location ~* \.php$ {
       fastcgi_pass 127.0.0.1:9000;
       include         fastcgi_params;
       fastcgi_param   SCRIPT_FILENAME           $document_root$fastcgi_script_name;
       fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
     }
 }

Save the configuration file file by pressing CTRL + X (Windows) or CMD + X (Mac).

Then restart the Nginx:

systemctl restart nginx

Step #5: Testing LEMP Stack

In order to test our LEMP Stack configuration, create a basic PHP file index.php inside the root directory of your website example.com.

sudo nano /var/www/example.com/index.php

Paste the following one line code:

<?php phpinfo(); ?>

Save the file by pressing CTRL + X.

Now restart the PHP-FPM and reload the Nginx:

sudo systemctl restart php-fpm
sudo nginx -s reload

Now go to http://example.com/index.php.

Replace example.com with your domain name.

It will open a page containing PHP information which will look something like this:

Default php-fpm

It also confirms the successful installation of LEMP Stack.

Conclusion

In this guide, we learnt how to install LEMP on CentOS 7. I hope you will find this tutorial useful.

If you have any questions or feedback, feel free to comment below.

Written by

Website Vidya Team

Technology educator and researcher at Website Vidya.

Discussion

0
Share your thoughts, ask a question, or suggest a tip...
Write →

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Deployment•

How to Install PDO Extension in PHP

PHP Data Objects (PDO) is one of the most important extensions in PHP for working with databases in a secure, consistent, and modern way. If you are building dynamic websites, APIs, CMS platforms, or enterprise applications, PDO is not opti...

RR
Rajesh Rai
Read More
Deployment•

How to Install PHPMyAdmin on Ubuntu: 4 Simple Steps

If you’re working with MySQL or MariaDB on Ubuntu, managing databases purely from the command line can quickly feel overwhelming. That’s where phpMyAdmin comes in. PHPMyAdmin is a web-based application that provides a Graphical User Interfa...

RR
Rajesh Rai
Read More
Deployment•

25 Best Linux Commands Every Sysadmin Should Know

If you’ve spent any time managing Linux servers, you already know this truth: the command line is not optional. It’s where real troubleshooting happens, where performance issues are understood, and where outages are prevented before users e...

RR
Rajesh Rai
Read More
Deployment•

How to Fix Snapd Consuming High CPU / RAM on Linux

If you’ve ever opened your system monitor and noticed snapd quietly eating up your CPU or RAM, you’re not alone. Many Linux users—especially on Ubuntu and Ubuntu-based distributions—eventually run into this issue and wonder what’s going on ...

RR
Rajesh Rai
Read More
Deployment•

How to Fix Broken Packages in Ubuntu: 7 Simple Ways

If you’ve been using Ubuntu for a while, chances are you’ve run into package-related issues at least once. A failed update, an interrupted installation, or conflicting dependencies can easily leave your system in a messy state. When that ha...

RR
Rajesh Rai
Read More