WordPress Install
目录树结构说明
/var/www/html/
├── wp-admin/ # WordPress 管理后台核心文件
├── wp-content/ # 用户内容目录
│ ├── plugins/ # 插件目录
│ │ └── redis-cache/ # Redis 缓存插件
│ ├── themes/ # 主题目录
│ ├── uploads/ # 上传文件目录
│ └── object-cache.php # Redis 对象缓存文件
├── wp-includes/ # WordPress 核心包含文件
├── wp-config.php # WordPress 配置文件(数据库、Redis 等)
├── wp-settings.php # WordPress 设置入口
├── index.php # WordPress 前端入口
└── .htaccess # Apache 重写规则
静态目录(运行过程中不变化)
/wp-admin - WordPress 管理界面文件
/wp-includes - WordPress 核心库和函数文件
/wp-content/plugins - 插件目录(插件文件本身不变)
/wp-content/themes - 主题目录(主题文件本身不变)
动态目录(用户使用过程中数据更新)
/wp-content/uploads - 用户上传的媒体文件(图片、视频等)
/wp-content/backup - 备份数据(如果存在)
- 数据库目录 - 存储所有文章、评论、用户等数据
Istall
ENV
WORDPRESS_DB_HOST
WORDPRESS_DB_NAME
WORDPRESS_DB_USER
WORDPRESS_DB_PASSWORD
WORDPRESS_REDIS_HOST
WORDPRESS_REDIS_PORT
WORDPRESS_REDIS_DATABASE
WORDPRESS_REDIS_PASSWORD
WORDPRESS_DEBUG
ECS Install
function log_prefix() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')]"
}
sudo su - ecs-user
sudo dnf install -y httpd php php-mysqlnd php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc
echo "$(log_prefix) installing wordpress..."
wget https://cn.wordpress.org/wordpress-6.9-zh_CN.tar.gz -O wordpress-6.9-zh_CN.tar.gz
tar -zxf wordpress-6.9-zh_CN.tar.gz
echo "$(log_prefix) config wordpress..."
sudo cat << 'EOF' > wordpress/wp-config.php
<?php
// a helper function to lookup "env_FILE", "env", then fallback
if (!function_exists('getenv_docker')) {
// https://github.com/docker-library/wordpress/issues/588 (WP-CLI will load this file 2x)
function getenv_docker($env, $default) {
if ($fileEnv = getenv($env . '_FILE')) {
return rtrim(file_get_contents($fileEnv), "\r\n");
}
else if (($val = getenv($env)) !== false) {
return $val;
}
else {
return $default;
}
}
}
/** The name of the database for WordPress */
define( 'DB_NAME', getenv_docker('WORDPRESS_DB_NAME', 'wordpress') );
/** Database username */
define( 'DB_USER', getenv_docker('WORDPRESS_DB_USER', 'example username') );
/** Database password */
define( 'DB_PASSWORD', getenv_docker('WORDPRESS_DB_PASSWORD', 'example password') );
/** Database hostname */
define( 'DB_HOST', getenv_docker('WORDPRESS_DB_HOST', 'mysql') );
/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', getenv_docker('WORDPRESS_DB_CHARSET', 'utf8mb4') );
/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', getenv_docker('WORDPRESS_DB_COLLATE', '') );
// add Redis Config
// adjust Redis host and port if necessary
define( 'WP_REDIS_HOST', getenv_docker('WORDPRESS_REDIS_HOST', '127.0.0.1') );
define( 'WP_REDIS_PORT', getenv_docker('WORDPRESS_REDIS_PORT', 6379) );
// change the prefix and database for each site to avoid cache data collisions
define( 'WP_REDIS_PREFIX', getenv_docker('WORDPRESS_REDIS_PREFIX', 'my-moms-site') );
define( 'WP_REDIS_DATABASE', getenv_docker('WORDPRESS_REDIS_DATABASE', 0) ); // 0-15
define( 'WP_REDIS_PASSWORD', getenv_docker('WORDPRESS_REDIS_PASSWORD', '') );
// reasonable connection and read+write timeouts
define( 'WP_REDIS_TIMEOUT', getenv_docker('WORDPRESS_REDIS_TIMEOUT', 1) );
define( 'WP_REDIS_READ_TIMEOUT', getenv_docker('WORDPRESS_REDIS_READ_TIMEOUT', 1) );
define( 'AUTH_KEY', getenv_docker('WORDPRESS_AUTH_KEY', 'put your unique phrase here') );
define( 'SECURE_AUTH_KEY', getenv_docker('WORDPRESS_SECURE_AUTH_KEY', 'put your unique phrase here') );
define( 'LOGGED_IN_KEY', getenv_docker('WORDPRESS_LOGGED_IN_KEY', 'put your unique phrase here') );
define( 'NONCE_KEY', getenv_docker('WORDPRESS_NONCE_KEY', 'put your unique phrase here') );
define( 'AUTH_SALT', getenv_docker('WORDPRESS_AUTH_SALT', 'put your unique phrase here') );
define( 'SECURE_AUTH_SALT', getenv_docker('WORDPRESS_SECURE_AUTH_SALT', 'put your unique phrase here') );
define( 'LOGGED_IN_SALT', getenv_docker('WORDPRESS_LOGGED_IN_SALT', 'put your unique phrase here') );
define( 'NONCE_SALT', getenv_docker('WORDPRESS_NONCE_SALT', 'put your unique phrase here') );
// (See also https://wordpress.stackexchange.com/a/152905/199287)
$table_prefix = getenv_docker('WORDPRESS_TABLE_PREFIX', 'wp_');
define( 'WP_DEBUG', !!getenv_docker('WORDPRESS_DEBUG', '') );
/* Add any custom values between this line and the "stop editing" line. */
// If we're behind a proxy server and using HTTPS, we need to alert WordPress of that fact
// see also https://wordpress.org/support/article/administration-over-ssl/
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) {
$_SERVER['HTTPS'] = 'on';
}
// (we include this by default because reverse proxying is extremely common in container environments)
if ($configExtra = getenv_docker('WORDPRESS_CONFIG_EXTRA', '')) {
eval($configExtra);
}
/* That's all, stop editing! Happy publishing. */
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
EOF
sudo cp -arf wordpress/* /var/www/html/
echo "$(log_prefix) installing plugin redis-cache..."
wget https://github.com/rhubarbgroup/redis-cache/archive/refs/tags/2.6.5.tar.gz -O redis-cache.2.6.5.tar.gz
tar -zxf redis-cache.2.6.5.tar.gz
sudo cp -arf redis-cache-2.6.5 /var/www/html/wp-content/plugins/redis-cache
sudo cp /var/www/html/wp-content/plugins/redis-cache/includes/object-cache.php /var/www/html/wp-content/
echo "$(log_prefix) starting httpd..."
sudo systemctl restart httpd
sudo systemctl enable httpd
echo "$(log_prefix) done"
Dockerfile
- images: wordpress:6.9.4-redis-v1
- crpi-35tjd6nv3bx5haji.cn-shanghai.personal.cr.aliyuncs.com/rocdove/wordpress:6.9.4-redis-v1
FROM php:8.5-fpm-alpine
RUN set -eux; \
apk add --no-cache \
bash \
ghostscript \
imagemagick \
;
RUN set -ex; \
\
apk add --no-cache --virtual .build-deps \
$PHPIZE_DEPS \
freetype-dev \
icu-dev \
imagemagick-dev libheif-dev \
libavif-dev \
libjpeg-turbo-dev \
libpng-dev \
libwebp-dev \
libzip-dev \
; \
\
docker-php-ext-configure gd \
--with-avif \
--with-freetype \
--with-jpeg \
--with-webp \
; \
docker-php-ext-install -j "$(nproc)" \
bcmath \
exif \
gd \
intl \
mysqli \
zip \
; \
pecl install imagick-3.8.1; \
docker-php-ext-enable imagick; \
rm -r /tmp/pear; \
\
out="$(php -r 'exit(0);')"; \
[ -z "$out" ]; \
err="$(php -r 'exit(0);' 3>&1 1>&2 2>&3)"; \
[ -z "$err" ]; \
\
extDir="$(php -r 'echo ini_get("extension_dir");')"; \
[ -d "$extDir" ]; \
runDeps="$( \
scanelf --needed --nobanner --format '%n
| tr ',' '\n' \
| sort -u \
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 {
next } {
print "so:" $1 }' \
)"; \
apk add --no-network --virtual .wordpress-phpexts-rundeps $runDeps; \
apk del --no-network .build-deps; \
\
! {
ldd "$extDir"/*.so | grep 'not found'; }; \
err="$(php --version 3>&1 1>&2 2>&3)"; \
[ -z "$err" ]
RUN set -eux; \
{
\
echo 'opcache.memory_consumption=128'; \
echo 'opcache.interned_strings_buffer=8'; \
echo 'opcache.max_accelerated_files=4000'; \
echo 'opcache.revalidate_freq=2'; \
} > "$PHP_INI_DIR/conf.d/opcache-recommended.ini"
RUN set -eux; \
{
\
echo 'error_reporting = E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_RECOVERABLE_ERROR'; \
echo 'display_errors = Off'; \
echo 'display_startup_errors = Off'; \
echo 'log_errors = On'; \
echo 'error_log = /dev/stderr'; \
echo 'log_errors_max_len = 1024'; \
echo 'ignore_repeated_errors = On'; \
echo 'ignore_repeated_source = Off'; \
echo 'html_errors = Off'; \
} > "$PHP_INI_DIR/conf.d/error-logging.ini"
RUN set -eux; \
version='7.0'; \
sha1='e50bb75667ecaa0eac0694fb3c7b024afc96fde0'; \
\
curl -o wordpress.tar.gz -fL "https://wordpress.org/wordpress-$version.tar.gz"; \
echo "$sha1 *wordpress.tar.gz" | sha1sum -c -; \
\
tar -xzf wordpress.tar.gz -C /usr/src/; \
rm wordpress.tar.gz; \
\
[ ! -e /usr/src/wordpress/.htaccess ]; \
{
\
echo '
echo ''; \
echo 'RewriteEngine On'; \
echo 'RewriteRule .* - [E=HTTP_AUTHORIZATION:%{
HTTP:Authorization}]'; \
echo 'RewriteBase /'; \
echo 'RewriteRule ^index\.php$ - [L]'; \
echo 'RewriteCond %{
REQUEST_FILENAME} !-f'; \
echo 'RewriteCond %{
REQUEST_FILENAME} !-d'; \
echo 'RewriteRule . /index.php [L]'; \
echo ''; \
echo '
} > /usr/src/wordpress/.htaccess; \
\
chown -R www-data:www-data /usr/src/wordpress; \
mkdir wp-content; \
for dir in /usr/src/wordpress/wp-content/*/ cache; do \
dir="$(basename "${
dir%/}")"; \
mkdir "wp-content/$dir"; \
done; \
chown -R www-data:www-data wp-content; \
chmod -R 1777 wp-content
VOLUME /var/www/html
COPY --chown=www-data:www-data wp-config-docker.php /usr/src/wordpress/
COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -svfT docker-entrypoint.sh /usr/local/bin/docker-ensure-installed.sh
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["php-fpm"]