第一次用 PHP 配置 NGINX,不知道要修改哪个服务器块

第一次用 PHP 配置 NGINX,不知道要修改哪个服务器块

我正在构建 LEMP 堆栈服务器和本地开发环境,主要遵循 Linode 和 DigitalOcean 提供的教程。我已经安装了所有不同的组件,但对配置 NGINX 来处理 PHP 有点困惑。教程描述了编辑server {}在 中找到的块sites-available/default。问题是,我的默认配置文件有服务器块,我不确定我应该编辑哪一个。

第一个包含:

server {
   listen 80 default_server;
   listen [::]:80 default_server;

   # SSL configuration
   #
   # listen 443 ssl default_server;
   # listen [::]:443 ssl default_server;
   #
   # Note: You should disable gzip for SSL traffic.
   # See: https://bugs.debian.org/773332
   #
   # Read up on ssl_ciphers to ensure a secure configuration.
   # See: https://bugs.debian.org/765782
   #
   # Self signed certs generated by the ssl-cert package
   # Don't use them in a production server!
   #
   # include snippets/snakeoil.conf;

   root /var/www/html;

   # Add index.php to the list if you are using PHP
   index index.html index.htm index.nginx-debian.html;

   server_name _;

   location / {
      # First attempt to serve request as file, then
      # as directory, then fall back to displaying a 404.
      try_files $uri $uri/ =404;
   }

   # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
   #
   #location ~ \.php$ {
   #   include snippets/fastcgi-php.conf;
   #
   #   # With php7.0-cgi alone:
   #   fastcgi_pass 127.0.0.1:9000;
   #   # With php7.0-fpm:
   #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
   #}

   # deny access to .htaccess files, if Apache's document root
   # concurs with nginx's one
   #
   #location ~ /\.ht {
   #   deny all;
   #}
}

而第二个包含:

# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#   listen 80;
#   listen [::]:80;
#
#   server_name example.com;
#
#   root /var/www/example.com;
#   index index.html;
#
#   location / {
#      try_files $uri $uri/ =404;
#   }
#}

我应该编辑哪一个?我将要正在处理/托管多个虚拟托管站点,如果这有区别的话。

答案1

这只是两个例子

第一个是设置默认服务器,您可以处理指向您的服务器的所有域,因此对于没有虚拟主机的域来说,默认虚拟主机是这样的。

第二个是设置一个域名“example.com”

您需要在 sites-available/yourdomain.com 中创建新文件

也许这可以帮助您入门,这是基本配置。

您需要为 php-fpm 配置创建用户“yourdomain”(或任何顺序用户)

server {
        listen 80;
        server_name yourdomain.com;
        root /home/yourdomain;
        index index.html index.htm;
        autoindex off;

###
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                fastcgi_pass unix:/var/run/yourdomain.sock;
                fastcgi_index index.php;
               fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

}

对于 php-fpm

[yourdomain]

listen = /var/run/yourdomain.sock
listen.owner = yourdomain
listen.group = yourdomain
listen.mode = 0660

user = yourdomain
group = yourdomain

pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 5
pm.max_requests = 0

chdir = /

你也可以查看官方文档

https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/

相关内容