Install and Configure Nginx on Ubuntu 20.04: The Ultimate Guide
Need to quickly set up a web server on your Ubuntu 20.04 system? This in-depth guide will walk you through installing and configuring Nginx, the high-performance web server and reverse proxy, step-by-step. Learn how to install Nginx on Ubuntu, manage its processes, and set up server blocks for hosting multiple domains.
Why Choose Nginx?
Nginx is a popular web server known for its speed, stability, and resource efficiency. It's perfect for hosting everything from small personal websites to large, high-traffic applications. Nginx excels as both a web server and a reverse proxy.
Prerequisites
Before you begin, ensure you have:
- An Ubuntu 20.04 server
- A non-root user with
sudo
privileges (refer to the official Ubuntu documentation for creating one) - (Optional) A registered domain name for setting up server blocks
Once you have those, log in to your server as the non-root user.
Step 1: Installing Nginx on Ubuntu
Installing Nginx on Ubuntu is easy. First, update your package index:
sudo apt update
Then install Nginx:
sudo apt install nginx
This command will install Nginx and any necessary dependencies.
Step 2: Adjusting the Firewall for Nginx
Configure your firewall to allow access to Nginx. Nginx registers itself as a ufw
service, simplifying the process.
First, list available application profiles:
sudo ufw app list
Choose the appropriate profile:
Nginx Full
: Allows both HTTP (port 80) and HTTPS (port 443) traffic.Nginx HTTP
: Allows only HTTP traffic.Nginx HTTPS
: Allows only HTTPS traffic.
For this basic setup, enable HTTP by running:
sudo ufw allow 'Nginx HTTP'
Verify the change:
sudo ufw status
Step 3: Checking Your Nginx Web Server
Ubuntu 20.04 starts Nginx automatically after installation. To confirm it's running, check its status:
systemctl status nginx
A successful output indicates that the service is active. You can also check it by browsing to your server's IP address in your web browser.
To find your server's IP address, use the following command:
curl -4 icanhazip.com
Enter the IP address into your browser. You should see the default Nginx landing page.
Step 4: Managing the Nginx Process
Learn how to manage your web server with these commands:
- Stop Nginx:
sudo systemctl stop nginx
- Start Nginx:
sudo systemctl start nginx
- Restart Nginx:
sudo systemctl restart nginx
- Reload Nginx:
sudo systemctl reload nginx
(for configuration changes without dropping connections) - Disable auto-start on boot:
sudo systemctl disable nginx
- Enable auto-start on boot:
sudo systemctl enable nginx
Step 5: Setting Up Nginx Server Blocks (Virtual Hosts)
Server blocks (similar to virtual hosts in Apache) allow you to host multiple domains on a single server. Here's how to set one up for your_domain.com
. Remember to replace your_domain.com
with your actual domain name.
-
Create a Directory: Create a directory for your domain's files:
sudo mkdir -p /var/www/your_domain/html
-
Assign Ownership: Assign ownership of the directory to your user:
sudo chown -R $USER:$USER /var/www/your_domain/html
-
Set Permissions: Set appropriate permissions:
sudo chmod -R 755 /var/www/your_domain
-
Create an
index.html
File: Create a sampleindex.html
file:sudo nano /var/www/your_domain/html/index.html
Add the following HTML to the file:
-
Create a Server Block File: Create a new server block file in
/etc/nginx/sites-available/
:sudo nano /etc/nginx/sites-available/your_domain
Add the following configuration, replacing
your_domain
with your actual domain: -
Enable the Server Block: Create a symbolic link to enable the server block:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
-
Adjust
server_names_hash_bucket_size
: Edit/etc/nginx/nginx.conf
and uncomment theserver_names_hash_bucket_size
line:sudo nano /etc/nginx/nginx.conf
Uncomment this line:
server_names_hash_bucket_size 64;
-
Test Configuration: Test for syntax errors:
sudo nginx -t
-
Restart Nginx: Restart Nginx to apply the changes:
sudo systemctl restart nginx
Now, navigate to http://your_domain.com
in your browser. You should see your custom index.html
page.
Step 6: Important Nginx Files and Directories
Get familiar with these key Nginx locations:
- /var/www/html: Default directory for web content.
- /etc/nginx: Nginx configuration directory.
- /etc/nginx/nginx.conf: Main Nginx configuration file.
- /etc/nginx/sites-available: Directory for storing server block files.
- /etc/nginx/sites-enabled: Directory for enabled server blocks (symlinks to files in
sites-available
). - /etc/nginx/snippets: Directory for reusable configuration fragments.
- /var/log/nginx/access.log: Access log file.
- /var/log/nginx/error.log: Error log file.
Conclusion
You have successfully installed and configured Nginx on Ubuntu 20.04! You can now host websites and applications on your server. Consider exploring advanced configurations, such as setting up HTTPS with Let's Encrypt, to further enhance your setup. Also, you can improve the performance of you Nginx setup by using the long tail keyword Nginx virtual machine.