Install LEMP Stack on CentOS 7

A 5-part series covering lemp and centos

By Dejan Panovski

Updated on

5 parts

Install LEMP Stack on CentOS 7

The term LEMP is an acronym of the names of its four open-source components:

  • L - Linux operating system
  • E - Nginx [engine x], an HTTP and reverse proxy server
  • M - MySQL or MariaDB relational database management system
  • P - PHP programming language.

This series of tutorials will show you how to install Nginx, generate a free Let’s Encrypt SSL certificate, install and secure MySQL, and install PHP 7.

The detailed tutorials that are part of this series are listed at the end of this page.

If you are in a hurry and don’t want to read more detailed documentation, you can install a LEMP stack on your CentOS 7 server by following our Quickstart section.

Install LEMP Stack on CentOS 7 [Quickstart]

This quickstart will show you the basic steps required to get a LEMP stack installed on a CentOS 7 server.

Prerequisites

The user you are logged in as must have sudo privileges to be able to install packages.

Step 1. Installing Nginx

Nginx is not available in the default CentOS 7 repository so we will use the EPEL repositories. To add the EPEL repository to your system, use the following command:

Terminal
sudo yum install epel-release

Now that the EPEL repository is enabled, install the Nginx package with:

Terminal
sudo yum install nginx

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

Terminal
sudo systemctl start nginx
sudo systemctl enable nginx

Step 2. Installing MariaDB

The next step is to install the MariaDB packages. To do so, type:

Terminal
sudo yum install mariadb-server

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

Terminal
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service
Info
At the time of writing, MariaDB 5.5 is available in the official CentOS 7 repository. If you want to install a newer version please refer to this tutorial . To install MySQL instead of MariaDB, check our tutorial for installation instructions.

Step 3. Installing PHP

CentOS 7 ships with PHP version 5.4 which has been EOL for quite some time, so we’ll use the Remi repository to install PHP 7.2.

Run the following command to add the Remi repository to your system:

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

Once it is added, install the yum-utils package and enable the remi-php72 repository:

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

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

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

By default, PHP-FPM will run as user apache on port 9000. We’ll change the user to nginx and switch from TCP socket to Unix socket. To do so, edit the lines highlighted in yellow:

Terminal
sudo nano /etc/php-fpm.d/www.conf
ini
...
user = nginx
...
group = nginx
...
listen = /run/php-fpm/www.sock
...
listen.owner = nginx
listen.group = nginx

Make sure the /var/lib/php directory has the correct permissions:

Terminal
chown -R root:nginx /var/lib/php

Save the file, enable and start the PHP FPM service with:

Terminal
sudo systemctl enable php-fpm
sudo systemctl start php-fpm

Step 4. Configuring Nginx to Process PHP Pages

Now that we have all of the LEMP components installed, we can edit the Nginx server block configuration file and add the following lines so Nginx can process PHP files:

nginx
server {
  # other code

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/run/php-fpm/www.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}

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

Terminal
sudo systemctl restart nginx

More Information

For more detailed instructions about each step, please consult the following tutorials.

Articles