Install a LEMP Stack on CentOS 7

What is a LEMP Stack?

The LAMP stack (Linux, Apache, MariaDB, and PHP) is a popular server configuration for developing and hosting web applications. The four components of the stack are not tightly coupled, making it possible to substitute your preferred technologies. The LEMP stack is a common variant in which the Apache web server is replaced by NGINX.

Before You Begin

You will need root access to the system, or a user account with sudo privilege.

Installation

NGINX

These instructions install NGINX Mainline on CentOS 7 from NGINX Inc’s official repository. For other distributions, see the NGINX admin guide. For information on configuring NGINX for production environments, see our Getting Started with NGINX series.

Create the file /etc/yum.repos.d/nginx.repo in a text editor and add the following:

PHP

Install the PHP FastCGI Processing Manager, which will bring in the core PHP dependencies:

sudo yum install php-fpm

Ensure PHP-FPM is running and and enabled to start automatically on reboots:

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

Tell PHP to only accept URIs for files that actually exist on the server. This mitigates a security vulnerability where the PHP interpreter can be tricked into allowing arbitrary code execution if the requested .php file is not present in the filesystem. See this tutorial for more information about this vulnerability.

sudo sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php.ini

PHP is set to run under the apache user by default, but this user needs to match the user and group used by NGINX. If you installed NGINX from the NGINX repository as described above, NGINX will use the nginx user and group. Change the user and group variables in www.conf to match: