Install LEMP Stack on Centos 7

In this article, we want to teach you to install the LEMP stack on Centos 7.

LEMP is short for Linux, Nginx, MySQL, and PHP. You can use this software for your web development with Nginx. In this tutorial, we use MariaDB instead of MySQL. Follow the steps below to complete this guide.

Easy Guide To Install LEMP Stack on Centos 7

To install LEMP on Centos 7, you need to log in as a non-root user with root privileges that can run Sudo commands. Follow our article about the Initial server setup with Centos 7.

Step 1 – LEMP Stack on Centos 7- Install Nginx

For installing Nginx you should get the latest version of Nginx. So for that, you need to install the EPEL repository with the following command:

#sudo yum update -y
# sudo yum install epel-release -y

Now install Nginx on Centos 7 with the command below:

sudo yum install nginx -y

Then, start the Nginx service with the following command:

sudo systemctl start nginx

Also, enable Nginx to start on boot with the following command:

sudo systemctl enable nginx

Step 2 – Configure Firewall Settings For LEMP Stack

At this point, you need to install the Firewalld on your Centos 7 and start it. To do this, you can visit this guide on Set up a Firewall with FirewallD on Centos 7.

Then, allow port 80 HTTP through firewalld with the following command:

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

Next, reload the firewall to apply the new rules:

sudo firewall-cmd --reload

Step 3 – Check Nginx Service Status on Centos 7

At this point, you can use the command below to check your Nginx is up and running:

sudo systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2023-10-16 08:58:37 EDT; 55s ago
 Main PID: 8759 (nginx)
   CGroup: /system.slice/nginx.service
           ├─8759 nginx: master process /usr/sbin/nginx

You can type your public IP address in your web browser to see the default Centos 7 Nginx web page.

You can get your public IP address with the following command:


Then, from your web browser type:


You should see the following page:

Nginx default page Centos 7

Step 4 – LEMP Stack on Centos 7 – MariaDB Installation

To install LEMP on Centos 7, you need MariaDB or MySQL as your database management system. You can install MariaDB and then start it with the following commands:

# sudo yum install mariadb-server mariadb -y
# sudo systemctl start mariadb

Now you need to run a security script for your database:

sudo mysql_secure_installation

Here you will be asked for your current root password press enter to leave it blank then you will be asked to set a root password enter Y and set your password. For the rest of the questions simply type y to accept the default.

Also, enable MariaDB to start on boot with the following command:

sudo systemctl enable mariadb

Note: You can upgrade your MariaDB version to the latest by visiting this guide on Upgrade MariaDB on Centos 7.

Step 5 – LEMP Stack – Install PHP on Centos 7

Here to complete the installation of the LEMP stack on Centos 7, you need to install the REMI repository for Centos 7 for the up-to-date PHP releases. Run the following command:

# sudo yum install
# sudo yum install yum-utils -y

Then enable the repository and check the available packages:

sudo yum --disablerepo="*" --enablerepo="remi-safe" list php[7-9][0-9].x86_64

In your output, you will see the available packages and choose the most updated version of PHP.

Available Packages
php70.x86_64                      7.0-1.el7.remi                       remi-safe
php71.x86_64                      7.1-1.el7.remi                       remi-safe
php72.x86_64                      7.2-1.el7.remi                       remi-safe
php73.x86_64                      7.3-1.el7.remi                       remi-safe
php74.x86_64                      7.4-3.el7.remi                       remi-safe
php80.x86_64                      8.0-3.el7.remi                       remi-safe
php81.x86_64                      8.1-4.el7.remi                       remi-safe
php82.x86_64                      8.2-5.el7.remi                       remi-safe
php83.x86_64                      8.3-1.el7.remi                       remi-safe

Run the following command: Here we choose PHP 8.2.

sudo yum-config-manager --enable remi-php82

Now install PHP for the LEMP stack with the following command:

sudo yum install php php-mysqlnd php-fpm -y

To see that PHP is available as your chosen version, run the command below:

php  -v
PHP 8.2.11 (cli) (built: Sep 26 2023 11:11:58) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.2.11, Copyright (c) Zend Technologies

At this point, you need to make some changes to the default configuration. Open the file with the vi or your favorite editor:

sudo vi /etc/php-fpm.d/www.conf

Find the user and group in the file. Then change the Apache to Nginx for them.

Next, locate the listen directive and change the line to the following:

listen = /var/run/php-fpm/php-fpm.sock;

Finally, you need to look for “listen.owner”, “” and “listen.mode” directives. Uncomment them by removing the preceding “;” sign at the beginning of the line. Then, change the owner and group to Nginx.

listen.owner = nginx = nginx

Save and close the file.

Enable and start the PHP-fpm with the following command:

# sudo systemctl start php-fpm
# sudo systemctl enable php-fpm

Step 6 – Configure Nginx Server Blocks on Centos 7

At this point when you have finished the installation of PHP for the LEMP stack on Centos 7, you need to tell Nginx to use your PHP processor for dynamic content.

Create a new file as a default PHP website in the /etc/nginx/nginx.conf file.

Open a new file in the /etc/nginx/conf.d directory with the following command:

sudo vi /etc/nginx/conf.d/default.conf

Copy the following PHP server definition block to your configuration file:

server {
listen 80;
server_name server_domain_or_IP;

root /usr/share/nginx/html;
index index.php index.html index.htm;

location / {
try_files $uri $uri/ =404;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;

location = /50x.html {
root /usr/share/nginx/html;

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

Save and close the file.

Next, restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 7 – Test PHP Processing on your Web Server

Now that the LEMP stack is installed on Centos 7, we can create a test PHP script to make sure Nginx is correctly handling .php scripts with the help of PHP-fpm.

Before this, you need to change the default ownership settings on Nginx’s document root.
Run the following command:

sudo chown -R nginx.nginx /usr/share/nginx/html/

Then create a new PHP file called info.php at the /usr/share/nginx/html directory:

vi /usr/share/nginx/html/info.php

Copy the below commands into your file:


Save and close the file.

Now you can test your web server by typing your IP address or server host in your web browser.

PHP information LEMP Stack

After checking information about your PHP server through that page, it’s better to remove the file you created as it contains sensitive information about your PHP environment and your CentOS server. You can use rm to remove that file:

rm /usr/share/nginx/html/info.php


At this point, you have learned to Install LEMP Stack which is Nginx, MariaDB, and PHP on Centos 7. Also, you have learned to create the Nginx Server blocks which are the Virtual host files in Apache. Hope you enjoy it.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Stay informed and not overwhelmed, subscribe now!