Nginx is a powerful, high-performance web server and reverse proxy server widely used for hosting websites, load balancing, and caching. In this guide, we’ll walk you through the process of installing Nginx on an Ubuntu server.
Step 1: Update Your Package List
Before installing any software, it’s always a good idea to update your package list to ensure you get the latest version of the software.
sudo apt update
This command fetches the latest package information from the configured repositories.
Step 2: Install Nginx
Nginx is included in the default Ubuntu package repository, so you can install it directly using the apt
command.
sudo apt install nginx -y
The -y
flag automatically confirms the installation prompts.
Step 3: Verify Installation
Once the installation is complete, verify that Nginx is installed by checking its version.
nginx -v
This will display the installed Nginx version, confirming the installation.
Step 4: Start and Enable Nginx
Start the Nginx service using the systemctl
command:
sudo systemctl start nginx
To ensure Nginx starts automatically when the server boots, enable the service:
sudo systemctl enable nginx
Step 5: Check Nginx Status
You can check if Nginx is running properly:
sudo systemctl status nginx
This will display the status of the Nginx service. Look for "active (running)" in the output.
Step 6: Configure Firewall (Optional)
If you’re running a firewall (e.g., ufw
), you need to allow HTTP and HTTPS traffic for Nginx. To do this, use the following commands:
Allow HTTP traffic:
sudo ufw allow 'Nginx HTTP'
Allow both HTTP and HTTPS traffic:
sudo ufw allow 'Nginx Full'
Reload the firewall:
sudo ufw reload
Verify the status of the firewall to ensure the rules are applied:
sudo ufw status
Step 7: Test Nginx
Now, test if Nginx is working. Open your web browser and navigate to your server's IP address:
http://your-server-ip
If Nginx is installed and running correctly, you’ll see the default Nginx welcome page, confirming that the server is operational.
Step 8: Configure Nginx (Optional)
The default configuration file for Nginx is located at:
/etc/nginx/nginx.conf
To configure a specific site or application, create a new configuration file in:
/etc/nginx/sites-available/
For example, to create a new site configuration:
sudo nano /etc/nginx/sites-available/mywebsite
Once done, create a symbolic link to enable the site:
sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/
Finally, test the configuration for errors:
sudo nginx -t
If there are no errors, reload Nginx:
sudo systemctl reload nginx
Conclusion
You’ve successfully installed Nginx on your Ubuntu server. Nginx is now ready to serve as your web server or reverse proxy. From here, you can begin configuring virtual hosts, setting up SSL certificates, or optimizing Nginx for your specific use case.
Let us know in the comments if you have any questions or need help with further configuration!