简体   繁体   English

在 LAMP 堆栈环境中为每个项目目录运行不同版本的 php

[英]Running a different version of php per project directory on a LAMP stack enviroment

Is there a way to configure or a program that allow me to run a different version of php based on my project directories for example /project1 running on php 5 and /project2 running on php 7?有没有一种方法可以配置或允许我根据我的项目目录运行不同版本的 php,例如在 php 5 上运行的 /project1 和在 ZE1BFD762321E409CEE4AC0B6E8497 上运行的 /project2? I have the need to run both projects at the same time.我需要同时运行两个项目。

I followed a guide that used xampp but it didn't work always loaded the version of php that came with the xampp installer.我遵循了使用xampp的指南,但它始终无法加载 xampp 安装程序随附的 php 版本。

I'm open to suggestions of another programs that install that stack and allows that kind of configuration or pointed out to a guide to set it up with docker to which I have no experience.我愿意接受安装该堆栈并允许这种配置的其他程序的建议,或者指出使用我没有经验的 docker 设置它的指南。

You can run both of them simultaneously with simple docker-compose您可以使用简单的 docker-compose 同时运行它们

version: '3'
services:
  #PHP5 Service
  app1:
    build:
      context: .
      dockerfile: Dockerfile-php5
    container_name: app1
    restart: unless-stopped
    tty: true
    env_file:
      - ./.env/php5.env
      - ./.env/.env
    working_dir: /var/www/project1
    networks: 
      - app-network
    volumes:
      - ./project1:/var/www/project1

  #PHP7 Service
  app2:
    build:
      context: .
      dockerfile: Dockerfile-php7
    container_name: app2
    restart: unless-stopped
    tty: true
    env_file:
      - ./.env/php7.env
      - ./.env/.env
    working_dir: /var/www/project2
    networks: 
      - app-network
    volumes:
      - ./project2:/var/www/project2
  
  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
      - "8081:8081" #For Project1
      - "8082:8082" #For Project2
    networks:
      - app-network
    volumes:
      - ./project1:/var/www/project1
      - ./project2:/var/www/project2
      - ./nginx/conf.d:/etc/nginx/conf.d/
      - ./nginx/log:/var/log/nginx
networks:
  app-network:
    driver: bridge

and you should use fastcgi to pass requests on your different apps as follow:并且您应该使用 fastcgi 在您的不同应用程序上传递请求,如下所示:

#Project1
server {
    listen 8081; 
    index index.php index.html;
    error_log /var/log/nginx/api-error-php5.log;
    access_log /var/log/nginx/api-access-php5.log;
    root /var/www/project1/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

and for project2和项目2

#Project2
server {
    listen 8082; 
    index index.php index.html;
    error_log /var/log/nginx/api-error-php7.log;
    access_log /var/log/nginx/api-access-php7.log;
    root /var/www/project2/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app2:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

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

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