How To Install Nginx on Ubuntu 20.04
How To Install Nginx on Ubuntu 20.04
Nginx (pronounced “engine-ex”) is a high-performance web server, reverse proxy server, and load balancer widely used for serving static content and handling concurrent connections efficiently. In this guide, we’ll walk you through the steps to install Nginx on Ubuntu 20.04, configure it, and ensure that it runs properly.
1. Update Your System
Before installing Nginx, it’s a good practice to update your system’s package index. This ensures that you install the latest version of the software available in the repository.
Open a terminal and run:
sudo apt update
sudo apt upgrade -y
This command updates the package lists and installs any available upgrades.
2. Install Nginx
Once your system is updated, you can proceed to install Nginx. The installation process is straightforward using the APT package manager.
Run the following command in your terminal:
sudo apt install nginx -y
This command will download and install Nginx along with its dependencies.
3. Start and Enable Nginx
After installation, you need to start the Nginx service and enable it to start on boot.
Start Nginx with the following command:
sudo systemctl start nginx
To enable Nginx to start automatically at boot time, run:
sudo systemctl enable nginx
4. Check Nginx Status
To confirm that Nginx is running properly, you can check its status with the following command:
sudo systemctl status nginx
You should see output indicating that the service is active (running). If you encounter any issues, you can check the logs in /var/log/nginx/error.log.
5. Configure Firewall
If you have a firewall enabled (which is recommended for security), you’ll need to allow traffic on HTTP (port 80) and HTTPS (port 443). You can use UFW (Uncomplicated Firewall) to manage your firewall rules.
First, check the status of UFW:
sudo ufw status
If it is inactive, you can enable it later. To allow HTTP and HTTPS traffic, run:
sudo ufw allow ‘Nginx Full’
This command opens both ports 80 and 443.
If you want to allow only HTTP traffic initially, you can use:
sudo ufw allow ‘Nginx HTTP’
After adding the rules, enable UFW if it was inactive:
sudo ufw enable
To confirm the rules were applied, run:
sudo ufw status
6. Test Nginx Installation
You can now test your Nginx installation by opening a web browser and navigating to your server’s IP address. You can find your server’s public IP address with the following command:
hostname -I
Type the IP address in your web browser’s address bar. If Nginx is working correctly, you should see the default Nginx welcome page, which confirms that the server is installed and running.
7. Basic Nginx Configuration
Nginx’s configuration files are located in the /etc/nginx/ directory. The main configuration file is nginx.conf, but the default server block configuration is found in /etc/nginx/sites-available/default.
To edit the default server block configuration, run:
sudo nano /etc/nginx/sites-available/default
You can modify this file to change the server’s root directory, index file, and more. Here’s a simple example of a basic configuration:
server {
listen 80;
server_name example.com;location / {
root /var/www/html;
index index.html index.htm;
}
}
Make sure to replace example.com with your domain name or server IP address. The root directive specifies the directory that will serve your website files.
After making changes, save and exit the editor (CTRL + X, then Y, then Enter).
8. Testing Nginx Configuration
Before applying any changes, it’s crucial to test the configuration for syntax errors. Run:
sudo nginx -t
If everything is configured correctly, you should see a message indicating that the configuration file is valid.
9. Restart Nginx
For the changes to take effect, restart Nginx with the following command:
sudo systemctl restart nginx
10. Set Up a Basic HTML Page
To see your server in action, you can create a simple HTML file. The default directory for web files is /var/www/html. Create a new file named index.html:
echo “
Welcome to Nginx!
” | sudo tee /var/www/html/index.html
Now, if you refresh your web browser pointing to your server’s IP address, you should see your custom message.
If you would like to improve yourself in server management, you can purchase a server from our site, experiment and improve yourself in an affordable and reliable environment. I wish you good luck.
Conclusion
Congratulations! You have successfully installed and configured Nginx on Ubuntu 20.04. With Nginx running, you can serve static websites, handle high traffic loads, and use it as a reverse proxy for dynamic applications.
For further customization and advanced features, consider exploring the official Nginx documentation.