Connect host nginx and msql with a docker container of php5-fpm - php

I have a legacy Wordpress installation that run with Nginx/MySQL and php5-fpm and I have to move it to a recent VPS that features php7-fpm.
I'd like to leverage the new host VPS already installed Nginx/MySQL and install and use a container for managing php5 requests.
I pulled the official php5-fpm image from Docker Hub
docker pull php:5.6-fpm-alpine
... then I run the the image remapping the port 9000 where php5-fpm is listening and mounting the wordpress root folder (uncertain about that) and mysql socket, without -d flag in order to see the logs live:
docker run --name phpFpm5.6 -p 127.0.0.1:9999:9000 -v /home/ubuntu/www/html:/var/www/html -v /var/run/mysqld/mysqld.sock:/tmp/mysql.sock php:5.6-fpm-alpine
Here's the console logs:
[21-Aug-2017 12:36:19] NOTICE: fpm is running, pid 1
[21-Aug-2017 12:36:19] NOTICE: ready to handle connections
172.17.0.1 - 21/Aug/2017:12:37:09 +0000 "GET /index.php" 404
172.17.0.1 - 21/Aug/2017:12:39:30 +0000 "GET /index.php" 404
When I request the index page from a browser, I'm getting a 404 (not found) response.
Nginx directive:
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9999;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Any clue is really appreciated.
Thanks in advance.
Best,
Luca

Think of container as the remote server. Now the mapping of ports that you are doing 9999:9000 tells docker to connect port number 9999 on host to port number 9000 of the container.
Now in your nginx directive, the one running inside container, you are redirecting at 9999. Instead, it should be 9000.
According to your scenario, all resources are served by the container at port 9999, but port 9999 of container is not accessible to host.

Related

Xdebug silently catches connections with no reason

The application runs on docker containers: nginx and php-fpm. Xdebug is configured with PhpStorm. The app was working correctly until suddenly Xdebug started to catch all connections even when I didn't enable debugging. I didn't even change anything in configuration - it just started to do this (a bit magic but of course there should be something).
Why it's Xdebug: if I remove the Xdebug settings from Dockerfile, everything starts working. Also, requests hang like it happens when I debug them, i.e. they die after a few minutes waiting with the 504 Gateway Time-out error.
PhpStorm doesn't start a debug session, so it happens silently. Closing PhpStorm doesn't help. Restart of containers, the docker daemon itself and even OS don't help as well. Nothing changes in different browsers.
php-fpm/Dockerfile:
FROM php:7.3.18-fpm-alpine
RUN apk add --no-cache $PHPIZE_DEPS \
&& pecl install xdebug-2.9.8 \
&& docker-php-ext-enable xdebug
#...there are more lines, but even when I remove them, the issue remains
#When I comment this line and do `docker-compose build && docker-compose down && docker-compose up -d`,
# the app returns to life.
COPY xdebug.ini /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
php-fpm/xdebug.ini:
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_connect_back=off
xdebug.remote_host=docker.for.mac.localhost
xdebug.remote_port=10000
xdebug.idekey=PHPSTORM
xdebug.remote_autostart=true
xdebug.var_display_max_depth = 16
xdebug.var_display_max_children = 256
xdebug.var_display_max_data = -1
docker-compose.yml:
version: '3.7'
services:
nginx:
image: nginx:stable
volumes:
- ./docker/nginx/vhost.conf.template:/tmp/vhost.conf.template
- ./docker/nginx/logs:/logs
- ./:/app
depends_on:
- php-fpm
php-fpm:
build: docker/php-fpm
environment:
PHP_IDE_CONFIG: serverName=app.local
volumes:
- ./:/app
nginx/vhost.conf:
server {
charset utf-8;
client_max_body_size 250M;
listen 80;
server_name app.local;
root /app/public;
index index.php;
access_log /logs/nginx.app.access.log;
error_log /logs/nginx.app.error.log;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass fpm:9000;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
location ~* "/\." {
deny all;
return 404;
}
}
It's Docker Desktop 2.5.0 on MacOS 10.14.6.
What could it be?
Why it's Xdebug: if I remove the Xdebug settings from Dockerfile, everything starts working. Also, requests hang like it happens when I debug them, i.e. they die after a few minutes waiting with the 504 Gateway Time-out error.
Enable Xdebug log to confirm that the debug session is established and check what communication is going on there (if any). This should give you some clues on what that might be.
Anyway, it looks like you have some service on that TCP 10000 port already (on your host OS (Mac)) that prevents PhpStorm from listening there (IDE can detect already occupied port on Windows and Linux but not on Mac -- WI-29443).
Use something like sudo lsof -nP -iTCP -sTCP:LISTEN and check what that service might be. Then either shutdown that app or use another port (either for that app or for your Xdebug communications).

Nginx + php-fpm only works with all ports open on the EC2 instance

I have a nginx server with following config for
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
and php-fpm 7.2 www conf listening on listen = /run/php/php7.2-fpm.sock, rest all options are defaults.
This is all on ubuntu 18.04.2 on a EC2 on aws.
If I open all ports on the EC2 i can connect to site without any issues. Having only 443 and 80 keeps throwing "504 gateway timeout"
Don't see anything in /var/log/php7.2-fpm.log and nginx logs just say connection timed out.
I have tried switching both php to listen on 9000 and nginx proxy pass it to 9000, instead of unix socket. Gets same behavior.

Can I have NginX on a host and multiple versions of PHP in docker containers

