Skip to content

Commit

Permalink
Add NGINX Build and flexible configuration for fast cgi
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed May 24, 2024
1 parent 6476822 commit 0cc216d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 61 deletions.
51 changes: 0 additions & 51 deletions assets/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,64 +20,13 @@ then
echo
fi

if [ -z "$PHP_FPM_SERVER" ]
then
PHP_FPM_SERVER=127.0.0.1:9000
fi

# Adjust NGINX
if [[ -f /etc/nginx/http.d/default.conf ]]
then
if [ -z "$NGINX_ROOT" ]
then
NGINX_ROOT=/var/www/html
fi

${VERBOSE_MODE} && echo "Setting root to '$NGINX_ROOT'"
sed -i "s|^\(\s*root\s*\)@root@;$|\1$NGINX_ROOT;|g" /etc/nginx/http.d/default.conf
${VERBOSE_MODE} && echo "Setting NGINX fastcgi to '$PHP_FPM_SERVER'"
sed -i "s|@fastcgi@|${PHP_FPM_SERVER}|g" /etc/nginx/http.d/default.conf

if [[ -n "$PHP_CONTROLLER" ]]
then
${VERBOSE_MODE} && echo "Setting controller as '$PHP_CONTROLLER'"
sed -i "s|^\(\s*\)#\(try_files.*\)@controller@\(.*\);$|\1\2$PHP_CONTROLLER\3;|g" /etc/nginx/http.d/default.conf
sed -i "s|^\(\s*\)\(index index.php.*\)$|\1#\2|g" /etc/nginx/http.d/default.conf
fi

if [[ -n "$NGINX_SSL_CERT" ]]
then
${VERBOSE_MODE} && echo "Setting CERT as '$NGINX_SSL_CERT'"
${VERBOSE_MODE} && echo "Setting CERT_KEY as '$NGINX_SSL_CERT_KEY'"
sed -i "s|^\(\s*\)#\(listen 443.*\)$|\1\2|g" /etc/nginx/http.d/default.conf
sed -i "s|^\(\s*\)#\(ssl_certificate .*\)@cert@;$|\1\2$NGINX_SSL_CERT;|g" /etc/nginx/http.d/default.conf
sed -i "s|^\(\s*\)#\(ssl_certificate_key .*\)@certkey@;$|\1\2$NGINX_SSL_CERT_KEY;|g" /etc/nginx/http.d/default.conf
fi
fi

# Adjust PHP
if [[ -f /usr/bin/php ]]
then
# Getting PHP INI Paths
PHPINI=`php -i | grep "Loaded Configuration File" | awk -F" => " '{print $2}'`
PHPMODULES=`php -i | grep "Scan this dir" | awk -F" => " '{print $2}'`
PHPCUSTOM=/etc/php/conf.d
PHPWWWCONFPATH=$(find /etc -name www.conf 2>/dev/null)
PHPBASECONFPATH=$(find /etc -name base.conf 2>/dev/null)

# Setting PHP FPM Server
if [ -n "$PHP_FPM_SERVER" ]
then
${VERBOSE_MODE} && echo "Setting FPM fastcgi to '$PHP_FPM_SERVER'"
if [ -f "$PHPWWWCONFPATH" ]
then
sed -i "s|@fastcgi@|${PHP_FPM_SERVER}|g" $PHPWWWCONFPATH
fi
if [ -f "$PHPBASECONFPATH" ]
then
sed -i "s|@fastcgi@|${PHP_FPM_SERVER}|g" $PHPBASECONFPATH
fi
fi

# Disable modules
for VAR in `printenv | grep DISABLEMODULE | cut -d= -f1 | cut -d_ -f2- | awk '{print tolower($0)}'`
Expand Down
3 changes: 0 additions & 3 deletions assets/fpm-nginx/conf/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
user nginx;

# Run in foreground
daemon off;

# Set number of worker processes automatically based on number of CPU cores.
worker_processes auto;

Expand Down
2 changes: 1 addition & 1 deletion assets/fpm-nginx/conf/supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
command=/start-nginx.sh
autostart=true
autorestart=false
priority=10
Expand Down
21 changes: 16 additions & 5 deletions assets/script/start-fpm.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
#!/usr/bin/env bash

FPMBASE="/etc/$PHP_VARIANT/php-fpm.d/base.conf"
FPMWWW="/etc/$PHP_VARIANT/php-fpm.d/www.conf"
FPMENV="/etc/$PHP_VARIANT/php-fpm.d/env.conf"
FPMMAIN="/etc/$PHP_VARIANT/php-fpm.conf"

if [ -z "$PHP_FPM_SERVER"]; then
sed -i 's/^\(listen *=\).*/\1 0.0.0.0:9000/g' $FPMBASE
if [ "$VERBOSE" == "true" ]
then
VERBOSE_MODE=true
fi

if [ -z "$PHP_FPM_SERVER" ]
then
PHP_FPM_SERVER=0.0.0.0:9000
fi
${VERBOSE_MODE} && echo "Setting FPM fastcgi to '$PHP_FPM_SERVER'"
sed -i "s|@fastcgi@|${PHP_FPM_SERVER}|g" $FPMBASE
sed -i "s|@fastcgi@|${PHP_FPM_SERVER}|g" $FPMWWW

