trevcrps
trevcrps

Reputation: 1

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? 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.

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.

Upvotes: 0

Views: 137

Answers (1)

MrTshoot
MrTshoot

Reputation: 113

You can run both of them simultaneously with simple 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:

#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

#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;
    }
}

Upvotes: 1

Related Questions