チェ・ゲバムラの日記

脱犬の道を目指す男のブログ

VirtualHostの書き方.CentOS

コピペで済ませてて、Directoryを書く意味がよくわかってなかったのでメモ。

オプションの指定とかっぽいね。

 

とりあえず基本のコピペ用

NameVirtualHost *:80

 

<VirtualHost *:80>
ServerName any
Redirect / https://xxxxxxxxx.jp/
</VirtualHost>

 

<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName XXXXXXX
<Directory "/var/www/html">
Options -Indexes
AllowOverride All
</Directory>
</VirtualHost>

 

<VirtualHost *:80>
DocumentRoot /var/www/html/XXXXXXX
ServerName XXXXXXX.jp
ErrorLog logs/error_log_XXXXXXX
CustomLog logs/access_log_XXXXXXX combined
<Directory "/var/www/html/XXXXXXX">
Options -Indexes
AllowOverride All
</Directory>

</VirtualHost>

 

下記はSSL対応しつつ、AWSつかった時の。

TraceEnable Off

<Files ~ "\.(html|php|js|css)$">
FileEtag None
RequestHeader unset If-Modified-Since
Header set Cache-Control no-store
</Files>

NameVirtualHost *:80
<VirtualHost *:80>
ServerName 192.168.XXXXX
DocumentRoot /var/www/html
<Directory /var/www/html>
Options -Indexes
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

 

<VirtualHost *:80>
ServerName XXXXXXXXXXXX
DocumentRoot /var/www/XXXX
ErrorLog /var/log/httpd/error_XXXX_log
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /var/log/httpd/access_XXXX_log combined
# 送信元IPアドレスとして、X-Forwarded-Forを利用する
RemoteIPHeader X-Forwarded-For
<Directory /var/www/XXXX>
Options -Indexes
AllowOverride All

Require all granted
#Require ip 127.0.0.1 XXXX XXXX
</Directory>
</VirtualHost>


<VirtualHost *:8080>
ServerName XXXXXXXXXXX
DocumentRoot /var/www/XXXX
ErrorLog /var/log/httpd/error_XXXX_log
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /var/log/httpd/access_XXXX_log combined
# RewriteEngine On
# RewriteCond %{HTTPS} off
# RewriteCond %{REQUEST_URI} ^/users/.*$
# RewriteRule /.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RemoteIPHeader X-Forwarded-For
<Directory /var/www/XXXX>
Options -Indexes
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

  

 

あと何気に大事なことで、IPアドレスでアクセスされたときとかはリダイレクトさせたりとか。

virtualhost.confで最初に書いた設定はデフォルトの設定になるのでちゃんと処理した方がいい。詳細は下記。

qiita.com

 

■解説

NameVirtualHost *:80

<VirtualHost IPアドレス:ポート番号>
ServerName 仮想ホスト名
DocumentRoot ドキュメントルートのパス
ServerAdmin 管理者のメールアドレス

# 必要ならここにディレクトリ固有の設定を記述
<Directory "ドキュメントルートのパスなど">
Options All
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>

 

ちなみにパーフェクトPHPで書いたやつ

NameVirtualHost *:80

<VirtualHost *:80>

ServerName study.localhost

Documentroot /var/www/study.localhost

DirectoryIndex index.php index.html

 

<Directory "/var/www/study.localhost"> 

        AllowOverride All

        Allow from All

</Directory>

</Virtualhost>

 

 ■VHOST内でリダイレクト設定をするには以下だけ。
<VirtualHost 192.168.0.200>
ServerName www.example.jp
Redirect / http://www.example.com/
</VirtualHost>

 

■WWWありなしの設定
Servername example.com
ServerAlias www.example.com

■Virtualhost文法チェック                     

httpd -S

 

MacVagrantいれてvirtualhost設定したけどhostsに設定しても動かない。

よくわからないけどhost prefpane gitで出てくるのをインストールして環境設定にIPアドレスと好きな名前をつけたらいけた。

/etc/hostsでの設定はなぜかきかないのでここは謎。