# Function to update the fpm configuration to make the service environment variables available
function setEnvironmentVariable() {

if [ -z "$2" ]; then
echo "Environment variable '$1' not set."
${VERBOSE_MODE} && echo "Environment variable '$1' is empty."
return
fi

${VERBOSE_MODE} && echo "$1 = '$2'"
# Check whether variable already exists
if grep -q $1 "$FPMENV"; then
# Reset variable
Expand All @@ -29,7 +40,7 @@ function setEnvironmentVariable() {
# Grep for variables that look like docker set them (_PORT_)
if [ -z "$PHP_FPM_IGNORE_ENV"]
then
echo "clear_env = no" >> $FPMENV
echo "clear_env = no" > $FPMENV
for _curVar in `env | awk -F = '{print $1}'`;do
# awk has split them by the equals sign
# Pass the name and value to our function
Expand All @@ -38,7 +49,7 @@ then
fi

# Log something to the supervisord log so we know this script as run
echo "DONE"
${VERBOSE_MODE} && echo "DONE"

# Now start php-fpm
$(command -v php-fpm) --nodaemonize --fpm-config "$FPMMAIN"
34 changes: 34 additions & 0 deletions assets/script/start-nginx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

if [ "$VERBOSE" == "true" ]
then
VERBOSE_MODE=true
fi

if [ -z "$NGINX_ROOT" ]
then
NGINX_ROOT=/var/www/html
fi

${VERBOSE_MODE} && echo "Setting root to '$NGINX_ROOT'"
sed -i "s|^\(\s*root\s*\)@root@;$|\1$NGINX_ROOT;|g" /etc/nginx/http.d/default.conf
${VERBOSE_MODE} && echo "Setting NGINX fastcgi to '$PHP_FPM_SERVER'"
sed -i "s|@fastcgi@|${PHP_FPM_SERVER}|g" /etc/nginx/http.d/default.conf

if [[ -n "$PHP_CONTROLLER" ]]
then
${VERBOSE_MODE} && echo "Setting controller as '$PHP_CONTROLLER'"
sed -i "s|^\(\s*\)#\(try_files.*\)@controller@\(.*\);$|\1\2$PHP_CONTROLLER\3;|g" /etc/nginx/http.d/default.conf
sed -i "s|^\(\s*\)\(index index.php.*\)$|\1#\2|g" /etc/nginx/http.d/default.conf
fi

if [[ -n "$NGINX_SSL_CERT" ]]
then
${VERBOSE_MODE} && echo "Setting CERT as '$NGINX_SSL_CERT'"
${VERBOSE_MODE} && echo "Setting CERT_KEY as '$NGINX_SSL_CERT_KEY'"
sed -i "s|^\(\s*\)#\(listen 443.*\)$|\1\2|g" /etc/nginx/http.d/default.conf
sed -i "s|^\(\s*\)#\(ssl_certificate .*\)@cert@;$|\1\2$NGINX_SSL_CERT;|g" /etc/nginx/http.d/default.conf
sed -i "s|^\(\s*\)#\(ssl_certificate_key .*\)@certkey@;$|\1\2$NGINX_SSL_CERT_KEY;|g" /etc/nginx/http.d/default.conf
fi

$(command -v nginx) -g "daemon off;"
4 changes: 3 additions & 1 deletion generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def build_fpm_nginx(self, arch):
self._run_cli(["buildah", "copy", container, "assets/fpm-nginx/conf/nginx.vh.default.conf", "/etc/nginx/http.d/default.conf"])
self._run_cli(["buildah", "copy", container, "assets/fpm-nginx/conf/supervisord.conf", "/etc/supervisord.conf"])
self._run_cli(["buildah", "copy", container, "assets/script/exit-event-listener.py", "/exit-event-listener.py"])
self._run_cli(["buildah", "copy", container, "assets/script/start-nginx.sh", "/start-nginx.sh"])
script = self.parse_config("php-fpm-nginx.j2", False)
with open(".tmp.sh", "w") as file:
file.write(script)
Expand All @@ -158,10 +159,11 @@ def build_nginx(self, arch):
self._banner("nginx")
base_image = self.content["image"][arch] if arch in self.content["image"] else self.content["image"]["default"]
container = self._from("docker://" + base_image, arch)
self._run_cli(["buildah", "config", "--cmd", "nginx", container])
self._run_cli(["buildah", "config", "--cmd", "/start-nginx.sh", container])
self._run_cli(["buildah", "config", "--entrypoint", '["/entrypoint.sh"]', container])
self._run_cli(["buildah", "config", "--workingdir", "/srv", container])
self._run_cli(["buildah", "copy", container, "assets/fpm-nginx/conf/nginx.conf", "/etc/nginx/nginx.conf"])
self._run_cli(["buildah", "copy", container, "assets/fpm-nginx/conf/nginx.vh.default.conf", "/etc/nginx/http.d/default.conf"])
self._run_cli(["buildah", "copy", container, "assets/script/start-nginx.sh", "/start-nginx.sh"])
self._run_cli(["buildah", "copy", container, "assets/entrypoint.sh", "/"])
return self._build(container, "nginx", arch)

0 comments on commit 0cc216d

Please sign in to comment.