What is Nginx?

Nginx is a high-performance web server and reverse proxy server. It is lightweight, highly stable, and capable of handling high concurrent access. It is commonly used to build personal blogs, enterprise websites, etc. Compared to traditional Apache, Nginx consumes fewer resources and offers more flexible configuration, making it an excellent choice for beginners to get started with web servers.

I. Installing Nginx

Before configuration, you need to install Nginx first. The following are installation methods for mainstream Linux systems (Windows and macOS users can skip this or use Docker; here we take Linux as an example):

Ubuntu/Debian Systems

Open the terminal and execute the following commands:

# Update package list
sudo apt update  
# Install Nginx
sudo apt install nginx -y  
# Start Nginx and set it to start on boot
sudo systemctl start nginx  
sudo systemctl enable nginx  

CentOS/RHEL Systems

# Install Nginx (pre-installed in CentOS 8+, but run if not installed)
sudo dnf install nginx -y  
# Start and set to start on boot
sudo systemctl start nginx  
sudo systemctl enable nginx  

Verify Installation

After installation, check the Nginx status with the following command:

sudo systemctl status nginx  

If it shows “active (running)”, the installation is successful. You can also directly access the server’s IP (e.g., http://127.0.0.1 or http://localhost) in a browser. If you see the default Nginx welcome page, the installation is complete.

II. Understanding Nginx Configuration Files

The core of Nginx is its configuration files, which are by default located in the /etc/nginx/ directory. Key files and their roles:
- nginx.conf: Main configuration file, containing global settings (e.g., number of worker processes, log paths).
- conf.d/: Sub-configuration directory, where configurations for each virtual host (website) are typically written in separate .conf files.

III. Configuring Your First Website

We will configure a simple web server with the following steps:

1. Create the Website File Directory

Nginx’s default website files are stored in /var/www/html (Ubuntu/Debian) or /usr/share/nginx/html (CentOS). You can use these directories directly:

# Create a test directory (if it doesn't exist)
sudo mkdir -p /var/www/html  
# Create the index.html homepage file
sudo nano /var/www/html/index.html  

2. Write Simple HTML Content

Enter the following content in index.html (customizable):

<!DOCTYPE html>
<html>
<head>
    <title>My First Nginx Website</title>
</head>
<body>
    <h1>Hello Nginx!</h1>
    <p>This is my first web server built with Nginx~</p>
</body>
</html>

Press Ctrl+X to save and exit the editor.

3. Configure the Virtual Host

Nginx defines virtual hosts (websites) via the server block. Create a configuration file (e.g., default.conf) in the /etc/nginx/conf.d/ directory:

sudo nano /etc/nginx/conf.d/default.conf  

Add the following configuration in the file (explanations in comments):

server {
    # Listen on port 80 (default HTTP port)
    listen 80;  
    # Access domain/IP (for beginners, use localhost or server IP, e.g., 192.168.1.100)
    server_name localhost;  

    # Path to website files (consistent with step 1)
    root /var/www/html;  
    # Homepage file (searches in order, prioritizes index.html)
    index index.html;  

    # (Optional) Default 404 page
    location / {
        try_files $uri $uri/ =404;
    }
}

4. Check Configuration and Reload

After modifying the configuration, check for syntax errors to avoid startup failures:

sudo nginx -t  

If it shows “test is successful”, the configuration is correct. Then reload the configuration (no need to restart the service):

sudo systemctl reload nginx  

IV. Testing Access to the Website

After configuration, enter the server’s IP or localhost in the browser to see the index.html content you just wrote:
- If the server IP is 192.168.1.100, access http://192.168.1.100
- If using localhost, access http://localhost

If the website is not accessible? Check these common issues:
1. Firewall not opening port 80
- Ubuntu/Debian: sudo ufw allow 80/tcp (allow HTTP traffic)
- CentOS/RHEL: sudo firewall-cmd --add-port=80/tcp --permanent + sudo firewall-cmd --reload
- For cloud servers, add an “80 port inbound rule” in the console security group.

  1. File permission issues
    Nginx typically runs as the www-data user (Ubuntu) or nginx user (CentOS). Ensure the directory has read permissions:
   sudo chown -R www-data:www-data /var/www/html  # Ubuntu/Debian
   sudo chown -R nginx:nginx /usr/share/nginx/html  # CentOS

V. Common Nginx Management Commands

  • Start/Stop/Restart:
  sudo systemctl start nginx    # Start
  sudo systemctl stop nginx     # Stop
  sudo systemctl restart nginx  # Restart
  sudo systemctl reload nginx   # Reload configuration (recommended, no service interruption)
  • Check Status:
  sudo systemctl status nginx   # Check running status

VI. Summary

Through the above steps, you have successfully built a accessible web server with Nginx! The core process is: Install Nginx → Configure website files and virtual hosts → Check configuration and test access. You can further add CSS, JS, etc., or configure HTTPS (requires SSL certificate) to enhance the website.

Pro Tip: After modifying configuration files, always use nginx -t to check syntax first, then reload to apply changes to avoid service interruption!

Xiaoye