Installing NGINX web server for working with virtual hosts, PHP-FPM in Sock mode, MariaDB Mysql server on Centos 7

nginx – web server and mail proxy server running on Unix-like operating systems

A series of articles on installing and configuring the NGINX web server

  • Installing NGINX web server for working with virtual hosts, PHP-FPM in Sock mode, MariaDB Mysql server on Centos 7
  • Password for file / directory NGINX web server in Centos / Ubuntu
  • Enabling SSL in NGINX on Centos 7
  • Free SSL and Widacard SSL certificate from Let’s Encrypt, NGINX connection and auto-renewal on Centos 7

Let’s consider the process of configuring the NGINX web server to work with virtual hosts, using the example of a virtual machine from the company “Cloud”

Add EPEL and Remi repositories, required software and update CentOS

[[email protected]]# yum install epel-release
[[email protected]]# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[[email protected]]# yum install wget nano mc zip upzip htop
[[email protected]]# yum install update

Add the NGINX repository by creating a file with the contents:

[[email protected]]# nano /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1 

Add the MariaDB repository by creating a file with the contents:

[[email protected]]# nano /etc/yum.repos.d/mariadb.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

Install the NGINX web server

[[email protected]]# yum install nginx

Launch NGINX and add it to startup

[[email protected]]# systemctl start nginx
[[email protected]]# systemctl enable nginx

Let’s open ports 80 and 443 in the firewall

[[email protected]]# firewall-cmd --zone=public --permanent --add-service=http
[[email protected]]# firewall-cmd --zone=public --permanent --add-service=https
[[email protected]]# firewall-cmd --reload

Install the MariaDB mysql server

[[email protected]]# yum install mariadb-server mariadb

We start the database server and run the configuration script

[[email protected]]# systemctl start mariadb
[[email protected]]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

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.

Set root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Add the service to startup

[[email protected]]# systemctl enable mariadb

Disable SELinux (not safe!)

[[email protected]]# nano /etc/sysconfig/selinux
...
SELINUX=disabled
...

Perboot

[[email protected]]# reboot

Install PHP 7.3, configure PHP-FPM in SOCK mode

First, let’s install yum-utils for the yum-config-manager tool

[[email protected]]# yum install -y yum-utils

Enable remi repository for php 7.3 installation

[[email protected]]# yum-config-manager --enable remi-php73

Install php 7.3 and some components

[[email protected]]# yum install php php-xml php-soap php-xmlrpc php-mbstring php-json php-gd php-mcrypt php-common php-fpm php-pdo php-mysqlnd php-imap php-embedded php-ldap php-odbc php-zip php-fileinfo php-process php-opcache

We check:

[[email protected]]#  php -v
PHP 7.3.7 (cli) (built: Jul  3 2019 11:30:22) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.7, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.7, Copyright (c) 1999-2018, by Zend Technologies

Setting up PHP-FPM in SOCK mode, launch php-fpm and add it to autoload

[[email protected]]# systemctl start php-fpm
[[email protected]]# systemctl enable php-fpm

Check if it started

[[email protected]]# netstat -tulpn | grep php-fpm
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      3031/php-fpm: maste

If the netstat utility is missing, install it

[[email protected]]# yum install net-tools

Configuring php-fpm

[[email protected]]# nano /etc/php.ini

We are interested in the cgi.fix_pathinfo parameter. By default it is commented out and set to 1. Let’s uncomment the line and change the value to 0.

cgi.fix_pathinfo=0

Also, do not forget to change the time zone, for this we uncomment the line; date.timezone = and enter the required time zone.

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Europe/Moscow

Enable short_open_tag to enable short form PHP tag writing

short_open_tag = On

Let’s change the user php-fpm will run from. Instead of apache, let’s specify nginx.

[[email protected]]# nano /etc/php-fpm.d/www.conf
...
user = nginx
group = nginx

listen.owner = nginx
listen.group = nginx
...

Find the line starting with listen and replace it with a completely new value.

;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock

Restart php-fpm

[[email protected]]# systemctl restart php-fpm

Why switch to unix socket?

UDS (unix domain socket), in contrast to communication via the TCP stack, has significant advantages:

  • do not require context switching, UDS uses netisr
  • the UDS datagram is written directly to the destination socket
  • sending a UDS datagram requires fewer operations (no checksums, no TCP headers, no routing)
  • UDS has ~ 66% less latency and 7 times more bandwidth than TCP

Restart NGINX

[[email protected]]# systemctl restart nginx

After restarting NGINX, I got an error:

nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so" version 1012002 instead of 1014000 in /usr/share/nginx/modules/mod-http-geoip.conf:1

This error appeared due to the fact that I installed NGINX from the standard repository, and then connected the official NGINX repository and updated.

To solve this problem, remove all modules from the NGINX repository:

[[email protected]]# sudo yum remove nginx-mod*

Install modules from the official repository:

[[email protected]]# sudo yum install nginx-module-*

Restart NGINX:

[[email protected]]# sudo systemctl restart nginx

Configuring NGINX to work with virtual hosts

Let’s create the necessary directories for the virtual host

[[email protected]]# mkdir -p  /var/www/example.ru/public_html
[[email protected]]# mkdir /var/www/example.ru/logs

Create an html file at the root

[[email protected]]# nano /var/www/exampl.ru/public_html/index.html
<html>
  <head>
    <title>Sample web page on example.ru website</title>
  </head>
  <body>
    <h1>Nginx server</h1>
    This sample web page confirms that the first Nginx virtual host 
   or server block is working for example.ru
  </body>
</html>

We set the rights to the public_html directory

[[email protected]]# chmod -R 755 /var/www/example.ru/public_html

Create directories for virtual host configs

[[email protected]]# mkdir /etc/nginx/sites-available
[[email protected]]# mkdir /etc/nginx/sites-enabled

Let’s edit the main nginx.conf config

[[email protected]]# nano /etc/nginx/nginx.conf

Before the closing curly brace of the http block, add the lines:

include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;

Create a config file for our virtual host

[[email protected]]# nano /etc/nginx/sites-available/example.ru.conf
server {
    listen  80;
    server_name iexample.ru www.example.ru;
	access_log /var/www/example.ru/logs/access.log;
    error_log /var/www/example.ru/logs/error.log;
	root /var/www/example.ru/public_html;
	
location / {
        try_files $uri $uri/ /index.html;
    }
 
      location ~ /.ht {
        deny all;
    }
 
    location ~ /. {
        deny all;
        access_log off;
        log_not_found off;
    }
 
    location ~ .(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
        try_files $uri =404;
    }
 
    location ~* .php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        #fastcgi_pass  127.0.0.1:9000; ## Если работаем не по сокетам
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/example.ru/public_html$fastcgi_script_name;
    }
}

We make a symbolic link. This makes it easier to disconnect / reconnect new virtual hosts.

[[email protected]]# ln -s /etc/nginx/sites-available/example.ru.conf /etc/nginx/sites-enabled/example.ru.conf

And restart NGINX

[[email protected]]# systemctl restart nginx

Checking the work of NGINX and PHP

Create info.php file

[[email protected]]# nano /var/www/example.ru/public_html/info.php
<?php phpinfo(); ?>

In the hosts file on the working PC, we register the correspondence between the ip and the site address (you can ask me how to do this in the comments) and make sure that nginx and php work.

Sidebar