【改定１】Apache 2.2 / 2.4 バーチャルホスト設定ファイル テンプレート 

メモ

※　2.2 と 2.4 同じ conf ファイルを使用できるようにした。

[前バージョン](http://qiita.com/asahina_dev/items/771209180ed4a2eb73a5)

## SSL 対応版

```apache
<VirtualHost 192.168.0.2:80>
        DocumentRoot /var/www/vhost/bts.example.test/public_html
        ServerName bts.example.test

        <IfModule ssl_module>
                Redirect / https://bts.example.test
        </IfModule>

        ErrorLog  "| /usr/local/apache2/bin/rotatelogs  /var/www/vhost/bts.example.test/logs/error_log 86400"
        CustomLog "| /usr/local/apache2/bin/rotatelogs  /var/www/vhost/bts.example.test/logs/access_log 86400" common
        CustomLog "| /usr/local/apache2/bin/rotatelogs  /var/www/vhost/bts.example.test/logs/host.log 86400" host
        CustomLog "| /usr/local/apache2/bin/rotatelogs  /var/www/vhost/bts.example.test/logs/agent.log 86400" agent
        CustomLog "| /usr/local/apache2/bin/rotatelogs  /var/www/vhost/bts.example.test/logs/referer.log 86400" referer

</VirtualHost>


<IfModule ssl_module>
<VirtualHost 192.168.0.2:443>

        DocumentRoot /var/www/vhost/bts.example.test/public_html
        ServerName bts.example.test

        ErrorLog  "| /usr/local/apache2/bin/rotatelogs  /var/www/vhost/bts.example.test/logs/error_log 86400"
        CustomLog "| /usr/local/apache2/bin/rotatelogs  /var/www/vhost/bts.example.test/logs/access_log 86400" common
        CustomLog "| /usr/local/apache2/bin/rotatelogs  /var/www/vhost/bts.example.test/logs/host.log 86400" host
        CustomLog "| /usr/local/apache2/bin/rotatelogs  /var/www/vhost/bts.example.test/logs/agent.log 86400" agent
        CustomLog "| /usr/local/apache2/bin/rotatelogs  /var/www/vhost/bts.example.test/logs/referer.log 86400" referer

        SSLEngine on
        SSLProtocol all -SSLv2
        SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
        SSLCertificateFile    /var/www/vhost/bts.example.test/conf/server.crt
        SSLCertificateKeyFile /var/www/vhost/bts.example.test/conf/server.key

</VirtualHost>
</IfModule>

<Directory  /var/www/vhost/bts.example.test/public_html>
    # default charset UTF-8
    AddDefaultCharset utf-8

    # .htaccess setting
    AllowOverride All

    # CGI OK
    Options +FollowSymLinks +ExecCGI

    <IfVersion = /^2.2./ >
        # version 2.2
        Order Deny,Allow
        Allow from all
    </IfVersion>
    <IfVersion = /^2.4./ >
        # version 2.4
        Require all granted
    </IfVersion>
</Directory>
```

## SSL 非対応版
IfVersion でエラーになる場所を変更するのが楽そう

```apache:vhost.conf
<VirtualHost _default_:80>
        DocumentRoot /var/www/vhost/vhost1.localhost/html
        ServerName vhost1.localhost

        ErrorLog logs/vhost1-error_log
        CustomLog logs/vhost1-access_log common
        <Directory  /var/www/vhost/vhost1.localhost/html >
                # default charset UTF-8
                AddDefaultCharset utf-8

                # .htaccess setting
                AllowOverride All

                # SSI OK
                # SymbolicLink OK
                Options +Includes +FollowSymLinks

                <IfVersion = /^2.2./ >
                    # version 2.2
                    Order Deny,Allow
                    Allow from all
                </IfVersion>
                <IfVersion = /^2.4./ >
                    # version 2.4
                    Require all granted
                </IfVersion>
        </Directory>

</VirtualHost>
```

# 自動設定

```bash#!/bin/sh

hostname=$1


mkdir -p /var/www/$hostname/{work,conf,public_html}
[ -e /var/www/$hostname/work ] || ( echo execute rooted && exit 9 )
mkdir -p /etc/httpd/logs/${hostname}

cd /var/www/$hostname/conf

echo "

<VirtualHost _default_:80>

        DocumentRoot /var/www/${hostname}/public_html
        ServerName ${hostname}

        ErrorLog logs/${hostname}/error_log
        CustomLog logs/${hostname}/access_log common

        <Directory  /var/www/${hostname}/public_html>
                # default charset UTF-8
                AddDefaultCharset utf-8

                # .htaccess setting
                AllowOverride All

                # CGI OK
                Options +FollowSymLinks +ExecCGI

                <IfVersion = /^2.2./>
                    # apache 2.2 changed config
                    Order allow,deny
                    allow from ip 192.168.0.3
                    allow from ip 192.168.0.4
                </IfVersion>
                <IfVersion = /^2.4./>
                    Require ip 192.168.0.3
                    Require ip 192.168.0.4
                </IfVersion>
        </Directory>

</VirtualHost>
" > httpd.${hostname}.conf
find /var/www/$hostname/
```