Nginx is a powerful, open-source web server that’s widely used for hosting websites, managing load balancing, and serving as a reverse proxy. It’s known for its speed and scalability, making it an excellent choice for web applications. This guide will walk you through how to install and configure Nginx on Ubuntu.
Step 1: Update the Package Repository
Before installing any software, it’s always a good idea to ensure your package list is up to date.
Step 1.1: Open Terminal
Press Ctrl + Alt + T
to open the terminal.
Step 1.2: Update Package List
Run the following command to update the package list:
sudo apt update
Step 2: Install Nginx
Now, we’ll install Nginx on your system.
Step 2.1: Install Nginx
To install Nginx, run:
sudo apt install nginx
Step 2.2: Verify Installation
After installation, check if Nginx is running by using:
sudo systemctl status nginx
You should see that Nginx is active (running).
Step 3: Adjust Firewall Settings
If your firewall is enabled, you’ll need to allow Nginx to receive HTTP and HTTPS traffic.
Step 3.1: Allow Nginx Through the Firewall
Run the following command to allow web traffic through the firewall:
sudo ufw allow 'Nginx Full'
Step 3.2: Verify Firewall Status
Check the status of UFW to confirm the rules:
sudo ufw status
Step 4: Test Nginx Installation
Let’s ensure that Nginx is properly installed by accessing its default welcome page.
Step 4.1: Access Nginx in a Web Browser
Open a web browser and go to the following URL:
- Local Machine: If you’re installing Nginx on your local machine, navigate to
http://localhost
. - Remote Server: If Nginx is installed on a remote server, visit
http://your_server_ip
.
You should see the Nginx Welcome page, which confirms that Nginx is up and running.
Additional Configuration (Optional)
If you want to host multiple websites or applications on the same server, you can configure Nginx using server blocks (similar to virtual hosts in Apache).
Step 5: Configure Server Blocks
Step 5.1: Create a New Server Block
Create a new configuration file for your domain (replace example.com
with your actual domain name):
sudo nano /etc/nginx/sites-available/example.com
Add the following content to the file:
server { listen 80; server_name example.com www.example.com; root /var/www/example.com/html; index index.html index.htm; location / { try_files $uri $uri/ =404; } }
Step 5.2: Enable the Server Block
Create a symbolic link to enable the server block:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Step 5.3: Test Nginx Configuration
Before restarting Nginx, test the configuration for any syntax errors:
sudo nginx -t
Step 5.4: Restart Nginx
Finally, restart Nginx to apply the changes:
sudo systemctl restart nginx
Conclusion
You have successfully installed and configured Nginx on your Ubuntu system! You can now start using it to host websites and manage web applications. For more advanced configurations, refer to the official Nginx documentation.
Explore more guides and tutorials on web development and server management at CodeAllow.