Why Learn Nginx?¶
If you’re a beginner in web servers or want to build your own website, Nginx is definitely a tool worth mastering. It’s lightweight, efficient, and highly configurable, making it one of the most popular web servers today. It’s also commonly used for reverse proxying, load balancing, and more. This article will guide you from installation to startup, even if you’re new to servers!
1. Preparation: Confirm Your Operating System¶
Nginx supports Windows, Linux (Ubuntu, CentOS, etc.). This guide uses Ubuntu/Debian and CentOS/RHEL as examples. For Windows installation, see the supplementary notes at the end.
2. Install Nginx (Ubuntu/Debian)¶
1. Update Package List¶
First, update the system packages to avoid outdated versions:
sudo apt update # Update package list
2. Install Nginx¶
Execute the following command to install Nginx:
sudo apt install nginx -y # -y confirms installation automatically
3. Verify Installation¶
Check the Nginx version to confirm success:
nginx -v # View version (lowercase v for basic info, uppercase V for details)
If you see output like nginx version: nginx/1.21.6, installation is successful!
3. Install Nginx (CentOS/RHEL)¶
1. Install Nginx¶
CentOS/RHEL may not include Nginx in default repositories. First install the EPEL repo:
sudo yum install epel-release -y # Install EPEL (third-party repo)
sudo yum install nginx -y # Install Nginx
2. Start Nginx and Enable Auto-Startup¶
sudo systemctl start nginx # Start Nginx
sudo systemctl enable nginx # Enable auto-start on boot
4. Start and Verify Nginx¶
1. Start Nginx¶
The startup command is universal for both Ubuntu and CentOS:
sudo systemctl start nginx # Start Nginx
sudo systemctl status nginx # Check status (green "active(running)" means normal)
2. Browser Verification: Access Local Server¶
Open your browser and visit http://localhost or http://127.0.0.1. If you see the default Nginx welcome page (“Welcome to nginx!” on a blue background), Nginx is running successfully!
5.初探 Nginx Configuration Files (Critical!)¶
Nginx core configurations are in /etc/nginx/. Beginners should focus on:
- nginx.conf: Main configuration file with global settings (e.g., worker processes).
- sites-available/default: Default site config (Ubuntu/Debian) or /etc/nginx/conf.d/default.conf (CentOS): Defines listening ports, root directories, etc.
1. View Default Configuration¶
Open the default config file (Ubuntu example):
sudo nano /etc/nginx/sites-available/default
Key content (understanding Nginx request rules):
server {
listen 80; # Listen on port 80 (HTTP default)
server_name localhost; # Match local domain
root /var/www/html; # Website file directory
index index.html index.htm; # Default homepage files
}
In short: Nginx listens on port 80, routes requests to /var/www/html, and serves index.html to users.
6. Nginx Common Commands (Must-Know!)¶
| Command | Purpose |
|---|---|
sudo systemctl start nginx |
Start Nginx |
sudo systemctl stop nginx |
Stop Nginx |
sudo systemctl restart nginx |
Restart Nginx (after config changes) |
sudo systemctl status nginx |
Check Nginx status |
sudo nginx -t |
Check config syntax errors |
sudo systemctl reload nginx |
Reload config (no restart) |
7. Troubleshooting Common Issues (Newbies Essential!)¶
1. Startup Failure: Port Already Occupied¶
If sudo systemctl start nginx shows “inactive”, the 80 port may be occupied (e.g., by Apache, Tomcat).
- Check 80-port usage:
sudo lsof -i :80 # View process using port 80
sudo kill -9 PID # Terminate the process (replace PID)
2. Config Error: Can’t Start After Modification¶
After changing configs, always check syntax first:
sudo nginx -t # If output: "nginx: configuration file ... test is successful" → valid
If syntax errors occur, edit the config file (e.g., /etc/nginx/sites-available/default) based on the error message.
8. Practice: Customize Nginx Homepage¶
1. Navigate to Website Root¶
- Ubuntu/Debian:
/var/www/html - CentOS/RHEL:
/usr/share/nginx/html
2. Create a Custom Page¶
sudo nano /var/www/html/index.html # Create homepage file
Add the following (replace with any HTML):
<!DOCTYPE html>
<html>
<body>
<h1>Hello, Nginx!</h1>
<p>This is my first page built with Nginx!</p>
</body>
</html>
Save with Ctrl+O, Enter, then Ctrl+X.
3. Refresh Browser¶
Visit http://localhost again to see your custom page!
9. Windows Installation (Optional)¶
- Download the Windows version from Nginx Official Site (e.g.,
nginx-1.24.0.zip). - Extract to a directory (e.g.,
C:\nginx), then open Command Prompt (CMD):
cd C:\nginx
nginx.exe # Start Nginx
nginx.exe -s stop # Stop Nginx
- Access
http://localhostto verify. Operations are similar to Linux.
Summary¶
Congratulations! You’ve completed Nginx installation, startup, and basic configuration. While “install to start” seems simple, you’ve already mastered core server operations. Next, explore advanced features like reverse proxying and virtual hosts. Remember: practice and troubleshooting are key—Nginx configurations are highly flexible!
If you encounter issues, use nginx -t to check configs, systemctl status nginx to check status, or leave a comment for help.
Supplementary: Learning Resources
- Nginx Official Docs: https://nginx.org/en/docs/
- Runoob Nginx Config Examples: https://www.runoob.com/w3cnote/nginx-setup-intro.html