我已經按照這個論壇中的一些說明、教程和問題進行了操作,但我仍然無法正常作業。我已經使用 PHP Apache Docker 容器設定了一個 REST API,并且需要在我的 apache 配置中創建一個重寫規則,以將 API 請求重新路由到index.php
(REST 控制器)。目前,根據下面的內容,我得到了 404:
本地機器上的檔案結構(列出了除 php 源代碼之外的所有內容;此處不需要):
php
conf.d
- error_reporting.ini
- xdebug.ini
Dockerfile
index.php
apache
- apache2.conf
docker-compose.yml
Dockerfile 的內容是:
FROM php:8.1-apache
WORKDIR /var/www/html/
RUN pecl install xdebug \
&& docker-php-ext-enable xdebug \
&& a2enmod rewrite
COPY . .
EXPOSE 80
docker-compose.yml 的內容是:
services:
php:
build: ./php
depends_on:
- db
container_name: php-apache
ports:
- 80:80
volumes:
- ./php:/var/www/html
- ./php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
- ./php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
- ./apache/apache2.conf:/etc/apache2/apache2.conf
environment:
MARIADB_HOST: localhost
MARIADB_USER: root
MARIADB_PASSWORD: top_very_secret
MARIADB_DB: apidb
adminer:
image: adminer
depends_on:
- db
restart: always
ports:
- 8080:8080
db:
image: mariadb
container_name: db
volumes:
- maria-db-storage:/var/lib/mysql
environment:
MARIADB_ROOT_PASSWORD: top_very_secret
MARIADB_DATABASE: apidb
volumes:
maria-db-storage:
關于內容apache2.conf
;我已完成以下操作以在本地計算機上創建它:
- 使用 . 進入容器的虛擬檔案系統
docker exec -t -i <container_name> /bin/bash
。 - 徘徊
/etc/apache2
- 通過列印檔案內容
cat apache2.conf
- 將內容復制粘貼到我的本地
/apache/apache2.conf
檔案中 - 將以下指令行添加到該本地檔案的末尾:
# Custom directives start here
# Set Server's name
ServerName 'localhost'
# Rewrite for routing of all requests through REST controller
RewriteEngine On
# If requested resource is index.php, do nothing
RewriteRule ^index\.php$ - [L]
# If requested resource is a file or directory that does not exist, reroute to REST
# controller index.php
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule . /index.php [L]
在構建映像并運行容器后,我再次通過 CLI 檢查了容器的虛擬檔案系統。成功保存了我的本地檔案的/etc/apache2/apache2.conf
內容,我也在apachectl -M
容器內做了一個,并且可以看到rewrite_module (shared)
列印出來。
同樣,我只是得到一個 404;例如,如果我搜索http://localhost/xyz
. 如果我不省略埠(并搜索http://localhost:80/xyz
),則相同。搜索http://localhost
和http://localhost:80
兩者都作業;所以看來我的重寫規則根本沒有被應用。
apachectl configtest
在 docker 容器中運行時,我也得到Syntax OK
.
只是猜測;這可能與來自容器埠 9003 的 xdebugs 傳出通信有關嗎?
我錯過了什么?
顯然 serverfault 是這些問題的預期位置;所以我把它貼在那里(不知道如何遷移):https ://serverfault.com/questions/1115336/cant-get-apache-rewrite-to-work-on-docker-php-apache-container
uj5u.com熱心網友回復:
我其實已經想通了。問題似乎是,在使用 docker 時,您必須在您指定的埠的虛擬主機背景關系中指定重寫規則。至少我已經完成了上面寫的,加上以下步驟:
- 在容器中,我已(在 CLI 中)遷移到檔案夾
etc/apache2/sites-enabled
- 其中有一個檔案
000-default.conf
,其中包含當前在相關 apache 單元上啟用的站點的默認配置。就這個應用程式而言,修改默認配置就可以了,因為容器將專門用于這個 API。至少這是我的想法。 - 所以我又做了同樣的事;復制檔案內容,因為它們與啟動的 docker 容器一起提供,通過
cat 000-default.conf
. - 復制粘貼本地
apache/sites-enabled/000-default.conf
檔案中的內容(您新創建的) - 現在您采用以前在本地
apache2.conf
檔案中的以下部分(略有改進):
# Rewrite for routing of all requests through REST controller
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ /index.php [L]
并將其粘貼到本地000-default.conf
檔案中;在結束</VirtualHost>
標記之前。然后從檔案中洗掉重寫規則apache/apache2.conf
,但將其留給添加的ServerName
指令。
- 然后,您將路徑映射添加到您的 php 容器的卷中,以將您的自定義虛擬主機默認組態檔加載到您的容器中,您應該一切順利;所以你的 docker-compose.yml 變成:
services:
php:
....
volumes:
- ./php:/var/www/html
- ./php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
- ./php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
- ./apache/apache2.conf:/etc/apache2/apache2.conf
- ./apache/sites-enabled/000-default.conf:/etc/apache2/sites-enabled/000-default.conf
....
重寫現在功能齊全!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/533098.html