The term, LEMP, is an acronym which stands for:
- L- Linux Operating System
- E- Nginx, an HTTP and reverse proxy server
- M- MariaDB relational database management system
- P- PHP programming language
LEMP stack is used for building dynamic websites and web applications. It is very similar to LAMP (Linux, Apache, MariaDB, and PHP), the only difference being, Nginx is replacing Apache.
A common question that arises over here is, why did we need LEMP when we had LAMP. The following reasons will help in clarifying that doubt.
- Nginx is more efficient than Apache, in terms of static content service
- In case you’ll use the server for a site, Nginx will give you better performance for large unit of traffic. Also, scalability, in case of Nginx is easy.
- In case of test environments, Nginx will give you more confidence than Apache.
- With the same set of hardware, Nginx responds fast and is able to handle much higher load compared to Apache.
This guide will provide you details on how to install LEMP server on CentOS 7 server.
Prior to installing LEMP Stack
Before you begin:
Update the software packages of your server to the latest version.
Run the command:
sudo yum -y update
You need to have a root access to the system.
Set your system’s hostname.
Update it.
How to Install LEMP Stack on CentOS 7
To install LEMP stack on CentOS 7, you will have to install Nginx, MySQL, and PHP. This step-by-step guide will enlighten you on how to do so.
Step #1: Installing Nginx
Nginx stands for Engine X, a free, open-source, high-performance HTTP and reverse proxy server. It is responsible for handling big and dynamic websites on the internet.
Before you start installing Nginx, make sure that you are logged in with a user account with Sudo privileges. Also, there should be no service running on the port 80 or 443.
Follow the steps below to install Nginx on your CentOS server.
1. Since Nginx is not a default sever available with the CentOS repository, you will have to install the EPEL repository for it. Nginx packages are available in the EPEL repositories. Aces it through the command:
sudo yum install epel-release
If EPEL is not installed on your server, run the command:
sudo yum install epel-release -y
This command will ask you to enter the user’s password, to check permissions to run it.
2. Now, you can install Nginx by typing the yum command:
sudo yum install Nginx
This will prompt you with Yes, to install Nginx and No, you don’t want to install it option. Click on Yes.
3. Once the installation is complete, enable and start Nginx service with the command:
sudo systemctl enable nginx sudo systemctl start nginx
4. You can check the status of Nginx running on your web server by typing the command:
sudo systemctl status nginx
5. If the server is protected by firewall, you need to open both, HTTP and HTTPS ports. To do so, type the following commands:
sudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload
6. Check whether Nginx is running or not. Open http:// YOUR_IP
in the web browser. If correctly installed, you will see the default Nginx welcome page:

Step #2: Installing MariaDB (MySQL)
MariaDB is an open-source relational database management system. It is a drop-in replacement of MySQL and is built by the MySQL developers.
The latest version available in default CentOS is 5.5. It is not the latest version but very stable.
Follow the steps below to install MariaDB on your CentOS server.
1. Install the MariaDB package using the command:
sudo yum install mariadb-server
Press Y when prompted.
2. Enable and start the MariaDB server.
sudo systemctl start mariadb sudo systemctl enable mariadb
3. To verify the installation, check its status by typing:
sudo systemctl status mariadb
4. Secure its installation, by running:
sudo mysql_secure_installation
5. You will be prompted with several security question. Answer Y to all the prompts.
6. Now, enable the MariaDB server to run automatically every time it’s rebooted. Use command:
sudo systemctl enable mariadb
Step #3: Installing PHP
PHP helps in processing our web application or website code, to display dynamic content.
Follow these steps:
1. Install PHP by running the command:
sudo yum install php php-mysql php-fpm
2. Check whether it is running or not.
sudo systemctl start php-fpm
3. Enable it to start automatically on server reboot.
sudo systemctl enable php-fpm
4. By default, PHP is configured to run through the Apache server. To configure it for Nginx, you have to match the Apache user with the Nginx server and group. Use this command to change the user and group:
sudo sed -i 's/user = apache/user = nginx/g' /etc/php-fpm.d/www.conf sudo sed -i 's/group = apache/group = nginx/g' /etc/php-fpm.d/www.conf
5. Now, make changes in the PHP-FRM configuration.
nano /etc/php-fpm.d/www.conf
6. Make the following changes.
user = apache to user = nginx group = apache to group = nginx listen.owner = nobody to listen.owner = nginx listen.group = nobody to listen.group = nginx
7. Save the configuration using CTRL+X and start the PHP-FRM.
sudo systemctl start php-fpm.service
8. Enable it to automatically reboot.
sudo systemctl start php-fpm.service
Step #4: Configure Nginx server to PHP pages
Now that everything is installed, you need to configure Nginx to server PHP pages.
Follow these steps:
1. Create a root directory for your website. Replace example with your domain name.
sudo mkdir -p /var/www/html/example.com
2. Remove the default configuration file, by using the code:
sudo rm -f /etc/nginx/sites-enabled/default
The configuration files for your website are stored in the /etc/nginx/conf.d
directory. You will have to create a configuration file inside the directory named, example.com.conf
. Replace the example with your domain name in the following code:
server { listen 80 default_server; listen [::]:80 default_server; server_name example.com www.example.com; root /var/www/html/example.com; 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; } }
4. Save the configuration file using CTRL+X (Windows) or CMD+X (Mac).
5. Restart Nginx server using code:
systemctl restart nginx
Step #5: Testing of LEMP Stack
The final step is to test the LEMP Stack configuration.
Follow these steps:
1. Create a basic PHP file index.php inside your website’s root directory.
sudo nano /var/www/example.com/index.php
2. Paste the code:
<?php phpinfo(); ?>
3. Save the file using CTRL+X (Windows) or CMD+X (Mac).
4. Restart the PHP-FRM using code:
sudo systemctl restart php-fpm sudo nginx -s reload
5. Now go to your domain example.com/index.php
.
6. LEMP stack is successfully installed.
Conclusion
Hope this guide helps you in successfully installing LEMP stack on CentOS server.