Dot-Htaccess

Docker httpd & htaccess - 無法打開密碼文件

  • August 9, 2019

我有一個我無法解決的錯誤,即使我最近的研究也是如此。

文件夾結構:

-rw-r--r-- 1 root root   105 Aug  8 15:58 Dockerfile
-rw-r--r-- 1 root root 21371 Aug  8 16:05 my-httpd.conf
drwxr-xr-x 2 root root  4096 Aug  8 15:37 www

1 - 我用一個簡單的 Dockerfile 創建了一個 apache 的圖像

Dockerfile:

FROM httpd:2.4
WORKDIR /usr/local/apache2/htdocs
COPY ./my-httpd.conf /usr/local/apache2/conf/httpd.conf

命令:

docker build -t apache .

2 - 我用新圖像執行一個容器

命令:

docker run -v /MY_PATH/www/:/usr/local/apache2/htdocs/ -dit --name apache -p 80:80 apache

3 - 我想用 htaccess 保護對 www 文件夾的訪問

文件夾結構:

-rwxr-xr-x 1 root root  116 Aug  8 15:37 .htaccess
-rw-r--r-- 1 root root   46 Aug  8 15:34 .htpasswd
-rwxrwxrwx 1 root root  169 Aug  7 10:52 index.html
-rw-r--r-- 1 root root  354 Aug  7 10:56 master.css
-rw-r--r-- 1 root root   26 Aug  7 10:57 robots.txt

.htaccess

AuthType Basic
AuthName "Restricted Area"
AuthUserFile /MY_PATH/www/.htpasswd
Require valid-user

4 - 我嘗試在瀏覽器上訪問 index.html

連接視窗出現,我輸入我的登錄名和密碼。

然後我有一個500 內部伺服器錯誤

5 - 日誌說“無法打開密碼文件”

當我檢查 apache 容器日誌時,我有這個:

[Fri Aug 09 07:19:58.458778 2019] [authn_file:error] [pid 7:tid 140331369154304] (2)No such file or directory: [client 194.214.141.5:64325] AH01620: Could not open password file: /MY_PATH/www/.htpasswd
194.214.141.5 - user [09/Aug/2019:07:19:58 +0000] "GET / HTTP/1.1" 500 528
[Fri Aug 09 07:19:58.534805 2019] [authn_file:error] [pid 7:tid 140331135788800] (2)No such file or directory: [client 194.214.141.5:55037] AH01620: Could not open password file: /MY_PATH/www/.htpasswd, referer: http://mywebsite.com/

6 - 我需要你的幫助 :)

我在Google上搜尋了很多次,我唯一找到的就是“SELinux”,像這樣:權限被拒絕:無法打開密碼文件。

我不確定這與使用 Docker 是否相同。

因此,如果有人可以幫助我,那就太好了!:)

容器中的 Apache 不知道你的外部路徑/MY_PATH/www/,你需要使用容器內部的路徑:

AuthUserFile /usr/local/apache2/htdocs/.htpasswd

引用自:https://serverfault.com/questions/978584