From 3cbb36eb663b70969df456d78fa847afeb804974 Mon Sep 17 00:00:00 2001 From: "Junior L. Botelho (JLB)" Date: Sun, 12 Feb 2023 11:13:58 -0300 Subject: [PATCH] chore: configured Nginx Dockerfile, updated PHP requirements in the PHP Dockerfile, and made other changes --- Dockerfile | 4 ++-- docker/attributes.sh | 3 ++- docker/php/php.dockerfile | 8 +++----- docker/php/prepare.sh | 4 ++-- docker/server/fastcgi.conf | 26 ++++++++++++++++++++++++++ docker/server/nginx.conf | 3 +-- docker/server/nginx.dockerfile | 10 +++++++++- docker/server/prepare.sh | 8 ++++++++ 8 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 docker/server/fastcgi.conf diff --git a/Dockerfile b/Dockerfile index 25a059d..4ce97f2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # syntax = edrevo/dockerfile-plus ARG VERSION="3.17" FROM alpine:${VERSION} AS runner -WORKDIR "/home/librex" +WORKDIR "/var/www/html" # Docker metadata contains information about the maintainer, such as the name, repository, and support email # Please add any necessary information or correct any incorrect information @@ -24,7 +24,7 @@ ARG DOCKER_SCRIPTS="docker" ENV TZ="America/New_York" # Include docker scripts, docker images, and the 'GNU License' in the Librex container -ADD "." "/home/librex" +ADD "." "/var/www/html" # Set permissions for script files as executable scripts inside 'docker/scripts' directory RUN chmod u+x "${DOCKER_SCRIPTS}/php/prepare.sh" &&\ diff --git a/docker/attributes.sh b/docker/attributes.sh index 1ea69fb..db05452 100755 --- a/docker/attributes.sh +++ b/docker/attributes.sh @@ -7,6 +7,7 @@ # If these locations do not already exist within the Docker container, they will be created export CONFIG_PHP_TEMPLATE="$(pwd)/config.php" export CONFIG_OPEN_SEARCH_TEMPLATE="$(pwd)/opensearch.xml" +export CONFIG_NGINX_TEMPLATE="/etc/nginx/http.d/librex.conf" # Configure 'opensearch.xml' with Librex configuration metadata, such as the encoding and the host that stores the site # These configurations will replace the 'opensearch.xml' inside '.dockers/templates' for the best setup for your instance @@ -14,7 +15,7 @@ export OPEN_SEARCH_TITLE=${OPEN_SEARCH_TITLE:-"LibreX"} export OPEN_SEARCH_DESCRIPTION=${OPEN_SEARCH_DESCRIPTION:-"Framework and javascript free privacy respecting meta search engine"} export OPEN_SEARCH_ENCODING=${OPEN_SEARCH_ENCODING:-"UTF-8"} export OPEN_SEARCH_LONG_NAME=${OPEN_SEARCH_LONG_NAME:-"LibreX Search"} -export OPEN_SEARCH_HOST=${OPEN_SEARCH_HOST:-"http://localhost:80"} +export OPEN_SEARCH_HOST=${OPEN_SEARCH_HOST:-"127.0.0.1"} # Replace the 'config.php' script, which contains the most common search engine configurations, with these environment setups # These environment setups can be found in 'config.php', and the default configurations can be useful for most use cases diff --git a/docker/php/php.dockerfile b/docker/php/php.dockerfile index e8ca94e..61387ab 100644 --- a/docker/php/php.dockerfile +++ b/docker/php/php.dockerfile @@ -7,7 +7,7 @@ ENV OPEN_SEARCH_TITLE="LibreX" ENV OPEN_SEARCH_DESCRIPTION="Framework and javascript free privacy respecting meta search engine" ENV OPEN_SEARCH_ENCODING="UTF-8" ENV OPEN_SEARCH_LONG_NAME="LibreX Search" -ENV OPEN_SEARCH_HOST="http://localhost:80" +ENV OPEN_SEARCH_HOST="127.0.0.1" # Replace the 'config.php' script, which contains the most common search engine configurations, with these environment setups # These environment setups can be found in 'config.php', and the default configurations can be useful for most use cases @@ -49,12 +49,10 @@ ENV CURLOPT_VERBOSE=false # Install PHP-FPM using Alpine's package manager, apk # Configure PHP-FPM to listen on a Unix socket instead of a TCP port, which is more secure and efficient -RUN apk add php7 php7-fpm abuild-rootbld --no-cache --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing &&\ +RUN apk add php7 php7-fpm php7-dom php7-curl php7-xml abuild-rootbld --no-cache --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing &&\ sed -i 's/^\s*listen = 127.0.0.1:9000/listen = \/run\/php-fpm\/php-fpm.sock/' ${WWW_CONFIG} &&\ sed -i 's/^\s*;\s*listen.owner = nobody/listen.owner = nginx/' ${WWW_CONFIG} &&\ sed -i 's/^\s*;\s*listen.group = nobody/listen.group = nginx/' ${WWW_CONFIG} &&\ sed -i 's/^\s*;\s*listen.mode = 0660/listen.mode = 0660/' ${WWW_CONFIG} - -RUN echo $(pwd) - + RUN docker/php/prepare.sh diff --git a/docker/php/prepare.sh b/docker/php/prepare.sh index 0809fd1..0a84a12 100755 --- a/docker/php/prepare.sh +++ b/docker/php/prepare.sh @@ -5,8 +5,8 @@ source "docker/attributes.sh" # The lines below will replace the environment variables in the templates with the corresponding variables listed above. To accomplish this, the GNU 'envsubst' package will be used # Although not recommended (if you do not know what you are doing), you still have the option to add new substitution file templates using any required environment variables -[[ ! -s ${CONFIG_PHP_TEMPLATE} ]] && cat 'docker/php/config.php' | envsubst | AwkTrim > ${CONFIG_PHP_TEMPLATE}; -[[ ! -s ${CONFIG_OPEN_SEARCH_TEMPLATE} ]] && cat 'docker/php/opensearch.xml' | envsubst | AwkTrim > ${CONFIG_OPEN_SEARCH_TEMPLATE}; +[[ ! -s ${CONFIG_PHP_TEMPLATE} ]] && cat 'docker/php/config.php' | envsubst > ${CONFIG_PHP_TEMPLATE}; +[[ ! -s ${CONFIG_OPEN_SEARCH_TEMPLATE} ]] && cat 'docker/php/opensearch.xml' | envsubst > ${CONFIG_OPEN_SEARCH_TEMPLATE}; # If it is empty or proxy is not enabled, we are using sed to delete # any line that contains the string 'CURLOPT_PROXY' or 'CURLOPT_PROXYTYPE' diff --git a/docker/server/fastcgi.conf b/docker/server/fastcgi.conf new file mode 100644 index 0000000..7e453dc --- /dev/null +++ b/docker/server/fastcgi.conf @@ -0,0 +1,26 @@ +# These settings should work well for serving as a front-end of many search engines +# on our PHP website. However, we may need to adjust them based on our specific requirements. +fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; +fastcgi_param QUERY_STRING $query_string; +fastcgi_param REQUEST_METHOD $request_method; +fastcgi_param CONTENT_TYPE $content_type; +fastcgi_param CONTENT_LENGTH $content_length; + +fastcgi_param SCRIPT_NAME $fastcgi_script_name; +fastcgi_param REQUEST_URI $request_uri; +fastcgi_param DOCUMENT_URI $document_uri; +fastcgi_param DOCUMENT_ROOT $document_root; +fastcgi_param SERVER_PROTOCOL $server_protocol; + +fastcgi_param GATEWAY_INTERFACE CGI/1.1; +fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; + +fastcgi_param REMOTE_ADDR $remote_addr; +fastcgi_param REMOTE_PORT $remote_port; +fastcgi_param SERVER_ADDR $server_addr; +fastcgi_param SERVER_PORT $server_port; +fastcgi_param SERVER_NAME $server_name; + +# PHP only, required if PHP was built with --enable-force-cgi-redirect +fastcgi_param HTTPS $https if_not_empty; +fastcgi_param REDIRECT_STATUS 200; diff --git a/docker/server/nginx.conf b/docker/server/nginx.conf index ff2d9db..e99910c 100644 --- a/docker/server/nginx.conf +++ b/docker/server/nginx.conf @@ -12,7 +12,6 @@ server { location ~ \.php$ { fastcgi_pass unix:/run/php-fpm/php-fpm.sock; fastcgi_index index.php; - fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; - include fastcgi_params; + include fastcgi.conf; } } diff --git a/docker/server/nginx.dockerfile b/docker/server/nginx.dockerfile index 86de060..327a640 100644 --- a/docker/server/nginx.dockerfile +++ b/docker/server/nginx.dockerfile @@ -1 +1,9 @@ -RUN echo "Hello Nginx" +RUN apk add nginx + +ADD "docker/server/fastcgi.conf" /etc/nginx/fastcgi.conf +ADD "docker/server/nginx.conf" /etc/nginx/http.d/librex.conf + +RUN chmod u+x "/etc/nginx/fastcgi.conf" &&\ + chmod u+x "/etc/nginx/http.d/librex.conf" + +RUN docker/server/prepare.sh diff --git a/docker/server/prepare.sh b/docker/server/prepare.sh index e69de29..fdf26cf 100755 --- a/docker/server/prepare.sh +++ b/docker/server/prepare.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +# Load all environment variables from 'attributes.sh' using the command 'source /path/attributes.sh' +source "docker/attributes.sh" + +# The lines below will replace the environment variables in the templates with the corresponding variables listed above. To accomplish this, the GNU 'envsubst' package will be used +# Although not recommended (if you do not know what you are doing), you still have the option to add new substitution file templates using any required environment variables +exec cat 'docker/server/nginx.conf' | envsubst > ${CONFIG_NGINX_TEMPLATE};