如何在Docker环境部署October CMS?遇500/502错误求助
October CMS Docker 部署问题排查与修复
问题描述
我从仓库下载了October CMS,想在Docker本地环境完整运行。写了包含Nginx、PHP-FPM和Postgres容器的Docker Compose配置,这套配置在其他Laravel项目里能正常工作,但October CMS跑不起来,出现500、502等多种错误。用php artisan serve启动服务器的话,项目看似能运行,但JS和CSS资源加载不出来。
现有配置文件
Docker Compose 配置
version: '3.8' services: #PHP Service app: build: context: . dockerfile: ./docker/php/Dockerfile container_name: app volumes: - ./:/var/www/html - ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini ports: - "9000:9000" networks: postgres: #Nginx Service webserver: image: nginx:stable-alpine container_name: webserver restart: unless-stopped tty: true ports: - "80:80" - "443:443" volumes: - ./:/var/www/html - ./docker/nginx/conf.d/:/etc/nginx/conf.d/ networks: - postgres #PostgreSQL postgres: container_name: postgres image: postgres:16 hostname: postgres environment: POSTGRES_DB: postgres POSTGRES_USER: postgres POSTGRES_PASSWORD: secret PGDATA: /data/postgres volumes: - postgres:/data/postgres ports: - "5432:5432" networks: - postgres restart: unless-stopped #PGAdmin pgadmin: container_name: pgadmin image: dpage/pgadmin4 environment: PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4@pgadmin.org} PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin} volumes: - pgadmin:/root/.pgadmin ports: - "${PGADMIN_PORT:-5050}:80" networks: - postgres restart: unless-stopped depends_on: - postgres networks: postgres: volumes: postgres: pgadmin: database_data:
PHP Dockerfile
FROM php:8.3-fpm WORKDIR /var/www/html RUN apt-get update && apt-get install -y \ build-essential \ libzip-dev \ libpng-dev \ libpq-dev \ libjpeg62-turbo-dev \ libwebp-dev libjpeg62-turbo-dev libpng-dev libxpm-dev \ libfreetype6 \ libfreetype6-dev \ locales \ zip \ jpegoptim optipng pngquant gifsicle \ vim \ unzip \ git \ curl \ libgd-dev RUN apt-get update \ && apt-get install --assume-yes --no-install-recommends --quiet \ build-essential \ libmagickwand-dev \ && apt-get clean all RUN pecl install imagick \ && docker-php-ext-enable imagick RUN docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg RUN apt-get install -y librabbitmq-dev libssl-dev RUN pecl install amqp RUN docker-php-ext-enable amqp RUN docker-php-ext-install gd RUN docker-php-ext-install sockets RUN apt-get clean && rm -rf /var/lib/apt/lists/* RUN docker-php-ext-install zip exif pcntl pdo mysqli pdo_mysql pdo_pgsql RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer EXPOSE 9000 CMD ["php-fpm"]
Nginx app.conf
server { listen 80; index index.php index.html; error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; root /var/www/html; location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass app:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } location / { try_files $uri $uri/ /index.php?$query_string; gzip_static on; } }
Nginx Docker 日志
webserver | 172.18.0.1 - - [19/Dec/2024:14:29:11 +0000] "GET / HTTP/1.1" 500 731 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36" webserver | 172.18.0.1 - - [19/Dec/2024:14:29:12 +0000] "GET /favicon.ico HTTP/1.1" 500 731 "http://localhost/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36" webserver | 172.18.0.1 - - [19/Dec/2024:14:29:27 +0000] "GET / HTTP/1.1" 500 731 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36" webserver | 172.18.0.1 - - [19/Dec/2024:14:29:27 +0000] "GET / HTTP/1.1" 500 731 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36" webserver | 172.18.0.1 - - [19/Dec/2024:14:29:27 +0000] "GET /favicon.ico HTTP/1.1" 500 731 "http://localhost/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36" webserver | 172.18.0.1 - - [19/Dec/2024:15:18:25 +0000] "GET / HTTP/1.1" 500 731 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0" webserver | 172.18.0.1 - - [19/Dec/2024:15:18:38 +0000] "GET / HTTP/1.1" 500 731 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36" webserver | 172.18.0.1 - - [19/Dec/2024:15:18:38 +0000] "GET /favicon.ico HTTP/1.1" 500 731 "http://localhost/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
PHP-FPM 容器日志
172.18.0.3 - 19/Dec/2024:15:18:38 +0000 "GET /index.php" 500 172.18.0.3 - 19/Dec/2024:15:18:38 +0000 "GET /index.php" 500 172.18.0.3 - 19/Dec/2024:15:23:22 +0000 "GET /index.php" 500
修复步骤
1. 修复文件权限
October CMS的storage和bootstrap/cache目录需要写入权限,进入PHP容器执行:
docker exec -it app bash chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache exit
或者在Docker Compose的app服务中添加用户映射,避免宿主机权限冲突:
app: # ... 其他配置 user: "${UID:-1000}:${GID:-1000}"
2. 更新Nginx配置
原配置缺少静态资源处理规则,且gzip_static on会导致无预生成gzip文件时出错,替换为以下配置:
server { listen 80; index index.php index.html; error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; root /var/www/html; # 处理静态资源,缓存一年 location ~* \.(jpg|jpeg|png|gif|css|js|ico|svg)$ { expires 1y; add_header Cache-Control "public, immutable"; try_files $uri =404; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass app:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; # 传递October CMS所需环境变量 fastcgi_param APP_ENV local; fastcgi_param APP_DEBUG true; } location / { try_files $uri $uri/ /index.php?$query_string; } }
修改后重启Nginx容器:docker restart webserver
3. 补充PHP扩展
October CMS依赖fileinfo扩展,在PHP Dockerfile中添加:
RUN docker-php-ext-install fileinfo
重新构建PHP镜像:docker-compose build app
4. 配置数据库连接
确保项目根目录的.env文件配置正确:
APP_URL=http://localhost DB_CONNECTION=pgsql DB_HOST=postgres DB_PORT=5432 DB_DATABASE=postgres DB_USERNAME=postgres DB_PASSWORD=secret
进入PHP容器执行初始化命令:
docker exec -it app php artisan october:up docker exec -it app php artisan migrate
5. 清理缓存并生成资源
执行以下命令修复静态资源缺失问题:
docker exec -it app php artisan cache:clear docker exec -it app php artisan config:clear docker exec -it app php artisan asset:clear docker exec -it app php artisan october:cache
内容的提问来源于stack exchange,提问作者ШайтанЯкубович
相关产品推荐
相关产品推荐

