Install and configure Nginx on Ubuntu

Little about nginx

Dear readers, the topic of today's discussion is the installation and configuration of Nginx Ubuntu. Recall that the utility - one of the most popular and popular web-servers around the world. It helps in the work of most of the pages on the Internet (large sites), with a huge number of visitors almost every hour. Why do so many developers give their preference to this particular application? Next is how to install and configure Nginx on Ubuntu.

The application was developed in 2002 and is currently the most popular.

Unlike Apache, which was released relatively long ago - in 1995 - this server is more secure and convenient, which allows you to take this server as the main web or reverse proxy. The fact is that when creating Apache it was impossible to provide a set of functions that a modern user might need. The utility came out not too recently either - in 2002, but it receives stable high-quality updates.

How to install Nginx on Ubuntu?

Before we carry out the installation procedure, which, by the way, is very simple (you will see for yourself soon), it is important to decide on the preparatory work. Without this, installation and launch will be simply impossible. For example, you cannot install Windows without preparing special tools. Let's see what we will need:

  • Naturally, the Ubuntu operating system should be in excellent working condition. It should not have errors or any other spaces.
  • Make sure you have a “regular user”. It is he, not the superuser, who is often required to perform certain operations.
  • If you don’t have one, be sure to create one! Only then can you proceed to further action. A regular account will be a kind of pass for you into the world of installing the utility. After creating a user, log in under him.

Now you can say that you are fully prepared to install the program on Ubuntu. Please note that after the description of each item there is a code that will help you follow this step. There will only be two of them, so let's get started:

  1. First you need to update the local package index:

$ sudo apt-get update

  1. Install the required server using the command:

$ sudo apt-get install nginx

  1. Done! Wait until the end of the process and proceed to setting up and mastering a new web server.

You need to install the web server itself in the system, the program is in the official repositories

Ubuntu software setup

  1. We will show the list of programs with a ufw profile using the command:

$ sudo ufw app list

  1. You will see a list with the types of profiles:
  • Full - a full profile that opens ports 80 and 443. The first is normal with unencrypted traffic, and the second is encrypted.
  • HTTP - will open you only port 80.
  • HTTPS - 143 will be here.

It is usually recommended to set HTTP, but if you consider yourself to be a more advanced user, you can choose the port that you need.

  1. To select, enter the code:

$ sudo ufw allow 'Nginx XXX'

Where instead of XXX you need to write the name of the selected port. For example, $ sudo ufw allow 'Nginx Full'.

  1. Do not forget to fix the set parameters:

$ sudo ufw status

  1. In the next step, the system will display information about your allowed traffic. Here you can already consider the configuration of the Firewall completed.
  2. Now you need to run a check. In general, after the installation is completed, Ubuntu should start Nginx, and the server should work. Just in case, you can verify this with the command:

$ systemctl status nginx

  1. In the data window that appears, you should focus your attention on the line with activity, where the moment from which the server starts its work will be indicated. This will allow you to understand exactly if everything is in order. If there are any problems or problems, be sure to carefully try to repeat the above steps. If all the same does not work, then seek the help of a specialist. Otherwise, you can move on.
  2. Next, we will need to display the public ip address:

$ sudo apt-get install curl

$ curl -4 icanhazip.com

  1. After obtaining the necessary information, enter it in the field for writing the address in the browser:

// domain_name_or_ip_ip_address

  1. If you did everything correctly, then you will see the welcome page of the application, where it will be said that the web server is successfully installed and running. There will also be links for online documentation and regular support, as well as for commercial support. You can congratulate yourself, installation and configuration completed successfully!

Commands, directories and more

Now you can go, in fact, to the study of basic commands, files and directories of the program. This will help you manage the web server correctly.

Teams

  • Stop

$ sudo systemctl stop nginx

  • Run after stopping

$ sudo systemctl start nginx

  • Restart

$ sudo systemctl restart nginx

  • Reboot without canceling the connection (it will remain active)

$ sudo systemctl reload nginx

  • Disable Nginx auto start when server is running

$ sudo systemctl disable nginx

  • Contrary to the previous point - inclusion

$ sudo systemctl enable nginx

Files and directories

  1. Content
  • / var / www / html is a welcome page that we saw a bit earlier. This can be changed by adjusting the configuration files.
  1. Server configuration
  • / etc / nginx - directory where all server parameters and settings are located. Just in this menu you can carry out the procedure indicated in the previous paragraph.
  • /etc/nginx/nginx.conf is the main parameterization file. Used when you want to change something common in Nginx. Therefore, do not be afraid of this file, as some users do.
  • / etc / nginx / sites-available - is the place where server files are stored for all sites pages. That is, it contains and edits all server-parameters that can be enabled (associated with other directories).
  • / etc / nginx / sites-enabled / - if you linked some server files with other sites, as mentioned in the previous paragraph, here you will see a list of them, since this directory is the place where they are stored.
  • / etc / nginx / snippets - there are-configuration fragments that can be included in the Nginx configuration.
  1. Server log
  • /var/log/nginx/access.log - your request to the web server is recorded here. You can also change this position in the settings.
  • /var/log/nginx/error.log is a very useful file containing error data. May save you in some moments.

Conclusion

Friends, today we discussed the rather simple question of how to install and configure Nginx on Ubuntu. Indeed, only a couple of teams helped us solve the first part of the question! Although I had to tinker a bit with the latter: it could take some time to configure the firewall. Tell us in the comments, did you manage to do everything? We hope that the answers will be only positive. Also share your personal experiences, opinions and impressions.