How to Install Linux, Nginx, MySQL, PHP (LEMP stack) On CentOS 7



LEMP is an open source software configuration used in web server and it stands for Linux, Nginx, MySQL, and PHP. This tutorial describes how to install LEMP stack on CentOS 7. The only difference between LAMP and LEMP stack is the web server in which the stack runs.

Since the server is already set up with CentOS, the Linux part of the stack is taken care of. This tutorial will show you how to install nginx, mysql and php-fpm, configure as well as verify them.

Before proceeding to install, make sure you have done the initial server setup for CentOS 7.

Installation and Configuration of Nginx on VPS


In order to install nginx, you need to first add the CentOS 7 Nginx yum repository.

For that, open the terminal and run the following command:

sudo rpm -Uvh


Once the installation of repository is completed, you can now move on to installing nginx on your VPS. Run the following command to install nginx:

sudo yum install nginx


Since nginx does not start on its own, you need to get it running:

sudo systemctl start nginx.service


Now, you can verify the nginx installation by directing your web browser to your IP address. Get your VPS IP address using:

ifconfig eth0 | grep inet | awk '{ print $2 }'

http://server_ IP_OR_domain_name/


You will see the CentOS 7 Nginx page like this:




To enable Nginx to run at the system bbot time, run the following command:

sudo systemctl enable nginx.service


Installation of MariaDB


MariaDB is a MySQL drion-in replacement to organize, store and access data.  We will use the yum command to install the required packages.

sudo yum install mariadb-server mariadb


Once the installation in complete, start the MariaDB using the command:

sudo systemctl start mariadb


Now, you can start running the secure installation processes. This will let you make some configuration of MySQL:

sudo mysql_secure_installation


While the secure install scrip is running, the system will ask for your MySQL root password. You will be able to set the MySQL root password here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

You will be asked whether you want to set the root password. You can choose Y and follow the instructions.

You will be given a series of questions which you can say ‘yes’ and MySQL will be reloaded with all the changes. The screen should look similar to this:

By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them.  This is intended only for testing, and to make the installation go a bit smoother.  You should remove them before moving into a production environment. 

Remove anonymous users? [Y/n] y                                            
 ... Success! 

Normally, root should only be allowed to connect from 'localhost'.  This ensures that someone cannot guess at the root password from the network. 

Disallow root login remotely? [Y/n] y
... Success! 

By default, MySQL comes with a database named 'test' that anyone can access.  This is also intended only for testing, and should be removed before moving into a production environment.

Remove test database and access to it? [Y/n] y 

- Dropping test database... 
... Success! 

- Removing privileges on test database... 
... Success! 

Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y 
... Success!


Now, to enable the MariaDB to run at the system boot time, run the following command:

sudo systemctl enable mariadb.service


Installing and Configuring PHP


Let’s go ahead and install php-fpm package located in REMI repository. Run the following command to enable REMI repository and install php and php-fpm:

sudo yum --enablerepo=remi install php-fpm php-mysqlsudo yum install php php-mysql php-fpm


Now, open the php configuration file and update it with one change.

sudo vi /etc/php.ini


Search for the line ‘cgi.fix_pathinfo=1’ and replace 1 to 0.


This is to ensure that the php interpreter processes only the exact file path. After making the changes, save and exit the file.

Now, open up the php-fpm configuration file:

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


Update it with the following line:

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

Once you are done with the changes, save the file and quit.

Start the PHP processor using the below command:

sudo systemctl start php-fpm


Here also, run the command to enable php-fpm to start at the boot time:

sudo systemctl enable php-fpm.service


Configuration of Nginx to Process PHP Pages


Open the default nginx config file:

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


You need to make the following changes:

  1. Change the root to /usr/share/nginx/html
  2. Add index.php to index line.
  3. Replace local host (server_name) to your domain name or IP.
  4. Change the root to access document root – /var/www/wordpress
  5. Uncomment the lines in “location ~ \.php$ {“ section
  6. Change fastcgi_param line so as to help PHP interpreter find php script that was stored in document root.

Once you are done with the changes, the file should look like this:

server {
    listen       80;
    server_name server_domain_name_or_IP;
     root   /usr/share/nginx/html;
    indexindex.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.

Restart the nginx to bring in your changes:

sudo systemctl restart nginx


Creating a PHP Info Page for Verification


Now that the LEMP stack is completely installed on your system, you may have to verify it by creating a simple php info page.

For that, you need to create a new blank file and paste the following code.

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

Save and exit the file.

Visit your php info page by going to your web address, http://youripaddress/info.php. It should look similar to this:



You will be able to view the nginx and php-fpm configuration details too by visiting it.

LEMP stack is now successfully installed on your VPS.

support2 has written 111 articles

Leave a Reply