我是 nginx 和 php-fpm 的新手。
我的问题:我是否需要创建两个 php-fpm 配置文件还是只需要一个?
两个 nginx 配置(不同的域/应用程序)是否可以指向同一个 php 套接字?如果是这样,是否会导致会话冲突或任何其他问题?
下面有两个 nginx 配置和一个 php-fpm 配置。如上所述,我应该有两个 php-fpm 配置吗?
php-fpm 配置:
[appname1]
listen = /var/www/apps/appname1/tmp/php.sock
user = www-data
group = www-data
pm = dynamic
pm.max_children = <%= node['php5-fpm']['max_children'] %>
pm.start_servers = <%= node['php5-fpm']['start_servers'] %>
pm.min_spare_servers = <%= node['php5-fpm']['min_spare_servers'] %>
pm.max_spare_servers = <%= node['php5-fpm']['max_spare_servers'] %>
pm.max_requests = 1000
pm.status_path = /php_status
request_terminate_timeout = 0
request_slowlog_timeout = 0
slowlog = /var/www/apps/appname1/logs/slow.log
nginx 配置1:
upstream backend {
server unix:/var/www/apps/appname1/tmp/php.sock;
}
server {
listen 80 default;
root /var/www/apps/appname1/public/app/webroot;
index index.php index.html index.htm;
access_log /var/www/apps/appname1/logs/access.log;
error_log /var/www/apps/appname1/logs/error.log;
client_max_body_size 20M;
rewrite_log on;
# Not found this on disk?
# Feed to CakePHP for further processing!
if (!-e $request_filename) {
rewrite ^/(.+)$ /index.php last;
break;
}
# Pass the PHP scripts to FastCGI server
# listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_intercept_errors on; # to support 404s for PHP files not found
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# ... some other stuff hidden ...
location ~ ^/(php_status|php_ping)$ {
fastcgi_pass backend;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
include fastcgi_params;
allow 127.0.0.1;
deny all;
}
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
# Deny access to .htaccess files,
# git & svn repositories, etc
location ~ /(\.ht|\.git|\.svn) {
deny all;
}
}
nginx 配置2:
upstream backend {
server unix:/var/www/apps/appname1/tmp/php.sock;
}
server {
listen 80 default;
server_name test2.com
root /var/www/apps/appname2/public/app/webroot;
index index.php index.html index.htm;
access_log /var/www/apps/appname2/logs/access.log;
error_log /var/www/apps/appname2/logs/error.log;
client_max_body_size 20M;
rewrite_log on;
# Not found this on disk?
# Feed to CakePHP for further processing!
if (!-e $request_filename) {
rewrite ^/(.+)$ /index.php last;
break;
}
# Pass the PHP scripts to FastCGI server
# listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_intercept_errors on; # to support 404s for PHP files not found
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# ... some other stuff hidden ...
location ~ ^/(php_status|php_ping)$ {
fastcgi_pass backend;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
include fastcgi_params;
allow 127.0.0.1;
deny all;
}
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
# Deny access to .htaccess files,
# git & svn repositories, etc
location ~ /(\.ht|\.git|\.svn) {
deny all;
}
}
答案1
通常你应该选择nfpm 实例n您正在配置的域(除非它们都指向同一个应用程序)。
将每个 Web 应用程序保留在其自己的空间中,并为其创建一个单独的 UNIX 用户,稍后您将使用该用户来处理 FPM 实例。
这样,您将拥有权限分离(非常重要),因为即使有人入侵了您的应用程序 1,他们仍然无法访问应用程序 2 的写权限。
此配置还有许多其他好处,例如控制哪个应用程序使用更多的 CPU 或 RAM(ps 将显示用户拥有的 FPM 进程)。
请停止将其www-data
用于 Web 应用程序!!!当以非特权用户身份运行时,它是为 Web 服务器保留的,如果您想允许浏览器访问您的数据,请使用辅助组或设置权限以允许其他人读取您的文件。
答案2
为了使用 Nginx 和 PHP 设置我的服务器,我遵循了 Ars Technica网络服务系列。我有一台服务器为多个域提供服务,这些域都以某种方式使用 PHP,而且我没有遇到任何与 PHP 相关的错误报告。也许它也能帮到你?
答案3
另一个选项是使用 Docker 在它们自己的容器中托管多个 php5-fpm 应用程序,然后告诉 nginx 代理请求。我还没有尝试过,但我打算尝试。类似的设置适用于我的 Django 应用程序。