docker-library / php

Docker Official Image packaging for PHP
https://php.net
MIT License
3.84k stars 2k forks source link

legacy symfony2.2 app with php7.4 #1474

Closed idc77 closed 11 months ago

idc77 commented 11 months ago

Sorry, but I need help.

I had to upgrade my server to use openssl 3, thus I had to remove PHP7.4 from the system. Now I'd like to use docker and this whatever it is, to act as a php-fpm proxy. The system has nginx installed natively, this symfony app uses mysql, which is also installed natively, os packages.

So far I have

FROM php:7.4-fpm-alpine
RUN apk add --update --no-cache libgd libpng-dev libjpeg-turbo-dev freetype-dev 
RUN docker-php-ext-install -j$(nproc) mysqli opcache gd
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

Then I

docker build -t p74fpm .

and run, but

File not found.

The host's nginx

server {
        listen 80;
        listen [::]:80;
        server_name example.com;
        include /etc/nginx/acme.conf;
        location / {
        return 301 https://$host$request_uri;
        }
}

server {
        listen 443 ssl;
        listen [::]:443 ssl;
        http2 on;
        server_name example.com;
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    root /var/www/example.com/htdocs/web;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }
    # PROD
    location ~ ^/app\.php(/|$) {
        fastcgi_pass 127.0.0.1:9004;
        #fastcgi_pass 127.0.0.1:9082;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        # When you are using symlinks to link the document root to the
        # current version of your application, you should pass the real
        # application path instead of the path to the symlink to PHP
        # FPM.
        # Otherwise, PHP's OPcache may not properly detect changes to
        # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        # for more information).
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/app.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

    # return 404 for all other php files not matching the front controller
    # this prevents access to other php files you don't want to be accessible.
    location ~ \.php$ {
      return 404;
    }

    error_log /var/log/nginx/example.com_error.log;
    access_log /var/log/nginx/example.com_access.log;
}

This vhost.conf file worked when php-fpm7.4 was installed natively.

But, I'm blind.

I run the built image with

docker run -d --restart always -p 127.0.0.1:9004:9000 -v /var/www/example.com/htdocs:/var/www/html  p74fpm:latest
idc77 commented 11 months ago

Ok I got it to work. I had to make the host's mysqld listen on the docker0 iface and change the user to be able to connect from %. I didn't have to expose the mysql.socket because it's not used, but I had to adjust the mysql host params in the app's config.

Entering the docker image:

docker exec -it <image_name> sh

I also had to chown 82:82 ./htdocs aka the root of the app. 82 is the www-data user and group id.

And now i'll rebuild the thing with prod php.ini

FROM php:7.4-fpm-alpine
RUN apk add --update --no-cache libgd libpng-dev libjpeg-turbo-dev freetype-dev 
RUN apk add --no-cache --upgrade grep
RUN docker-php-ext-install -j$(nproc) mysqli opcache gd pdo pdo_mysql
RUN cp "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
#RUN cp "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
WORKDIR /var/www/example.com/htdocs
docker run -d --restart always -p 127.0.0.1:9004:9000 -v /var/www/example.com/htdocs:/var/www/example.com/htdocs p74fpm:latest