Installing the LEMP battery on Centos 7

What is a PILE LEMP?

Linux, a free operating system, is the most popular operating system among web hosting providers. To successfully host a website on a Linux server, we need four important software components: an operating system, a web server, a database server, and a platform to run the code on our website. to display web pages. These four components support each other and constitute a STACK OF PEM.

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

The 4 common components composing a STACK are:

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

So what is the use of this information?

Well, if you are hosting a website, you will need a SERVER and a server on which LEMP Stack is not installed is not intended for the server.

In this article, I will show you how to prepare your Linux server for hosting a website.

Preconditions

Before you start installing the LEMP stack, you must follow these steps:

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

Let's start with the installation!

NGINX

Follow the steps below to install NGINX on Centos 7.

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

  1. Using any text editor, create a "nginx.repo" file in the "/etc/yum.repos.d/" location
vi /etc/yum.repos.d/nginx.repo

And add the following content to the file:

[nginx]

name = nginx repo

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

gpgcheck = 0

activated = 1

  1. Update your server again and install NGINX
yum updated

yum install nginx

  1. Start NGINX and enable it when the server is restarted.
systemctl start nginx

systemctl enable nginx

  1. Check the status of NGINX

NGINX is now installed successfully!

You can now check whether NGINX is working correctly or not by directly accessing the IP address of your server on the browser, as shown below:

http: // server_name_or_IP_name /

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

NOTE: If the default NGINX page does not load in the browser, you must allow port 80 in the server's default FIREWALL firewall.

Run the following commands to allow port 80 in FIREWALL:

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

firewall-cmd -reload

MariaDB

This is the database server used to create and manage the databases used by your websites and web applications.

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

  1. Start MariaDB and enable it when restarting the server.
systemctl start mariadb

systemctl activate mariadb

  1. Secure your MariaDB server.
mysql_secure_installation

It will ask you for the root password, just press "enter"Because there is no password initially.

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

Reply Y the following guests:

  • Delete anonymous users?
  • Prohibit remote root connection?
  • Delete the test database and access it?
  • Reload the privilege tables now?

  1. MariaDB is now secure and you can access the SQL shell with the following command:

When prompted, enter the MariaDB password you defined in step 3.

PHP

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

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

systemctl enable php-fpm

systemctl status php-fpm

Secure PHP configuration

  1. Using any text editor, open the file "/etc/php.ini" (main php configuration file

  1. Look for the "cgi.fix_pathinfo = 1" parameter.

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

The final result should look like the one shown in the screen shot below:

  1. Save and close the file.

We did it because "cgi.fix_pathinfo = 1" is an unsafe parameter because it tells PHP to try to run the nearest file that it can find if the Real PHP file does not match exactly. Putting "cgi.fix_pathinfo = 0"We are in the process of mitigating the possibility of executing an arbitrary php code if the requested .php file is not present on the server."

  1. By default, PHP runs under the Apache user, that is, "person." Since you have installed NGINX as a web server, you must modify the user and group so that they match the user and NGINX group.

NGINX uses "nginx" as a 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 screen shots below:

Before making any 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 in the location "/etc/nginx/conf.d/". Here I create a file with the name "newnginx.conf"
vi /etc/nginx/conf.d/newnginx.conf

Paste the following content into the newly created nginx configuration file and save it:

server {

listen 80 default_server;

listening [::]: 80 default_server;

Server name votre_domaine_ou_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_scriptname;

fastcgi_param SCRIPT_NAME $ fastcgi_script_name;

}

}

NOTE: replace "your_domain_or_IP" with your domain name or the IP address of the server.

  1. Restart NGINX

Test PHP processing on your server

  1. In order to test if PHP is configured and working correctly or not, we will test it by creating a php script. To do this, create a file using any text editor with the name "info.php" in the location "/ var / www / html".
vi /var/www/html/info.php

Paste the following code in the file "info.php" and save it:

So, the file will look like the screenshot below:

  1. You can now check if PHP works correctly or not by directly accessing the IP address of your server on the browser, as shown below:
http: //server_domain_name_or_IP/info.php

You will see the page as shown in the screenshot below:

This page essentially provides you with information about your server from the point of view of PHP.

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

Congratulations! The LEMP stack is now installed successfully.