How to Install LEMP on CentOS 7

Install LEMP on CentOS 7

In this tutorial we’ll learn how to install LEMP on CentOS 7. We will also install and configure its prerequisites. LEMP (a server stack) stands for Linux, Nginx, MariaDB, and PHP. Nginx is a free, open-source, high-performance HTTP web server; known for its stability, rich feature set, simple configuration, and low resource consumption.

Install LEMP on CentOS 7

Step 1. First, ensure your system is fully up-to-date by running the following:

yum -y update

Step 2. Installing and configuring Nginx.

In order to install Nginx on CentOS 7, we need to add the EPEL repository using the following command:

yum install epel-release
yum install nginx

After installation completes, enable Nginx start on boot and run it:

systemctl start nginx
systemctl enable nginx

Step 3. Installing and configuring MariaDB.

MariaDB is a drop-in replacement for MySQL and is the default database server used in CentOS 7. Proceed with installing it using yum as in:

yum install mariadb-server mariadb

After finishing the installation, enable and start the service:

systemctl start mariadb
systemctl enable mariadb

Now your MySQL database is running. Next you will need to run the MySQL secure script to set your MySQL root password to prevent unauthorized access to MySQL:


Next prompt will ask if you want to set a root password, enter Y and follow the instructions:

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 MariaDB
root user without the proper authorization.
New password: password
Re-enter new password: password
Password updated successfully!
Reloading privilege tables..
 ... Success!

Step 4. Installing and configuring PHP 7.

Install PHP and related packages using the following command:

yum install php php-common php-fpm php-mysql

After finishing the installation, enable and start the service:

systemctl start php-fpm
systemctl enable php-fpm

Step 5. Configuring Nginx to work with PHP 7.

Next, Create a test index.php file:

nano /var/www/html/index.php

Paste in the following code, then save and exit the file:

echo "Hello world, I'm PHP!";

Now edit the deafult Nginx config file:

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

Replace the contents of the file with the following:

server {
 listen 80;
 server_name localhost;

#charset koi8-r;
 #access_log /var/log/nginx/log/host.access.log main;

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

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /usr/share/nginx/html;

location ~ \.php$ {
 try_files $uri =404;
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;

# deny access to .htaccess files, if Apache's document root
 # concurs with nginx's one
 #location ~ /\.ht {
 # deny all;

Now restart php7.0-fpm and nginx:

systemctl restart php7.0-fpm
systemctl restart nginx

In your browser, you can go to your server’s IP address in your browser once again and you should see the text “Hello world, I’m PHP!” instead of the default Nginx page.

Congratulation’s! You have successfully configured LEMP Stack on your CentOS 7 server.