如何用Docker部署带持久化的phpBB(NGINX+Alpine)并连接远程MySQL
Got it, let's break down exactly how to build your Alpine-based Docker image with Nginx, PHP, and phpBB, connect it to your remote MySQL instance at 192.168.2.233:3307, and make sure all forum changes stick around even if the container restarts.
First, set up a simple directory structure to organize your files neatly:
phpbb-docker/ ├── Dockerfile ├── nginx.conf └── phpbb/ # We'll fill this with phpBB files in a bit
This Dockerfile installs all required dependencies, sets up Nginx and PHP-FPM, and pulls in phpBB. I've added comments to explain each step clearly:
# Start with the latest stable Alpine Linux image FROM alpine:latest # Install core packages: Nginx, PHP-FPM, and PHP extensions phpBB needs RUN apk update && apk add --no-cache \ nginx \ php82-fpm \ php82-mysqli \ php82-gd \ php82-mbstring \ php82-xml \ php82-json \ php82-session \ php82-zip \ wget \ unzip # Download and extract the latest phpBB version (swap the URL for a specific version if needed) RUN wget -O phpBB.zip https://download.phpbb.com/pub/release/3.3/3.3.11/phpBB-3.3.11.zip && \ unzip phpBB.zip -d /var/www/ && \ rm phpBB.zip # Fix permissions so Nginx can access phpBB files (Alpine runs Nginx as the 'nginx' user) RUN chown -R nginx:nginx /var/www/phpBB && \ chmod -R 755 /var/www/phpBB # Copy our custom Nginx config into the container COPY nginx.conf /etc/nginx/nginx.conf # Expose port 80 (standard HTTP port for web traffic) EXPOSE 80 # Start both PHP-FPM and Nginx when the container launches CMD sh -c "php-fpm82 && nginx -g 'daemon off;'"
We need to configure Nginx to hand off PHP requests to PHP-FPM. Replace the default Nginx config with this custom one:
user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; root /var/www/phpBB; index index.php index.html; # Serve static files directly with Nginx location / { try_files $uri $uri/ /index.php?$args; } # Route PHP requests to PHP-FPM for processing location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # Block access to sensitive phpBB files for security location ~ /(config\.php|common\.php|cache|files|images/avatars/upload|includes|store) { deny all; return 403; } } }
To make sure your forum changes (like uploaded avatars, attachments, and config edits) don't disappear when the container restarts, we'll use Docker volumes to persist critical directories.
Critical Directories to Persist:
/var/www/phpBB/config.php: Stores your database connection settings and core forum config/var/www/phpBB/files: Stores user-uploaded attachments/var/www/phpBB/images/avatars/upload: Stores user avatars/var/www/phpBB/store: Stores cached templates, backups, and temporary data
Installation Steps:
When you first run the container, navigate to http://<your-container-ip-or-host-port> in your browser. Follow the phpBB installation wizard:
- For the database settings, input:
- Database server hostname:
192.168.2.233 - Database server port:
3307 - Database name: (the name of your existing MySQL database)
- Database username/password: (credentials with full access to that database)
- Database server hostname:
Once installation finishes, make sure to delete the /var/www/phpBB/install directory (you can run rm -rf /var/www/phpBB/install inside the container or add this step to your Dockerfile for automation).
Build the Image:
From the phpbb-docker directory, run this command to build your custom image:
docker build -t phpbb-nginx-alpine .
Run the Container with Persistent Volumes:
Use this command to start the container and mount volumes for persistent data:
docker run -d \ -p 8080:80 \ --name phpbb-forum \ -v phpbb-config:/var/www/phpBB/config.php \ -v phpbb-files:/var/www/phpBB/files \ -v phpbb-avatars:/var/www/phpBB/images/avatars/upload \ -v phpbb-store:/var/www/phpBB/store \ phpbb-nginx-alpine
-p 8080:80: Maps port 8080 on your host machine to port 80 in the container (access your forum viahttp://localhost:8080)- The
-vflags create Docker volumes that save your critical data outside the container, so it survives container restarts/recreations.
- Check if the container is running:
docker ps - Navigate to
http://localhost:8080(or your host's public IP if running remotely) - Post a test message, upload an avatar, or add an attachment
- Stop and restart the container:
docker stop phpbb-forum && docker start phpbb-forum - Refresh the forum—your changes should still be there!
内容的提问来源于stack exchange,提问作者FranzHuber23

