Phone :  +370 (5) 204-1903
Email: sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. Install LEMP on CentOS 7
Install LEMP on CentOS 7


Jan, 18

To enable a server to host dynamic websites and web apps, there is a software stack called LEMP. LEMP is a group of open source software that is typically installed together. LEMP is actually an acronym which represents the Linux operating system, with the ENginx web server. To store site data, LEMP uses MariaDB database , and to process dynamic content it uses PHP. Nginx replaces the popular Apache package found in LAMP stack.

In this guide, we will install a LEMP stack on an CentOS 7 operating system.

Notice: You must execute commands with root permission. If you don’t have root permission, you may start commands with “sudo”.

Install LEMP on CentOS 7

Step One — Install Nginx

Install Nginx web server to display web pages to site visitors.

Add CentOS 7 EPEL repository using following command:

sudo yum install epel-release

After installing Nginx repository on your server, use the following yum command to install Nginx:

sudo yum install nginx

Once your web server is installed, start Nginx using the following command:

sudo systemctl start nginx

To verify that everything works correctly, visit your server's public IP address in your web browser. You must see the default CentOS 7 Nginx web page. It should look something like this:


If you see this page, then your web server is now correctly installed. If you do not see that page, open web server port using the following command:

sudo firewall-cmd --zone=public --add-service=http --permanent

Then reload your firewall rules using the command below:

systemctl enable firewalld

Use the following command to enable Nginx to start on boot:

sudo systemctl enable nginx 

Step Two — Install MySQL (MariaDB)

Install MariaDB package using the following command:

sudo yum install mariadb-server mariadb

After installation is complete, start MariaDB with the following command:

sudo systemctl start mariadb

Enable MariaDB to start on boot using the following command:

sudo systemctl enable mariadb

Run the following script to configure MariaDB security options:

sudo mysql_secure_installation

Press enter and put your password twice:

mysql_secure_installation prompts:

Enter current password for root (enter for none):

OK, successfully used password, moving on... 

Setting the root password ensures that nobody can log into the MariaDBroot user without the proper authorisation. 

New password: password

Re-enter new password: password

Password updated successfully!

Reloading privilege tables.. ...


Reply all questions. We recommend reply all of them with Y. This will remove some sample users and databases, disable remote root logins, and load these new rules so that MySQL immediately respects the changes we have made.

Step Three — Install PHP

To display dynamic content, we will install PHP. Install PHP using the following command:

sudo yum install php php-mysql php-fpm

To Configure the PHP Processor make the following changes.

Open the php.ini file with root privileges:

sudo vi /etc/php.ini

In this file look for the parameter that sets cgi.fix_pathinfo. You must uncomment this parameter by removing “;” character from the beginning of line. Then set its value to “0”. Save your changes and close file.

Next, open the www.conf file:

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

Find the line that specifies the listen parameter, and change it so it looks like the following:

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

Next, find the lines that set the listen.owner and listen.group and uncomment them. They should look like this:

listen.owner = nobodylisten.group = nobody

Lastly, find the lines that set the user and group and change their values from "apache" to "nginx":

user = nginx

group = nginx

Then save and quit.

Now start our PHP processor and add it to startup services by typing:

sudo systemctl start php-fpm

sudo systemctl enable php-fpm

Step Four — Configure Nginx to Process PHP Pages

We must tell Nginx to use our PHP processor for dynamic content. Open the default Nginx server block configuration file by typing:

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

The Nginx default server block looks like this:

server {   

listen       80; 

 server_name  localhost;

location / {  

root   /usr/share/nginx/html;       

index  index.html index.htm; 


error_page   500 502 503 504  /50x.html;   

location = /50x.html {       

root   /usr/share/nginx/html;    }



Remove all its content and replace the following lines in it: 

server {

 listen 80;

 server_name IP-SERVER;

# 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;



Save and close the file. Restart Nginx to make the necessary changes:

sudo systemctl restart nginx 

Step Five — Test PHP Processing on your Web Server

In order to test that our system is configured properly for PHP, we can create a very basic PHP script. Use the following command to create info.php file:

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

Put following lines inside this created file:

Save and close the file. To verify that web server operates correctly, visit the following address in your browser.


The page that you come to should look something like this:


Congratulations! You have installed LEMP on CentOS 7

 If you are facing any problem with the installation, feel free to comment here. We will help you to solve the issue.







author img


Generic placeholder image

2020 Jan 05, 23:01:21

We absolutely love your blog and find nearly all of your post's to be what precisely I'm looking for. Would you offer guest writers to write content for you personally? I wouldn't mind publishing a post or elaborating on a lot of the subjects you write related to here. Again, awesome web log!

Leave A Comment