How to Install LEMP Stack on CentOS 7

Install LEMP Stack On CentOS

LEMP stack stands for Linux, Nginx, MariaDB, and PHP. Here in LAMP stack which stands for Linux, Apache, MariaDB, and PHP, all components are not tightly coupled. So by replacing Apache With Nginx, we are installing LEMP stack. This tutorial outlines how to install LEMP stack on CentOS 7.


Before you start to install LEMP on CentOS 7. You must have a non-root user account on your server with sudo privileges.

1. Install NGINX

To install Nginx on CentOS 7, add EPEL reository on CentOS running below command:

sudo yum install epel-release

Now install Nginx by typing:

sudo yum install nginx

Start and enable nginx typing below commands:

sudo systemctl enable nginx
sudo systemctl start nginx

You can check the status of Nginx by typing:

systemctl status nginx

The output should be:

    ● nginx.service - A high performance web server and a reverse proxy server
    Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
    Active: active (running) since Fri 2018-07-01 16:08:19 UTC; 1 days ago
    Docs: man:nginx(8)
    Main PID: 2369 (nginx)
    Tasks: 2 (limit: 1153)
    CGroup: /system.slice/nginx.service
        ├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
        └─2380 nginx: worker process

2. Install MariaDB

To Install MariaDB type following command.

sudo yum install mariadb-server

Start and enable mysql typing below commands:

sudo systemctl enable mariadb
sudo systemctl start mariadb

To configure secure installation enter following command, you will be prompted for the password which is not set previously that time press ENTER only.

sudo mysql_secure_installation

3. Install PHP

Add remi repository in CentOS so we can install latest PHP 7.2, typing following command:

sudo yum install

Now enable PHP72 package using below command:

yum install yum-utils -y
yum-config-manager --enable remi-php71

This is third step to install LEMP stack on CentOS. By default, Nginx does not support native PHP processing. So you will need to install php-fpm (“fastCGI process manager”) package to install PHP. Now you can install php-fpm by typing following command.

sudo yum install php-fpm php-mysql php-cli php-mbstring php-opcache php-curl php-gd

You can check the status where it is correctly installed or not by using the following command

systemctl status php7.2-fpm

4. Set up Nginx configuration file

Create directory inside var/www/html named (you can use your domain name).

sudo mkdir -p /var/www/html/

Now you should remove the default configuration file provided. To remove default Nginx configuration file type following.

sudo rm -f /etc/nginx/sites-enabled/default

Configuration files for the website are stored inside /etc/nginx/conf.d directory so you need to create configuration file inside this directory named (you can use your domain name). Then enter following code inside that file by replacing with your domain name.

server { 
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html/; 
    index index.php; 

    location / { 
        try_files $uri $uri/ =404; 

    location ~* \.php$ { 
        fastcgi_pass unix:/run/php/php7.2-fpm.sock; 
        include fastcgi_params; 
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
        fastcgi_param SCRIPT_NAME $fastcgi_script_name; 

5. Testing LEMP stack

Ensure your domain reaching at your server by configuring DNS records of your domain.

Now you need to restart PHP and reload Nginx configuration file as you have made changes in Nginx configuration directory. Type following command to restart PHP and reload Nginx.

sudo systemctl restart php-fpm
sudo nginx -s reload

You can check the status of Nginx by typing following.

sudo nginx -t

Create an index.php file inside /var/www/html/ directory and enter following code inside the file.

        <h2>Index Page</h2>
        echo '<p>Hello,</p>';
        // Define PHP variables for the MySQL connection.
        $servername = "localhost";
        $username = "test_user";
        $password = "password";
        // Creating a MySQL connection.
        $conn = mysqli_connect($servername, $username, $password);
        // Show if the connection fails or is successful.

        if (!$conn) {
            exit('<p>Your connection has failed.<p>' . mysqli_connect_error());
        else {
            echo '<p>You have connected successfully.</p>';


In this tutorial, you have successfully learned how to install LEMP stack on CentOS 7. If you have any queries regarding this please don’t forget to comment below.

Detailed tutorials