devspace: from unique deployment PHP:apache to PHP-FPM + Nginx

370 Views Asked by At

How can I do a PHPFPM+Nginx deployment for devspace?

Actually, I'm working with PHP-Apache and have this devspace.yaml

[...]
deployments:
- name: panel
  helm:
    componentChart: true
    values:
      containers:
      - image: registry.digitalocean.com/mycompany/myapp
      service:
        ports:
        - port: 80
      ingress:
        rules:
        - host: "mydomain.com.ar"

My Dockerfile is like

FROM php:7.4.4-apache
[...]
EXPOSE 80
CMD ["apache2-foreground"]

All is working fine and Host is registered on Ingress. But, I like to upgrade from PHPApache to PHP-FPM + Nginx.

I change my Dockerfile from FROM php:7.4.4-apache to FROM php:7.4.4-fpm and EXPOSE and COMMAND are removed. But now? Particular configurations for PHP and NGinx are no neccesary now.

Then, how can I add nginx service to devspace.yaml and connect to php-fpm?

2

There are 2 best solutions below

0
On BEST ANSWER

devspace.yaml:

version: v1beta9
images:
  app-nginx:
    image: registry.digitalocean.com/reyesoft/app-nginx
    dockerfile: build/nginx/Dockerfile
    # preferSyncOverRebuild: true
    preferSyncOverRebuild: true
    appendDockerfileInstructions:
      - USER root
  app-php:
    image: registry.digitalocean.com/reyesoft/app-php
    dockerfile: build/php/Dockerfile
    preferSyncOverRebuild: true
    injectRestartHelper: true
    appendDockerfileInstructions:
    - USER root
deployments:
- name: app
  helm:
    componentChart: true
    values:
      containers:
      - name: app-nginx
        image: registry.digitalocean.com/reyesoft/app-nginx
      - name: panel
        image: registry.digitalocean.com/reyesoft/app-php
        env: &panelEnv
          - name: APP_ENV
            value: "develop"
      service:
        ports:
          - port: 80
      ingress:
        # tls: true
        rules:
          - host: "reyesoft.com"
[...]

nginx.conf:

server {

    # [...]

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        root           /usr/share/nginx/html/public/;

        include php_location.include/*.conf;

        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        # fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
        fastcgi_param   SCRIPT_FILENAME         $fastcgi_script_name;
        include        fastcgi_params;
    }
}
1
On

You can use two containers in one pod, one for the PHP-FPM and one for the Nginx. This way (as they are in the same pod), they can communicate easily via port 9000.

PHP Container:

FROM php:7.4-fpm ...

Nginx Container:

FROM nginx:1.9-alpine ...

Make sure to include FPM in Nginx configuration:

location ~* \.php$ {
    fastcgi_index   index.php;
    fastcgi_pass    127.0.0.1:9000;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

On "devspace.yaml" configuration, you will list both of them under the images stanza.

You can mount the website-data to both containers; beware, you can not have more than one readOnly: false volume mount; in the following snippet, both mount it as read-only; if you need to write, then change accordingly.

deployments:
- name: my-component
  helm:
    componentChart: true
    values:
      containers:
      - name: nginx
        image: "nginx:1.9-alpine"
        volumeMounts:
        - containerPath: /var/www/html
          volume:
            name: website-data
            subPath: /website-data
            readOnly: true
      - name: php-fpm
        image: "php:7.4-fpm"
        volumeMounts:
        - containerPath: /var/www/html
          volume:
            name: website-data
            subPath: /website-data
            readOnly: true
      volumes:
      - name: website-data
        size: "5Gi"