Im trying to run a NGINX + PHP-FPM application but im getting stuck here and there.
I followed this main question here at StackOverFlow: Question Went through all comments, made a lot of changes and none of it worked...
Everytime i open a PHP file, my browser downloads its instead of running the script on PHP FPM.
The steps i made are the following:
# Create Network
docker network create nginx-php
docker network ls
# Run NGINX and PHP-FPM.
docker run -d -p 9000:9000 --name php-fpm `
--network=nginx-php `
-v D:\Docker\Html:/usr/share/nginx/html php:7.4-fpm-alpine
docker run --name nginx -p 80:80 `
--network=nginx-php `
-v D:\Docker\Html:/usr/share/nginx/html `
-v D:\Docker\Config\nginx:/etc/nginx `
-d nginx:1.23.1-alpine
My NGINX Config file:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
root /usr/share/nginx/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass php-fpm:9000; # Nome do container PHP-FPM e onde ele ta hospedado
fastcgi_index index.php;
include fastcgi_params; # Adaptado para Docker
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
} # End of PHP FPM Nginx config example
}
As you can see, the block with PHP-FPM Pass is exactly as the one stated on the question and documentation.
So i really have no idea whats happening, can someone help me?
PS: Just for clarity, you may be wondering why not take the Docker Compose / DockerFile approach, but i am taking this approach because im currently studying docker and do not know how Docker Compose / DockerFile works that well, so i sticky with the basics for now.
Edit : I discovered that the problem only occurs to fastcgi_pass using TCP connection to the container. If i copy /run/php/ from PHP-FPM and mount it as a volume on both containers, and use unix:/run/php/php7.4-fpm.sock instead of php-fpm:9000 it works... ._. Yeah, but i would still like to use TCP method instead since its faster to deploy on any computer.
For some reason using the "Server Block" in default.conf inside conf.d instead of leaving inside HTTP Block solved the problem...
Really have no idea why, i just tested it to see if it would work and worked.
So, nginx.conf stayed the same (Equals to the original... Without the Server Block) and Default.conf stayed like this:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /usr/share/nginx/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass php-fpm:9000; # Nome do container PHP-FPM e onde ele ta hospedado
fastcgi_index index.php;
include fastcgi_params; # Adaptado para Docker
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
} # End of PHP FPM Nginx config example
Go figure ♂️
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.