通过nginx的多个版本的PHP

weixin_38062058 2019-09-12 12:56:13
编辑:供将来参考 > Multiple versions of PHP through nginx> Nginx and different versions of PHP FPM + PHP 我正在使用PHP 5.5.12运行带有LEMP堆栈的Ubuntu 14.10.我有许多遗留的WordPress站点需要PHP 5.3.3以及一些使用相当新版本的PHP的WP站点,所有这些站点都在我本地计算机上的nginx上运行. 我的双手是关于虚拟机和沙箱的,所有我可以玩的是nginx,因此这个问题.我理解人们的安全问题,但我需要这些站点在本地运行,因此我可以测试损坏的功能,因为我将它们更新到最新的PHP / WP版本. 我想让nginx运行正确版本的PHP(使用php-fpm),具体取决于WordPress网站.根据another SF问题,实现此目的的一种方法是在不同的端口/套接字上运行不同的PHP版本,并配置nginx服务器块以使用相应的端口/套接字. 我已手动编译PHP 5.3.3以包含php-fpm,但这是我得到的最远的. 实际上,我希望有人能够更详细地解释this答案.我无法弄清楚如何“在不同的端口(或套接字)上运行每个版本的php-fpm”或“在你的nginx服务器块中的fastcgi_pass参数中配置相应的端口”. 我的一个服务器块看起来像这样供参考 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(尽管我对所有建议持开放态度).
...全文
158 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38066403 2019-09-12
  • 打赏
  • 举报
回复
好的,你想通过nginx运行多个PHP版本,配置文件应该包括将PHP脚本放在不同版本或扩展名中的特定路径. 但是,我想解释一下您帖子中提供的SF问题链接. 答案是给你一种方法来修改你的nginx的conf,这假设提问者在nginx中有背景. 通过在conf中给出一个特定的端口,nginx将通过该FastCGI通道执行脚本. 让我们假装您在服务器中安装了不同的PHP版本,并且已经在php-fpm.conf中修改了端口. 您希望在5.5.0版本中执行所有php文件,在5.6.0中执行php2文件. nginx的配置示例如下, 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; } } 通过使用上面的配置,您可以轻松切换PHP脚本的不同版本结果,而且,您可以使用NFS(如果您在* nix环境中)来安装磁盘,在这种情况下,您将无需放置文件手动分成两个文件夹. 至于Fastcgi传递方法,我建议使用PORT而不是Socket. 我们都知道Unix Socket具有更好的性能,因为TCP端口甚至在同一台机器上使用整个网络堆栈. 但是,您节省的时间非常少,而且,在高流量时间使用Socket时可能会遇到此问题, connect() to unix:/var/run/php5-fpm.sock failed or apr_socket_recv: Connection reset by peer (104) 此外,如果将nginx和php-fpm放在不同的服务器中,TCP端口可以为您提供更快的管理方式. 我正在使用小笔记本电脑来编辑这篇文章,所以代码并不漂亮,但我试过…. EDITED 请记住修改/ etc / hosts以匹配您的主机名(nginx.conf中的server_name)

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