Install Nginx on Ubuntu Server 20.04
Nginx is a popular lightweight web server that delivers excellent performance and very flexible configuration. We often use it as a load balancer or a reverse proxy.
You can install it and set it up in just a few minutes. Later on you can extend it to complex powerful setups.
How to install Nginx
You don’t have any prerequisites. In this tutorial you need just a cloud server running Ubuntu Server 20.04
It is of course recommended to start with the update of the packages lists:
apt update
To start the Nginx installation, simply install the ‘nginx’ package:
apt install nginx
Once the installation is complete, the Nginx service will start automatically with a default configuration and an example page.
Some basic configuration
You will find the Nginx configuration files in the /etc/nginx directory. The /etc/nginx/nginx.conf is the main configuration file. It includes the rest of the configuration. The nginx.conf contains some global configurations like TCP settings, timeouts and SSL parameters.
Site specific parameters are in /etc/nginx/sites-available and /etc/nginx/sites-enabled directories. In there you’ll find the /etc/nginx/sites-enabled/default file which contains your default site config. The one currently serving your site on http://your-server-ip-address/. It looks similarly to the code below:
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
Your very first configuration knowledge should be the server block. It configures an instances of a web site configuration or in other words a virtual host.
The listen directive specifies the TCP port this site will be listening on. For example:
listen 80;
When you are setting up multiple web sites on a single web server (well, that’s most of the practical cases) you would rather distinguish them by using multiple hostname. So here comes the server_name directive that will specify the various hostnames, domains or subdomains:
server_name www.cloudbalkan.com;
And of course the other important setting on this stage is what files are we going to serve – the web root. This one is configured with the root directive:
root /var/www/html;
Last piece is the location block. Those can be a bit more complex rules for handling different locations or location patters. When you come to setting up a PHP handler, a reverse proxy or specific file type handling you’ll need to learn a bit more on this block. For now it simply says that the web server will look for the requested files in your web root directory and will kindly return 404 error if they do not exist.
Starting, stopping and reloading
Controlling the Nginx web server is simple. You can start and stop it using the systemd service:
systemctl stop nginx
systemctl start nginx
For most of the configuration changes you can use the graceful reload:
systemctl reload nginx
Last good advice is to check the configuration before restarting and reloading:
sudo nginx -t
Welcome to nginx!
Welcome to the Nginx world. It’s an entire universe related to Nginx and we’ll get more articles related to it. So follow CloudBalkan blog and ping us at support@cloudbalkan.com if you have any questions.