記事一覧表示

localhostを開こうとしたらForbiddenエラーを吐かれた話

 久しぶり(5〜6年前位ぶり?)にMacBookAir(10.13.6 HighSierra)で http://localhost/ を開こうとしたら、以下のようなエラーが出た。

Forbidden

You don't have permission to access / on this server.

 権限が無いとか書いてあるので、ひとまず、設定ファイル(httpd.conf)にエラーが無いかを確かめる為に以下のコマンドを実行。

% apachectl configtest
AH00112: Warning: DocumentRoot [/usr/docs/dummy-host.example.com] does not exist
AH00112: Warning: DocumentRoot [/usr/docs/dummy-host2.example.com] does not exist
Syntax OK

 DocumentRoot /usr/docs/dummy-host2.example.com が無いって言ってるから、それを呼び込んでいるファイルを /private/etc/apache2 から探してみる。

/private/etc/apache2% grep -r "/usr/docs/dummy-host2.example.com" ./*
        ︙
./extra/httpd-vhosts.conf:    DocumentRoot "/usr/docs/dummy-host2.example.com"
        ︙

 調べてみるとhttpd-vhosts.confは、仮想ホストの設定を行っているファイルらしい1
 書き方2を調べてhttpd-vhosts.confを以下のように書き換えた。

     ︙
#<VirtualHost *:80>                                                                    
#    ServerAdmin webmaster@dummy-host.example.com                                      
#    DocumentRoot "/usr/docs/dummy-host.example.com"                                   
#    ServerName dummy-host.example.com                                                 
#    ServerAlias www.dummy-host.example.com                                            
#    ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"              
#    CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common     
#</VirtualHost>                                                                        
#                                                                                      
#<VirtualHost *:80>                                                                    
#    ServerAdmin webmaster@dummy-host2.example.com                                     
#    DocumentRoot "/usr/docs/dummy-host2.example.com"                                  
#    ServerName dummy-host2.example.com                                                
#    ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"             
#    CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common    
#</VirtualHost>                                                                        

<VirtualHost *:80>
   ServerName localhost
   DocumentRoot /Users/username/Sites
</VirtualHost>

 要らない設定(仮想ホスト dummy-host.example.comdummy-host2.example.com )をコメントアウトし、新しくlocalhostを設定。
 sudo apachectl restart したら無事に動いた。
 うーん、こんな設定した覚え全く無いけど、前(5〜6年程前)動いてた時は、標準で設定してくれてたのか?OSのバージョンアップで知らない内に無くなってしまったのか?謎だ…


  1. このページを参照。

  2. このページを参照。