How to Install LEMP Stack on CentOS 7

Install LEMP Stack on CentOS 7

In this article, we will have explained the necessary steps to install and configure LEMP Stack on CentOS 7. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

LEMP is an acronym for Linux, Nginx, MySQL, and PHP. LEMP stack just like LAMP stack (with Apache instead of Nginx) is used for development and deployment of web applications. Nginx in LEMP provides a modular event-driven architecture handling requests using asynchronous events. This feature enables it to have high performance under high loads. The MySQL is used to store the website’s data whereas PHP is for processing the dynamic content of the sites.

Install LEMP Stack on CentOS 7

Step 1. The first command will update the package lists to ensure you get the latest version and dependencies.

sudo yum install epel-release
sudo yum update

Step 2. Installing Nginx.

Now that the repository is added, it’s time to install Nginx:

sudo yum install nginx

Once it is installed, start and enable the Nginx service by typing:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 3. Installing MariaDB.

To install MariaDB type following command:

sudo yum install mariadb-server

Once MariaDB server is installed, start and enable the service with:

sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

Step 4. Installing PHP.

Add remi repository in CentOS so we can install latest PHP 7.2, typing following command:

sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Now enable PHP72 package using below command:

sudo yum install yum-utils
sudo yum-config-manager --enable remi-php72

Then, that we have Remi repository enabled, we can install PHP FPM and several most common PHP modules with:

sudo yum install php-fpm php-opcache php-cli php-gd php-curl php-mysql

Now, open PHP-FPM configuration:

nano /etc/php-fpm.d/www.conf

Find and replace these lines:

user = apache to user = nginx
group = apache to group = nginx
listen.owner = nobody to listen.owner = nginx
listen.group = nobody to listen.group = nginx
And, lastly, under ;listen = 127.0.0.1:9000 add this line:
listen = /var/run/php-fpm/php-fpm.sock

Step 5. Set up Nginx configuration file.

Create directory inside var/www/html named idroot.us (you can use your domain name):

sudo mkdir -p /var/www/html/idroot.com

Configuration files for the website are stored inside /etc/nginx/conf.d directory so you need to create configuration file inside this directory named idroot.us.conf (you can use your domain name). Then enter following code inside that file by replacing idroot.us with your domain name:

nano /etc/nginx/conf.d/example.com.conf
server { 
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name idroot.us www.idroot.us; 
    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; 
    }  
}

Do not forget to restart the Nginx service for the changes to take effect:

sudo systemctl restart php-fpm
sudo nginx -s reload

That’s all you need to do to Install LEMP Stack on CentOS 7. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.