Install Nginx on Ubuntu 22.04

Install Nginx on Ubuntu 22.04

Nginx is an open-source web server that serves web content to clients. It can also be used as a reverse proxy, load balancer, cache server, and more.

This tutorial demonstrates how to install Nginx on Ubuntu 22.04.

Install Nginx

Download GPG key:

sudo wget -qO /etc/apt/trusted.gpg.d/nginx-key.asc https://nginx.org/keys/nginx_signing.key

Add repository:

echo "deb [arch=amd64] https://nginx.org/packages/mainline/ubuntu/ $(lsb_release -sc) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list

To update the package lists, execute the following command:

sudo apt update

Install Nginx:

sudo apt install -y nginx

Add the nginx user to the www-data group. It will ensure that the Nginx process has appropriate permissions to access and manage web-related files and directories.

sudo usermod -aG www-data nginx

Run the following command to start Nginx:

sudo service nginx start

We can check Nginx version as follows:

nginx -v

We can check whether Nginx service is running with command:

sudo service nginx status

Also, we can stop or restart the Nginx service:

sudo service nginx stop
sudo service nginx restart

Testing Nginx

Get the IP address of your machine:

hostname -I

Open a web browser and type the IP address. The default Nginx welcome page will be presented as shown below:

Nginx default page on Ubuntu

Uninstall Nginx

If you decided to completely remove Nginx and related dependencies, execute the following command:

sudo apt purge --autoremove -y nginx

Remove Nginx user:

sudo deluser nginx

Remove GPG key and repository:

sudo rm -rf /etc/apt/trusted.gpg.d/nginx-key.asc
sudo rm -rf /etc/apt/sources.list.d/nginx.list

Leave a Comment

Cancel reply

Your email address will not be published.