How to Install Nginx on Ubuntu 22.04

Prerequisites
- A HostingDuty VPS with Ubuntu 22.04 — Basic command line usage knowledge is required.
Step 1 - Update Package Lists
Before installing any software, we must update the local package index. This ensures that the package manager knows about the latest available versions of Nginx and its dependencies in the official repositories. We execute this command using sudo privileges.
sudo apt updateVerify: The output should show package lists being updated successfully.
Step 2 - Install Nginx
We install Nginx using the apt package manager. Running 'sudo apt install nginx' fetches and installs the stable version of the web server from the Ubuntu repositories. After this command completes, Nginx should be installed and potentially started automatically.
sudo apt install nginxVerify: The installation process completes with confirmation that Nginx is installed.
Step 3 - Verify Installation
To confirm Nginx is operational, we can point a web browser to the server's IP address. If the installation was successful, the default Nginx welcome page should load in the browser. Alternatively, we can check the service status on the command line.
sudo systemctl status nginxVerify: Accessing the server IP in a browser displays the default Nginx welcome page.
Conclusion
We have successfully installed and verified the Nginx web server on Ubuntu 22.04. You can confirm its operation by viewing the default welcome page via your server's IP address. For next steps, we recommend securing your server by configuring the firewall using UFW, which is crucial for production readiness. We also suggest learning how to configure virtual hosts to manage multiple domains on this single server. For advanced users, exploring PPA methods allows you to install the latest mainline or stable versions of Nginx, bypassing the stock repository version.
Frequently asked questions
How do I check if Nginx is running?
You can check the service status using the command sudo systemctl status nginx. The output should indicate that the service is active and running, confirming Nginx is operational on your Ubuntu 22.04 server.
Where is the default website content located?
The default index page for Nginx is placed in the /var/www/html/ directory. This location is where you can place static HTML pages for your basic website setup.