Apache-2.2

如何在 Apache httpd 虛擬主機中配置基本身份驗證?

  • January 24, 2018

我正在嘗試使用 Apache http 配置 mercurial 訪問。它需要身份驗證。我的/etc/apache2/sites-enabled/mercurial樣子是這樣的:

NameVirtualHost *:8080

<VirtualHost *:8080>
   UseCanonicalName Off
   ServerAdmin  webmaster@localhost
   AddHandler cgi-script .cgi
   ScriptAliasMatch ^(.*) /usr/lib/cgi-bin/hgwebdir.cgi/$1
</VirtualHost>

我在網際網路上閱讀的每個教程都告訴我插入這些行:

AuthType Basic
AuthUserFile /usr/local/etc/httpd/users

但是當我這樣做時,我收到以下錯誤:

# /etc/init.d/apache2 reload
Syntax error on line 8 of /etc/apache2/sites-enabled/mercurial:
AuthType not allowed here

我的發行版是一個定制的 Ubuntu,稱為 Turnkey Linux Redmine

你應該把它放在一個 Location 指令中:

<VirtualHost *:8080>

<Location /> #the / has to be there, otherwise Apache startup fails
           Deny from all
           #Allow from (You may set IP here / to access without password)
           AuthUserFile /usr/local/etc/httpd/users
           AuthName authorization
           AuthType Basic
           Satisfy Any # (or all, if IPs specified and require IP + pass)
                       # any means neither ip nor pass
           require valid-user
</Location>
...
</VirtualHost>

我在 ubuntu 10.04 上執行 Apache2——同樣的問題,感謝您的解決方案。我發現我必須將配置放入/etc/apache2/apache2.conf

您可以使用 htpasswd 生成使用者名和密碼。新文件:

$ htpasswd -c /srv/auth/.htpasswd squire

要附加到現有文件:

$ htpasswd -b /srv/auth/.htpasswd squire2 tickleme2

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