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

Setup LEMP

Simply put, a LEMP stack is a convenient way of bundling  open source software that enables a server to host web apps as well as ever-evolving websites. Centered around the Linux operating system, LEMP stands for Linux, Nginx (replacing the Apache server), MySQL (which acts as the database storage), and it’s dynamic  content is then adapted by PHP.

The focus here will be on helping you install your stack on an CentOS 7 Virtual Private Server.

Before You Get Started:

To follow along with this tutorial, you’ll need to have a separate, non-root user account installed on your server so that you can utilize the sudo  and yum  commands we are about to go over.

Step One – Install Nginx

In order for site visitors to be able 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.

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

When prompted, press y  and hit enter to start the installation. When this has completed, a message stating Complete! will appear.

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

Your web server is now installed. Once you have it installed, start Nginx by entering the command:

Note that, since a sudo  command is being used, all these operations will now get executed with root privileges. In order to 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:

nginx default welcome message

Before moving on, you’ll want to enable Nginx to start on boot up. You can do so by entering the following command:

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 the MySQL database management.  It not only organizees, but also allows access to the databases storing the information of your website.

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

Optional: A lot of users find it helpful to additionally install some extra software packages to help in getting the various components to talk to one another. Again, while this is not required, you might find it making life easier for you. To do so, you’ll want to enter this command:

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

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

(Don’t worry of the prompt asks you for your current root password. At this stage you won’t have one yet, so just leave it blank by hitting enter. When you are prompted to set a root password, just type Y  and follow the instructions to set up your root password and make 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 added bonus of allowing the MySQL to immediately acknowledge the changes you’ve made, by removing things like: sample users and databases, as well as disabling remote root logins and then reloading the privilege table.

To allow MariaDB to start on boot up, use this command:

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

Step Three – Installing PHP

In order 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:

Configure PHP

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

You should have “cgi.fix_pathinfo = 0;” in php.ini

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:

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


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

You can confirm that this is set properly through 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:






Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.