Notes fanzru's shorts
Date 12 / 22 / 2024
E = mc²
∇²Ψ + V(x)Ψ = EΨ
∫f(x)dx
Back to Shorts
ubuntunginxweb-serverdevops

Ubuntu Nginx Setup

1,234 views

Install Nginx

bash
# Update package list
sudo apt update

# Install Nginx
sudo apt install nginx -y

# Start and enable Nginx
sudo systemctl start nginx
sudo systemctl enable nginx

# Check status
sudo systemctl status nginx

Basic Configuration

bash
# Test configuration
sudo nginx -t

# Reload configuration
sudo systemctl reload nginx

# Restart Nginx
sudo systemctl restart nginx

# Check if running
sudo systemctl is-active nginx

Firewall Setup

bash
# Allow Nginx through firewall
sudo ufw allow 'Nginx Full'

# Check firewall status
sudo ufw status

# Enable firewall if not enabled
sudo ufw enable

Create Simple Site

bash
# Create site directory
sudo mkdir -p /var/www/example.com/html

# Set permissions
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chmod -R 755 /var/www/example.com

# Create index.html
echo "<h1>Hello from Nginx!</h1>" | sudo tee /var/www/example.com/html/index.html

Nginx Configuration

bash
# Create site config
sudo nano /etc/nginx/sites-available/example.com

# Add this content:
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

# Enable site
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

# Test and reload
sudo nginx -t
sudo systemctl reload nginx

Useful Commands

bash
# View Nginx logs
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log

# Check Nginx version
nginx -v

# Check configuration syntax
sudo nginx -t

# Graceful reload
sudo systemctl reload nginx

Quick Aliases

Add to ~/.bashrc:

bash
alias nginx-test="sudo nginx -t"
alias nginx-reload="sudo systemctl reload nginx"
alias nginx-restart="sudo systemctl restart nginx"
alias nginx-logs="sudo tail -f /var/log/nginx/access.log"
alias nginx-errors="sudo tail -f /var/log/nginx/error.log"

Web server ready! 🌐