목. 8월 7th, 2025

Introduction

Nginx (pronounced “engine-x”) is a high-performance web server, reverse proxy, and load balancer trusted by millions of websites. This guide covers installation and basic configuration on Linux systems. Whether you’re setting up a personal blog or production infrastructure, these steps will get you started.


Prerequisites

  1. A Linux system (Ubuntu/Debian or CentOS/RHEL recommended)
  2. Terminal access with sudo privileges
  3. Active internet connection

Step 1: Install Nginx

For Ubuntu/Debian:

sudo apt update
sudo apt install nginx -y

For CentOS/RHEL:

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

Step 2: Manage Nginx Service

Start and enable auto-launch on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Verify status:

sudo systemctl status nginx  # Look for "active (running)"

Step 3: Configure Firewall

Allow HTTP/HTTPS traffic:

sudo ufw allow 'Nginx Full'  # Ubuntu
# OR
sudo firewall-cmd --permanent --add-service={http,https}  # CentOS/RHEL
sudo firewall-cmd --reload

Step 4: Test Default Installation

  1. Open your browser and visit:
    http://your_server_ip
  2. You should see the “Welcome to Nginx” landing page.

Nginx default page
If this fails, check:

sudo nginx -t  # Tests configuration syntax

Step 5: Set Up a Server Block (Virtual Host)

  1. Create site directory:
    sudo mkdir -p /var/www/your_domain/html
  2. Assign ownership:
    sudo chown -R $USER:$USER /var/www/your_domain/html
  3. Create a test page:
    echo "
    <h1>Hello World!</h1>" | sudo tee /var/www/your_domain/html/index.html
  4. Create a config file:

    sudo nano /etc/nginx/sites-available/your_domain

    Paste:

    server {
       listen 80;
        server_name your_domain www.your_domain;
        root /var/www/your_domain/html;
        index index.html;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
  5. Enable the site:
    sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
  6. Reload Nginx:
    sudo systemctl reload nginx

Step 6: Verify DNS Settings

Add a local hosts entry for testing (if DNS isn’t configured):

# On your local computer (not server):
sudo nano /etc/hosts
# Add line: your_server_ip your_domain www.your_domain

Visit http://your_domain to see your test page.


Step 7: Key Configuration Files

File Purpose
/etc/nginx/nginx.conf Main configuration
/etc/nginx/sites-available/ Site-specific configs
/var/log/nginx/access.log Access logs
/var/log/nginx/error.log Error logs

Troubleshooting Tips

  • Port Conflict? Ensure Apache isn’t running:
    sudo systemctl stop apache2  # Ubuntu
    sudo systemctl stop httpd    # CentOS
  • Permission Issues? Run:
    sudo chmod -R 755 /var/www/your_domain
  • Test configs before reloading:
    sudo nginx -t  # Essential before every reload!

Next Steps

  1. Enable HTTPS with Let’s Encrypt:
    sudo apt install certbot python3-certbot-nginx  # Ubuntu
    sudo certbot --nginx
  2. Set Up Reverse Proxy:
    location /app {
        proxy_pass http://localhost:3000;
    }
  3. Optimize Performance: Adjust worker processes in /etc/nginx/nginx.conf.

Conclusion

You’ve now installed Nginx, configured a virtual host, and served custom content. Nginx’s flexibility makes it ideal for everything from small projects to enterprise applications. Check the official Nginx documentation for advanced configurations!

> Pro Tip: Always back up config files before editing:
> sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다