How to install and configure Nginx web page server

Author:  
Tanvir Hasan
Category: 
May 17, 2022

Nginx is one of the servers for web pages, both static and dynamic, that offer the best results, especially on web pages with a large number of visits. Although for users there are hardly any differences between using one server or another, for web administrators, one of the features beyond performance and the way of working with processes is configuration.

How to install and control the Nginx daemon

The first thing we have to do to use Nginx is to install it on our system. To do this, the first thing we must do is type in our terminal:

If we use a distribution based on Debian:

  • apt-get install nginx

If we use a distribution based on Red Hat:

  • yum install nginx

Once the installation of Nginx is finished, we will have the daemon running, ready to start loading our websites. We can easily control the web server daemon with the following commands:

  • service nginx start | stop | restart -> To start, stop or restart the service or daemon.
  • service nginx configtest | reload -> To test a configuration or reload it after making changes to the server.
  • service nginx status -> To be able to know the status of the server at that moment.

How to configure Nginx

Basic configuration

One of the characteristics of this web server is that it allows you to configure the server and each one of the web pages that we mount on it separately. On the one hand, the basic configuration of the server is found in the file:

  • /etc/nginx/nginx.conf

Within this file we will be able to configure the general functions of the web server, among others:

  • The user that will run the server.
  • Number of server processes (based on the number of CPU cores).
  • The server's master process (pid)
  • The path where the log files will be saved.
  • Maximum users connected to the server.
  • HTTP configuration (file types, data delivery, Gzip compression, web server path, mail server configuration, etc).

At the end of this file, inside the http block, we can see a default line called “include /etc/nginx/sites-enabled/*”. This line tells the server to load specific settings from different files and directories, in order to function as "virtual servers" and allow you to easily enable and disable settings without having to delete them.

How to set up your first website

As we have said, the configuration of the virtual server that creates us by default is found in the path “/etc/nginx/sites-available/default”.

If we edit this file we will be able to find, among other functions:

  • The listening ports (by default, 80).
  • The directory where the web is stored.
  • The default file when accessing the web, by default, «index.html».
  • Behavior in case of 404 error.
  • Setting up a "virtual" server based on it.
  • HTTPS configuration.

All the configuration of this file will be «overwritten» to the general configuration of the server.

By default, as indicated in the previous configuration file, when accessing the web through port 80, the index.html file will be opened by default. In the case of having a PHP server associated with Nginx (which we will see later), the file that will be opened by default will be index.php.

We can open the "index.html" file with "nano", see its content and even modify it or change it for another so that it shows our own web page.

Useful tips and practices for Nginx

With this, we will be able to set up a simple HTTP server ready to host static pages. In summary, some of the aspects that we must remember when configuring our server are:

  • We must separate the configurations as much as possible.
  • It is recommended to make a separate configuration for each server, even if they are the same.
  • We must use "include" to import each configuration to the Nginx server.
  • The names of each server should use a clear pattern and easy to identify names.
TAGS
Related Articles
0
    0
    Your Cart
    Your cart is emptyHome
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram