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

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 on CentOS7 From EPEL

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

Enable Nginx to start at system boot time.

sudo systemctl enable nginx

Check if it’s running:

systemctl status nginx

Sample output:

 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 2018-09-24 08:36:31 UTC; 1min 19s ago
 Main PID: 13692 (nginx)
   CGroup: /system.slice/nginx.service
           ├─13692 nginx: master process /usr/sbin/nginx
           └─13693 nginx: worker process

Check Nginx version:

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

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

or

curl http://icanhazip.com

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 on CentOS 7

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

Enable MariaDB to start at system boot time.

sudo systemctl enable mariadb

Check status:

systemctl status mariadb

outputs:

 mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2018-09-24 08:48:46 UTC; 23s ago
 Main PID: 13970 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─13970 /bin/sh /usr/bin/mysqld_safe --basedir=/usr

Check version:

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

Now we need to 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.

install nginx php fpm mariadb centos 7

After you set the root password, you can simply press enter to answer all of other questions. (Note that the letter Y is capitalized, which means it’s the default answer.)

centos 7 nginx php-fpm

Now let’s move on to the PHP part.

Install PHP-FPM on CentOS7

Install PHP and related packages using the following command:

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

Now edit the php-fpm config file:

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

Find the following line:

listen = 127.0.0.1:9000

change it to this:

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

then find the following two lines:

;listen.owner = nobody
;listen.group = nobody

remove the preceding semicolons. Lastly, 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/example.com.conf

Add the following lines to it. Replace www.example.com and example.com with your own domain. Don’t forget to set A record for your domain name.

server {
  listen 80;
  server_name www.example.com example.com;
  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 reload nginx:

sudo systemctl reload 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 the file

<?php phpinfo(); ?>

Save and close it. Then visit you newly created file by typing the following in your browser:

http://example.com/info.php

or

your ip address/info.php

If you will see something like in the following screenshot, then your PHP is working correctly.

LEMP On CentOS

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: 7 Average: 4.4]

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.