apache2でマルチドメイン(Virtual Host)の運用をする

2017/02/23

t f B! P L
【キーワード】

apache2,マルチドメイン,Virtual Host

【目的】

Apach2で独自ドメインを運用する。
mod_rewirteモジュールの有効化を実施する。

【ドメイン】

hogehoge.com(便宜上ドメインは各自の環境で読み替える)

【アプローチ】

1:Apache2の設定ファイルを新規作成(hogehoge.com.confの作成)
 ┣【ファイルの作成】を参照
 ┗ <修正前データ(元データ)>を参照し<修正後データ>に

2:公開用のディレクトリを作成する

$ sudo mkdir -p /var/www/vhost/sesiri.club/html
$ sudo chown -R www-data:www-data /var/www/vhost/

3:a2ensiteコマンドで、設定を登録する
$ sudo a2ensite hogehoge.com.conf

4:Apache2、再読込処理

$ sudo service apache2 reload

<ここからmod_rewriteの設定>

5:mod_rewriteが利用可能になっているかを確認する
$ cat /etc/apache2/mods-available/rewrite.load
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

6:a2enmodコマンドでmod_rewriteを実行する
$ sudo a2enmod rewrite

7:Apache2の再起動を実行
$ sudo service apache2 restart


【バージョンの確認】

<Apach2のバージョン>

$ apachectl -v
Server version: Apache/2.4.18 (Ubuntu)
Server built:   2016-07-14T12:32:26

【ファイルの作成】

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/hogehoge.com.conf

<修正前データ(元データ)>

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

<修正後データ>

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin info@hogehoge.com
        ServerName hogehoge.com
        DocumentRoot /var/www/vhost/hogehoge.com/html
Options -Indexes +FollowSymLinks

<Directory "/var/www/vhost/hogehoge.com/html"> AllowOverride All </Directory>
        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

※設定したconfファイルの文法をチェックするコマンド
$ apachectl configtest

このブログを検索

フォロワー

QooQ