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

Setup LEMP

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. 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 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 you non-root user account 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

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. This is the E in LEMP due to its pronunciation (engine – x)

* 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. This will install the required archive so that Nginx can be installed. 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. You can start the Nginx service by entering the command:

 

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

 

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 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 is a popular choice. To install MariaDB, 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:

To install PHP, type this command:

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

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:

 

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:

cgi.fix_pathinfo=0

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 that were made, both services will need to be restarted. To restart PHP, type the following command:

To restart the Nginx service, type the following command:

You can confirm that this is set properly through creating a php.info 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 are now able to upload your sites 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, do feel free to contact us through Live Chat, on our Phones, or by submitting a ticket with our Technical Support team.

 

Related Articles


Share:

Leave a Reply

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