Setting up Nginx on Ubuntu 22.04

Install nginx from the apt package manager.

sudo apt install nginx -y

Enable firewall.

sudo ufw enable

List the available applications the firewall can allow.

sudo ufw app list

Allow “Nginx Full” which is a combination of “Nginx HTTP” and “Nginx HTTPS”.

sudo ufw allow “Nginx Full”

Confirm that Nginx has been granted access.

sudo ufw status

Confirm that Nginx is enabled and running.

sudo systemctl status nginx

Install the OpenSSH Server.

sudo apt install openssh-server -y

Move your project folder to /var/www/.

sudo cp -r <your_project_folder> /var/www/

Open the /etc/hosts file.

sudo vi /etc/hosts

Add the IP address 0.0.0.0 and its domain name. The domain name can be any name you want.

Give all permissions to the contents of your project.

cd /var/www/<your_project_folder>
sudo chmod -R 777 *

Secure-copy your project files to the exact same folder in your server while maintaining the existing file hierarchy.

scp -r * 0.0.0.0:/var/www/<your_project_folder>

Create a new file named after your project folder in the sites-available directory.

sudo vi /etc/nginx/sites-available/<your_project_folder_name>

Copy and paste the following code, replacing <your_server’s_name> with a name of your choosing.:

server {
    listen 80 default_server;
    listen [::] default_server;
    root /var/www/website;
    index index.html;
    server_name <your_server’s_name>;
    location / {
        try_files $uri $uri/ =404;
    }
}

Create a symbolic link pointing to that recently created file in /etc/nginx/sites-enabled so it can be enabled. Give it the same name.

sudo ln -s /etc/nginx/sites-available/<your_project_folder> /etc/nginx/sites-enabled/<your_project_folder>

Nginx, by default, has a file called default which contains default configurations which will conflict with our project files when it comes to accessing port 80. Move the file to a safe location you’ll remember. Here, I am moving it to my Desktop.

Once you’re done with that, restart Nginx.

sudo mv /etc/nginx/sites-enabled/default Desktop/default
sudo systemctl restart nginx

Go to your browser and type 0.0.0.0 in the address bar to see your website up and running.