Build Your Own Static Site Using Nginx On Debian
提醒:本文发布于 天前,内容可能因【技术时效性】过期 或【被重新修改】,请谨慎参考。
Prerequisites
- A Debian-based server
- Root Or Sudo privileges
Step 1: Update Your System
Start by updating your package list and upgrading the system to make sure everything is up-to-date.
1 | sudo apt update && sudo apt upgrade -y |
Step 2: Install NGINX
Install NGINX from the official Debian Repo
1 | sudo apt install nginx -y |
Step 3: Start and Enable Nginx
Start the NGINX service and enable it to start automatically on boot.
1 | sudo systemctl start nginx |
Step 4 (Optional): Allow NGINX Through the Firewall
If running a firewall you need to allow traffic on the HTTP and HTTPS ports
1 | sudo ufw allow 'Nginx Full' |
Step 5: Create a Directory for Your Website
Create a directory to store your static website files. For example, we create a website directory.
1 | sudo mkdir -p /var/www/mywebsite/html |
Step 6: Add Your Website Files
Now, place your website’s file (HTML, CSS, JS, image, etc) into the directory you just created.
For Example, let’s create a simple index.html file
1 | echo "<html><head><title>My Website</title></head><body><h1>Welcome to My Website</h1></body></html>" | sudo tee /var/www/mywebsite/html/index.html > /dev/null |
Step 7: Set Permissions
Set the appropriate ownership and permission for the web directory
1 | sudo chown -R www-data: www-data /var/www/mywebsite/html |
Step 8: Configure NGINX for Your Website
Now, you need to create a new configuration file for your website. NGINX configuration files are located
in /etc/nginx/sites-available/ and /etc/nginx/site-enabled/
Create a new file for your website under /etc/nginx/site-available/:
1 | sudo nano /etc/nginx/site-available/mywebsite |
Add the following configuration to the file:
1 | server { |
Step 9: Enable the Site
Create a symbolic link in the site-enable directory to enable the site.
1 | sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/ |
Step 10: Test the NGINX Configuration
Before restarting NGINX, its important to test the configuration for syntax errors.
1 | sudo nginx -t |
If there are no errors, you’ll see a message saying that the syntax is ok. If there are errors, resolve them before proceeding.
Step 11: Restart NGINX
Restart NGINX to apply the new configuration
1 | sudo systemctl restart nginx |
Step 12: Access Your Website
Now, open a web browser and navigate to your server’s IP address or domain name (e.g.,
http://your_server_ip or http://mywebsite.com). You Should see your static website!
Step 13: Optional - Setting Up SSL with Let’s Encrypt (HTTPS)
For enhanced security, you might want to set up SSL using Let’s Encrypt. Here how you can do that:
- Install Certbot:
1 | sudo apt install certbot python3-certbot-nginx -y |
2.Obtain an SSL certificate:
1 | sudo certbot --nginx -d mywebsite.com -d www.mywebsite.com |
3.Certbot will automatically configure SSL for your NGINX server and reload it.
Step 14: Renewing SSL Certificates
Let’s Encrypt certificates expires after 90 days, so it’s essential to renew them. You can set up a cron job
to automatically renew the certificate:
1 | sudo crontab -e |
Add the following line to check for renewals twice a day:
1 | 0 12 * * * certbot renew --quiet |
- 1. Prerequisites
- 2. Step 1: Update Your System
- 3. Step 2: Install NGINX
- 4. Step 3: Start and Enable Nginx
- 5. Step 4 (Optional): Allow NGINX Through the Firewall
- 6. Step 5: Create a Directory for Your Website
- 7. Step 6: Add Your Website Files
- 8. Step 7: Set Permissions
- 9. Step 8: Configure NGINX for Your Website
- 10. Step 9: Enable the Site
- 11. Step 10: Test the NGINX Configuration
- 12. Step 11: Restart NGINX
- 13. Step 12: Access Your Website
- 14. Step 13: Optional - Setting Up SSL with Let’s Encrypt (HTTPS)
- 15. Step 14: Renewing SSL Certificates