Install Nginx on Ubuntu 20.04: The Comprehensive Guide
Looking for a reliable web server? This guide walks you through how to install Nginx on Ubuntu 20.04, covering setup, configuration, and essential management tips. Get your server running smoothly!
Why Choose Nginx? The Powerhouse Web Server
Nginx is a powerful, lightweight web server known for its performance and scalability. It excels as both a web server and a reverse proxy, making it a great choice for hosting high-traffic websites. Ready to get started with Nginx Ubuntu?
Prerequisites: Getting Ready to Install Nginx
Before we dive into the installation, make sure you have the following:
- An Ubuntu 20.04 server.
- A non-root user with sudo privileges (refer to the Initial server setup guide for Ubuntu 20.04).
- (Optional) A registered domain name for setting up server blocks.
Step 1: Installing Nginx – The Quick and Easy Way
Installing Nginx is straightforward using Ubuntu's apt
package manager. First, update your package index, then install Nginx:
sudo apt update
sudo apt install nginx
apt
will handle the installation process, including any necessary dependencies.
Step 2: Fine-Tuning the Firewall for Nginx
Adjust your firewall to allow access to Nginx. Use ufw
to manage firewall rules efficiently. Here's how:
- List available application profiles:
sudo ufw app list
- Enable the 'Nginx HTTP' profile for port 80:
sudo ufw allow 'Nginx HTTP'
- Verify the changes:
sudo ufw status
This allows unencrypted web traffic to reach your Nginx server.
Step 3: Verify Your Nginx Installation
Time to check if Nginx is running correctly.
- Check the service status:
systemctl status nginx
- Access the default Nginx landing page by entering your server's IP address in your browser. You can find your IP address using
curl -4 icanhazip.com
.
If you see the default Nginx page, congratulations, your server is running!
Step 4: Mastering Nginx Process Management
Knowing how to manage the Nginx process is crucial. Here are the essential commands:
- Stop the server:
sudo systemctl stop nginx
- Start the server:
sudo systemctl start nginx
- Restart the server:
sudo systemctl restart nginx
- Reload configuration (without dropping connections):
sudo systemctl reload nginx
- Disable auto-start on boot:
sudo systemctl disable nginx
- Enable auto-start on boot:
sudo systemctl enable nginx
Step 5: Configuring Nginx Server Blocks for Multiple Domains
Server blocks (similar to virtual hosts in Apache) allow you to host multiple domains on a single server.
- Create a directory for your domain:
sudo mkdir -p /var/www/your_domain/html
(Replaceyour_domain
with your actual domain name). - Assign ownership:
sudo chown -R $USER:$USER /var/www/your_domain/html
- Set permissions:
sudo chmod -R 755 /var/www/your_domain
- Create an
index.html
file:sudo nano /var/www/your_domain/html/index.html
- Create a server block:
sudo nano /etc/nginx/sites-available/your_domain
Paste the following configuration, adjusting the root
and server_name
directives.
server {
listen 80;
listen [::]:80;
root /var/www/your_domain/html;
index index.html index.htm index.nginx-debian.html;
server_name your_domain www.your_domain;
location / {
try_files $uri $uri/ =404;
}
}
- Enable the server block:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
- Adjust
server_names_hash_bucket_size
: Uncomment the line in/etc/nginx/nginx.conf
. - Test configuration:
sudo nginx -t
- Restart Nginx:
sudo systemctl restart nginx
Now, Nginx should serve your domain!
Step 6: Important Nginx Files and Directories
/var/www/html
: Default directory for web content./etc/nginx
: Nginx configuration directory./etc/nginx/nginx.conf
: Main configuration file./etc/nginx/sites-available
: Directory for storing server blocks./etc/nginx/sites-enabled
: Directory for enabled server blocks (symlinks to files insites-available
)./var/log/nginx/access.log
: Records all requests to your web server./var/log/nginx/error.log
: Records any Nginx errors.
Next Steps: Beyond Basic Nginx Installation and Configuration
Now that you've installed and configured Nginx, consider these next steps:
- Install a LEMP stack: Learn how to install Linux, Nginx, MySQL, and PHP on Ubuntu 20.04.
- Secure Nginx with Let's Encrypt: Set up HTTPS for your domain with a free SSL certificate using Let's Encrypt.
- Explore Nginx virtual machine: Streamline setup with a pre-configured Nginx virtual machine.