From 71688f4384c8d6290f4abe265a5bf610f69e70bc Mon Sep 17 00:00:00 2001 From: Michael RICOIS Date: Tue, 27 Mar 2018 12:49:28 +0200 Subject: [PATCH] DATA path, vhost automatically enable --- README.md | 18 +++++--- compose/docker-compose-2.sample.yml | 20 +++------ compose/env.sample | 1 + dockerfiles/httpd/2.4/debian-9/Dockerfile | 23 ++++++---- dockerfiles/httpd/2.4/debian-9/run.sh | 4 ++ dockerfiles/httpd/2.4/debian-9/vhost.sh | 43 +++++++++++++++++++ .../httpd/2.4/debian-9/vhosts/test.conf | 40 +++++++++++++++++ .../mariadb/10.2/official/init_user.sh | 6 +-- 8 files changed, 124 insertions(+), 31 deletions(-) create mode 100644 dockerfiles/httpd/2.4/debian-9/vhost.sh create mode 100644 dockerfiles/httpd/2.4/debian-9/vhosts/test.conf diff --git a/README.md b/README.md index 456ed8f..affb588 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,11 @@ Windows: ```C:\\worspace``` Always go in `/devcontainer` to execute docker-compose command. +Create a `.env` file and add configuration vars: +- DATA : where you store database, vhost, ... +- WORKSPACE : your workspace files + + ## Launch services (first time build) ```bash docker-compose up -d @@ -71,6 +76,8 @@ docker-compose exec ``` ## PHP-FPM & Apache vhosts +Create a VHOST file in `/httpd/`. Add this to your vhost to connect php. + ``` # Redirect to local php-fpm if mod_php is not available @@ -81,7 +88,7 @@ docker-compose exec - SetHandler "proxy:fcgi://fpm70/:9000 + SetHandler "proxy:fcgi://fpm70:9000 # Deny access to raw php sources by default @@ -96,16 +103,15 @@ docker-compose exec ``` +Don't forget to create the directory in your workspace. +To enable the new vhosts, simply stop an start the `httpd` container (`docker-compose stop && docker-compose start`). ## Database -Database files are store in `/database`. -Even if you delete your container date are preserved. +Database files are store in `/`. +Even if you delete your container, all data are preserved. Read the doc official MariaDB: `https://hub.docker.com/_/mariadb/` -## HTTPd - - # Additional ## Ip Address diff --git a/compose/docker-compose-2.sample.yml b/compose/docker-compose-2.sample.yml index b929bed..4f6364e 100644 --- a/compose/docker-compose-2.sample.yml +++ b/compose/docker-compose-2.sample.yml @@ -1,4 +1,4 @@ -version: '2.3' +version: '3' services: # PHP 5.6 FPM fpm56: @@ -30,7 +30,7 @@ services: image: mailcatcher:0.6.5 container_name: mailcatcher ports: - - '1080:1080' + - '1080:1080' logging: driver: 'json-file' options: @@ -44,14 +44,15 @@ services: ports: - '3306:3306' volumes: - - ${WORKSPACE}/container-data/db:/var/lib/mysql + - ${DATA}/db:/var/lib/mysql logging: driver: 'json-file' options: max-size: '500m' max-file: '9' environment: - MYSQL_ROOT_PASSWORD: 'password' + MYSQL_ALLOW_EMPTY_PASSWORD: 'yes' + MYSQL_ADMIN_PASSWORD: 'password' # Apache HTTPd httpd: build: ./dockerfiles/httpd/2.4/debian-9 @@ -60,19 +61,10 @@ services: ports: - '80:80' volumes: - - ${WORKSPACE}/container-data/httpd:/etc/apache2/sites-available + - ${DATA}/httpd:/sites-available - ${WORKSPACE}:/home/vhosts logging: driver: 'json-file' options: max-size: '1g' max-file: '10' - -networks: - default: - driver: bridge - ipam: - driver: default - config: - - subnet: 172.18.0.0/16 - gateway: 172.18.0.1 diff --git a/compose/env.sample b/compose/env.sample index 8f8f7a5..44c8b6c 100644 --- a/compose/env.sample +++ b/compose/env.sample @@ -1 +1,2 @@ WORKSPACE=/home/ubuntu/workspace +DATA=/home/ubuntu/cdata diff --git a/dockerfiles/httpd/2.4/debian-9/Dockerfile b/dockerfiles/httpd/2.4/debian-9/Dockerfile index 0205a41..7350e8c 100644 --- a/dockerfiles/httpd/2.4/debian-9/Dockerfile +++ b/dockerfiles/httpd/2.4/debian-9/Dockerfile @@ -6,22 +6,29 @@ ENV DEBIAN_FRONTEND noninteractive ENV TZ=Europe/Paris # Installation -RUN apt-get update && apt-get -y upgrade && apt-get -y dist-upgrade && \ - apt-get -y install nano apache2 libapache2-mod-xsendfile && \ - echo "ServerName 127.0.0.1" > /etc/apache2/conf-available/httpd.conf && \ - mkdir /var/run/apache2 && mkdir /var/lock/apache2 && \ - rm -f /var/log/apache2/error.log && ln -s /dev/stderr /var/log/apache2/error.log && \ - rm -f /var/log/apache2/access.log && ln -s /dev/stdout /var/log/apache2/access.log +RUN apt-get update && apt-get -y upgrade && apt-get -y dist-upgrade \ + && apt-get -y install nano apache2 libapache2-mod-xsendfile \ + && echo "ServerName 127.0.0.1" > /etc/apache2/conf-available/httpd.conf \ + && mkdir /var/run/apache2 && mkdir /var/lock/apache2 \ + && rm -f /var/log/apache2/error.log && ln -s /dev/stderr /var/log/apache2/error.log \ + && rm -f /var/log/apache2/access.log && ln -s /dev/stdout /var/log/apache2/access.log \ + && echo "Europe/Paris" > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata \ + && apt-get clean && rm -rf /var/lib/apt/lists/* && mkdir /docker-entrypoint.d && mkdir /sites-available RUN perl /usr/sbin/a2enconf httpd RUN perl /usr/sbin/a2enmod autoindex deflate expires headers rewrite actions proxy proxy_fcgi -RUN apt-get clean && rm -rf /var/lib/apt/lists/* + +ADD vhosts/test.conf /etc/apache2/sites-available/ ADD run.sh /run.sh RUN chmod +x /run.sh +# Entrypoint +ADD vhost.sh /docker-entrypoint.d/ +RUN chmod 775 /docker-entrypoint.d/vhost.sh + # Add VOLUMEs -VOLUME ["/home/vhosts"] +VOLUME ["/home/vhosts", "/sites-available"] # Expose and Run EXPOSE 80 diff --git a/dockerfiles/httpd/2.4/debian-9/run.sh b/dockerfiles/httpd/2.4/debian-9/run.sh index 6f010d5..df0c7f2 100644 --- a/dockerfiles/httpd/2.4/debian-9/run.sh +++ b/dockerfiles/httpd/2.4/debian-9/run.sh @@ -4,5 +4,9 @@ set -e # Apache gets grumpy about PID files pre-existing rm -f /usr/local/apache2/logs/httpd.pid +if [ -f /docker-entrypoint.d/vhost.sh ]; then + /docker-entrypoint.d/vhost.sh +fi + source /etc/apache2/envvars exec apache2 -D FOREGROUND diff --git a/dockerfiles/httpd/2.4/debian-9/vhost.sh b/dockerfiles/httpd/2.4/debian-9/vhost.sh new file mode 100644 index 0000000..95cf1c1 --- /dev/null +++ b/dockerfiles/httpd/2.4/debian-9/vhost.sh @@ -0,0 +1,43 @@ +#!/bin/bash +set -e + +# --- Disable all website +cd /etc/apache2/sites-available/ +for f in *.conf; do + if [ $f = "000-default.conf" ]; then + continue + fi + if [ $f = "default-ssl.conf" ]; then + continue + fi + if [ $f = "test.conf" ]; then + continue + fi + a2dissite $f; + rm -vf /etc/apache2/sites-available/$f; +done; + +if [ ! -d /home/vhosts/test ]; then + mkdir /home/vhosts/test +fi + +# --- Copy Vhosts files +VHOSTS='000-default.conf test.conf' +cd /etc/apache2/sites-available/ +for f in *.conf; do + if [ $f = "000-default.conf" ]; then + continue + fi + if [ $f = "default-ssl.conf" ]; then + continue + fi + if [ $f = "test.conf" ]; then + continue + fi + if [ -f $f ]; then + cp -vf /sites-available/$f /etc/apache2/sites-available/ + VHOSTS+=" $f" + fi +done; + +a2ensite $VHOSTS diff --git a/dockerfiles/httpd/2.4/debian-9/vhosts/test.conf b/dockerfiles/httpd/2.4/debian-9/vhosts/test.conf new file mode 100644 index 0000000..d019002 --- /dev/null +++ b/dockerfiles/httpd/2.4/debian-9/vhosts/test.conf @@ -0,0 +1,40 @@ + + ServerName test.local + ServerAdmin webmaster@localhost + DocumentRoot /home/vhosts/test + + # Redirect to local php-fpm if mod_php is not available + + + # Enable http authorization headers + + + SetEnvIfNoCase Authorization "(.+)" HTTP_AUTHORIZATION=$1 + + + + + SetHandler "proxy:fcgi://fpm70:9000 + + + # Deny access to raw php sources by default + # To re-enable it's recommended to enable access to the files + # only in specific virtual host or directory + Require all denied + + # Deny access to files without filename (e.g. '.php') + + Require all denied + + + + + + Options FollowSymLinks Indexes + AllowOverride all + Require all granted + + + ErrorLog ${APACHE_LOG_DIR}/error.log + CustomLog ${APACHE_LOG_DIR}/access.log combined + diff --git a/dockerfiles/mariadb/10.2/official/init_user.sh b/dockerfiles/mariadb/10.2/official/init_user.sh index 7fd9747..429dcf0 100644 --- a/dockerfiles/mariadb/10.2/official/init_user.sh +++ b/dockerfiles/mariadb/10.2/official/init_user.sh @@ -1,10 +1,10 @@ #!/bin/bash -PASS=${MYSQL_ROOT_PASSWORD:-$(pwgen -s 12 1)} -_word=$( [ ${MYSQL_ROOT_PASSWORD} ] && echo "preset" || echo "random" ) +PASS=${MYSQL_ADMIN_PASSWORD:-$(pwgen -s 12 1)} +_word=$( [ ${MYSQL_ADMIN_PASSWORD} ] && echo "preset" || echo "random" ) echo "=> Creating MariaDB admin user with ${_word} password" -mysql -uroot -e "CREATE USER 'admin'@'%' IDENTIFIED BY '$PASS'" +mysql -uroot -e "CREATE USER IF NOT EXISTS 'admin'@'%' IDENTIFIED BY '$PASS'" mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION" echo "=> Done!"