Hostwinds Tutorials

Search results for:

Table of Contents

Setup LEMP
Before You Get Started:
Step One – Install Nginx
Step Two – Installing MySQL
Step Three – Installing PHP
Configure PHP

Guide on How To Setup LEMP Stack on CentOS 7 (Nginx, MySQL, PHP)

Setup LEMP
Before You Get Started:
Step One – Install Nginx
Step Two – Installing MySQL
Step Three – Installing PHP
Configure PHP

Setup LEMP

LEMP stands for Linux, Nginx (replacing the Apache server), MySQL (which acts as the database storage), and its dynamic content is adapted by PHP. Simply put, a LEMP stack is a convenient way of bundling open-source software that enables a server to host web apps and ever-evolving websites and is centered around the Linux operating system. This article is intended for Hostwinds clients who have a Cloud or VPS server.

The focus of this guide here will be on helping you install the LEMP stack on the CentOS 7 Operating System.

Before You Get Started:

To follow along with this tutorial, you'll need to have root access. If your non-root user account is installed on your server, make sure that they have sudo privileges so that you can utilize the sudo and yum commands we are about to use. As with all changes, we highly recommend taking a snapshot backup before proceeding with this guide.

Step One – Install Nginx

For site visitors to use what you wish to display, you'll need a capable,  up-to-date web server. For the sake of this article, we're going to use Nginx. This is the E in LEMP due to its pronunciation (engine – x)

* To add the CentOS 7 EPEL archive, open your terminal and type in the following yum  command:

sudo yum install epel-release

When prompted, press y  and hit enter to start the installation. This will install the required archive so that Nginx can be installed. When this has been completed, a message stating Complete! will appear.

Once the Nginx archive is installed on your server, you'll want actually to install Nginx using the following command:

sudo yum install nginx

Your web server is now installed. You can start the Nginx service by entering the command:

sudo systemctl start nginx

Since a sudo command is being used, all these operations will now get executed with root privileges. To verify that you have permission to run commands with these root privileges, you'll be asked for your regular user's password.

At this point, a quick check to verify that nothing has gone sideways thus far is always helpful. Pay a visit to your server's public IP address in your web browser, and you should see the default CentOS 7 Nginx web page as confirmation that Nginx is installed and running. You should be greeted with a message similar to the following:

Before moving on, you'll want to enable Nginx to start on bootup. That way, you do not have to retype the above command each time the server starts. You can do so by entering the following command:

sudo systemctl enable nginx

Step Two – Installing MySQL

Next, we're going to install a MySQL drop-in alternative called MariaDB. MariaDB is essentially a community-developed branch of MySQL database management.  It not only organizes but also allows access to the databases storing the information of your website. Syntactically, MySQL and MariaDB are very similar, and there aren't major differences. This is the M in LEMP.

Since we've already employed yum, we'll use it again to grab and install the necessary software.

MySQL / MariaDB is not required to run all web applications, though it is a popular choice. To install MariaDB, you'll want to enter this command:

sudo yum install mariadb-server mariadb

When this has finished, we'll start MariaDB by entering:

sudo systemctl start mariadb

Next, you'll want to employ a simple security script to remove some of the potentially harmful defaults and anchor your database:

sudo mysql_secure_installation

(Don't worry if the prompt asks you for your current root password. At this stage, you won't have one yet, so leave it blank by hitting enter when you are prompted to set a root password, type Y, and follow the instructions to set up your root password and make a note of it.

By hitting "Enter" for the next series of prompts, you'll be able to accept the default values. This will have the bonus of allowing MySQL to immediately acknowledge the changes you've made by removing things like sample users and databases and disabling remote root logins, and then reloading the privilege table.

To allow MariaDB to start on bootup, use this command:

sudo systemctl enable mariadb

Feel free to pat yourself on the back; you just set up your whole database system!

Step Three – Installing PHP

To process your web pages that are written in PHP, you will want to install PHP to your web server and also make sure it starts up if your system reboots using the following commands:

To install PHP, type this command:

sudo yum install php php-common php-fpm php-mysql -y

To initially start the PHP service to run, enter the following command:

sudo systemctl start php-fpm

For enabling the PHP service to start automatically, so you don't have to type the command above every time the server restarts, use this command here:

sudo systemctl enable php-fpm

Configure PHP

It is important to configure PHP properly once it is installed.

You should have the following in php.ini :

“cgi.fix_pathinfo = 0;”

You can accomplish this by editing your php.ini file in /etc/php.ini and saving this file. Here are the commands to accomplish this using the vim text editor:

sudo vi /etc/php.ini

Find the line;cgi.fix_pathinfo=1 and change this to remove the comment and change the value to 0, like so:


You can also use the nano text editor if that is easier.

Once this is done, make sure to save your changes to /etc/php.ini

To allow PHP and the webserver to see the new changes, both services will need to be restarted. To restart PHP, type the following command:

sudo systemctl restart php-fpm

To restart the Nginx service, type the following command:

sudo systemctl restart nginx

You can confirm that this is set properly by creating a page and viewing this page in your browser. The phpinfo.php page should contain the following and be removed once you have confirmed this setting is active:

Make sure that you place the file into your /usr/share/nginx/HTML directory, as this is where your web server will grab files from.

Congratulations! If you have followed the above steps successfully, you should now have the LEMP stack installed on your server. You can now upload your site's files to the /usr/share/nginx/HTML directory or another directory if you update the Nginx config (located at /etc/nginx/nginx.conf).

If you should have any questions or would like assistance, please contact us through Live Chat or submit a ticket with our Technical Support team.

Written by Peter H  /  November 28, 2016

Need help? Chat now!