Home » #Technology » Getting Started with NGINX: Install, Configure, and Secure Your Web Server Easily 

Getting Started with NGINX: Install, Configure, and Secure Your Web Server Easily 

NGINX is a high-performance web server that efficiently handles web traffic, acts as a reverse proxy, and provides load balancing. Whether you’re launching a personal project or deploying a production application, NGINX offers speed, flexibility, and scalability.

For over two decades, I’ve been at the forefront of the tech industry, championing innovation, delivering scalable solutions, and steering organizations toward transformative success. My insights have become the trusted blueprint for businesses ready to redefine their technological future. This tech concept, walks you through installing and configuring NGINX for a simple website or application.

Installing NGINX

Install NGINX on Ubuntu/Debian

Run the following commands to install NGINX:

sudo apt update
sudo apt install nginx -y

Start and enable NGINX:

sudo systemctl start nginx
sudo systemctl enable nginx

Check the status:

sudo systemctl status nginx

Install NGINX on CentOS/RHEL

sudo yum install epel-release -y
sudo yum install nginx -y

Start and enable the service:

sudo systemctl start nginx
sudo systemctl enable nginx

Configuring NGINX

Understanding NGINX Configuration

  • Main configuration file: /etc/nginx/nginx.conf
  • Site configurations: /etc/nginx/sites-available/ and /etc/nginx/sites-enabled/
  • Log files: /var/log/nginx/access.log and /var/log/nginx/error.log

Create a Simple Website with NGINX

  1. Create a directory for your website:
sudo mkdir -p /var/www/example.com/html sudo chown -R $USER:$USER /var/www/example.com/html
  1. Add an index.html file:
echo "<h1>Welcome to Example.com</h1>" | sudo tee /var/www/example.com/html/index.html
  1. Set up an NGINX server block:
sudo nano /etc/nginx/sites-available/example.com
  1. Add the following configuration:
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;
    }
}
  1. Enable the site and restart NGINX:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo systemctl restart nginx
  1. Update your /etc/hosts file for local testing:
echo "127.0.0.1 example.com" | sudo tee -a /etc/hosts 

Now, visit http://example.com in your browser.

Securing NGINX with SSL

Secure your website with Let’s Encrypt:

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Generate and install an SSL certificate:

sudo certbot --nginx -d example.com -d www.example.com

Verify auto-renewal:

sudo certbot renew --dry-run

Now, your website runs securely over HTTPS!

Setting Up NGINX as a Reverse Proxy

Use NGINX to forward requests to a backend application.

Modify your server block:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Restart NGINX:

sudo systemctl restart nginx

Now, NGINX routes traffic to a backend application running on port 5000.

Optimizing NGINX Performance

Enable Gzip Compression

Improve loading speed with Gzip compression:

gzip on;
gzip_types text/plain text/css application/json application/javascript;

Cache Static Content

Reduce server load by caching static files:

location /static/ {
    expires 30d;
    add_header Cache-Control "public, max-age=2592000";
}

My Tech Advice: Mastering NGINX starts with the fundamentals—installation, configuration, website setup, SSL security, and performance optimization—one step at a time. Whether you’re hosting modern day application or using NGINX as a reverse proxy, this powerful web server offers flexibility and speed. Start experimenting with NGINX today and unlock its full potential! 🚀

#AskDushyant
#TechConcept #TechAdvice #Ngnix #WebServer

Leave a Reply

Your email address will not be published. Required fields are marked *