nginx新增一个网站并绑定域名
Ubuntu 22.04 + Nginx新增一个网站并绑定域名。
一、创建网站目录
sudo mkdir -p /var/www/你的域名.com sudo chown -R www-data:www-data /var/www/你的域名.com sudo chmod -R 755 /var/www/你的域名.comc测试
二、测试用HTML
sudo nano /var/www/xnadev.com/index.html
保存退出(Ctrl+O → Enter → Ctrl+X)
三、新建 Nginx 站点配置文件
1.新建配置
sudo nano /etc/nginx/sites-available/你的域名.com
2.基础 HTTP 配置(无 PHP 版)
server {
listen 80;
server_name 你的域名.com www.你的域名.com;
root /var/www/你的域名.com;
index index.html index.htm index.php;
access_log /var/log/nginx/你的域名.access.log;
error_log /var/log/nginx/你的域名.error.log;
location / {
try_files $uri $uri/ =404;
}
}
四、启用站点(关键一步)
sudo ln -s /etc/nginx/sites-available/你的域名.com /etc/nginx/sites-enabled/
这一步很多人会忘
五、检查配置并重载 Nginx
sudo nginx -t sudo systemctl reload nginx
六、浏览器访问测试
http://你的域名.com
七、如果是 PHP 网站(PHP 7.4 示例)
在 server {} 中 加上:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
并确保默认文件
index index.php index.html;
八、可选:禁用默认站点(推荐)
避免抢占域名:
sudo unlink /etc/nginx/sites-enabled/default sudo systemctl reload nginx
完整配置
server {
listen 80;
server_name 你的域名.com www.你的域名.com;
root /var/www/xnadev.com;
index index.php index.html index.htm;
access_log /var/log/nginx/你的域名.access.log;
error_log /var/log/nginx/你的域名.error.log;
# 主访问规则
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# PHP 支持(PHP 7.4-FPM)
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 禁止访问隐藏文件(安全)
location ~ /\. {
deny all;
}
}