The Easiest Guide to Install and Configure Nginx on Ubuntu 20.04
Nginx is a powerful and popular web server known for its speed, reliability, and versatility. It's a favorite for hosting high-traffic websites and applications. Are you ready to get your website up and running? This step-by-step guide will teach you exactly how to install Nginx on Ubuntu 20.04.
Why Choose Nginx? Top Benefits Explained
Nginx is great for several reasons:
- High Performance: Handles lots of traffic without bogging down.
- Reverse Proxy: Improves security and load balancing
- Lightweight: Doesn't use many server resources.
Prerequisites: What You Need Before You Start
Before you begin, make sure you have:
- An Ubuntu 20.04 server.
- A non-root user with
sudo
privileges (follow the Initial Server Setup Guide if needed). - (Optional) A registered domain name.
Once you are ready, log in to your server as the non-root user.
Step 1: Installing Nginx the Easy Way
Nginx is available in Ubuntu's default repositories, making installation a breeze:
- Update your package index:
sudo apt update
- Install Nginx:
sudo apt install nginx
This will install Nginx and all necessary dependencies.
Step 2: Configure the Firewall to Allow Traffic
To make your web server accessible, you need to adjust the firewall.
- List available
ufw
application profiles:
sudo ufw app list
- Enable the
Nginx HTTP
profile that allows traffic on port 80:
sudo ufw allow 'Nginx HTTP'
- Verify the firewall changes:
sudo ufw status
Step 3: Check Your Web Server: Is Nginx Running?
Confirm that Nginx is running as expected. Ubuntu 20.04 starts Nginx automatically after installation.
- Check the service status using
systemd
:
systemctl status nginx
A status showing "active (running)" confirms that Nginx is up.
- Access the default Nginx landing page by entering your server's IP address in a web browser. If you don't know your server's IP address:
curl -4 icanhazip.com
Enter the displayed IP address into your browser. You should see the default Nginx landing page assuring you that everything is working correctly.
Step 4: Essential Nginx Management Commands
Here's how to manage the Nginx process:
- Stop:
sudo systemctl stop nginx
- Start:
sudo systemctl start nginx
- Restart:
sudo systemctl restart nginx
- Reload (for configuration changes):
sudo systemctl reload nginx
- Disable auto-start on boot:
sudo systemctl disable nginx
- Enable auto-start on boot:
sudo systemctl enable nginx
Step 5: Setting Up Server Blocks (Virtual Hosts)
Server blocks allow you to host multiple websites on a single server. Here's how to configure them:
- Create a directory for your domain (replace
your_domain
with your actual domain name):
sudo mkdir -p /var/www/your_domain/html
- Assign ownership to the directory:
sudo chown -R $USER:$USER /var/www/your_domain/html
- Set the correct permissions on the directory:
sudo chmod -R 755 /var/www/your_domain
- Create an
index.html
file in the directory:
sudo nano /var/www/your_domain/html/index.html
- Add sample HTML content to the
index.html
file:
- Create a new server block configuration file:
sudo nano /etc/nginx/sites-available/your_domain
- Paste in the following configuration, replacing
your_domain
with your actual domain:
- Enable the server block by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
- Adjust
server_names_hash_bucket_size
in/etc/nginx/nginx.conf
to avoid potential issues:
sudo nano /etc/nginx/nginx.conf
Uncomment the line server_names_hash_bucket_size 64;
by removing the #
symbol.
- Test for syntax errors:
sudo nginx -t
- Restart Nginx to apply the changes:
sudo systemctl restart nginx
- Visit your domain in a web browser. You should see the
index.html
page you created.
Step 6: Important Nginx Files and Directories Explained
/var/www/html
: Default directory for web content./etc/nginx
: Main Nginx configuration directory./etc/nginx/nginx.conf
: Main configuration file./etc/nginx/sites-available
: Directory for storing server block files./etc/nginx/sites-enabled
: Directory for enabled server blocks (symbolic links to files insites-available
)./etc/nginx/snippets
: Directory for reusable configuration snippets./var/log/nginx/access.log
: Logs every request to your web server./var/log/nginx/error.log
: Records any Nginx errors.
Next Steps: What To Do After Install Nginx on Ubuntu 20.04
You've successfully set up Nginx! Here are some great next steps:
- Set up a LEMP stack: For dynamic websites, install Linux, Nginx, MySQL, and PHP.
- Secure Nginx with Let's Encrypt: Enable HTTPS for secure connections.
Now you know how to install Nginx on Ubuntu 20.04! You're on your way to hosting your own high-performance websites and applications.