简体   繁体   English

如何在 ubuntu 14.04 上使用 nginx 运行多个 php 版本

[英]how to run multiple php versions with nginx on ubuntu 14.04

I watched a gist on github explains how run multiple versions of php on ubuntu 14.04 with apache.我在 github 上看了一个要点,解释了如何在 ubuntu 14.04 上运行 php 的多个版本和 ZFFFFFD606D1416D。

but how can we get the same result with nginx?但是我们怎样才能得到与 nginx 相同的结果呢?

Take a look at this website i think it is what you want. 看看这个网站,我认为这是您想要的。 How to Run Multiple Versions of PHP on One Server 如何在一台服务器上运行多个版本的PHP

In case if you are using Nginx Server: 如果您使用的是Nginx服务器:

If your different applications are in different folders, 如果您不同的应用程序位于不同的文件夹中,

You can try writing nested location tags and write your php configuration based on that. 您可以尝试编写嵌套的位置标记,然后根据此标记来编写php配置。

For example if a sub folder having an application that needs a php 5.6 version and your root application need a php version that needs php 5.5 version then on your nginx configuration inside the location stanza of your application(say app1) put your copy and paste php execution related stanza relevant to your app. 例如,如果一个子文件夹包含一个需要php 5.6版本的应用程序,而您的根应用程序需要一个php 5.5版本的php版本,则在您应用程序位置节(例如app1)内的nginx配置上放上复制并粘贴php与您的应用相关的执行相关节。

example

    server{

location app1{

location  ~ \.php$ {
...
    fastcgi_pass 127.0.0.1:9001;
..
}


}

location  ~ \.php$ {
...
    fastcgi_pass 127.0.0.1:9000;
..
}

}

You have to install multiple php versions and configure its fpm conf to match the port address 您必须安装多个php版本并配置其fpm conf以匹配端口地址

You can run multiple PHP versions on Ubuntu without any issue.您可以在 Ubuntu 上运行多个 PHP 版本而不会出现任何问题。 You require Multiple PHP versions if applications are built in different PHP versions like PHP 8.0 and PHP 8.1. C 如果应用程序构建在不同的 PHP 版本(如 PHP 8.0 和 Z2FEC38304A7F9B37CZ 8.0 和 Z2FEC38304A7F9B37C1)中,则需要多个 PHP 版本。 Here, we will install PHP8.0, and PHP8.1 on the server.在这里,我们将在服务器上安装 PHP8.0 和 PHP8.1。

  1. First, we will add PPA, using the below command.首先,我们将使用以下命令添加 PPA。

     sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/php
  2. Run the Update Command.运行更新命令。

     sudo apt update
  3. Install PHP 8.0 with the below command.使用以下命令安装 PHP 8.0。

     sudo apt install php8.0 php8.0-fpm
  4. Install PHP 8.1 with the below command.使用以下命令安装 PHP 8.1。

     sudo apt install php8.1 php8.1-fpm
  5. Install Nginx with the below command.使用以下命令安装 Nginx。

     sudo apt install nginx
  6. Create 2 folders to test the website with PHP 8.0 and PHP 8.1创建 2 个文件夹以使用 PHP 8.0 和 PHP 8.1 测试网站

    echo "<?php phpinfo(); ?>" > /var/www/html/php80/index.php echo "<?php phpinfo(); ?>" > /var/www/html/php81/index.php
  7. We will create server blocks for both websites on Nginx.我们将为 Nginx 上的两个网站创建服务器块。

sudo nano /etc/nginx/sites-available/php80须藤纳米 /etc/nginx/sites-available/php80

Insert the following content for the site with PHP 8.0为具有 PHP 8.0 的站点插入以下内容

     # Application with PHP 8.0
     server {
        listen 80;

        root /var/www/html/php80;
        index index.php;
        server_name php80.test.com;

        location ~* \.php$ {
            # With php-fpm unix sockets
            fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
            include         fastcgi_params;
            fastcgi_param SCRIPT_FILENAME          $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }
     }
  1. Create a second VirtualHost configuration file to work with PHP 8.1.创建第二个 VirtualHost 配置文件以使用 PHP 8.1。

sudo nano /etc/nginx/sites-available/php81 sudo nano /etc/nginx/sites-available/php81

Insert the following content for the site with PHP 8.1为具有 PHP 8.1 的站点插入以下内容

     # Application with PHP 8.1
     server {
        listen 80;

        root /var/www/html/php81;
        index index.php;
        server_name php81.test.com;

        location ~* \.php$ {
            # With php-fpm unix sockets
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
            include         fastcgi_params;
            fastcgi_param SCRIPT_FILENAME          $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }
     }
  1. Create a symbolic link for both config files under /etc/nginx/sites-enabled directory./etc/nginx/sites-enabled目录下为两个配置文件创建一个符号链接。

     sudo ln -s /etc/nginx/sites-available/php80 /etc/nginx/sites-enabled/ sudo ln -s /etc/nginx/sites-available/php81 /etc/nginx/sites-enabled/
  2. Let us restart the Nginx.让我们重新启动 Nginx。

     sudo systemctl restart nginx.service
  3. You can access both websites in the browser and see the different versions for each.您可以在浏览器中访问这两个网站并查看每个网站的不同版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM