Nginx 配置新站点

Configure a new website with nginx

Posted by Lv Hui on June 21, 2024

新建站点目录

1
sudo mkdir -p /var/www/test.com

修改目录所有者与权限

1
2
sudo chown -R www-data:www-data /var/www/test.com
sudo chmod -R /var/www/test.com

为新站点生成新配置

创建配置文件

1
sudo vim /etc/nginx/sites-available/test.com

配置文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
server {
    listen 80;
    server_name test.com;

    root /var/www/test.com;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    error_log /var/log/nginx/example.com.error.log;
    access_log /var/log/nginx/example.com.access.log;
}

location ~ \.php$ 部分是用来对.php请求的处理

location ~ /\.ht 部分禁止访问 .ht 文件

在sites-enable中建立软链接

1
sudo ln -s /etc/nginx/sites-available/test.com /etc/nginx/sites-enable/

测试配置并重载Nginx配置

测试

1
sudo nginx -t

没问题会有如下输出,有问题会有提示

1
2
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重新加载配置

1
sudo systemctl reload nginx