编辑:供将来参考
我使用PHP 5.5.12运行带有LEMP堆栈的Ubuntu 14.10。 我有一些旧版的WordPress网站,它们需要PHP 5.3.3以及一些使用相当新版PHP的WP网站,所有这些网站都运行在本地机器上的nginx 上 。
我的手绑在虚拟机和沙箱上,我可以玩的是nginx,所以这个问题。 我理解人们的安全问题,但我需要这些网站在本地运行,以便在更新到最新的PHP / WP版本时testing破损的function。
我想让nginx根据WordPress网站运行正确的PHP版本(使用php-fpm)。 根据另一个 SF问题,实现这一目标的一种方法是让不同的PHP版本在不同的端口/套接字上运行,并将nginx服务器块configuration为使用相应的端口/套接字。
我已经编译了PHP 5.3.3手动包含php-fpm,但是这是我得到的最远。
实际上,我希望有人更详细地解释这个答案。 我不能完全弄清楚如何“在不同的端口(或套接字)上运行每个版本的php-fpm”或“在你的nginx服务器模块中的fastcgi_pass参数中configuration适当的端口” 。
我的一个服务器块看起来像这个参考
server { listen 80; listen [::]:80; root /usr/share/nginx/html/testsite1; index index.php index.html index.htm; server_name local.testsite1.com; location / { try_files $uri $uri/ /index.php?q=$uri&$args; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi.conf; } }
编辑:
我使用单独的文件中的单独服务器块设置每个站点,sym链接在/var/nginx/sites-available/testsite1和/var/nginx/sites-enabled/testsite1 。 所以var/nginx/sites-available包含;
testsite1 testsite2 testsite3 ...
所以理想情况下,我想知道是否有可能下面的东西是可能的(因为这是类似于如何设置不同的PHP版本的Apache)
testsite1 – 运行旧版本的PHP
server { listen 80; listen [::]:80; root /usr/share/nginx/html/testsite1; index index.php index.html index.htm; server_name local.testsite1.com; ...settings to use older PHP version... ...remaining nginx settings... }
testsite2 – 运行当前版本的PHP
server { listen 80; listen [::]:80; root /usr/share/nginx/html/testsite2; index index.php index.html index.htm; server_name local.testsite2.com; ...settings to use currrent PHP version (if needed)... ...remaining nginx settings... }
这可能吗? 我问的原因是,我宁愿避免重命名所有的php文件到php2为了运行(使版本控制痛苦)。 我不介意编辑nginx.conf文件或任何需要的步骤,只要我不必重命名文件。
我也相信我需要使用套接字(fastcgi_pass unix:/var/run/php5-fpm.sock;)由于WordPress的端口(尽pipe我打开所有的build议)。
好的,你想通过nginx运行多个PHP版本,configuration文件应该包括你的PHP脚本以不同的版本或扩展名命名的特定path。
不过,我想解释一下在你的文章中给出的SF问题链接。
这个答案给了你一个方法来修改你的nginx的conf ,它假设提问者在nginx有背景。
通过给conf一个端口,nginx将通过该FastCGI通道执行脚本。
假设你已经在服务器上安装了不同的PHP版本,并且已经在php-fpm.conf修改了port 。
你希望在5.5.0版本中执行所有的php文件,在5.6.0版本中执行php2文件。
nginx的configuration示例如下,
listen 8080; server_name localhost; root html; location / { index index.php index.php2; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME "${document_root}${fastcgi_script_name}"; include fastcgi_params; } location ~ \.php2$ { fastcgi_pass 127.0.0.1:9001; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME "${document_root}${fastcgi_script_name}"; include fastcgi_params; }
在这种情况下, php通过端口9000处理, php2转到9001
这是一个简单的例子,对于高级的你可以制作两个不同的文件夹来存放php文件,例如/home/php55和/home/php56 ,然后编辑你的nginx.conf 。
为您的编辑问题
如果你想尝试添加不同的服务器来处理不同版本的脚本,那么确定你可以这样做,因为nginx可以是一个负载平衡器,它也可以处理这种问题。
第一次申请
server{ listen 80; server_name php1.localhost; root /home/www/php5.5; location / { index index.php; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9002; (You can change it to your private IP in the future) fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
第二次申请
server{ listen 80; server_name php2.localhost; root /home/www/php5.6; location / { index index.php; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9004; (You can change it to your private IP in the future) fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
通过使用上述configuration,您可以轻松地切换PHP脚本的不同版本结果,而且,您可以使用NFS(如果您处于* nix环境中)来挂载磁盘,在这种情况下,您不需要将文件分成两个文件夹手动。
至于Fastcgi传递方法,我build议使用PORT而不是Socket。
我们都知道,由于TCP端口即使在同一台机器上也使用整个networking堆栈,因此Unix Socket具有更好的性能。
但是,保存的时间很less,而且在高stream量时间使用Socket时可能会遇到这个问题,
连接()到unix:/var/run/php5-fpm.sock失败或apr_socket_recv:连接重置由同级(104)
另外,如果你把nginx和php-fpm放在独立的服务器上,TCP端口可以给你更快的pipe理方式。
我正在使用小型笔记本电脑编辑这篇文章,所以代码不漂亮,但我试过….
EDITED
请记住修改您的/etc/hosts以匹配您的主机名( nginx.conf server_name)
出于testing的目的,我想配置我的nginx在同一服务器部分的不同位置下使用不同的php版本。 最后它与这个工作:
#Folder to run some tests with the new php-7 location ~ "^\/test_php7.0.3\/.*\.php$" { try_files $uri =404; fastcgi_pass unix:/var/run/php7-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #Folder to run some tests with a custom compiled version of php5.6 location ~ "^\/test_php5.6.18\/.*\.php$" { try_files $uri =404; fastcgi_pass unix:/var/run/php56-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } #The default php version: location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
希望这可以帮助别人:)