我有一个基于 laravel 的项目,必须使用 docker 将其托管在 digital ocean 上。docker 在 localhost 上运行良好,但在服务器上显示 404,而 phpmyadmin 在8080
端口上运行良好的服务器上也运行良好。
这是我的docker-compose.yml
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8088:80"
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:5.7.22
restart: unless-stopped
tty: true
ports:
- "4306:3306"
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "laravel_test"
MYSQL_USER: "root"
MYSQL_PASSWORD: "root"
SERVICE_TAGS: "dev"
SERVICE_NAME: "mysql"
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php
volumes:
- ./src:/var/www/html
ports:
- "9000:9000"
restart: unless-stopped
networks:
- laravel
phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8080:80
environment:
- PMA_ARBITRARY=1
links:
- mysql
networks:
- laravel
npm:
image: node:18-alpine3.14
container_name: npm
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
entrypoint: [ 'npm', '--no-bin-links' ]
networks:
- laravel
composer:
image: composer:latest
container_name: composer
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
depends_on:
- php
networks:
- laravel
Dockerfile
FROM php:8.1.0-fpm
RUN docker-php-ext-install pdo pdo_mysql
默认配置文件
server{
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
我的 php 文件位于./src/public/