Installing Nginx in Ubuntu 20.04 LTS
The guide below describes how to install the Nginx web server on Ubuntu 20.04 LTS
We will do the whole thing through the console in a few minutes.
We start the terminal and update and install the nginx server
1 | sudo apt update |
1 | sudo apt install nginx |
We add the web server to the firewall:
1 | sudo ufw allow 'Nginx HTTP' |
We can verify the status of the Nginx service by issuing the command:
1 | systemctl status nginx |
An example result is shown below:
1 2 3 4 5 6 7 8 9 10 11 | Output ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2020-04-20 16:08:19 UTC; 3 days ago Docs: man:nginx(8) Main PID: 2369 (nginx) Tasks: 2 (limit: 1153) Memory: 3.5M CGroup: /system.slice/nginx.service ├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─2380 nginx: worker process |
Now we will start creating the file structure for the vhost:
We create the directory structure:
1 | sudo mkdir -p /var/www/nasza_domena/html |
We give permission:
1 | sudo chown -R $USER:$USER /var/www/nasza_domena/html |
We set chmod
1 | sudo chmod -R 755 /var/www/nasza_domena |
We create an example index.html of our website to validate our configuration:
1 | nano /var/www/nasza_domena/html/index.html |
Contents:
1 2 3 4 5 6 7 8 | <html> <head> <title>Witaj na domena.pl!</title> </head> <body> <h1>Sukces! Wszystko działa poprawnie!</h1> </body> </html> |
save the file.
We create a new vhost:
1 | sudo nano /etc/nginx/sites-available/nasza_domena |
And the content:
1 2 3 4 5 6 7 8 9 10 11 12 13 | server { listen 80; listen [::]:80; root /var/www/nasza_domena/html; index index.html index.htm index.nginx-debian.html; server_name nasza_domena www.nasza_domena; location / { try_files $uri $uri/ =404; } } |
We're making a symbolic link:
1 | sudo ln -s /etc/nginx/sites-available/nasza_domena /etc/nginx/sites-enabled/ |
We edit the nginx configuration file
1 | sudo nano /etc/nginx/nginx.conf |
And we uncomment the line:
1 | server_names_hash_bucket_size |
The whole should look like:
1 2 3 4 5 6 7 | ... http { ... server_names_hash_bucket_size 64; ... } ... |
restart the nginx server, our vhost should work properly.
1 | sudo systemctl restart nginx |