Skip to content

Commit

Permalink
Basic apache implementation (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre-Lematre authored Jan 8, 2025
1 parent b258962 commit 68877bb
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 55 deletions.
22 changes: 22 additions & 0 deletions assets/000-default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<VirtualHost *:80>
DocumentRoot /var/www/html

<FilesMatch "\.php$">
SetHandler "proxy:unix:/var/run/php/php-fpm.sock|fcgi://localhost/"
</FilesMatch>

<Proxy "fcgi://localhost/" enablereuse=on flushpackets=on max=10>
</Proxy>

<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

ErrorLog /var/log/apache2/error.log
CustomLog /var/log/apache2/access.log combined

</VirtualHost>

ServerName localhost
79 changes: 66 additions & 13 deletions assets/alpine-base-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,80 @@
set -eu

# Install base tools, PHP requirements and dev-tools
apk --no-cache add -U \
bash less vim geoip git tzdata zip curl jq autoconf findutils \
nginx nginx-mod-http-headers-more nginx-mod-http-geoip \
nginx-mod-stream nginx-mod-stream-geoip ca-certificates \
packages="bash less vim geoip git tzdata zip curl jq autoconf findutils \
ca-certificates \
php-common php-iconv php-gd mariadb-client sudo libjpeg libxml2 \
build-base linux-headers freetype-dev zlib-dev libjpeg-turbo-dev \
libpng-dev oniguruma-dev libzip-dev icu-dev libmcrypt-dev libxml2-dev \
openssh-client libcap shadow
openssh-client libcap shadow"

if [ "$SERVER_FLAVOUR" = "nginx" ]; then
packages="$packages nginx nginx-mod-http-headers-more nginx-mod-http-geoip nginx-mod-stream nginx-mod-stream-geoip"
else
packages="$packages apache2 apache2-proxy"
fi

# shellcheck disable=SC2086
set -- $packages
apk --no-cache add -U "$@"

# Help mapping to Linux users' host
usermod -u 1000 www-data
groupmod -g 1000 www-data

# Configure php-fpm and nginx
# Configure php-fpm
/tmp/php-configuration.sh
rm -rf /var/log/php* /etc/php*/php-fpm.conf /etc/php*/php-fpm.d
mkdir -p /var/log/php /var/run/php /var/run/nginx /var/log/nginx /var/tmp/nginx
touch /var/log/nginx/access.log /var/log/nginx/error.log
chown -R www-data:www-data /var/log/php /var/run/php "$PHP_INI_DIR" \
/var/run/nginx /var/log/nginx /var/lib/nginx /var/tmp/nginx /var/opt/prestashop
setcap cap_net_bind_service=+ep /usr/sbin/nginx
mkdir -p /var/log/php /var/run/php
chown -R www-data:www-data /var/log/php /var/run/php "$PHP_INI_DIR" /var/opt/prestashop

# Compute the short version (8.1.27 becomes 8.1)
PHP_SHORT_VERSION=$(echo "$PHP_VERSION" | cut -d '.' -f1-2)
# Configure server
if [ "$SERVER_FLAVOUR" = "nginx" ]; then
rm -rf /etc/apache2
else
a2enmod() {
while test $# -gt 0; do
MODULE="$1"
echo "Enabling module $MODULE"
sed -i "/^#LoadModule ${MODULE}_module/s/^#//g" /etc/apache2/httpd.conf
shift
done
}

a2dismod() {
while test $# -gt 0; do
MODULE="$1"
echo "Disabling module $MODULE"
sed -i "/^LoadModule ${MODULE}_module/s/^LoadModule/#LoadModule/g" /etc/apache2/httpd.conf
shift
done
}

a2enmod proxy \
&& a2enmod proxy_fcgi \
&& a2enmod rewrite \
&& a2enmod mpm_event \
&& a2dismod mpm_prefork

echo "include /etc/apache2/sites-available/000-default.conf" >> /etc/apache2/httpd.conf
rm -rf /etc/nginx
fi

if [ "$SERVER_FLAVOUR" = "nginx" ]; then
mkdir -p /var/run/nginx /var/log/nginx /var/tmp/nginx
touch /var/log/nginx/access.log /var/log/nginx/error.log
chown -R www-data:www-data /var/run/nginx /var/log/nginx /var/tmp/nginx
chown -R www-data:www-data /var/lib/nginx
setcap cap_net_bind_service=+ep /usr/sbin/nginx
else
mkdir -p /var/run/apache2 /var/log/apache2 /var/tmp/apache2
touch /var/log/apache2/access.log /var/log/apache2/error.log
chown -R www-data:www-data /var/run/apache2 /var/log/apache2 /var/tmp/apache2
chown -R www-data:www-data /usr/lib/apache2
setcap cap_net_bind_service=+ep /usr/sbin/httpd
fi

rm -f "$PS_FOLDER"/index*

# Install composer
curl -s https://getcomposer.org/installer | php
Expand All @@ -36,6 +86,9 @@ chown -R www-data:www-data "$COMPOSER_HOME"
# Install PrestaShop tools required by prestashop coding-standards
composer require nikic/php-parser --working-dir=/var/opt || true

# Compute the short version (8.1.27 becomes 8.1)
PHP_SHORT_VERSION=$(echo "$PHP_VERSION" | cut -d '.' -f1-2)

# Install phpunit
PHPUNIT_VERSION=$(jq -r '."'"${PHP_SHORT_VERSION}"'".phpunit' < /tmp/php-flavours.json)
if [ "$PHPUNIT_VERSION" != "null" ]; then
Expand Down
51 changes: 40 additions & 11 deletions assets/debian-base-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,19 @@ curl -s -L -H "Content-Type: application/octet-stream" \
"https://packages.sury.org/php/apt.gpg"
apt-get update
apt-get install --no-install-recommends -qqy apt-transport-https ca-certificates
apt-get install --no-install-recommends -o Dpkg::Options::="--force-confold" -qqy bash less vim git sudo mariadb-client \
packages=(bash less vim git sudo mariadb-client \
tzdata zip unzip curl wget make jq netcat-traditional build-essential \
lsb-release libgnutls30 gnupg libiconv-hook1 libonig-dev nginx libnginx-mod-http-headers-more-filter libnginx-mod-http-geoip \
libnginx-mod-http-geoip libnginx-mod-stream openssh-client libcap2-bin;
lsb-release libgnutls30 gnupg libiconv-hook1 libonig-dev libnginx-mod-http-headers-more-filter libnginx-mod-http-geoip \
libnginx-mod-http-geoip libnginx-mod-stream openssh-client libcap2-bin)
if [ "$SERVER_FLAVOUR" = "nginx" ]; then
packages+=(nginx)
else
packages+=(apache2)
fi

apt-get install --no-install-recommends -o Dpkg::Options::="--force-confold" -qqy "${packages[@]}"


if [ "$VERSION_CODENAME" != "stretch" ] && [ "$VERSION_CODENAME" != "buster" ]; then
echo "deb [trusted=yes] https://packages.sury.org/php/ $VERSION_CODENAME main" > /etc/apt/sources.list.d/php.list
fi
Expand All @@ -59,17 +68,34 @@ apt-get install --no-install-recommends -qqy \
usermod -u 1000 www-data
groupmod -g 1000 www-data

# Configure php-fpm and nginx
# Configure php-fpm
/tmp/php-configuration.sh
rm -rf /var/log/php* /etc/php*/php-fpm.conf /etc/php*/php-fpm.d
mkdir -p /var/log/php /var/run/php /var/run/nginx /var/log/nginx /var/tmp/nginx
touch /var/log/nginx/access.log /var/log/nginx/error.log
chown -R www-data:www-data /var/log/php /var/run/php "$PHP_INI_DIR" \
/var/run/nginx /var/log/nginx /var/lib/nginx /var/tmp/nginx /var/opt/prestashop
setcap cap_net_bind_service=+ep /usr/sbin/nginx
mkdir -p /var/log/php /var/run/php
chown -R www-data:www-data /var/log/php /var/run/php "$PHP_INI_DIR" /var/opt/prestashop

# Configure server
if [ "$SERVER_FLAVOUR" = "nginx" ]; then
rm -rf /etc/apache2
else
a2enmod proxy \
&& a2enmod proxy_fcgi \
&& a2enmod rewrite
rm -rf /etc/nginx
fi
if [ "$SERVER_FLAVOUR" = "nginx" ]; then
mkdir -p /var/run/nginx /var/log/nginx /var/tmp/nginx
touch /var/log/nginx/access.log /var/log/nginx/error.log
chown -R www-data:www-data /var/run/nginx /var/log/nginx /var/tmp/nginx /var/lib/nginx
setcap cap_net_bind_service=+ep /usr/sbin/nginx
else
mkdir -p /var/run/apache2 /var/log/apache2 /var/tmp/apache2
touch /var/log/apache2/access.log /var/log/apache2/error.log
chown -R www-data:www-data /var/run/apache2 /var/log/apache2 /var/tmp/apache2 /var/lib/apache2
setcap cap_net_bind_service=+ep /usr/sbin/apache2
fi

# Compute the short version (8.1.27 becomes 8.1)
PHP_SHORT_VERSION=$(echo "$PHP_VERSION" | cut -d '.' -f1-2)
rm -f "$PS_FOLDER"/index*

# Install composer
curl -s https://getcomposer.org/installer | php
Expand All @@ -80,6 +106,9 @@ chown -R www-data:www-data "$COMPOSER_HOME"
# Install PrestaShop tools required by prestashop coding-standards
composer require nikic/php-parser --working-dir=/var/opt

# Compute the short version (8.1.27 becomes 8.1)
PHP_SHORT_VERSION=$(echo "$PHP_VERSION" | cut -d '.' -f1-2)

# Install phpunit
PHPUNIT_VERSION=$(jq -r '."'"${PHP_SHORT_VERSION}"'".phpunit' < /tmp/php-flavours.json)
if [ "$PHPUNIT_VERSION" != "null" ]; then
Expand Down
32 changes: 23 additions & 9 deletions assets/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export PS_PROTOCOL="${PS_PROTOCOL:-http}"
export SSL_REDIRECT="${SSL_REDIRECT:-false}"
export XDEBUG_ENABLED="${XDEBUG_ENABLED:-false}"


INIT_LOCK=/tmp/flashlight-init.lock
DUMP_LOCK=/tmp/flashlight-dump.lock
MODULES_INSTALLED_LOCK=/tmp/flashlight-modules-installed.lock
Expand Down Expand Up @@ -74,6 +75,11 @@ if [ ! -f $INIT_LOCK ] || [ "$INIT_ON_RESTART" = "true" ]; then
rm -f "$TMP_FILE"
export PS_DOMAIN="$PS_DOMAIN"

if [ "$SERVER_FLAVOUR" = "apache" ]; then
echo "* Applying PS_DOMAIN ($PS_DOMAIN) to root .htaccess..."
sed -i "s~localhost:80~$PS_DOMAIN~g" "$PS_FOLDER"/.htaccess
fi

# Note: use PS_TRUSTED_PROXIES for PrestaShop > 9 since bbdee4b6d07cf4c40787c95b8c948b04506208fd
# Note: PS_SSL_ENABLED_EVERYWHERE was missing in ps_configuration in 1.7.2.5
[ "$SSL_REDIRECT" = "true" ] && PS_PROTOCOL="https";
Expand Down Expand Up @@ -239,17 +245,25 @@ if [ "$DRY_RUN" = "true" ]; then
fi

echo "* Starting php-fpm..."
# Is running as root, set the php-fpm user and group to www-data
# If running as root, set the php-fpm user and group to www-data
[ "$(id -u)" -eq 0 ] && sed -i '/user\s=/s/^;//' /usr/local/etc/php-fpm.conf && sed -i '/group\s=/s/^;//' /usr/local/etc/php-fpm.conf
php-fpm -D

echo "* Starting nginx..."
# Is running as root, set the nginx user and group to www-data
[ "$(id -u)" -eq 0 ] && sed -i '/#\suser\swww-data/s/^#//' /etc/nginx/nginx.conf
nginx -g "daemon off;" &
NGINX_PID=$!
echo "* Starting $SERVER_FLAVOUR..."
if [ "$SERVER_FLAVOUR" = "nginx" ]; then
# If running as root, set the nginx user and group to www-data
[ "$(id -u)" -eq 0 ] && sed -i '/#\suser\swww-data/s/^#//' /etc/nginx/nginx.conf
nginx -g "daemon off;" &
elif service --status-all 2> /dev/null | grep -Fq 'apache2'; then
apache2ctl -D FOREGROUND &
else
# If running as root, set the nginx user and group to www-data
[ "$(id -u)" -eq 0 ] && sed -i '/User apache/s/apache/www-data/' /etc/apache2/httpd.conf && sed -i '/Group apache/s/apache/www-data/' /etc/apache2/httpd.conf
/usr/sbin/httpd -D FOREGROUND -f /etc/apache2/httpd.conf &
fi
SERVER_PID=$!
sleep 1;
echo "* Nginx started"
echo "* $SERVER_FLAVOUR started"

# Post-run scripts
if [ -d "$POST_SCRIPTS_DIR" ]; then
Expand Down Expand Up @@ -279,5 +293,5 @@ else
echo "* No post-script(s) found"
fi

# set back nginx to front process
wait $NGINX_PID
# set back server to front process
wait "$SERVER_PID"
Loading

0 comments on commit 68877bb

Please sign in to comment.