LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

LEMP Stack installation on Centos 7

What is a LEMP STACK?

Linux an open source operating, is most popular OS among web hosting providers. For successfully hosting a website on a Linux server we need four important software components i.e an operating system, a web server, a database server and a platform to run our website codes to display web pages. These four components support each other and makes up a LEMP STACK.

LEMP Stack is the most widely used web server configuration for developing and hosting websites and web applications.

The 4 common components that makes up a STACK are:

  1. Linux: Operating system
  2. Nginx (pronounced as “engine X”): Web server
  3. MariaDB: Database server
  4. PHP: Scripting language to run codes/scripts to display web pages.

So, what is the use of this information?

Well, if you are going to host a website you will be needing a SERVER and a server without LEMP Stack installed is not going to server the purpose.

In this article I will be demonstrating how to prepare your Linux Server for successfully hosting a website.

Prerequisites

Before you begin with the installation of LEMP stack following things are required:

  1. VPS or Dedicated server with Centos 7 installed.
  2. Root login details or a user login with sudo privileges.

Let’s begin with the Installation!

NGINX

Follow the steps given below for installing NGINX on Centos 7

  1. Update your server just to make sure all the packages are up-to date

 

yum update -y
  1. Using any text editior create a file “nginx.repo’ at location “/etc/yum.repos.d/”

 

vi /etc/yum.repos.d/nginx.repo

And add the following content in the file:

 

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/

gpgcheck=0

enabled=1

  1. Update your server again and install NGINX
yum update

yum install nginx

  1. Start NGINX and enable it on server reboot.
systemctl start nginx

systemctl enable nginx

  1. Check the Status of NGINX
systemctl status nginx

NGINX is now successfully installed!

You can now verify whether NGINX is working fine or not by directly accessing your server’s IP on the browser as shown below:

http://server_domain_name_or_IP/

You will see the default NGINX webpage as shown in the image below:

NOTE: If the default NGINX page is not loading in the browser then you need to allow port 80 in the server’s default FIREWALL.

Run the following commands to allow port 80 in FIREWALL:

firewall-cmd –permanent –add-port=80/tcp

firewall-cmd –reload

MariaDB

It is the database server used for creating and managing databases used by your websites and web applications.

  1. Install Mariadb and MySQL/MariaDB-PHP support
yum install mariadb-server php-mysql -y

  1. Start MariaDB and enable it on server reboot.
systemctl start mariadb

systemctl enable mariadb

  1. Secure your MariaDB server.
mysql_secure_installation

It will ask for the root password, just press “enter” as there is no password initially.

After this prompt will ask to set a new password. Press Y and set the new password.

Answer Y at the following prompts:

  • Remove anonymous users?
  • Disallow root login remotely?
  • Remove test database and access to it?
  • Reload privilege tables now?

  1. MariaDB is now secured and you can access SQL shell using the following command:
mysql -u root -p
When prompted, enter the MariaDB password you have set in step 3.

PHP

  1. Install PHP and PHP-FPM (FastCGI Processing Manager)
yum install php php-fpm -y

  1. Start and enable PHP-FMP on reboot
systemctl start php-fpm

systemctl enable php-fpm

systemctl status php-fpm

Secure the PHP setup

  1. Using any text editor open the file “/etc/php.ini” (main php configuration file
vi /etc/php.ini
  1. Search for the parameter “cgi.fix_pathinfo=1”.

Uncomment it by removing the semicolon and replace it with “cgi.fix_pathinfo=0”.

The final result should like as shown in the screenshot below:

  1. Save and close the file.

We did this because “cgi.fix_pathinfo=1” is an insecure setting as it tells PHP to try to execute the closest file it can find if the actual PHP file does not match exactly. By setting “cgi.fix_pathinfo=0” we are actually mitigating the possibility of the execution of an arbitrary php code if the requested .php file is not present in the server.

  1. By default, PHP runs under Apache user i.e. “nobody”. Since you have installed NGINX as the web server you need to change the User and Group to match with the NGINX’s User and Group.

NGINX uses “nginx” as the User and Group.

So, open the file “/etc/php-fpm.d/www.conf” using any text editor.

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

And replace user = apache and group = apache TO user = nginx and group = nginx as shown in the screenshots below:

Before the making changes.

After making the changes.

  1. Save and close the file.

 

  1. Restart PHP-FPM
systemctl restart php-fpm
Configure NGINX to Process PHP Pages
  1. Disable the default NGINX configuration file.
mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.back
  1. Create a new configuration file at location “/etc/nginx/conf.d/”. Here i am creating a file with the name “newnginx.conf”
vi /etc/nginx/conf.d/newnginx.conf

And paste the following content in the newly created nginx configuration file and save it:

server {

listen         80 default_server;

listen         [::]:80 default_server;

server_name    your_domain_or_IP;

root           /var/www/html;

index          index.html;




location / {

try_files $uri $uri/ =404;

}




location ~* \.php$ {

fastcgi_pass 127.0.0.1:9000;

include         fastcgi_params;

fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;

fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;

}

}

NOTE: replace “your_domain_or_IP” with your actual domain name or server’s IP address.

  1. Restart NGINX
systemctl restart nginx
Testing PHP Processing In Your Server
  1. In order to test whether PHP is configured and working fine or not we will test it by creating a php script. For this create a file using any text editor with the name “info.php” at location “/var/www/html”
vi /var/www/html/info.php

And paste the following code in “info.php” file and save it:

<?php

phpinfo();

?>

So, the file will look as shown in the screenshot attached below:

  1. You can now verify whether PHP is working fine or not by directly accessing your server’s IP on the browser as shown below:
http://server_domain_name_or_IP/info.php
You will the see the page as shown in the screenshot below:

This page basically gives you information about your server from the perspective of PHP.

  1. Remove the info.php file once you have confirmed that PHP is working fine.
rm /var/www/html/info.php

Congratulations! LEMP stack is now successfully installed.

Frank

1 Comment

  1. Val:

    What of ssl.. how do we make the page https

    May 9, 2019 @ 4:12 am | Reply

Leave a Reply

Some notes on commenting on LowEndBox:

  • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
  • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
  • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

Your email address will not be published. Required fields are marked *