Skip to main content

Installation

Installation on Ubuntu

  1. Update Package List
sudo apt update

Fetches the latest package information.

  1. Install NGINX
sudo apt install nginx -y
  • Downloads and installs NGINX
  • Automatically installs dependencies
  1. Start NGINX
sudo systemctl start nginx
  1. Enable NGINX at Boot
sudo systemctl enable nginx

Ensures NGINX starts automatically after system reboot. 5. Check Status

sudo systemctl status nginx

Expected Output: Active (running)

Verify NGINX Installation

  1. Open Browser
  2. Default Page
    • You should see: “Welcome to nginx!”
    • This confirms NGINX is installed correctly.

Important NGINX Directories & Files

PathPurpose
/etc/nginx/nginx.confMain config file
/etc/nginx/sites-available/Virtual host configs
/etc/nginx/sites-enabled/Enabled sites
/var/www/htmlDefault web root
/var/log/nginx/access.logAccess logs
/var/log/nginx/error.logError logs

Understanding nginx.conf

user www-data;
worker_processes auto;

events {
worker_connections 1024;
}

http {
include mime.types;

server {
listen 80;
location / {
root /var/www/html;
index index.html;
}
}
}
  • worker_processes auto → Uses CPU cores efficiently
  • events → Connection handling
  • http → HTTP server configuration
  • server → Virtual host
  • location → URL handling

Create a Simple Website

  1. Create Website Directory sudo mkdir /var/www/mysite
  2. Create an HTML File sudo nano /var/www/mysite/index.html
  3. Set Permissions sudo chown -R www-data:www-data /var/www/mysite

Configure a Server Block (Virtual Host)

  1. Create Config File sudo nano /etc/nginx/sites-available/mysite
  2. Configuration:
server {
listen 80;
server_name mysite.local;

root /var/www/mysite;
index index.html;

location / {
try_files $uri $uri/ =404;
}
}
  • listen 80 → HTTP port
  • server_name → Domain name
  • root → Website location
  • try_files → Prevents invalid URLs
  1. Enable the Site
sudo ln -s /etc/nginx/sites-available/mysite \
/etc/nginx/sites-enabled/
  1. Test Configuration
sudo nginx -t
  1. Reload NGINX
sudo systemctl reload nginx
  1. Access the Website Add to /etc/hosts (local machine):
127.0.0.1 mysite.local

Open browser:

http://mysite.local

Basic Troubleshooting

  • Port Already in Use sudo lsof -i :80
  • Check Logs tail -f /var/log/nginx/error.log