0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Apache VirtualHost を別の DocumentRoot に設定ごと切り替える

Posted at

OS

Ubuntu 24.04 (WSL2)

概要

A から B に DocumentRoot を切り替える。
このときに、A と B で別々の 設定ファイルを作成し、
それぞれを別のパスに保管しておきつつ、
新しい B の設定ファイルに記載した設定のみを反映させる方法です。

A ( 切り替える前 )

DocumentRoot /var/www/html
設定ファイルのパス /etc/apache2/sites-available/dev.local.conf

B ( 切り替えたあと )

DocumentRoot /var/www/wordpress
設定ファイルのパス /etc/apache2/sites-available/wordpress.conf

設定手順

.bash
# 現在有効な設定ファイルを確認する。
ls -l /etc/apache2/sites-enabled/
total 0
lrwxrwxrwx 1 root root 33 Aug 11 17:00 dev.local.conf -> ../sites-available/dev.local.conf

# 現在の設定ファイル (dev.local.conf) が存在することを確認。
cd /etc/apache2/sites-available
ls -al
total 24
drwxr-xr-x 2 root root 4096 Aug 22 13:42 .
drwxr-xr-x 8 root root 4096 Aug 15 16:48 ..
-rw-r--r-- 1 root root 1286 Mar 18  2024 000-default.conf
-rw-r--r-- 1 root root 4573 Mar 18  2024 default-ssl.conf
-rw-r--r-- 1 root root 1672 Aug 14 12:15 dev.local.conf

# 元の設定ファイルが参照している ディレクトリ
ls -al /var/www/html
total 24
drwxr-xr-x 2 root root  4096 Aug 14 09:48 .
drwxr-xr-x 3 root root  4096 Aug 10 15:18 ..
-rw-r--r-- 1 root root 10671 Aug 10 15:18 index.html
-rw-r--r-- 1 root root    18 Aug 14 09:48 phpinfo.php

# -a オプションで、パーミッションやタイムスタンプごとコピーする
sudo cp -a /var/www/html /var/www/wordpress
[sudo] password for username:

# 新しく DocumentRoot に設定するディレクトリが作成されたことを確認。
ls -al /var/www/wordpress
total 24
drwxr-xr-x 2 root root  4096 Aug 14 09:48 .
drwxr-xr-x 4 root root  4096 Aug 22 13:59 ..
-rw-r--r-- 1 root root 10671 Aug 10 15:18 index.html
-rw-r--r-- 1 root root    18 Aug 14 09:48 phpinfo.php

# 元の設定ファイルをコピーし、新しい設定ファイルを作成。
sudo cp -a dev.local.conf wordpress.conf

# 新しい設定ファイルを開き、DocumentRoot を設定する
# 設定内容は、下記を参照
sudo nano wordpress.conf

# DocumentRoot を適切に設定したことを確認。
sudo cat wordpress.conf | grep DocumentRoot
        DocumentRoot /var/www/wordpress

# 設定ファイルにエラーがないことを確認。
sudo apachectl configtest
Syntax OK

# 元の設定ファイルをバックアップ
sudo cp -a dev.local.conf disabled_dev.local.conf

# 元の設定を無効化。
sudo a2dissite dev.local
Site dev.local disabled.
To activate the new configuration, you need to run:
  systemctl reload apache2

# 設定 ( 無効化 ) を反映する。
sudo systemctl reload apache2

# 有効な設定から除外されたことを確認。
ls -l /etc/apache2/sites-enabled/
total 0

# 再度、設定ファイルにエラーがないことを確認。
sudo apachectl configtest
Syntax OK

# 新しい設定ファイルを有効化。
sudo a2ensite wordpress
Enabling site wordpress.
To activate the new configuration, you need to run:
  systemctl reload apache2

# 設定 ( 有効化 ) を反映する。
sudo systemctl reload apache2

# 設定が有効化されたことを確認。
ls -l /etc/apache2/sites-enabled/
total 0
lrwxrwxrwx 1 root root 33 Aug 22 14:15 wordpress.conf -> ../sites-available/wordpress.conf

Webブラウザでアクセスできるかテスト。

設定 A ( 切り替える前の 設定 )

後半に PHP-CGI の設定が入っていますが、なくても動きに変わりはないです。

/etc/apache2/sites-available/dev.local.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
        ServerName dev.local
        ServerAlias *.dev.local
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        <Directory /var/www/html>
            Require all granted
        </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
        <Directory "/usr/lib/cgi-bin">
            AllowOverride None
            Options +ExecCGI
            AddHandler cgi-script .php
            Require all granted
        </Directory>
        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
</VirtualHost>

設定 B ( 切り替えたあとの 設定 )

DocumentRoot と、ログのパスをかえています。

/etc/apache2/sites-available/wordpress.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/wordpress

        <Directory /var/www/wordpress>
            Require all granted
        </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}/wordpress_error.log
        CustomLog ${APACHE_LOG_DIR}/wordpress_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>

さいごに不要な設定ファイル ( 設定 A ) を削除しておきます。

.bash
# 確認
ls -al | grep dev.local
-rw-r--r-- 1 root root 1672 Aug 14 12:15 dev.local.conf
-rw-r--r-- 1 root root 1672 Aug 14 12:15 disabled_dev.local.conf

# 削除
sudo rm -f dev.local.conf
[sudo] password for username:

# 再度確認 ( バックアップした設定 A のファイルだけが残っている )
ls -al | grep dev.local
-rw-r--r-- 1 root root 1672 Aug 14 12:15 disabled_dev.local.conf
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?