Setup LEMP Stack on CentOS 7

Posted on January 20th, 2020

Setup LEMP Stack on CentOS 7

LEMP stack is similar to that of LAMP, but it is approximately 2.5 times faster than the LAMP stack. The full form of LEMP is Linux, Nginx (Engine-X), MySQL, and PHP.

To improve the speed and SEO ranking of your website, you can install WordPress LEMP CentOS. The PHP-FPM is an alternative FastCGI implementation, and it significantly faster than mod_PHP. Before installing the LEMP stack on CentOS, make sure that you have a non-root user account with sudo privileges on your server.


1) Install Nginx

Nginx is a free, open-source web server that can act as a reverse proxy, load balancer, TCP/UDP proxy server, and IMAP/POP3 proxy server.

1) First, you need to add EPEL repository on CentOS.

$ sudo yum install epel-release

2) Install Nginx web server using the following command.

$ sudo yum install nginx

3) After installing Nginx, you need to enable it to start during the boot-up. You can use the following commands to start, stop, and enable Nginx.

$ sudo systemctl start nginx

$ sudo systemctl stop nginx

$ sudo systemctl enable nginx

4) To check the status of Nginx, you can run the following command. This command’s output should be similar to the one shown below:

$ systemctl status nginx

#nginx.service – A high performance web server and a reverse proxy server

Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)

Active: active (running) since <date>

Docs: man:nginx(8)

Main PID: 2369 (nginx)

Tasks: 2 (limit: 1153)

CGroup: /system.slice/nginx.service

├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;

└─2380 nginx: worker process

5) You can check the Nginx version installed by running the following command.

$ nginx -v


2) Install MariaDB

The LEMP CentOS installation frequently runs MariaDB, the commercially supported fork of MySQL, and it is a backward-compatible replacement for MySQL. To install MariaDB on your CentOS server, you need to run the following command.

$ sudo yum install mariadb-server

To start, stop, and enable MariaDB, you need to run the following commands.

$ sudo systemctl start mariadb

$ sudo systemctl stop mariadb

$ sudo systemctl enable mariadb

After installation of MariaDB, you can secure installation by entering the following command.

$ sudo mysql_secure_installation

This above command prompts some questions, and you can set a new password.

Set a root password? Y

Enter password for user root: <Paste-copied-password>

Estimated strength of the password: 120

Do you wish to continue with the password provided? (Press Y for Yes, any other key for No) : <Answer Y/N as per your requirement>

Remove anonymous users? Y

Disallow root login remotely? Y

Remove test database and access to it? Y

Reload privilege tables now? Y

To check if MariaDB installation is correct, you can run the following command. This command prompts for the password, and you can give the password step previously.

$ sudo mysql -u root -p


3) Install PHP and PHP-FPM

PHP is an open-source Hypertext preprocessor programming language developed for website development. It is recommended to use the default repository to upgrade PHP configuration.

1) To install PHP and its related modules, run the following command.

$ sudo yum install php php-fpm php-mysql php-opcache php-xml php-mbstring php-gd  php-cli php-opcache php-curl

2) After the PHP and modules installation, you need to run the following command to start PHP-FPM.

$ sudo systemctl start php-fpm

3) Next, you need to enable the PHP-FPM service to start up during boot-up using the following command.

$ sudo systemctl enable php-fpm

4) After that, you can check the status of the PHP-FPM service using the following command.

$ sudo systemctl status php-fpm


4) Setup Nginx Configuration File

1) First, you need to create a directory under the /var/www/html named based on your domain name.

$ sudo mkdir -p /var/www/html/<>

For example,

$ sudo mkdir -p /var/www/html/

2) Next, remove the default configuration file. To remove the default Nginx configuration file, you need to run the following command.

$ sudo rm -f /etc/nginx/sites-enabled/default

3) Then create a new Nginx configuration file under the default configuration files directory. The default configuration directory is /etc/nginx/conf.d. After creating the Nginx configuration file, you need to enter the following code inside that file by replacing with your domain name.

$ sudo vi /etc/nginx/conf.d/<domain_name>.conf

For example,
$ sudo vi /etc/nginx/conf.d/

server {

listen 80 default_server;

listen [::]:80 default_server;



root /var/www/html/;

index index.php;


location / {

try_files $uri $uri/ =404;



location ~* \.php$ {

fastcgi_pass unix:/run/php/php7.2-fpm.sock;

include fastcgi_params;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;




5) Test LEMP Stack

Before testing, you need to configure the DNS records of your domain to ensure that it is reaching your server. After configuring the DNS record, you need to restart PHP. Then, reload the Nginx configuration file as you have made changes to the Nginx configuration directory. To restart PHP and reload Nginx, run the following command.

$ sudo systemctl restart php-fpm

$ sudo nginx -s reload

Next, check the Nginx status by running the following command.

$ sudo nginx -t

To test LEMP stack, you can create an index.php file under the /var/www/html/<> directory and enter the following code inside the file.

$ sudo vi /var/www/html/<>/index.php

For example,

$ sudo vi /var/www/html/


<head> <h2>Index Page</h2>  </head>



echo ‘<p>Welcome,</p>’;

// Define PHP variables for the MySQL connection.

$servername = “localhost”;

$username = “testuser”;

$password = “password”;

// Creating a MySQL connection.

$conn = mysqli_connect($servername, $username, $password);

// Show if the connection is successful or fails.

if (!$conn) {

exit(‘<p>Connection failed.<p>’ . mysqli_connect_error());


else {

echo ‘<p>You have connected successfully.</p>’;




Now, you need to access the public IP address of your server in the browser. It should show a page with all the system information. If you see this page, then you have successfully configured the LEMP stack on your CentOS machine. Now at /var/www/html/, you can host your application files.


If you need any further help, please do reach our support department.

Leave a Reply