Installation and configurations of Nginx on Ubuntu | FLTech


Providing the latest Tech News

Saturday 11 November 2023

Installation and configurations of Nginx on Ubuntu

  Shakushakuvibe       Saturday 11 November 2023

Nginx is a powerful and widely used web server and reverse proxy server. Its efficiency in handling concurrent connections and low resource consumption makes it a popular choice for hosting websites.

In this comprehensive guide, we'll walk you through the step-by-step process of installing and configuring Nginx on an Ubuntu server.


Before proceeding with the installation, make sure you have:

  1. An Ubuntu server with sudo privileges.
  2. Basic knowledge of the Linux command line.

Step 1: Update the Package Repository:

Ensure that your package repository information is up-to-date:

sudo apt update

Step 2: Install Nginx:

Use the following command to install Nginx:

sudo apt install nginx

During the installation, you'll be prompted to confirm. Press 'Y' and Enter to proceed.

Step 3: Start and Enable Nginx:

Once the installation is complete, start the Nginx service:

sudo systemctl start nginx

To ensure that Nginx starts automatically on boot, enable it:

sudo systemctl enable nginx

Step 4: Check Nginx Status:

Verify that Nginx is running without any errors:

sudo systemctl status nginx

If Nginx is running correctly, you should see an active (running) status.

Step 5: Adjust Firewall Settings:

If a firewall is active, allow Nginx through:

sudo ufw allow 'Nginx Full'

Step 6: Access the Default Nginx Page:

Open a web browser and enter your server's IP address. You should see the default Nginx welcome page, indicating a successful installation.

Configuration Files and Directories:

  • Main Configuration File: /etc/nginx/nginx.conf
  • Server Block Configuration: /etc/nginx/sites-available/
  • Enabled Server Blocks (Symlinks): /etc/nginx/sites-enabled/
  • Default HTML Root: /var/www/html/

Step 7: Create a Basic Server Block:

Server blocks allow you to host multiple websites on a single server. Let's create a basic server block for a new website.

sudo nano /etc/nginx/sites-available/

Add the following configuration, replacing with your domain or IP address:

server { listen 80; server_name; root /var/www/; index index.html; location / { try_files $uri $uri/ =404; } }

Step 8: Create HTML Directory and Set Permissions:

Create the directory for your website's HTML files:

sudo mkdir -p /var/www/

Set the ownership to the Nginx user:

sudo chown -R $USER:$USER /var/www/

Step 9: Create a Test HTML File:

Create a simple HTML file for testing:

nano /var/www/

Add some content:

<!DOCTYPE html> <html> <head> <title>Welcome to!</title> </head> <body> <h1>Success! Your Nginx server block is working!</h1> </body> </html>

Step 10: Create a Symbolic Link:

Create a symbolic link to enable the server block:

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Step 11: Test Configuration and Reload Nginx:

Check the configuration for syntax errors:

sudo nginx -t

If there are no errors, reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 12: Access Your New Site:

Open your web browser and enter your domain or IP address. You should see the content from the test HTML file.

Additional Nginx Features and Configurations:

  • SSL/TLS Configuration: Implement SSL/TLS for secure connections using a certificate.
  • Reverse Proxy: Configure Nginx as a reverse proxy for applications like Node.js or Django.
  • Load Balancing: Distribute incoming traffic among multiple servers for improved performance.
  • Logging: Customize logging settings in the Nginx configuration.

Advanced Nginx Configurations:

1. SSL/TLS Configuration:

To secure your website with SSL/TLS, obtain an SSL certificate and update your server block configuration:

server { listen 80; server_name; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name; ssl_certificate /etc/nginx/ssl/; ssl_certificate_key /etc/nginx/ssl/; # Additional SSL settings location / { try_files $uri $uri/ =404; } }

2. Reverse Proxy Configuration:

If you have an application running on a different port or server, configure Nginx as a reverse proxy:

server { listen 80; server_name; location / { proxy_pass http://localhost:your_app_port; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }

3. Load Balancing Configuration:

Distribute traffic among multiple servers to improve performance and reliability:

http { upstream backend { server; server; # Add more backend servers as needed } server { listen 80; server_name; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } }

4. Logging Configuration:

Customize Nginx logging to capture specific information:

http { log_format custom_format '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log custom_format; server { # Your server block configuration } }


Nginx is a versatile and powerful web server that can be customized to meet various requirements. This guide covers the fundamental steps for installation and basic configuration on an Ubuntu server. As you delve into more advanced configurations, consider the specific needs of your website or application.

Whether you're implementing SSL/TLS, setting up a reverse proxy, configuring load balancing, or customizing logging, the modular nature of Nginx allows you to tailor your server's behavior. Regularly refer to the official Nginx documentation for detailed explanations and additional features. Happy Nginx-ing!


Thanks for reading Installation and configurations of Nginx on Ubuntu

« Prev Post

No comments:

Post a Comment