LoginSignup
17

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-01-17

メモ

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

前バージョン

SSL 対応版

<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 でエラーになる場所を変更するのが楽そう

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>

自動設定


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/

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17