How to install LEMP stack on CentOS 7

When you buy a new VPS. You need a web server to run the website on it. LEMP stands for the following software groups: Linux, NGINX, Maria DB, and PHP.

how to install lemp on centos7

Linux is a line of operating systems. Ubutun and CentOS are two popular Linux operating systems when building VPS running the WordPress website.

NGINX is a web server software that handles requests from the browser. It is a good web server performance compared to Apache.

Maria DB is the optimized version of the MySQL database.

PHP-FPM is responsible for handling the PHP code, the language used to develop the WordPress website.

In today’s article, I will show you how to install LEMP on the CentOS 7 operating system. After completing the tutorial, you will install LEMP using the latest software versions such as PHP 7.

Prepare for installing LEMP on CentOS

To perform the installation steps as below you need to prepare:

  • A new Linux server installs CentOS. You can choose Ramnode, Linode or Vultr.
  • Putty software to connect to the server with root user.
  • In your post using the nano editor on Linux

Step 1: Install NGINX on CentOS 7

Because NGINX does not have a CentOS repository available, we have to install the EPEL repository with the following command:

yum install epel-release -y

Next, we install NGINX:

yum install nginx -y

Start with Nginx with the following statement:

systemctl start nginx

To restart the VPS using Nginx, use the following command:

systemctl enable nginx

To check if Nginx was installed successfully, open your browser and access the server’s IP address. If you see the following page is successful:


Step 2 Install PHP 7.1 on CentOS 7

Your server is new so it may not have the wget utility. First you need to install it:

yum instal wget -y

Now you need to install the repository that contains the PHP 7.1 package

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

Next, you install the PHP package:

yum --enablerepo=remi,remi-php71 install php-fpm php-common

Install some additional PHP modules. Later if you need to install more then refer to the statement like this:

yum --enablerepo=remi,remi-php71 install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml -y

We need to do some PHP configuration steps to increase security and work with nginx.

First, you need to open php.ini with nano:

nano /etc/php.ini

Find the line containing cgi.fix_pathinfo, leave a comment as # and change the value from 1 to 0.

After fixing press Ctrl + O and then enter to save and Ctrl + X to exit. To

By default, PHP will execute the most recent PHP file if the requested PHP file is not found. Configuring this like blocking unwanted PHP implementations.


Tip: In nano, you can use the keyboard shortcut Ctrl + W to find the word you want.

Next open the file  /etc/php-fpm.d/www.conf

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

Find the line that listens to it listening to localhost with port 9000. You need to convert it to a Unix socket. That is, PHP will execute PHP at this address.

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

Next, find listen. owner and listengroup, leave comment and change as follows:

listen.owner = nginx = nginx

Next, replace the line  user = apache and  group = apache into

user = nginx
group = nginx

Make changes and save and exit the nano. Then you restart PHP-FPM:

systemctl restart php-fpm

Secure php-fpm.sock with the following two statements:

chmod 666 /run/php-fpm/php-fpm.sock
chown nginx:nginx /run/php-fpm/php-fpm.sock

Restart php again:

systemctl restart php-fpm

Configuring NGINX

The above you have configured php work nginx. You also need to configure Nginx.

Open the file /etc/nginx/nginx.conf with nano:

nano /etc/nginx/nginx.conf

Make sure the following line is in the server section:

include /etc/nginx/default.d/*.conf;

Get out of the nano. Create a new nano /etc/nginx/default.d/default.conf file:

nano /etc/nginx/conf.d/default.conf

Copy and paste the code into an open file in nano: (ignore your_server_ip with your ip server)

server {
listen 80;
server_name your_server_ip;
# note that these lines are originally from the "location /" block
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;

This code will tell you how to handle PHP code with FAST CGI.

Restart Nginx:

systemctl restart nginx

Check whether PHP is running or not

Create the file info.php in the following position:

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

Copy and paste the following code into the open file in nano:

< ? php
phpinfo ();


You can now access the following address: http://yourserverip/info.php. If you see the screen as follows:

php 7.1

Step 3: Install Maria DB

Run the following command to install Maria DB:

yum install mariadb mariadb-server

Run Maria DB with the following command:

systemctl start mariadb.service

Run Maria DB when booting VPS with the following command:

systemctl enable mariadb.service

The default is not very secure. You run the following command to add more security to Maria DB.


First, it asks you to enter the current password for root. Now that you have no root password so just type Enter.

Next, it asks Set Root Password? Type y and enter a password for the root account.

The next question you choose y all. To

Finally, you restart Maria DB:

systemctl restart mariadb.service

You have now installed LEMP for CentOS 7.

About us: admin

Leave a Reply

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