Skip to content

Instantly share code, notes, and snippets.

@dkesberg
Last active March 21, 2023 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkesberg/2ac9e12be9b6af2ef8bd49785a78c4c7 to your computer and use it in GitHub Desktop.
Save dkesberg/2ac9e12be9b6af2ef8bd49785a78c4c7 to your computer and use it in GitHub Desktop.
set www-data user to handle file permissions in nginx / php-fpm

Project structure

  • ./docker-compose.yml
  • ./php/Dockerfile
  • ./php/php.ini
  • ./nginx/Dockerfile
  • ./nginx/default.conf
  • ./nginx/certs (Directory for generated certificates)

Make mkcert work in WSL2

  1. Install mkcert in windows terminal: mkcert -install
  2. Install mkcert in WSL/ubuntu terminal: mkcert -install
  3. Copy windows root certificates to WSL
    • Run mkcert -CAROOT to find the root certificates directories in windows and in WSL/ubuntu
    • Copy windows certs to WSL/ubuntu directory
  4. Run mkcert -install again on WSL/ubuntu
  5. Generate certs on WSL/ubuntu: mkcert localhost 127.0.0.1 ::1

References

# ./nginx/default.conf
upstream php {
server unix:/tmp/php-cgi.socket;
server php:9000;
}
server {
listen [::]:80;
listen 80;
server_name localhost;
root /var/www/html;
index index.php;
client_max_body_size 30M;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_intercept_errors on;
fastcgi_pass php;
fastcgi_index index.php;
include fastcgi_params;
#The following parameter can be also included in fastcgi_params file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
server {
listen 443 ssl;
server_name localhost;
root /var/www/html;
index index.php;
client_max_body_size 30M;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_intercept_errors on;
fastcgi_pass php;
fastcgi_index index.php;
include fastcgi_params;
#The following parameter can be also included in fastcgi_params file
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
ssl_certificate /etc/nginx/certs/self-signed/localhost+2.pem;
ssl_certificate_key /etc/nginx/certs/self-signed/localhost+2-key.pem;
}
# ./docker-compose.yml
version: '3.9'
services:
nginx:
build:
context: './nginx'
restart: unless-stopped
ports:
- 80:80
- 443:443
volumes:
- ./src:/var/www/html
depends_on:
- php
php:
build:
context: './php'
restart: unless-stopped
volumes:
- ./src:/var/www/html
# ./nginx/Dockerfile
FROM nginx:stable-alpine
ADD ./default.conf /etc/nginx/conf.d/default.conf
ADD ./certs /etc/nginx/certs/self-signed
# ./php/Dockerfile
FROM php:7.4-fpm-alpine
# local config
# ADD ./php.ini /usr/local/etc/php/conf.d/local.ini
# set www-data user to handle file permissions
# see: https://stackoverflow.com/a/48718660
RUN apk add shadow && usermod -u 1000 www-data && groupmod -g 1000 www-data
WORKDIR /var/www/html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment