diff --git a/.env b/.env index 7c44926..1b6a156 100644 --- a/.env +++ b/.env @@ -2,6 +2,10 @@ # See https://docs.docker.com/compose/environment-variables/#the-env-file +# Nginx +NGINX_HOST=localhost + +# MySQL MYSQL_HOST=mysql MYSQL_DATABASE=test MYSQL_ROOT_USER=root diff --git a/Makefile b/Makefile index 4aca9df..459e8b7 100644 --- a/Makefile +++ b/Makefile @@ -53,7 +53,7 @@ docker-stop: @make clean gen-certs: - @docker run --rm -v $(shell pwd)/etc/ssl:/certificates -e "SERVER=localhost" jacoelho/generate-certificate + @docker run --rm -v $(shell pwd)/etc/ssl:/certificates -e "SERVER=$(NGINX_HOST)" jacoelho/generate-certificate logs: @docker-compose logs -f diff --git a/docker-compose.yml b/docker-compose.yml index f811cf3..419b3ca 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,15 +1,19 @@ version: '3' services: web: - image: nginx:latest - ports: - - "8000:80" - - "3000:443" - restart: always + image: nginx volumes: - "./etc/nginx/default.conf:/etc/nginx/conf.d/default.conf" - "./etc/ssl:/etc/ssl" - "./web:/var/www/html" + - "./etc/nginx/default.template:/etc/nginx/conf.d/default.template" + ports: + - "8000:80" + - "3000:443" + environment: + - NGINX_HOST=${NGINX_HOST} + command: /bin/bash -c "envsubst '$$NGINX_HOST' < /etc/nginx/conf.d/default.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'" + restart: always depends_on: - php - mysqldb @@ -49,4 +53,4 @@ services: ports: - "8989:3306" volumes: - - "./data/db/mysql:/var/lib/mysql" + - "./data/db/mysql:/var/lib/mysql" \ No newline at end of file diff --git a/etc/nginx/default.conf b/etc/nginx/default.conf index b62e955..81fb278 100644 --- a/etc/nginx/default.conf +++ b/etc/nginx/default.conf @@ -21,28 +21,28 @@ server { } } -# server { -# server_name localhost; +server { + server_name localhost; -# listen 443 ssl; -# fastcgi_param HTTPS on; + listen 443 ssl; + fastcgi_param HTTPS on; -# ssl_certificate /etc/ssl/server.pem; -# ssl_certificate_key /etc/ssl/server.key; -# ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; + ssl_certificate /etc/ssl/server.pem; + ssl_certificate_key /etc/ssl/server.key; + ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; -# index index.php index.html; -# error_log /var/log/nginx/error.log; -# access_log /var/log/nginx/access.log; -# root /var/www/html/public; + index index.php index.html; + error_log /var/log/nginx/error.log; + access_log /var/log/nginx/access.log; + root /var/www/html/public; -# location ~ \.php$ { -# try_files $uri =404; -# fastcgi_split_path_info ^(.+\.php)(/.+)$; -# fastcgi_pass php:9000; -# fastcgi_index index.php; -# include fastcgi_params; -# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; -# fastcgi_param PATH_INFO $fastcgi_path_info; -# } -# } + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass php:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } +} \ No newline at end of file diff --git a/etc/nginx/default.template b/etc/nginx/default.template new file mode 100644 index 0000000..f8d4819 --- /dev/null +++ b/etc/nginx/default.template @@ -0,0 +1,48 @@ +# Nginx configuration + +server { + listen 80 default_server; + listen [::]:80 default_server; + server_name ${NGINX_HOST}; + + index index.php index.html; + error_log /var/log/nginx/error.log; + access_log /var/log/nginx/access.log; + root /var/www/html/public; + + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass php:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } +} + +server { + server_name ${NGINX_HOST}; + + listen 443 ssl; + fastcgi_param HTTPS on; + + ssl_certificate /etc/ssl/server.pem; + ssl_certificate_key /etc/ssl/server.key; + ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; + + index index.php index.html; + error_log /var/log/nginx/error.log; + access_log /var/log/nginx/access.log; + root /var/www/html/public; + + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass php:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } +} \ No newline at end of file