From c8cc79dbb3ddddf6b270be130cf4e2483c69d7aa Mon Sep 17 00:00:00 2001 From: Deon George Date: Wed, 11 Mar 2020 14:43:15 +1100 Subject: [PATCH] Added nginx to server static content when app is in container --- Dockerfile | 6 ++++-- init | 14 ++++++++++++++ nginx-app.conf | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 nginx-app.conf diff --git a/Dockerfile b/Dockerfile index 958e38c..39266fe 100755 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,8 @@ FROM php:7.3-fpm -RUN apt-get update && apt-get install -y openssh-server unzip git msmtp \ +RUN apt-get update && apt-get install -y openssh-server unzip git msmtp nginx \ + && rm /etc/nginx/sites-enabled/default \ && apt-get -y autoremove \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* @@ -11,6 +12,7 @@ RUN apt-get update && apt-get install -y openssh-server unzip git msmtp \ RUN useradd -c "Hosting Admin User" -u 1000 -g users -G www-data -d /var/www/html -M lamp RUN sed -e 's/^expose_php = On/expose_php = Off/' /usr/local/etc/php/php.ini-production > /usr/local/etc/php/php.ini COPY www.conf /usr/local/etc/php-fpm.d/ +COPY nginx-app.conf /etc/nginx/conf.d/ COPY sshd_config.patch /tmp/ RUN (cd / && patch -p0 ) < /tmp/sshd_config.patch && rm /tmp/sshd_config.patch @@ -18,7 +20,7 @@ RUN (cd / && patch -p0 ) < /tmp/sshd_config.patch && rm /tmp/sshd_config.patch COPY msmtprc /etc/ RUN sed -i -e 's#^;sendmail_path =#sendmail_path = "/usr/bin/msmtp -t"#' /usr/local/etc/php/php.ini -EXPOSE 9000/tcp 22/tcp +EXPOSE 22/tcp # Add composer RUN curl https://getcomposer.org/installer|php -- --install-dir=/usr/local/bin --filename=composer diff --git a/init b/init index 8053c37..2de3ee4 100755 --- a/init +++ b/init @@ -5,6 +5,8 @@ role=${CONTAINER_ROLE:-app} env=${APP_ENV:-live} php=${PHP_DIR:-/var/www/html} composer=${COMPOSER_DIR:-/var/www/.composer} +NO_NGINX=${NO_NGINX:-TRUE} +SSH_START=${SSH_START:-FALSE} function mp() { set +e @@ -14,6 +16,14 @@ function mp() { echo ${mp} } +function nginx_start() { + # Start NGINX + if [ -x /usr/sbin/nginx -a "${NO_NGINX}" != "TRUE" ]; then + echo "* Starting NGINX..." + start-stop-daemon --start --pidfile /var/run/nginx.pid --exec /usr/sbin/nginx -- -g 'daemon on; master_process on;' + fi +} + # General Setup if [ -x /usr/sbin/sshd -a "${SSH_START}" = "TRUE" ]; then [ ! -d /var/run/sshd ] && mkdir /var/run/sshd @@ -79,6 +89,8 @@ if [ "${role}" = "app" -a -e artisan ]; then fi fi + nginx_start + exec /usr/local/bin/docker-php-entrypoint "$@" elif [ "$role" = "queue" -a -e artisan ]; then @@ -129,6 +141,8 @@ elif [ "$role" = "scheduler" -a -e artisan ]; then " else + nginx_start + echo "? NO container role \"${role}\", AND/OR no laravel install, just starting php-fpm" exec /usr/local/bin/docker-php-entrypoint "$@" fi diff --git a/nginx-app.conf b/nginx-app.conf new file mode 100644 index 0000000..280c40a --- /dev/null +++ b/nginx-app.conf @@ -0,0 +1,35 @@ +server { + listen 80 default_server; + listen [::]:80 default_server; + + access_log none; + client_max_body_size 10m; + error_log none; + fastcgi_buffers 16 16k; + fastcgi_buffer_size 16k; + index index.php index.html; + root /var/www/html; + server_tokens off; + + set $my_https "off"; + if ($http_x_forwarded_proto = "https") { + set $my_https "on"; + } + + location / { + try_files $uri $uri/ /index.php?$query_string; + } + + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass 127.0.0.1:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param SERVER_NAME $host; + fastcgi_param PATH_INFO $fastcgi_path_info; + fastcgi_param HTTPS $my_https; + fastcgi_param PHP_ADMIN_VALUE "sendmail_path=/usr/sbin/sendmail -i -t"; + } +}