How to Install Nginx on Ubuntu: Step-by-Step Guide
To install
nginx on Ubuntu, run sudo apt update to refresh packages, then sudo apt install nginx to install it. After installation, start the service with sudo systemctl start nginx and enable it to run on boot using sudo systemctl enable nginx.Syntax
These commands install and manage the Nginx web server on Ubuntu:
sudo apt update: Refreshes the list of available packages.sudo apt install nginx: Installs the Nginx package.sudo systemctl start nginx: Starts the Nginx service immediately.sudo systemctl enable nginx: Sets Nginx to start automatically on system boot.
bash
sudo apt update sudo apt install nginx sudo systemctl start nginx sudo systemctl enable nginx
Example
This example shows the full process of installing Nginx on Ubuntu, starting the service, and enabling it to launch on boot.
bash
sudo apt update sudo apt install -y nginx sudo systemctl start nginx sudo systemctl enable nginx sudo systemctl status nginx
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 Thu 2024-06-20 12:00:00 UTC; 10s ago
Docs: man:nginx(8)
Main PID: 1234 (nginx)
Tasks: 3 (limit: 1137)
Memory: 5.0M
CGroup: /system.slice/nginx.service
├─1234 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─1235 nginx: worker process
└─1236 nginx: worker process
Common Pitfalls
Common mistakes when installing Nginx on Ubuntu include:
- Not running
sudo apt updatebefore installing, which can cause package not found errors. - Forgetting to start the Nginx service after installation, so the server does not run.
- Not enabling Nginx to start on boot, causing it to stop after reboot.
- Running commands without
sudo, leading to permission denied errors.
bash
Wrong (missing update and start): apt install nginx Right: sudo apt update sudo apt install nginx sudo systemctl start nginx
Quick Reference
| Command | Purpose |
|---|---|
| sudo apt update | Refresh package list |
| sudo apt install nginx | Install Nginx server |
| sudo systemctl start nginx | Start Nginx service |
| sudo systemctl enable nginx | Enable Nginx to start on boot |
| sudo systemctl status nginx | Check Nginx service status |
Key Takeaways
Always run 'sudo apt update' before installing packages to get the latest versions.
Use 'sudo apt install nginx' to install the Nginx web server on Ubuntu.
Start Nginx with 'sudo systemctl start nginx' to run it immediately.
Enable Nginx to start on boot with 'sudo systemctl enable nginx'.
Check service status with 'sudo systemctl status nginx' to confirm it is running.