Skip to main content

Install LEMP stack (Nginx, MariaDB, PHP) on CentOS7 Step by Step Guide

install lemp stack on centos

LEMP stands for Linux, Nginx (Pronounced engine X), MySQL/MariaDB and PHP. It enables a server to host dynamic website and web apps. In this tutorial I will show you how to install LEMP stack on CentOS 7 Server. I choose to install MariaDB as the database server instead of MySQL.

Install Nginx

Nginx is a fast web server compared to Apache and becomes more popular these days. In order to install Nginx on CentOS 7, we need to add the EPEL repository using the following command. EPEL stands for Extra Packages for Enterprise Linux.

sudo yum install epel-release -y

Type your password. Now that the repository is added, it’s time to install Nginx:

sudo yum install nginx -y

After Nginx is installed, we need to start it.

sudo systemctl start nginx

Check if it’s running:

systemctl status nginx


nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
Active: active (running) since 五 2015-11-13 05:27:43 EST; 2s ago
Process: 1577 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 1574 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 1571 ExecStartPre=/usr/bin/rm -f /run/ (code=exited, status=0/SUCCESS)
Main PID: 1578 (nginx)
CGroup: /system.slice/nginx.service
├─1578 nginx: master process /usr/sbin/nginx
└─1579 nginx: worker process

Check Nginx version:

[[email protected] ~]$ nginx -v
nginx version: nginx/1.6.3

Enable Nginx to start on system boot

sudo systemctl enable nginx

Type your server ip address in your web browser, if you see the following, then Nginx is correctly installed.

LEMP on CentOS


You can find you server public IP address using the following command:

ip address



Your server firewall may have disabled public access to port 80. To allow public access, we tell iptables firewall to accept traffic to port 80 with the following command.

sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT

Now your server can host static html files, but in order to server a dynamic website, we need to install MariaDB and PHP.

Install MariaDB

MariaDB is a drop-in replacement for MySQL. Install it using following command:

sudo yum install mariadb-server mariadb -y

After it’s installed, we need to start it.

sudo systemctl start mariadb

Check status:

systemctl status mariadb


mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled)
Active: active (running) since 五 2015-11-13 05:45:06 EST; 27s ago
Process: 1760 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
Process: 1681 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 1759 (mysqld_safe)
CGroup: /system.slice/mariadb.service
├─1759 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─1915 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql...

Check version:

[[email protected] ~]$ mysql --version
mysql Ver 15.1 Distrib 5.5.44-MariaDB, for Linux (x86_64) using readline 5.1

Now run the security script

sudo mysql_secure_installation

When the script ask you for the current root password, just leave it blank and press enter. Then enter Y and set a root password. After you set the root password, you can simply press enter to answer all of other questions.

After the script is completed, we need to enable MariaDB to start on system boot.

sudo systemctl enable mariadb

Now let’s move on to the PHP part.

Install PHP

Install PHP and related packages using the following command:

sudo yum install php php-mysql php-fpm php-gd php-xml php-mbstring -y

Edit the main php-fpm config file:

sudo nano /etc/php.ini

Find the following line:


Remove the preceding semicolon and set its value to 0


Save and close the file. This is for security concerns.

Now edit the php-fpm config file:

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

Find the following line:

listen =

change it to this:

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

then find the following two lines:

;listen.owner = nobody
; = nobody

remove the preceding semicolons.

Last, change the user and group value from “apache” to “nginx”:

user = nginx
group = nginx

Once you save and close the file, start the PHP Processor:

sudo systemctl start php-fpm

Next, enable php-fpm to start on system boot.

sudo systemctl enable php-fpm

Configure Nginx Virtual Host

Create a new virtual host file in /etc/nginx/conf.d directory

sudo nano /etc/nginx/conf.d/

add the following lines to it. Replace and with your own domain.

server {
  listen 80;
  root /usr/share/nginx/html;
  index index.php index.html index.htm;
  location / {
    try_files $uri $uri/ /index.php$query_string;
  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. then restart nginx:

sudo systemctl restart nginx

You can add additional virtual host files to host multiple website on a single server.

Test PHP Processing

Create a info.php file in the web root directory:

sudo nano /usr/share/nginx/html/info.php

Put the following text into info.php file

<?php phpinfo(); ?>

Save and close it.

Then visit you newly created file by typing the following in your browser: or ip address/info.php

If you can see something like this, then your PHP is working correctly. Make sure you have added a A record for your domain name and point the IP address of your domain name to your CentOS server. If you still can’t see the PHP info, then delete the default virtual host.


This file is for testing only. For security reasons you can now remove it using the following command:

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

Now you have LEMP installed, you may wonder what you should do next. Well, you can install a WordPress or Drupal website on top of your LEMP stack. That’s just what I will show you in the next tutorial.

Rate this tutorial
[Total: 1 Average: 5]