From f2cc4b6f19959d453570c1e7a84c0e0c2984670f Mon Sep 17 00:00:00 2001 From: Roberto Bermejo Martinez Date: Fri, 28 Sep 2018 21:11:22 +0200 Subject: [PATCH] Allow user to change config and scripts folder --- docs/config_flags.md | 2 ++ docs/nginx_configs.md | 1 + docs/scripting_templating.md | 1 + scripts/letsencrypt-setup | 4 --- scripts/pull | 8 +++--- scripts/push | 2 +- scripts/start.sh | 47 +++++++++++++++++++++--------------- 7 files changed, 37 insertions(+), 28 deletions(-) diff --git a/docs/config_flags.md b/docs/config_flags.md index c1b8a6b..b9cd31a 100644 --- a/docs/config_flags.md +++ b/docs/config_flags.md @@ -21,6 +21,7 @@ The following flags are a list of all the currently supported options that can b | Name | Description | |-------------------------|----------------------------------------------------------------------------------------------------------------| | WEBROOT | Change the default webroot directory from `/var/www/html` to your own setting | +| CONFIG_FOLDER | Change the default config directory from `/var/www/html/conf` to your own setting | | ERRORS | Set to 1 to display PHP Errors in the browser | | HIDE_NGINX_HEADERS | Disable by setting to 0, default behaviour is to hide nginx + php version in headers | | PHP_MEM_LIMIT | Set higher PHP memory limit, default is 128 Mb | @@ -31,6 +32,7 @@ The following flags are a list of all the currently supported options that can b | REAL_IP_HEADER | set to 1 to enable real ip support in the logs | | REAL_IP_FROM | set to your CIDR block for real ip in logs | | RUN_SCRIPTS | Set to 1 to execute scripts | +| SCRIPTS_FOLDER | Change the default script folder from `/var/www/html/scripts` to your won setting | | PGID | Set to GroupId you want to use for nginx (helps permissions when using local volume) | | PUID | Set to UserID you want to use for nginx (helps permissions when using local volume) | | REMOVE_FILES | Use REMOVE_FILES=0 to prevent the script from clearing out /var/www/html (useful for working with local files) | diff --git a/docs/nginx_configs.md b/docs/nginx_configs.md index 436a798..5e18f82 100644 --- a/docs/nginx_configs.md +++ b/docs/nginx_configs.md @@ -1,5 +1,6 @@ ## Custom Nginx Config files Sometimes you need a custom config file for nginx to do rewrites or password protection, etc. For this reason we've included the ability to have custom nginx configs pulled directly from your git source. Please have a read of the [repo layout guidelines](repo_layout.md) for more information. Its pretty simple to enable this, all you need to do is include a folder in the root of your repository called ```conf/nginx/``` within this folder you need to include a file called ```nginx-site.conf``` which will contain your default nginx site config. If you wish to have a custom file for SSL you simply include a file called ```nginx-site-ssl.conf``` in the same directory. These files will then be swapped in after you code is cloned. +In addition, you can configure __CONFIG_FOLDER__ with your custome path. ## REAL IP / X-Forwarded-For Headers If you operate your container behind a load balancer, an ELB on AWS for example, you need to configure nginx to get the real IP and not the load balancer IP in the logs by using the X-Forwarded-For. We've provided some handy flags to let you do this. You need to set both of these to get this to work: diff --git a/docs/scripting_templating.md b/docs/scripting_templating.md index 02ab715..30693a8 100644 --- a/docs/scripting_templating.md +++ b/docs/scripting_templating.md @@ -1,5 +1,6 @@ ## Scripting There is often an occasion where you need to run a script on code to do a transformation once code lands in the container. For this reason we have developed scripting support. By including a scripts folder in your git repository and passing the __RUN_SCRIPTS=1__ flag to your command line the container will execute your scripts. Please see the [repo layout guidelines](https://gitlab.com/ric_harvey/nginx-php-fpm/blob/master/docs/repo_layout.md) for more details on how to organise this. +In addition, you can configure __SCRIPT_FOLDER__ with your custome path. ## Using environment variables / templating To set the variables pass them in as environment variables on the docker command line. diff --git a/scripts/letsencrypt-setup b/scripts/letsencrypt-setup index 45f560b..0a4f9f8 100755 --- a/scripts/letsencrypt-setup +++ b/scripts/letsencrypt-setup @@ -10,10 +10,6 @@ else # change nginx for webroot and domain name sed -i "s/##DOMAIN##/${DOMAIN}/g" /etc/nginx/sites-enabled/default-ssl.conf sed -i "s#root /var/www/html;#root ${WEBROOT};#g" /etc/nginx/sites-available/default-ssl.conf - if [ ! -z "$WEBROOT" ]; then - webroot=$WEBROOT - sed -i "s#root /var/www/html;#root ${webroot};#g" /etc/nginx/sites-available/default-ssl.conf - fi supervisorctl restart nginx diff --git a/scripts/pull b/scripts/pull index 2231550..8ec3045 100755 --- a/scripts/pull +++ b/scripts/pull @@ -11,10 +11,10 @@ if [ -z "$GIT_NAME" ]; then fi # Try auto install for composer -if [ -f "/var/www/html/composer.lock" ]; then - composer install --no-dev --working-dir=/var/www/html +if [ -f "${WEBROOT}/composer.lock" ]; then + composer install --no-dev --working-dir=${WEBROOT} fi -cd /var/www/html +cd ${WEBROOT} git pull || exit 1 -chown -Rf nginx:nginx /var/www/html +chown -Rf nginx:nginx ${WEBROOT} diff --git a/scripts/push b/scripts/push index bf577cf..ff89d39 100755 --- a/scripts/push +++ b/scripts/push @@ -15,7 +15,7 @@ if [ -z "$GIT_NAME" ]; then fi ts=$(timestamp) -cd /var/www/html +cd ${WEBROOT} git add . git commit -a -m "push from container $ts" git push diff --git a/scripts/start.sh b/scripts/start.sh index 61c7e2d..d709b7f 100644 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -21,7 +21,7 @@ fi if [ ! -z "$WEBROOT" ]; then sed -i "s#root /var/www/html;#root ${WEBROOT};#g" /etc/nginx/sites-available/default.conf else - webroot=/var/www/html + WEBROOT=/var/www/html fi # Setup git variables @@ -34,14 +34,14 @@ if [ ! -z "$GIT_NAME" ]; then fi # Dont pull code down if the .git folder exists -if [ ! -d "/var/www/html/.git" ]; then +if [ ! -d "${WEBROOT}/.git" ]; then # Pull down code from git for our site! if [ ! -z "$GIT_REPO" ]; then # Remove the test index file if you are pulling in a git repo if [ ! -z ${REMOVE_FILES} ] && [ ${REMOVE_FILES} == 0 ]; then echo "skiping removal of files" else - rm -Rf /var/www/html/* + rm -Rf ${WEBROOT}/* fi GIT_COMMAND='git clone ' if [ ! -z "$GIT_BRANCH" ]; then @@ -57,7 +57,7 @@ if [ ! -d "/var/www/html/.git" ]; then GIT_COMMAND=${GIT_COMMAND}" https://${GIT_USERNAME}:${GIT_PERSONAL_TOKEN}@${GIT_REPO}" fi fi - ${GIT_COMMAND} /var/www/html || exit 1 + ${GIT_COMMAND} ${WEBROOT} || exit 1 if [ ! -z "$GIT_TAG" ]; then git checkout ${GIT_TAG} || exit 1 fi @@ -65,22 +65,26 @@ if [ ! -d "/var/www/html/.git" ]; then git checkout ${GIT_COMMIT} || exit 1 fi if [ -z "$SKIP_CHOWN" ]; then - chown -Rf nginx.nginx /var/www/html + chown -Rf nginx.nginx ${WEBROOT} fi fi fi +if [ -z "$CONFIG_FOLDER" ]; then + CONFIG_FOLDER=${WEBROOT}/conf +fi + # Enable custom nginx config files if they exist -if [ -f /var/www/html/conf/nginx/nginx.conf ]; then - cp /var/www/html/conf/nginx/nginx.conf /etc/nginx/nginx.conf +if [ -f ${CONFIG_FOLDER}/nginx.conf ]; then + cp ${CONFIG_FOLDER}/nginx.conf /etc/nginx/nginx.conf fi -if [ -f /var/www/html/conf/nginx/nginx-site.conf ]; then - cp /var/www/html/conf/nginx/nginx-site.conf /etc/nginx/sites-available/default.conf +if [ -f ${CONFIG_FOLDER}/nginx-site.conf ]; then + cp ${CONFIG_FOLDER}/nginx-site.conf /etc/nginx/sites-available/default.conf fi -if [ -f /var/www/html/conf/nginx/nginx-site-ssl.conf ]; then - cp /var/www/html/conf/nginx/nginx-site-ssl.conf /etc/nginx/sites-available/default-ssl.conf +if [ -f ${CONFIG_FOLDER}/nginx-site-ssl.conf ]; then + cp ${CONFIG_FOLDER}/nginx-site-ssl.conf /etc/nginx/sites-available/default-ssl.conf fi @@ -169,7 +173,7 @@ if [[ "$ENABLE_XDEBUG" == "1" ]] ; then fi fi fi -else +else if [ -f $XdebugFile ]; then echo "Disabling Xdebug" rm $XdebugFile @@ -185,17 +189,22 @@ if [ ! -z "$PUID" ]; then adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx -u ${PUID} nginx else if [ -z "$SKIP_CHOWN" ]; then - chown -Rf nginx.nginx /var/www/html + chown -Rf nginx.nginx ${WEBROOT} fi fi # Run custom scripts if [[ "$RUN_SCRIPTS" == "1" ]] ; then - if [ -d "/var/www/html/scripts/" ]; then + + if [ -z "$SCRIPTS_FOLDER" ]; then + SCRIPTS_FOLDER=${WEBROOT}/scripts/ + fi + + if [ -d "${SCRIPTS_FOLDER}" ]; then # make scripts executable incase they aren't - chmod -Rf 750 /var/www/html/scripts/*; sync; + chmod -Rf 750 ${SCRIPTS_FOLDER}/*; sync; # run scripts in number order - for i in `ls /var/www/html/scripts/`; do /var/www/html/scripts/$i ; done + for i in `ls ${SCRIPTS_FOLDER}/`; do ${SCRIPTS_FOLDER}/$i ; done else echo "Can't find script directory" fi @@ -203,13 +212,13 @@ fi if [ -z "$SKIP_COMPOSER" ]; then # Try auto install for composer - if [ -f "/var/www/html/composer.lock" ]; then + if [ -f "${WEBROOT}/composer.lock" ]; then if [ "$APPLICATION_ENV" == "development" ]; then composer global require hirak/prestissimo - composer install --working-dir=/var/www/html + composer install --working-dir=${WEBROOT} else composer global require hirak/prestissimo - composer install --no-dev --working-dir=/var/www/html + composer install --no-dev --working-dir=${WEBROOT} fi fi fi