Skip to content

Commit 409fe1c

Browse files
committed
Add ability to customize nginx and php-fpm configs.
1 parent 685f016 commit 409fe1c

File tree

9 files changed

+81
-64
lines changed

9 files changed

+81
-64
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ If file not present Nginx proxy a request to PHP-FPM.
88
There must be a `app.php` inside the folder.
99

1010
```
11-
docker run --rm -it --env CUSTOM_DIR=/app/web -p 80:80 -v /home/user/app/:/app makasim/nginx-php-fpm
11+
docker run --rm -it --env NGINX_WEB_ROOT=/app/web -p 80:80 -v /home/user/app/:/app makasim/nginx-php-fpm
1212
```

base/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FROM ubuntu:16.04
33
MAINTAINER Kotliar Maksym kotlyar.maksim@gmail.com
44

55
RUN apt-get update && \
6-
apt-get install -y --no-install-recommends --no-install-suggests nginx php php-fpm ca-certificates && \
6+
apt-get install -y --no-install-recommends --no-install-suggests nginx php php-fpm ca-certificates gettext && \
77
rm -rf /var/lib/apt/lists/*
88

99

@@ -14,8 +14,8 @@ RUN ln -sf /dev/stdout /var/log/nginx/access.log \
1414

1515
RUN rm -f /etc/nginx/sites-enabled/*
1616

17-
COPY nginx.conf /etc/nginx/nginx.conf
18-
COPY php-fpm.conf /etc/php/7.0/fpm/pool.d/www.conf
17+
COPY nginx.conf.tpl /tmp/nginx.conf.tpl
18+
COPY php-fpm.conf.tpl /tmp/php-fpm.conf.tpl
1919

2020
RUN mkdir -p /run/php && touch /run/php/php7.0-fpm.sock && touch /run/php/php7.0-fpm.pid
2121

base/entrypoint.sh

100644100755
Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
#!/usr/bin/env bash
22

3-
TRAPPED_SIGNAL=false
3+
export NGINX_WEB_ROOT=${NGINX_WEB_ROOT:-'/var/www/html'}
4+
export NGINX_PHP_FALLBACK=${NGINX_PHP_FALLBACK:-'/app.php'}
5+
export NGINX_PHP_LOCATION=${NGINX_PHP_LOCATION:-'^/app\.php(/|$)'}
6+
export NGINX_USER=${NGINX_USER:-'www-data'}
7+
export NGINX_CONF=${NGINX_CONF:-'/etc/nginx/nginx.conf'}
8+
9+
export PHP_SOCK_FILE=${PHP_SOCK_FILE:-'/run/php.sock'}
10+
export PHP_USER=${PHP_USER:-'www-data'}
11+
export PHP_GROUP=${PHP_GROUP:-'www-data'}
12+
export PHP_MODE=${PHP_MODE:-'0660'}
13+
export PHP_FPM_CONF=${PHP_FPM_CONF:-'/etc/php/7.0/fpm/php-fpm.conf'}
14+
15+
envsubst '${NGINX_WEB_ROOT} ${NGINX_PHP_FALLBACK} ${NGINX_PHP_LOCATION} ${NGINX_USER} ${NGINX_CONF} ${PHP_SOCK_FILE} ${PHP_USER} ${PHP_GROUP} ${PHP_MODE} ${PHP_FPM_CONF}' < /tmp/nginx.conf.tpl > $NGINX_CONF
16+
envsubst '${NGINX_WEB_ROOT} ${NGINX_PHP_FALLBACK} ${NGINX_PHP_LOCATION} ${NGINX_USER} ${NGINX_CONF} ${PHP_SOCK_FILE} ${PHP_USER} ${PHP_GROUP} ${PHP_MODE} ${PHP_FPM_CONF}' < /tmp/php-fpm.conf.tpl > $PHP_FPM_CONF
417

5-
if [ -d $CUSTOM_DIR ]; then
6-
echo 'Custom dir setup'
7-
rm -rf /var/www/html;
8-
cd /var/www && ln -s $CUSTOM_DIR html
9-
fi
18+
cat $NGINX_CONF
19+
#cat $PHP_FPM_CONF
20+
21+
TRAPPED_SIGNAL=false
1022

1123
echo 'Starting NGINX';
12-
nginx -c ${NGINX_CONF:-/etc/nginx/nginx.conf} -g 'daemon off;' 2>&1 &
24+
nginx -c $NGINX_CONF -g 'daemon off;' 2>&1 &
1325
NGINX_PID=$!
1426

1527
echo 'Starting PHP-FPM';
16-
php-fpm7.0 -R -F -c ${PHP_FPM_CONF:-/etc/php/7.0/fpm/php-fpm.conf} 2>&1 &
28+
php-fpm7.0 -R -F -c $PHP_FPM_CONF 2>&1 &
1729
PHP_FPM_PID=$!
1830

1931
trap "TRAPPED_SIGNAL=true; kill -15 $NGINX_PID; kill -15 $PHP_FPM_PID;" SIGTERM SIGINT

base/nginx.conf renamed to base/nginx.conf.tpl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
user www-data;
1+
user $NGINX_USER;
22
worker_processes auto;
33
pid /run/nginx.pid;
44

@@ -63,15 +63,15 @@ http {
6363
6464
server {
6565
listen 80 default_server;
66-
root /var/www/html;
66+
root $NGINX_WEB_ROOT;
6767
6868
location / {
6969
# try to serve file directly, fallback to app.php
70-
try_files $uri /app.php$is_args$args;
70+
try_files $uri $NGINX_PHP_FALLBACK$is_args$args;
7171
}
7272
# PROD
73-
location ~ ^/app\.php(/|$) {
74-
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
73+
location ~ $NGINX_PHP_LOCATION {
74+
fastcgi_pass unix:$PHP_SOCK_FILE;
7575
fastcgi_split_path_info ^(.+\.php)(/.*)$;
7676
include fastcgi_params;
7777
# When you are using symlinks to link the document root to the
@@ -96,5 +96,3 @@ http {
9696
}
9797
}
9898
}
99-
100-

base/php-fpm.conf renamed to base/php-fpm.conf.tpl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
; Unix user/group of processes
2121
; Note: The user is mandatory. If the group is not set, the default user's group
2222
; will be used.
23-
user = www-data
24-
group = www-data
23+
user = $PHP_USER
24+
group = $PHP_GROUP
2525

2626
; The address on which to accept FastCGI requests.
2727
; Valid syntaxes are:
@@ -33,7 +33,7 @@ group = www-data
3333
; (IPv6 and IPv4-mapped) on a specific port;
3434
; '/path/to/unix/socket' - to listen on a unix socket.
3535
; Note: This value is mandatory.
36-
listen = /run/php/php7.0-fpm.sock
36+
listen = $PHP_SOCK_FILE
3737

3838
; Set listen(2) backlog.
3939
; Default Value: 511 (-1 on FreeBSD and OpenBSD)
@@ -44,8 +44,8 @@ listen = /run/php/php7.0-fpm.sock
4444
; BSD-derived systems allow connections regardless of permissions.
4545
; Default Values: user and group are set as the running user
4646
; mode is set to 0660
47-
listen.owner = www-data
48-
listen.group = www-data
47+
listen.owner = $PHP_USER
48+
listen.group = $PHP_GROUP
4949
;listen.mode = 0660
5050
; When POSIX Access Control Lists are supported you can set them using
5151
; these options, value is a comma separated list of user/group names.
@@ -413,9 +413,9 @@ pm.max_spare_servers = 3
413413
;php_admin_value[memory_limit] = 32M
414414

415415

416-
user = www-data
417-
group = www-data
418-
listen.owner = www-data
419-
listen.group = www-data
420-
listen.mode = 0660
416+
user = $PHP_USER
417+
group = $PHP_GROUP
418+
listen.owner = $PHP_USER
419+
listen.group = $PHP_GROUP
420+
listen.mode = $PHP_MODE
421421
clear_env = no

php-all-exts/Dockerfile

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,18 @@ FROM ubuntu:16.04
33
MAINTAINER Kotliar Maksym kotlyar.maksim@gmail.com
44

55
RUN apt-get update && \
6-
apt-get install -y --no-install-recommends --no-install-suggests nginx php php-fpm ca-certificates && \
6+
apt-get install -y --no-install-recommends --no-install-suggests nginx php php-fpm ca-certificates gettext && \
77
rm -rf /var/lib/apt/lists/*
88

9-
# exts
10-
RUN apt-get update && \
11-
apt-get install -y --no-install-recommends --no-install-suggests php-mongodb php-curl php-intl php-soap php-xml php-mysql php-amqp && \
12-
rm -rf /var/lib/apt/lists/*
13-
14-
159
# forward request and error logs to docker log collector
1610
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
1711
&& ln -sf /dev/stderr /var/log/nginx/error.log \
1812
&& ln -sf /dev/stderr /var/log/php7.0-fpm.log
1913

2014
RUN rm -f /etc/nginx/sites-enabled/*
2115

22-
COPY nginx.conf /etc/nginx/nginx.conf
23-
COPY php-fpm.conf /etc/php/7.0/fpm/pool.d/www.conf
16+
COPY nginx.conf.tpl /tmp/nginx.conf.tpl
17+
COPY php-fpm.conf.tpl /tmp/php-fpm.conf.tpl
2418

2519
RUN mkdir -p /run/php && touch /run/php/php7.0-fpm.sock && touch /run/php/php7.0-fpm.pid
2620

@@ -31,4 +25,7 @@ EXPOSE 80
3125

3226
CMD ["/entrypoint.sh"]
3327

34-
28+
# exts
29+
RUN apt-get update && \
30+
apt-get install -y --no-install-recommends --no-install-suggests php-mongodb php-curl php-intl php-soap php-xml php-mysql php-amqp && \
31+
rm -rf /var/lib/apt/lists/*

php-all-exts/entrypoint.sh

100644100755
Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
#!/usr/bin/env bash
22

3-
TRAPPED_SIGNAL=false
3+
export NGINX_WEB_ROOT=${NGINX_WEB_ROOT:-'/var/www/html'}
4+
export NGINX_PHP_FALLBACK=${NGINX_PHP_FALLBACK:-'/app.php'}
5+
export NGINX_PHP_LOCATION=${NGINX_PHP_LOCATION:-'^/app\.php(/|$)'}
6+
export NGINX_USER=${NGINX_USER:-'www-data'}
7+
export NGINX_CONF=${NGINX_CONF:-'/etc/nginx/nginx.conf'}
8+
9+
export PHP_SOCK_FILE=${PHP_SOCK_FILE:-'/run/php.sock'}
10+
export PHP_USER=${PHP_USER:-'www-data'}
11+
export PHP_GROUP=${PHP_GROUP:-'www-data'}
12+
export PHP_MODE=${PHP_MODE:-'0660'}
13+
export PHP_FPM_CONF=${PHP_FPM_CONF:-'/etc/php/7.0/fpm/php-fpm.conf'}
14+
15+
envsubst '${NGINX_WEB_ROOT} ${NGINX_PHP_FALLBACK} ${NGINX_PHP_LOCATION} ${NGINX_USER} ${NGINX_CONF} ${PHP_SOCK_FILE} ${PHP_USER} ${PHP_GROUP} ${PHP_MODE} ${PHP_FPM_CONF}' < /tmp/nginx.conf.tpl > $NGINX_CONF
16+
envsubst '${NGINX_WEB_ROOT} ${NGINX_PHP_FALLBACK} ${NGINX_PHP_LOCATION} ${NGINX_USER} ${NGINX_CONF} ${PHP_SOCK_FILE} ${PHP_USER} ${PHP_GROUP} ${PHP_MODE} ${PHP_FPM_CONF}' < /tmp/php-fpm.conf.tpl > $PHP_FPM_CONF
417

5-
if [ -d $CUSTOM_DIR ]; then
6-
echo 'Custom dir setup'
7-
rm -rf /var/www/html;
8-
cd /var/www && ln -s $CUSTOM_DIR html
9-
fi
18+
cat $NGINX_CONF
19+
#cat $PHP_FPM_CONF
20+
21+
TRAPPED_SIGNAL=false
1022

1123
echo 'Starting NGINX';
12-
nginx -c ${NGINX_CONF:-/etc/nginx/nginx.conf} -g 'daemon off;' 2>&1 &
24+
nginx -c $NGINX_CONF -g 'daemon off;' 2>&1 &
1325
NGINX_PID=$!
1426

1527
echo 'Starting PHP-FPM';
16-
php-fpm7.0 -R -F -c ${PHP_FPM_CONF:-/etc/php/7.0/fpm/php-fpm.conf} 2>&1 &
28+
php-fpm7.0 -R -F -c $PHP_FPM_CONF 2>&1 &
1729
PHP_FPM_PID=$!
1830

1931
trap "TRAPPED_SIGNAL=true; kill -15 $NGINX_PID; kill -15 $PHP_FPM_PID;" SIGTERM SIGINT

php-all-exts/nginx.conf renamed to php-all-exts/nginx.conf.tpl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
user www-data;
1+
user $NGINX_USER;
22
worker_processes auto;
33
pid /run/nginx.pid;
44

@@ -63,15 +63,15 @@ http {
6363
6464
server {
6565
listen 80 default_server;
66-
root /var/www/html;
66+
root $NGINX_WEB_ROOT;
6767
6868
location / {
6969
# try to serve file directly, fallback to app.php
70-
try_files $uri /app.php$is_args$args;
70+
try_files $uri $NGINX_PHP_FALLBACK$is_args$args;
7171
}
7272
# PROD
73-
location ~ ^/app\.php(/|$) {
74-
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
73+
location ~ $NGINX_PHP_LOCATION {
74+
fastcgi_pass unix:$PHP_SOCK_FILE;
7575
fastcgi_split_path_info ^(.+\.php)(/.*)$;
7676
include fastcgi_params;
7777
# When you are using symlinks to link the document root to the
@@ -96,5 +96,3 @@ http {
9696
}
9797
}
9898
}
99-
100-

php-all-exts/php-fpm.conf renamed to php-all-exts/php-fpm.conf.tpl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
; Unix user/group of processes
2121
; Note: The user is mandatory. If the group is not set, the default user's group
2222
; will be used.
23-
user = www-data
24-
group = www-data
23+
user = $PHP_USER
24+
group = $PHP_GROUP
2525

2626
; The address on which to accept FastCGI requests.
2727
; Valid syntaxes are:
@@ -33,7 +33,7 @@ group = www-data
3333
; (IPv6 and IPv4-mapped) on a specific port;
3434
; '/path/to/unix/socket' - to listen on a unix socket.
3535
; Note: This value is mandatory.
36-
listen = /run/php/php7.0-fpm.sock
36+
listen = $PHP_SOCK_FILE
3737

3838
; Set listen(2) backlog.
3939
; Default Value: 511 (-1 on FreeBSD and OpenBSD)
@@ -44,8 +44,8 @@ listen = /run/php/php7.0-fpm.sock
4444
; BSD-derived systems allow connections regardless of permissions.
4545
; Default Values: user and group are set as the running user
4646
; mode is set to 0660
47-
listen.owner = www-data
48-
listen.group = www-data
47+
listen.owner = $PHP_USER
48+
listen.group = $PHP_GROUP
4949
;listen.mode = 0660
5050
; When POSIX Access Control Lists are supported you can set them using
5151
; these options, value is a comma separated list of user/group names.
@@ -413,9 +413,9 @@ pm.max_spare_servers = 3
413413
;php_admin_value[memory_limit] = 32M
414414

415415

416-
user = www-data
417-
group = www-data
418-
listen.owner = www-data
419-
listen.group = www-data
420-
listen.mode = 0660
416+
user = $PHP_USER
417+
group = $PHP_GROUP
418+
listen.owner = $PHP_USER
419+
listen.group = $PHP_GROUP
420+
listen.mode = $PHP_MODE
421421
clear_env = no

0 commit comments

Comments
 (0)