I found a similar question here but it didn't quite answer my question.
I've always installed my stack locally for development, NginX, PHP7, MySQL, and Couchbase. No Problems.
Now I have to work on a project that requires a lower version of PHP... And my team will have to work on it as well, so I've looked to Docker to try to find the solution.
In my existing NGINX conf files I send the requests off to php-fpm like this
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_ignore_client_abort on;
fastcgi_param SERVER_NAME $http_host;
}
See the line
fastcgi_pass 127.0.0.1:9000;
?
I was hoping to be able to create a docker container running a specific version of PHP and write it into the server block as above but with
fastcgi_pass 172.17.0.1:9000;
Where 172.17.0.1 is the IP of the container.
I have used, very simply,
FROM php:7.1-fpm
EXPOSE 9000
As my dockerfile. I can build an image, run the container, run bash in the container and see that PHP -I and PHP -v return what I expect.
Running docker inspect has given me two IP addresses, 172.17.0.1, and 172.17.0.2 (I've tried both in the example above)
However, this set up is not working - when I try to visit the site in a browser I get an NGINX 504 gateway timeout error.
Guessing I'm missing something, but not sure what.
Happy to use docker compose if I need to, happy to mount volumes into the container if I need to. Just not sure what I need!:)

Trouble Installing Nginx and php5-fpm on Debian 7.3

I have a Debian 7.3 installation in a VM that I am practising installing Nginx and php5-fpm on. I got the Nginx working, by assigning it a manual port of :8080 and that points to /var/www/ for data and in that directory is an index.html and info.php file.
The config file for my Nginx is located at /etc/nginx/conf.d/default.conf and looks like this:
server {
listen 8080;
root /var/www;
index index.php index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.html;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
I have tried changing fastcgi_pass both ways:
fastcgi_pass 127.0.0.1:9000;
and also as:
fastcgi_pass unix:/var/run/php5-fpm.sock;
In my /etc/php5/fpm/pool.d/www.conf file I have the following configuration:
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000
;listen = /var/run/php5-fpm.sock
Here too, I have uncommented the line to match in the Nginx default.conf file.
In my php.ini file I have edited it so that it shows cgi.fix_pathinfo = 0 as required by most of the guides I have seen.
When I try to load nginx, it runs OK. When I try to run php5-fpm this is what happens:
root#debianx86:/# /etc/init.d/php5-fpm status
[FAIL] php5-fpm is not running ... failed!
root#debianx86:/# /etc/init.d/php5-fpm reload
[ ok ] Reloading PHP5 FastCGI Process Manager: php5-fpm.
root#debianx86:/# /etc/init.d/php5-fpm restart
[FAIL] Restarting PHP5 FastCGI Process Manager: php5-fpm failed!
root#debianx86:/# /etc/init.d/php5-fpm start
root#debianx86:/# /etc/init.d/php5-fpm status
[FAIL] php5-fpm is not running ... failed!
root#debianx86:/#
I then open up any of the browsers on my VM and point them to either 127.0.0.1:8080 or localhost:8080 and I get the custom index.html loading that I made and it works! So I then try to load theinfo.php file and I get presented with a 404 Not Found - nginx/1.4.4.
I don't understand what I'm doing wrong. Is there something I'm missing from all this?
I installed nginx from sudo apt-get -y install nginx and sudo apt-get -y install php5-fpm too. Any dependencies they required would have been installed along with that.
Is there a script that I can run on a fresh install of Debian 7.3 that someone has got that will install it properly for me, and make all the modifications so that nginx and php5-fpm are up and running? I've looked over many of the websites with the instructions and I seem to be doing pretty much everything they do, except for the default-sites and enabled-sites, as neither of those folders exist for me, and I don't want to run my virtual hosts like that. I will run them with their own servers listed in the default.conf file.
EDIT: I have even tried following this article at DigitalOcean and it still doesn't work.
EDIT #2: I also did chown -R www-data:www-data /var/www to ensure that the user and group match the information in the www.conf file. I also tried by changing it back to the original root:root specs too. Still nothing.
I think maybe port 9000 is already being used, so php5-fpm can't bind with that port and fails to start.
in the fpm pool settings swap the line of port 9000 with the line with the sock file, then try to start php5-fpm like you were doing, if it works then all you need is to update the nginx configuratuin to proxy pass to the sock file instead of the port.

Nginx connect() failed error

I don't know why I got this error every time I tried to open the page:
2013/04/06 17:52:19 [error] 5040#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost:8080"
I resolved it, it was a configuration file issue, I added:
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
For me the problem was my php-fpm service wasn't running. You can check it by running:
service php-fpm status
and start it by running
service php-fpm start
Sometimes php-fpm might have broken instances running, preventing a restart. This command is a clean way to clear them out and restart php-fpm
killall -9 php-fpm; service php-fpm restart
update your configurations as mentioned before:
location ~ .php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
but don't forget to restart both nginx server and php-fpm after updating
sudo /etc/init.d/nginx restart
sudo /etc/init.d/php-fpm restart
I found I had this same issue with PHP7 running in Docker on a Debian Jessie (8.3) instance.
running command 'ps -aux' showed that php-fpm wasn't running
running 'php-fpm -D' brought it up as deamonized process.
Rerunning 'ps -aux' showed that php-fpm was indeed running
Refreshing my test page showed me the servers PHP info.
Added 'php-fpm -D' to my start.sh script so that things started every time the container is loaded.
Hope this helps someone.
Use fastcgi_pass unix:/var/run/php5-fpm.sock; only nginx and php install same server. If nginx and php install in other server you must use fastcgi_pass ip server:port;

Categories