Introduction

LEMP software stack is a group of open source software, which is typically used to host dynamic websites and web applications. LEMP is an abbreviation of the names of installed software:

L - Linux, E - ENginx, M - MySQL(MariaDB), P - PHP.

How to install LEMP stack on VPS: step by step tutorial

Pay attention! You should be running CentOS 7.

Step 1 – Istall Nginx.

Nginx is a web-server used to display pages of the website.
First you need to add the repository:

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

Install Nginx, using yum command:

yum install nginx

Web-server Nginx will be insalled on the VPS.
When the installation is complete, run the command:

systemctl start nginx.service

To test Nginx open IP of your VPS in a browser.

http://server_domain_name_or_IP/

You should see a default Nginx page. It looks something like this:

LEMP Stack installation

If you see this page, then the web server is installed and configured correct.

Add Nginx to the autorun of the operating system. To do this, run the command:

systemctl enable nginx.service

 

Step 2 – MySQL(MariaDB) Installation.

Nginx web-server is installd on your VPS now. Install MariaDB as a replacement for standard MySQL.

acts from Wikipedia:

MariaDB is a community-developed fork of the MySQL relational database management system intended to remain free under the GNU GPL. Being a fork of a leading open source software system, it is notable for being led by the original developers of MySQL, who forked it due to concerns over its acquisition by Oracle.

Use yum command to install MariaDB (main and additional packages):

yum install mariadb-server mariadb

When installation is over run MariaDB:

systemctl start mariadb

MariaDB is now installed and running on the VPS. Next, you need to run a script to change the settings. This script will change some security settings and close the remote access to the database. To run the script use the following command:

mysql_secure_installation

Enter the root password for MariaDB. Taking into account that all the services were just installed, you may not have this password, so you can leave this field blank by pressing Enter.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

New password: password
Re-enter new password: password
Password updated successfully!
Reloading privilege tables..
 ... Success!

Note! For security reasons there is no feedback of passwords given in the terminal.

Now you need to answer a number of questions concerning security. In all cases, just press Enter and leave all by default. These changes will remove some users and database created by default and will disable root access to the DB.

Add MariaDB to autorun:

systemctl enable mariadb.service

MariaDB is install and setup.

 

Step 3 – Install PHP.

PHP is necessary to process the code and generate dynamic content.

To install PHP use yum. Additionally install PHP-Mysql and PHP-FPM packages:

yum install php php-mysql php-fpm

PHP configuration:

PHP is installed, but we have to configure it for security reasons.

Open php-fpm configuration file:

vi /etc/php.ini

Find and uncomment the parameter: cgi.fix_pathinfo and change its value to 0. In order to uncomment the line, You need to delete a character: “;” before the line:

cgi.fix_pathinfo=0

Save and close this file.

Edit www.conf file:

vi /etc/php-fpm.d/www.conf

Find listen line and set its value:

listen = /var/run/php-fpm/php-fpm.sock

Save and close this file.

Add PHP-FPM to autorun:

systemctl enable php-fpm.service

 

Step 4 – Setup Nginx to work with PHP.

We have installed all the necessary components recently. Finally you have to setup Nginx to work with php (using PHP-FPM while processing).

The setup of Nginx configurational file is done at the block level. The blocks have a lot in common with the description of the virtual hosts in Apache.
Open default configurational file:

vi /etc/nginx/conf.d/default.conf

Nginx default block:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

We have to correct this code.

  • First, we need to add “index.php” text as a first value for the “index” Directive. This Directive specifies the order of the index files. It means that “index.php” file shold be opened for request “http://domain.com/”. If the file index.php is not found, the server will try to open the next file in the list, and so on.
  • We also need to change the “server_name” Directive, put the domain name of your site or the external IP VPS there.
  • The configuration file includes some commented out lines that define the error handling procedure. We uncomment these lines to enable this functionality.
  • For PHP process we will need to uncomment or add some lines in another section of the file. The “location”. You also have to add “try_files” Directive to make sure Nginx doesn’t pass bad requests to our PHP processor.

After the changes the file should look like the following:

server {
    listen       80;
    server_name  server_domain_name_or_IP;

    root   /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

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

Save the changes and close the file.

Reboot Nginx for changes to take effect:

systemctl restart nginx

Step 5 -Test PHP processing on your web server

In order to verify that our system is configured properly to process PHP, we need to create a simple script.

Let`s call it info.php. To ensure that the web server can find the file and process it, it should be located in a specific directory, called “root”.

Root folder on CentOS 7 /usr/share/nginx/html. We may create this file in this directory:

vi /usr/share/nginx/html/info.php

This command opens an empty file. Type there PHP code:

<?php phpinfo(); ?>

Save and close he file

Now we can check whether the web server displays the content generated by the PHP script correctly. To do this we need to open the file that we previously created.

Open the following address in the browser:

http://your_server_IP_address/info.php

If everyhting is clear you will see:

LEMP stack installation

This page displays information about your server from the perspective of PHP. This is useful for debugging and to verify that settings are applied correctly.

If you see this page, it indicates that the server is correctly configured and PHP-FPM is working correctly.

After testing you can delete this file, because it can provide information about your server to unauthorized users. The removal can be done as follows:

rm /usr/share/nginx/html/info.php

You can create this file any time you need it

Conclusion

LEMP stack installation is completed, you have many options for how to use it in the future. This platform allows you to install most kinds of websites and web applications on the server.