Installation
Installation on Ubuntu
- Update Package List
sudo apt update
Fetches the latest package information.
- Install NGINX
sudo apt install nginx -y
- Downloads and installs NGINX
- Automatically installs dependencies
- Start NGINX
sudo systemctl start nginx
- 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
- Open Browser
- Visit: http://localhost
- or your server IP:
http://<server-ip>
- Default Page
- You should see: “Welcome to nginx!”
- This confirms NGINX is installed correctly.
Important NGINX Directories & Files
| Path | Purpose |
|---|---|
/etc/nginx/nginx.conf | Main config file |
/etc/nginx/sites-available/ | Virtual host configs |
/etc/nginx/sites-enabled/ | Enabled sites |
/var/www/html | Default web root |
/var/log/nginx/access.log | Access logs |
/var/log/nginx/error.log | Error 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_processesauto → Uses CPU cores efficientlyevents→ Connection handlinghttp→ HTTP server configurationserver→ Virtual hostlocation→ URL handling
Create a Simple Website
- Create Website Directory
sudo mkdir /var/www/mysite - Create an HTML File
sudo nano /var/www/mysite/index.html - Set Permissions
sudo chown -R www-data:www-data /var/www/mysite
Configure a Server Block (Virtual Host)
- Create Config File
sudo nano /etc/nginx/sites-available/mysite - Configuration:
server {
listen 80;
server_name mysite.local;
root /var/www/mysite;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
listen 80→ HTTP portserver_name→ Domain nameroot→ Website locationtry_files→ Prevents invalid URLs
- Enable the Site
sudo ln -s /etc/nginx/sites-available/mysite \
/etc/nginx/sites-enabled/
- Test Configuration
sudo nginx -t
- Reload NGINX
sudo systemctl reload nginx
- 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