Installation and configurations of Nginx on Ubuntu | FLTech

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.

Prerequisites:

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:

bash
sudo apt update

Step 2: Install Nginx:

Use the following command to install Nginx:

bash
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:

bash
sudo systemctl start nginx

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

bash
sudo systemctl enable nginx

Step 4: Check Nginx Status:

Verify that Nginx is running without any errors:

bash
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:

bash
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.

bash
sudo nano /etc/nginx/sites-available/example.com

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

nginx
server { listen 80; server_name example.com www.example.com; root /var/www/example.com/html; 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:

bash
sudo mkdir -p /var/www/example.com/html

Set the ownership to the Nginx user:

bash
sudo chown -R $USER:$USER /var/www/example.com/html

Step 9: Create a Test HTML File:

Create a simple HTML file for testing:

bash
nano /var/www/example.com/html/index.html

Add some content:

html
<!DOCTYPE html> <html> <head> <title>Welcome to example.com!</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:

bash
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Step 11: Test Configuration and Reload Nginx:

Check the configuration for syntax errors:

bash
sudo nginx -t

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

bash
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:

nginx
server { listen 80; server_name example.com www.example.com; return 301 https://$host$request_uri; } server { listen 443 ssl; server_name example.com www.example.com; ssl_certificate /etc/nginx/ssl/example.com.crt; ssl_certificate_key /etc/nginx/ssl/example.com.key; # 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:

nginx
server { listen 80; server_name example.com www.example.com; 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:

nginx
http { upstream backend { server backend1.example.com; server backend2.example.com; # Add more backend servers as needed } server { listen 80; server_name example.com www.example.com; 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:

nginx
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 } }

Conclusion:

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!

logoblog

Thanks for reading Installation and configurations of Nginx on Ubuntu

Previous
« Prev Post

No comments:

Post a Comment