Laravel项目迁移Docker后无法访问:重定向登录页后连接被拒
Laravel项目迁移Docker本地开发时登录页连接被拒问题
问题概述
将生产环境运行正常的Laravel项目迁移至Docker本地开发环境,页面重定向至/login路径后,出现“无法访问此网站-连接被拒”错误。已确认容器内文件挂载正常、文档根目录配置正确、本地无活跃防火墙。
现有配置
Apache站点配置(000-default.conf)
<VirtualHost *:80> ServerAdmin testmate-dev.com.au ServerName www.testmate-dev.com.au DocumentRoot /home/beanstalk/laravel/public <Directory /home/beanstalk/laravel> AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> <VirtualHost *:433> ServerAdmin testmate-dev.com.au ServerName www.testmate-dev.com.au ServerAlias www.testmate-dev.com.au DocumentRoot /home/beanstalk/laravel/public ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory /home/beanstalk/laravel> AllowOverride All Require all granted </Directory> </VirtualHost>
Docker Compose配置(docker-compose.yml)
version: '3.7' services: mysql-server: platform: linux/x86_64 image: mysql:8.0.25 command: --default-authentication-plugin=mysql_native_password restart: always environment: MYSQL_ROOT_PASSWORD: secret ports: - "3306:3306" phpmyadmin: image: phpmyadmin/phpmyadmin:4.9.7 depends_on: - mysql-server restart: always environment: PMA_HOST: mysql-server PMA_USER: root PMA_PASSWORD: secret UPLOAD_LIMIT: 700M PMA_ARBITRARY: 1 ports: - 8081:80 php: build: context: . network: host dockerfile: Dockerfile depends_on: - mysql-server ports: - 80:80 volumes: - ./:/home/beanstalk/laravel/ - ./apache/000-default.conf:/etc/apache2/sites-enabled/000-default.conf
Dockerfile配置
FROM php:8.1-apache WORKDIR /home/beanstalk/laravel RUN docker-php-ext-install mysqli RUN a2enmod headers RUN mkdir -p /home/logs/ RUN touch /home/logs/access.log RUN touch /home/logs/error.log RUN mkdir -p /home/beanstalk/laravel RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN apt-get update -y && apt-get install -y sendmail libpng-dev RUN apt-get update && \ apt-get install -y \ zlib1g-dev \ libzip-dev \ zip RUN docker-php-ext-install zip RUN docker-php-ext-install gd
调试与排查思路
- 修正Apache SSL端口错误:配置里的
<VirtualHost *:433>应为*:443,且容器未配置SSL证书、未映射443端口,若Laravel生成HTTPS链接会导致连接失败。 - 启用Apache重写模块:Laravel依赖
mod_rewrite解析.htaccess规则,当前Dockerfile未执行a2enmod rewrite,需添加该命令并重启Apache。 - 检查Laravel环境变量:确认
.env文件中APP_URL=http://www.testmate-dev.com.au,若设为HTTPS会触发不必要的SSL跳转。 - 配置本地hosts文件:添加
127.0.0.1 www.testmate-dev.com.au,确保浏览器能正确解析域名到本地Docker容器。 - 修复Laravel目录权限:执行
docker exec <php容器名称> chown -R www-data:www-data /home/beanstalk/laravel/storage /home/beanstalk/laravel/bootstrap/cache,赋予Apache用户读写权限。 - 查看Apache错误日志:执行
docker exec <php容器名称> tail -f /var/log/apache2/error.log,排查登录页请求时的具体错误(如文件缺失、权限问题)。 - 检查本地端口占用:执行
lsof -i :80确认80端口未被其他服务占用,避免容器端口映射失败。 - 直接访问登录路径:在浏览器输入
http://www.testmate-dev.com.au/login,跳过重定向流程测试页面是否能正常加载。
内容的提问来源于stack exchange,提问作者IndexZero
相关产品推荐
相关产品推荐

