Hosting a Static Website with Nginx on Ubuntu
作者:XD / 发表: 2025年5月8日 02:42 / 更新: 2025年5月8日 02:42 / 编程笔记 / 阅读量:19
If you have a folder containing an index.html
file along with supporting assets like CSS, fonts, JS, and images, you can easily serve it using Nginx and access it via a domain like xiedong.me
.
Here’s a simple step-by-step guide to get it done.
📁 Step 1: Organize Your Website Files
Place your website files in a directory, such as:
/var/www/xiedong
Make sure it contains:
/var/www/xiedong/
├── index.html
├── css/
├── js/
├── fonts/
└── images/
⚙️ Step 2: Configure Nginx
Edit your Nginx configuration file, e.g., /etc/nginx/conf.d/me.conf
:
server {
listen 80;
server_name xiedong.me www.xiedong.me;
root /var/www/xiedong;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
This tells Nginx to serve files from the /var/www/xiedong
directory and to display index.html
by default.
🔐 Step 3: Set Permissions
Ensure the Nginx user (usually www-data
) can read the files:
sudo chown -R www-data:www-data /var/www/xiedong
sudo chmod -R 755 /var/www/xiedong
🚀 Step 4: Test and Restart Nginx
Test your Nginx configuration:
sudo nginx -t
If there are no errors, restart Nginx:
sudo systemctl restart nginx
🌐 Step 5: Access Your Site
Now, navigate to:
http://xiedong.me
You should see your website live!