Skip to content

Commit

Permalink
feat: wp within the container (#150)
Browse files Browse the repository at this point in the history
**Refactor Acore-CMS: Docker Compose as the Sole Installation Method**

This pull request introduces a significant refactoring of Acore-CMS to standardize **Docker Compose** as the only supported installation method. By removing the WordPress sources (a common cause of conflicts with other forks) from the repository, we simplify the setup process and ensure seamless installation of WordPress and required plugins via WP-CLI.

### **Changes**
- Removed WordPress sources from the repository.
- Moved `acore-wp-plugin` under the `src` folder.
- Implemented automatic installation of required plugins (e.g., WooCommerce, myCred, WP-GraphQL).
- Fixed Redis integration.
- Set `wordpress:6.7.1-php8.3-fpm` as the default WordPress version.
- Updated README.md
  • Loading branch information
Yehonal authored Dec 31, 2024
1 parent c698a05 commit e211805
Show file tree
Hide file tree
Showing 5,651 changed files with 309 additions and 1,600,764 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
12 changes: 11 additions & 1 deletion .env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@
# related docker-compose.yml configurations
#


# DOCKER_WORDPRESS_URL=http://localhost
# DOCKER_WORDPRESS_TITLE=ACoreCMS
# DOCKER_WORDPRESS_ADMIN_USER=admin
# DOCKER_WORDPRESS_ADMIN_PASSWORD=admin
# DOCKER_WORDPRESS_ADMIN_EMAIL=admin@example.com

# DOCKER_AC_NETWORK_EXTERNAL=true

# DOCKER_USER_ID=1000
# DOCKER_GROUP_ID=1000
# DOCKER_HTTP_PORTS=80:80
# DOCKER_HTTPS_PORTS=443:443
# DOCKER_CONF_NGINX_PATH=./conf/dist/nginx-conf
# DOCKER_CONF_CERTS_PATH=./conf/dist/certs/
# DOCKER_CONF_PHP_PATH=./conf/dist/php-conf/upload.ini
# DOCKER_AC_NETWORK_EXTERNAL=true


20 changes: 3 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
# exclude everything ...
*
# ...except
!/.gitignore
!*/

!/**
/srv/*
!/srv/wordpress
/srv/wordpress/wp-config.php
/srv/wordpress/wp-content/*
!/srv/wordpress/wp-content/plugins
/srv/wordpress/wp-content/plugins/*
!/srv/wordpress/wp-content/plugins/askimet
!/srv/wordpress/wp-content/plugins/acore-wp-plugin
!/srv/wordpress/wp-content/plugins/wp-graph*
!/srv/wordpress/wp-content/themes/
/srv/wordpress/wp-content/themes/*
!/srv/wordpress/wp-content/themes/twenty*

/var
/srv

/conf/*
!/conf/dist


/docker-compose.*.yml
!docker-compose.default.yml

Expand Down Expand Up @@ -67,7 +52,8 @@ patches-*
# exclude in all levels
nbproject/private/
.sync.ffs_db
/.vscode/
/.vscode/*
!/.vscode/extensions.json

#
# Eclipse
Expand Down
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"xdebug.php-pack"
]
}
25 changes: 25 additions & 0 deletions apps/export-src.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

docker compose stop

docker_container_name="php"

# Get the container ID for the 'php' service (running or stopped)
php_container_id=$(docker ps -a -q --filter "name=-$docker_container_name")

# Check if the container exists (even if stopped)
if [ -z "$php_container_id" ]; then
echo "No container found for the '$docker_container_name' service (running or stopped)."
exit 1
fi

# Retrieve the full container name
php_container_name=$(docker inspect -f '{{.Name}}' "$php_container_id" | sed 's/^\/\+//')

# Extract the prefix from the container name (everything before '_php')
php_container_prefix=$(echo "$php_container_name" | sed "s/-$docker_container_name.*//")

echo "Container Prefix: $php_container_prefix"

# Use the prefix for volume operations
docker run --rm -v "${php_container_prefix}_wordpress-src:/var/www/html" -v "$(pwd)/srv:/srv" alpine sh -c "cp -r /srv/* /var/www/html/ && ls -l /var/www/html/"
47 changes: 47 additions & 0 deletions apps/init/init.lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

# Function to check if a plugin is installed
is_plugin_installed() {
wp plugin is-installed "$1" --allow-root
return $?
}

# Function to install and activate a plugin if not already installed
install_and_activate_plugin() {
local plugin_name="$1"
local plugin_slug="$2"

if ! is_plugin_installed "$plugin_slug"; then
echo "Installing and activating plugin: $plugin_name..."
wp plugin install "$plugin_slug" --activate --allow-root
else
echo "Plugin $plugin_name is already installed."
fi
}

# Special handling for plugin activations that should only be done once
handle_plugin_activation_once() {
local plugin_slug="$1"
local activation_flag="/usr/src/wordpress/.${plugin_slug}.activated"

if [ -f "$activation_flag" ]; then
echo "Plugin $1 has already been activated; skipping."
return
fi

if is_plugin_installed "$plugin_slug"; then
if ! wp plugin is-active "$plugin_slug" --allow-root; then
echo "Activating plugin: $1..."
wp plugin activate "$plugin_slug" --allow-root
# Mark as activated
touch "$activation_flag"
else
echo "Plugin $1 is already active."
# Mark as activated if not marked already
touch "$activation_flag"
fi
else
echo "Plugin $1 is not installed; skipping."
fi
}

78 changes: 78 additions & 0 deletions apps/init/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/bash

# List of plugins to install
# Array semplice con nome e slug separati da "|"
plugins_install=(
"WooCommerce|woocommerce"
"WPGraphQL|wp-graphql"
"WPGraphQL ACF|wpgraphql-acf"
"myCred|mycred"
"Advanced Custom Fields|advanced-custom-fields"
)

# List of plugins to activate only once
plugins_activate_only=(
"ACore WP Plugins|acore-wp-plugins"
)

CURPATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# Wait for the database to be available
echo "Waiting for database to be ready..."
until nc -z -v -w30 wp-db 3306; do
echo "Waiting for database connection..."
sleep 5
done
echo "Database is ready!"

# Execute the entrypoint script with the php-fpm command (use -v to only print the version and exit)
bash /usr/local/bin/docker-entrypoint.sh "php-fpm" "-v"

# Create the WordPress configuration file if it doesn't exist
if [ ! -f /var/www/html/wp-config.php ]; then
echo "Creating wp-config.php..."
wp config create --dbname=wordpress --dbuser=wordpress --dbpass=wordpress --dbhost=wp-db --allow-root
fi

# Perform the initial WordPress installation if it hasn't been installed yet
if ! wp core is-installed --allow-root; then
echo "Installing WordPress..."
wp core install --url="$WORDPRESS_URL" --title="$WORDPRESS_TITLE" --admin_user="$WORDPRESS_ADMIN_USER" --admin_password="$WORDPRESS_ADMIN_PASSWORD" --admin_email="$WORDPRESS_ADMIN_EMAIL" --allow-root
fi

wp maintenance-mode activate --allow-root

# Install and activate plugins
echo "Installing and activating plugins..."

source "$CURPATH/init.lib.sh"

# Install and activate each plugin in the list
for plugin in "${plugins_install[@]}"; do
# Dividi il nome e lo slug
IFS='|' read -r plugin_name plugin_slug <<< "$plugin"

echo "Installing $plugin_name ($plugin_slug)..."
install_and_activate_plugin "$plugin_name" "$plugin_slug"
done

# Handle Acore WP Plugins activation
for plugin in "${plugins_activate_only[@]}"; do
IFS='|' read -r plugin_name plugin_slug <<< "$plugin"

echo "Activating $plugin_name ($plugin_slug) only once..."
handle_plugin_activation_once "$plugin_slug"
done

# Correct permissions for non-root operations
chown -R www-data:www-data /run /var/www/html/

setfacl -R -m u:$DOCKER_USER_ID:rwx /var/www/html/
setfacl -R -d -m u:$DOCKER_USER_ID:rwx /var/www/html/

# Start a proxy from 127.0.0.1:6379 to the Redis container
socat TCP-LISTEN:6379,fork TCP:redis:6379 &

wp maintenance-mode deactivate --allow-root

exec "php-fpm"
18 changes: 15 additions & 3 deletions data/docker/Wordpress.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM wordpress:6-php8.3-fpm
FROM wordpress:6.7.1-php8.3-fpm

ARG USER_ID=1000
ARG GROUP_ID=1000
Expand All @@ -19,6 +19,11 @@ RUN apt-get update -y \
libgmp-dev \
redis-tools \
procps \
wget \
netcat-openbsd \
sendmail \
socat \
acl \
&& apt-get clean -y

# Install PHP extensions
Expand All @@ -40,11 +45,18 @@ RUN pecl install redis \
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN wget --no-check-certificate -O /usr/local/bin/wp \
https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \
&& chmod +x /usr/local/bin/wp

# Add init script for runtime configuration
COPY apps/init /usr/local/bin/acore-init
RUN chmod +x /usr/local/bin/acore-init/init.sh

# Remove and re-add www-data user with specified UID and GID
RUN deluser www-data \
&& addgroup --gid $GROUP_ID www-data \
&& adduser --disabled-password --gecos '' --uid $USER_ID --gid $GROUP_ID www-data \
&& passwd -d www-data

# Correct permissions for non-root operations
RUN chown -R www-data:www-data /run /var/www/html
ENTRYPOINT ["bash", "/usr/local/bin/acore-init/init.sh"]
25 changes: 22 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,36 @@ services:
depends_on:
wp-db:
condition: service_healthy
working_dir: "/var/www/html"
redis:
condition: service_started
volumes:
- type: bind
source: ${DOCKER_CONF_PHP_PATH:-./conf/dist/php-conf/upload.ini}
target: /usr/local/etc/php/conf.d/upload.ini
- ./srv/wordpress:/var/www/html
- ./src/acore-wp-plugin:/var/www/html/wp-content/plugins/acore-wp-plugins
- ${DOCKER_WORDPRESS_SRC_PATH:-wordpress-src}:/var/www/html
- ./apps/init/:/usr/local/bin/acore-init/
env_file:
# environment variables are retrieved by this file
# NOTE: you can add more variables to the .env file but
# you cannot override the .env.docker ones (by design)
- .env.docker
environment:
DOCKER_CONTAINER: 1
WORDPRESS_URL: ${DOCKER_WORDPRESS_URL:-http://localhost}
WORDPRESS_TITLE: ${DOCKER_WORDPRESS_TITLE:-ACoreCMS}
WORDPRESS_ADMIN_USER: ${DOCKER_WORDPRESS_ADMIN_USER:-admin}
WORDPRESS_ADMIN_PASSWORD: ${DOCKER_WORDPRESS_ADMIN_PASSWORD:-admin}
WORDPRESS_ADMIN_EMAIL: ${DOCKER_WORDPRESS_ADMIN_EMAIL:-admin@example.com}
WORDPRESS_DB_HOST: wp-db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
PHP_OPCACHE_VALIDATE_TIMESTAMPS: 1
_OPCACHE_MAX_ACCELERATED_FILES: 100000
PHP_OPCACHE_MEMORY_CONSUMPTION: 64
PHP_OPCACHE_MAX_WASTED_PERCENTAGE: 5
DOCKER_USER_ID: ${DOCKER_USER_ID:-1000}
DOCKER_GROUP_ID: ${DOCKER_GROUP_ID:-1000}
networks:
- local-private-net
- ac-network
Expand All @@ -75,8 +85,8 @@ services:
volumes:
- ${DOCKER_CONF_NGINX_PATH:-./conf/dist/nginx-conf}:/etc/nginx/conf.d/
- ${DOCKER_CONF_CERTS_PATH:-./conf/dist/certs/}:/etc/nginx/certs/
- ./srv/wordpress:/var/www/html
- ./var/logs:/var/log/nginx
- ${DOCKER_WORDPRESS_SRC_PATH:-wordpress-src}:/var/www/html:ro
extra_hosts:
- "web.local:127.0.0.1"
ports:
Expand All @@ -89,6 +99,14 @@ services:
- local-private-net
- local-shared-net
- ac-network

redis:
image: redis:alpine
networks:
- local-private-net
expose:
- "6379"

networks:
local-shared-net:
name: local-shared-net
Expand All @@ -100,4 +118,5 @@ networks:
external: ${DOCKER_AC_NETWORK_EXTERNAL:-false}
volumes:
mysql-data:
wordpress-src:

Loading

0 comments on commit e211805

Please sign in to comment.