Docker部署CodeIgniter项目报错:未找到匹配的DirectoryIndex(index.php,index.html)
Docker运行CodeIgniter提示找不到index.php/index.html的解决方法
问题场景
- phpMyAdmin可通过
localhost:8082正常访问 - CodeIgniter项目应运行在
localhost:8080,但日志提示未找到index.php/index.html - 本地目录结构:
var/www/html/ application db-data
- docker-compose.yml配置:
version: '3.9' services: codeigniter: image: php:7.4-apache volumes: - ./app:/var/www/html ports: - "8080:80" depends_on: - database database: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: testpassword MYSQL_DATABASE: sortdb volumes: - ./db-data:/var/lib/mysql phpmyadmin: image: phpmyadmin/phpmyadmin links: - database ports: - "8082:80" environment: PMA_HOST: database MYSQL_ROOT_PASSWORD: testpassword
- Dockerfile内容:
# Use an official PHP image as the base image FROM php:7.4-apache # Enable mod_rewrite RUN a2enmod rewrite # Set the working directory in the container WORKDIR /var/www/html # Grant permissions to the Apache user RUN chown -R www-data:www-data /var/www/html # Copy your CodeIgniter application files into the container COPY . . # Expose the Apache web server port EXPOSE 8080 # Start the Apache server CMD ["apache2-foreground"]
解决步骤
1. 让docker-compose使用自定义Dockerfile构建镜像
当前docker-compose直接调用官方PHP镜像,未执行你Dockerfile中开启rewrite、复制文件的操作。修改codeigniter服务配置:
codeigniter: build: . volumes: - ./app:/var/www/html ports: - "8080:80" depends_on: - database
build: .表示从当前目录的Dockerfile构建镜像,确保你的自定义配置生效。
2. 修正本地app目录的文件结构
CodeIgniter根目录必须包含index.php、application、system(CodeIgniter 3)等核心文件,你当前的目录缺少index.php和system目录。
将完整的CodeIgniter项目文件放入本地./app目录,确保容器挂载后/var/www/html下的结构为:
/var/www/html/ index.php application/ system/ # 其他CodeIgniter必要文件
3. 统一Apache端口配置
Dockerfile中EXPOSE 8080与容器内Apache实际运行的80端口不符,修改Dockerfile的EXPOSE指令更准确:
EXPOSE 80
(注:此步骤不影响运行,但能避免配置混淆)
4. 修复目录权限
本地挂载目录的权限可能与容器内Apache用户(www-data)不匹配,进入容器执行权限修正:
docker-compose exec codeigniter bash chown -R www-data:www-data /var/www/html
5. 配置CodeIgniter基础URL
编辑application/config/config.php,设置正确的base_url并开启URL重写:
$config['base_url'] = 'http://localhost:8080/'; $config['index_page'] = ''; // 去掉URL中的index.php
内容的提问来源于stack exchange,提问作者Tristan
相关产品推荐
相关产品推荐

