How to Install and Configure Nginx on Ubuntu 20.04
If you're looking to set up a high-performance web server, Nginx is an excellent choice. This guide will walk you through the process of installing and configuring Nginx on an Ubuntu 20.04 server. We'll cover everything from installation to setting up server blocks for hosting multiple domains. Let's get started.
Introduction
Nginx is a powerful, open-source web server that is widely used for its speed, scalability, and ability to handle multiple connections efficiently. Whether you are hosting a small personal website or a high-traffic enterprise application, Nginx can be configured to meet your needs. In this guide, we'll cover the entire setup process, ensuring that you can have your server up and running smoothly.
Step 1: Installing Nginx
The first step is to install Nginx on your Ubuntu server. Since Nginx is available in Ubuntu's default repositories, this process is straightforward.
Update your local package index:
$ sudo apt update
Install Nginx:
$ sudo apt install nginx
Accept the installation process, and apt will install Nginx along with any required dependencies.
Step 2: Adjusting the Firewall
Before you can test Nginx, you need to adjust the firewall settings to allow traffic to Nginx. Upon installation, Nginx registers itself as a service with ufw (Uncomplicated Firewall), making it easy to allow access.
List the available application configurations:
$ sudo ufw app list
You should see the following output:
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
Allow Nginx Full access (this opens ports 80 and 443):
$ sudo ufw allow 'Nginx Full'
Verify the firewall status:
$ sudo ufw status
The output will indicated which HTTP traffic is allowed:
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
The output will show the allowed HTTP traffic, confirming that Nginx can accept requests.
Step 3: Checking Your Web Server
After installation, Nginx should start automatically. To verify that the service is running, use the following command:
$ 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 Fri 2020-04-20 16:08:19 UTC; 3 days ago
Docs: man:nginx(8)
Main PID: 2369 (nginx)
Tasks: 2 (limit: 1153)
Memory: 3.5M
CGroup: /system.slice/nginx.service
├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─2380 nginx: worker process
You should see output indicating that Nginx is active and running. To further confirm, navigate to your server's IP address in a web browser. If you see the default Nginx landing page, the installation was successful.
Step 4: Managing the Nginx Process
With Nginx installed and running, you should learn some basic management commands. These commands allow you to control the Nginx service.
Stop the web server:
$ sudo systemctl stop nginx
Start the web server:
$ sudo systemctl start nginx
Restart the web server:
$ sudo systemctl restart nginx
Reload Nginx without dropping connections:
$ sudo systemctl reload nginx
Disable Nginx from starting automatically at boot:
$ sudo systemctl disable nginx
Re-enable Nginx to start at boot:
$ sudo systemctl enable nginx
Step 5: Setting Up Server Blocks (Recommended)
To host multiple domains on a single server, you can use Nginx server blocks (similar to virtual hosts in Apache).
Create a directory structure for your domain:
$ sudo mkdir -p /var/www/html/your_domain
Assign ownership of the directory:
$ sudo chown -R $USER:$USER /var/www/html/your_domain
Set the correct permissions:
$ sudo chmod -R 755 /var/www/your_domain
Create a sample index.html page:
$ sudo nano /var/www/html/your_domain/index.html
Add the content in index.html
Create a new server block configuration file:
$ sudo nano /etc/nginx/sites-available/your_domain
Add the following configuration:
server {
listen 80;
listen [::]:80;
root /var/www/html/your_domain;
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 by creating a symbolic link:
$ sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
Test the configuration for syntax errors:
$ sudo nginx -t
Restart Nginx to apply the changes:
$ sudo systemctl restart nginx
Nginx should now be serving your domain name. You can test this by navigating to http://your_domain in a web browser.
Conclusion
Setting up Nginx on Ubuntu 20.04 is a straightforward process that can greatly enhance your server's performance and scalability. By following the steps outlined in this guide, you can ensure that your Nginx server is properly installed, configured, and ready to serve multiple domains. Whether you're hosting a simple website or a complex application, Nginx provides the tools and flexibility you need to succeed
Comments