CatPtain commited on
Commit
ca8657b
·
verified ·
1 Parent(s): 4de31ce

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +130 -126
  2. README.md +20 -4
  3. start-hf.sh +11 -12
Dockerfile CHANGED
@@ -1,127 +1,131 @@
1
- # Dockerfile optimized for Hugging Face Spaces deployment
2
- # Based on WordPress 6.8.1 with PHP 8.3 Apache
3
-
4
- FROM php:8.3-apache
5
-
6
- # Set environment variables for HF Spaces
7
- ENV APACHE_DOCUMENT_ROOT=/var/www/html
8
- ENV APACHE_RUN_USER=www-data
9
- ENV APACHE_RUN_GROUP=www-data
10
-
11
- # Install system dependencies
12
- RUN set -eux; \
13
- apt-get update; \
14
- apt-get install -y --no-install-recommends \
15
- ghostscript \
16
- libfreetype6-dev \
17
- libicu-dev \
18
- libjpeg-dev \
19
- libpng-dev \
20
- libwebp-dev \
21
- libzip-dev \
22
- libsqlite3-dev \
23
- sqlite3 \
24
- wget \
25
- unzip \
26
- ; \
27
- rm -rf /var/lib/apt/lists/*
28
-
29
- # Configure and install PHP extensions
30
- RUN set -ex; \
31
- docker-php-ext-configure gd \
32
- --with-freetype \
33
- --with-jpeg \
34
- --with-webp \
35
- ; \
36
- docker-php-ext-install -j "$(nproc)" \
37
- bcmath \
38
- exif \
39
- gd \
40
- intl \
41
- mysqli \
42
- pdo \
43
- pdo_sqlite \
44
- zip \
45
- ;
46
-
47
- # Configure PHP settings
48
- RUN { \
49
- echo 'opcache.memory_consumption=128'; \
50
- echo 'opcache.interned_strings_buffer=8'; \
51
- echo 'opcache.max_accelerated_files=4000'; \
52
- echo 'opcache.revalidate_freq=2'; \
53
- } > /usr/local/etc/php/conf.d/opcache-recommended.ini
54
-
55
- RUN { \
56
- echo 'error_reporting = E_ERROR | E_WARNING | E_PARSE'; \
57
- echo 'display_errors = Off'; \
58
- echo 'log_errors = On'; \
59
- echo 'error_log = /dev/stderr'; \
60
- echo 'memory_limit = 256M'; \
61
- echo 'upload_max_filesize = 32M'; \
62
- echo 'post_max_size = 32M'; \
63
- } > /usr/local/etc/php/conf.d/wordpress.ini
64
-
65
- # Configure Apache for HF Spaces
66
- RUN set -eux; \
67
- a2enmod rewrite expires headers; \
68
- # Configure Apache to listen on port 7860 (HF Spaces requirement)
69
- sed -i 's/Listen 80/Listen 7860/' /etc/apache2/ports.conf; \
70
- sed -i 's/:80>/:7860>/' /etc/apache2/sites-available/000-default.conf; \
71
- # Set ServerName to avoid warnings
72
- echo 'ServerName localhost' >> /etc/apache2/apache2.conf
73
-
74
- # Download and install WordPress
75
- RUN set -eux; \
76
- version='6.8.1'; \
77
- sha1='52d5f05c96a9155f78ed84700264307e5dea14b4'; \
78
- curl -o wordpress.tar.gz -fL "https://wordpress.org/wordpress-$version.tar.gz"; \
79
- echo "$sha1 *wordpress.tar.gz" | sha1sum -c -; \
80
- tar -xzf wordpress.tar.gz -C /usr/src/; \
81
- rm wordpress.tar.gz; \
82
- cp -a /usr/src/wordpress/. /var/www/html/; \
83
- chown -R www-data:www-data /var/www/html
84
-
85
- # Install SQLite integration plugin for WordPress
86
- RUN set -eux; \
87
- wget -O sqlite-plugin.zip "https://downloads.wordpress.org/plugin/sqlite-database-integration.2.1.15.zip"; \
88
- unzip sqlite-plugin.zip -d /var/www/html/wp-content/plugins/; \
89
- rm sqlite-plugin.zip; \
90
- chown -R www-data:www-data /var/www/html/wp-content/plugins/sqlite-database-integration
91
-
92
- # Create WordPress configuration for SQLite
93
- COPY wp-config-hf.php /var/www/html/wp-config.php
94
- COPY db.php /var/www/html/wp-content/db.php
95
-
96
- # Create .htaccess for pretty permalinks
97
- RUN { \
98
- echo '# BEGIN WordPress'; \
99
- echo 'RewriteEngine On'; \
100
- echo 'RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]'; \
101
- echo 'RewriteBase /'; \
102
- echo 'RewriteRule ^index\.php$ - [L]'; \
103
- echo 'RewriteCond %{REQUEST_FILENAME} !-f'; \
104
- echo 'RewriteCond %{REQUEST_FILENAME} !-d'; \
105
- echo 'RewriteRule . /index.php [L]'; \
106
- echo '# END WordPress'; \
107
- } > /var/www/html/.htaccess
108
-
109
- # Set proper permissions
110
- RUN chown -R www-data:www-data /var/www/html && \
111
- chmod -R 755 /var/www/html && \
112
- chmod 644 /var/www/html/.htaccess
113
-
114
- # Create SQLite database directory
115
- RUN mkdir -p /var/www/html/wp-content/database && \
116
- chown -R www-data:www-data /var/www/html/wp-content/database && \
117
- chmod 755 /var/www/html/wp-content/database
118
-
119
- # Expose port 7860 for Hugging Face Spaces
120
- EXPOSE 7860
121
-
122
- # Copy startup script
123
- COPY start-hf.sh /usr/local/bin/start-hf.sh
124
- RUN chmod +x /usr/local/bin/start-hf.sh
125
-
126
- # Start Apache
 
 
 
 
127
  CMD ["/usr/local/bin/start-hf.sh"]
 
1
+ # Dockerfile optimized for Hugging Face Spaces deployment
2
+ # Based on WordPress 6.8.1 with PHP 8.3 Apache
3
+
4
+ FROM php:8.3-apache
5
+
6
+ # Set environment variables for HF Spaces
7
+ ENV APACHE_DOCUMENT_ROOT=/var/www/html
8
+ ENV APACHE_RUN_USER=www-data
9
+ ENV APACHE_RUN_GROUP=www-data
10
+
11
+ # Install system dependencies
12
+ RUN set -eux; \
13
+ apt-get update; \
14
+ apt-get install -y --no-install-recommends \
15
+ ghostscript \
16
+ libfreetype6-dev \
17
+ libicu-dev \
18
+ libjpeg-dev \
19
+ libpng-dev \
20
+ libwebp-dev \
21
+ libzip-dev \
22
+ libsqlite3-dev \
23
+ sqlite3 \
24
+ wget \
25
+ unzip \
26
+ ; \
27
+ rm -rf /var/lib/apt/lists/*
28
+
29
+ # Configure and install PHP extensions
30
+ RUN set -ex; \
31
+ docker-php-ext-configure gd \
32
+ --with-freetype \
33
+ --with-jpeg \
34
+ --with-webp \
35
+ ; \
36
+ docker-php-ext-install -j "$(nproc)" \
37
+ bcmath \
38
+ exif \
39
+ gd \
40
+ intl \
41
+ mysqli \
42
+ pdo \
43
+ pdo_sqlite \
44
+ zip \
45
+ ;
46
+
47
+ # Configure PHP settings
48
+ RUN { \
49
+ echo 'opcache.memory_consumption=128'; \
50
+ echo 'opcache.interned_strings_buffer=8'; \
51
+ echo 'opcache.max_accelerated_files=4000'; \
52
+ echo 'opcache.revalidate_freq=2'; \
53
+ } > /usr/local/etc/php/conf.d/opcache-recommended.ini
54
+
55
+ RUN { \
56
+ echo 'error_reporting = E_ERROR | E_WARNING | E_PARSE'; \
57
+ echo 'display_errors = Off'; \
58
+ echo 'log_errors = On'; \
59
+ echo 'error_log = /dev/stderr'; \
60
+ echo 'memory_limit = 256M'; \
61
+ echo 'upload_max_filesize = 32M'; \
62
+ echo 'post_max_size = 32M'; \
63
+ } > /usr/local/etc/php/conf.d/wordpress.ini
64
+
65
+ # Configure Apache for HF Spaces
66
+ RUN set -eux; \
67
+ a2enmod rewrite expires headers; \
68
+ # Configure Apache to listen on port 7860 (HF Spaces requirement)
69
+ sed -i 's/Listen 80/Listen 7860/' /etc/apache2/ports.conf; \
70
+ sed -i 's/:80>/:7860>/' /etc/apache2/sites-available/000-default.conf; \
71
+ # Set ServerName to avoid warnings
72
+ echo 'ServerName localhost' >> /etc/apache2/apache2.conf
73
+
74
+ # Download and install WordPress
75
+ RUN set -eux; \
76
+ version='6.8.1'; \
77
+ sha1='52d5f05c96a9155f78ed84700264307e5dea14b4'; \
78
+ curl -o wordpress.tar.gz -fL "https://wordpress.org/wordpress-$version.tar.gz"; \
79
+ echo "$sha1 *wordpress.tar.gz" | sha1sum -c -; \
80
+ tar -xzf wordpress.tar.gz -C /usr/src/; \
81
+ rm wordpress.tar.gz; \
82
+ cp -a /usr/src/wordpress/. /var/www/html/; \
83
+ chown -R www-data:www-data /var/www/html
84
+
85
+ # Install SQLite integration plugin for WordPress
86
+ RUN set -eux; \
87
+ wget -O sqlite-plugin.zip "https://downloads.wordpress.org/plugin/sqlite-database-integration.2.1.15.zip"; \
88
+ unzip sqlite-plugin.zip -d /var/www/html/wp-content/plugins/; \
89
+ rm sqlite-plugin.zip; \
90
+ chown -R www-data:www-data /var/www/html/wp-content/plugins/sqlite-database-integration
91
+
92
+ # Create WordPress configuration for SQLite
93
+ COPY wp-config-hf.php /var/www/html/wp-config.php
94
+ COPY db.php /var/www/html/wp-content/db.php
95
+
96
+ # Create .htaccess for pretty permalinks
97
+ RUN { \
98
+ echo '# BEGIN WordPress'; \
99
+ echo 'RewriteEngine On'; \
100
+ echo 'RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]'; \
101
+ echo 'RewriteBase /'; \
102
+ echo 'RewriteRule ^index\.php$ - [L]'; \
103
+ echo 'RewriteCond %{REQUEST_FILENAME} !-f'; \
104
+ echo 'RewriteCond %{REQUEST_FILENAME} !-d'; \
105
+ echo 'RewriteRule . /index.php [L]'; \
106
+ echo '# END WordPress'; \
107
+ } > /var/www/html/.htaccess
108
+
109
+ # Create necessary directories with proper permissions
110
+ RUN mkdir -p /var/www/html/wp-content/database && \
111
+ mkdir -p /var/www/html/wp-content/uploads && \
112
+ mkdir -p /var/www/html/wp-content/cache && \
113
+ chown -R www-data:www-data /var/www/html && \
114
+ chmod -R 755 /var/www/html && \
115
+ chmod 644 /var/www/html/.htaccess && \
116
+ chmod 777 /var/www/html/wp-content/uploads && \
117
+ chmod 777 /var/www/html/wp-content/database && \
118
+ chmod 777 /var/www/html/wp-content/cache
119
+
120
+ # Add user for HF Spaces compatibility
121
+ RUN usermod -u 1000 www-data && groupmod -g 1000 www-data
122
+
123
+ # Expose port 7860 for Hugging Face Spaces
124
+ EXPOSE 7860
125
+
126
+ # Copy startup script
127
+ COPY start-hf.sh /usr/local/bin/start-hf.sh
128
+ RUN chmod +x /usr/local/bin/start-hf.sh
129
+
130
+ # Start Apache
131
  CMD ["/usr/local/bin/start-hf.sh"]
README.md CHANGED
@@ -123,6 +123,7 @@ pinned: false
123
  2. **资源**: CPU 和内存有限制
124
  3. **网络**: 某些外部连接可能受限
125
  4. **插件**: 某些需要特殊权限的插件可能无法使用
 
126
 
127
  ### 建议用途
128
 
@@ -168,10 +169,25 @@ RUN { \
168
 
169
  ### 常见问题
170
 
171
- 1. **无法访问**: 检查端口是否为 7860
172
- 2. **数据库错误**: 确保 SQLite 扩展已安装
173
- 3. **权限错误**: 检查文件权限设置
174
- 4. **插件问题**: 某些插件可能不兼容 SQLite
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
 
176
  ### 调试方法
177
 
 
123
  2. **资源**: CPU 和内存有限制
124
  3. **网络**: 某些外部连接可能受限
125
  4. **插件**: 某些需要特殊权限的插件可能无法使用
126
+ 5. **权限**: 为了 HF Spaces 兼容性配置了宽松的文件权限 (777)
127
 
128
  ### 建议用途
129
 
 
169
 
170
  ### 常见问题
171
 
172
+ 1. **Space 启动失败**
173
+ - 检查 Dockerfile 语法
174
+ - 确认所有必需文件都已上传
175
+ - 查看 Space 日志获取详细错误信息
176
+
177
+ 2. **WordPress 无法访问**
178
+ - 等待几分钟让容器完全启动
179
+ - 检查端口 7860 是否正确配置
180
+ - 尝试访问 `/health.html` 检查服务状态
181
+
182
+ 3. **数据库连接错误**
183
+ - SQLite 数据库会自动创建
184
+ - 检查 `wp-content/database/` 目录权限
185
+ - 确认 `db.php` 文件存在
186
+
187
+ 4. **权限错误 (Permission Denied)**
188
+ - 容器已配置宽松权限 (777) 解决 HF Spaces 兼容性
189
+ - 如果仍有问题,检查 Space 日志中的具体错误
190
+ - 确保所有必需目录在 Dockerfile 中正确创建
191
 
192
  ### 调试方法
193
 
start-hf.sh CHANGED
@@ -3,26 +3,25 @@ set -e
3
 
4
  echo "Starting WordPress for Hugging Face Spaces..."
5
 
6
- # Create necessary directories
7
- mkdir -p /var/www/html/wp-content/database
8
- mkdir -p /var/www/html/wp-content/uploads
9
- mkdir -p /var/www/html/wp-content/cache
10
 
11
- # Set proper permissions
12
- chown -R www-data:www-data /var/www/html
13
- chmod -R 755 /var/www/html
14
- chmod 644 /var/www/html/.htaccess
15
 
16
  # Initialize SQLite database if it doesn't exist
17
  if [ ! -f "/var/www/html/wp-content/database/wordpress.db" ]; then
18
  echo "Initializing WordPress database..."
19
 
20
- # Create empty SQLite database
21
  touch /var/www/html/wp-content/database/wordpress.db
22
- chown www-data:www-data /var/www/html/wp-content/database/wordpress.db
23
- chmod 644 /var/www/html/wp-content/database/wordpress.db
24
 
25
- # Initialize WordPress tables using WP-CLI if available, or let WordPress handle it
26
  echo "Database will be initialized on first visit"
27
  fi
28
 
 
3
 
4
  echo "Starting WordPress for Hugging Face Spaces..."
5
 
6
+ # Ensure directories exist (they should already be created in Dockerfile)
7
+ test -d /var/www/html/wp-content/database || mkdir -p /var/www/html/wp-content/database
8
+ test -d /var/www/html/wp-content/uploads || mkdir -p /var/www/html/wp-content/uploads
9
+ test -d /var/www/html/wp-content/cache || mkdir -p /var/www/html/wp-content/cache
10
 
11
+ # Ensure proper permissions (fallback)
12
+ chmod 777 /var/www/html/wp-content/uploads 2>/dev/null || true
13
+ chmod 777 /var/www/html/wp-content/database 2>/dev/null || true
14
+ chmod 777 /var/www/html/wp-content/cache 2>/dev/null || true
15
 
16
  # Initialize SQLite database if it doesn't exist
17
  if [ ! -f "/var/www/html/wp-content/database/wordpress.db" ]; then
18
  echo "Initializing WordPress database..."
19
 
20
+ # Create empty SQLite database with proper permissions
21
  touch /var/www/html/wp-content/database/wordpress.db
22
+ chmod 666 /var/www/html/wp-content/database/wordpress.db 2>/dev/null || true
 
23
 
24
+ # Initialize WordPress tables - let WordPress handle it on first visit
25
  echo "Database will be initialized on first visit"
26
  fi
27