Hello and welcome,
using Docker and NginX together with PHP is nearly the same as you would do with NginX and PHP on an normal computer :). I don’t go into details for WSL2 (as I use Docker on Linux) but I think you will get the needed infos for integrating NginX and PHP in Docker.
But one thing after the other…
1 - My php-files (i.e. a file called phpinfo.php
containing only <?php phpinfo(); ?>
for verifying that PHP is working fine) and the static files (*.html, *.jpg, *.css, *.js, …) are located in /srv/www/html on my hosts harddrive.
2 - As I need some tweaks for PHP to upload larger files so I create a file called uploads.ini
with this content:
file_uploads = On
memory_limit = 128M
upload_max_filesize = 128M
post_max_size = 128M
3 - I need NginX to know that it should forward requests for *.php
to PHP, so I have this file I’ve called default.conf
- for this example I have removed all https-stuff, maintenance-mode-things, forwarding to Tomcat, to the webradio, … so that only the PHP-stuff is here:
client_max_body_size 128M;
server {
listen 80 default_server;
listen [::]:80 default_server;
index index.php index.html;
server_name _;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
location ~ /\.ht {
deny all;
}
}
4 - Now I use this docker-compose.yml
to configure what should be done - start PHP and NginX and you will be able to access the PHP-container with the name php
which is used in NginX’s config-file above. I also mount my webserver’s document-root into both containers and the config-file into its corresponding container:
version: 3.2
services:
nginx:
image: nginx:latest
ports:
- "3080:80"
volumes:
- "/srv/www/html:/var/www/html"
- ./default.conf:/etc/nginx/conf.d/default.conf
links:
- php
php:
image: php:7.4-fpm
volumes:
- "/srv/www/html:/var/www/html"
- "./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini:ro"
5 - Now I can start both services with docker-compose up -d
and access the webserver with http://<serverip>/phpinfo.php
and should see some information about the server and PHP. If you need additional modules for PHP (i.e. mysqli, gd, curl, exif, mongodb, radius, redis, …) you have to build your own image based on php:7.4-fpm
. If you need more information on this - feel free to ask