自宅サーバー ≫ Linux Server ≫ CentOS Stream 8 ≫ Webサーバー (バーチャルドメイン apache)

 
前提条件
Webサーバーが稼働していることが前提条件
バーチャルドメイン(バーチャルホスト)
  複数のドメインを取得して、それを1台のサーバーで運用する場合にバーチャルドメイン(バーチャルホスト)機能を使うことがある
これについては色々なサイト様で設定方法等を解説しています
しかし、一般的には個人が複数のドメインを取得する事はあまりない
当然、私も一つのドメインしか持っていない

私は、普通のサイト・ブログサイト・会員専用サイト等複数のサイトを一つのドメインで運用したいと・・・
しかし、色々なサイトを見ても複数ドメインの設定方法は書いてあるが、その設定方法では一つのドメインでの運用は上手く行かず試行錯誤の結果なんとか成功

ドメインの先頭部分下記の様に変更する事で運用
 
  ・普通のサイト
・ブログサイト
・会員専用サイト
www.papa-net.info
blog.papa-net.info
club.papa-net.info
ここからは、普通のサイト(www.papa-net.info)とブログサイト(blog.papa-net.info)を例にする
前提条件
apache のインストール及び設定が完了している事が前提
ホームページ用ディレクトリの作成
**** ディレクトリの中にホームページ公開用のサブディレクトリ blog を作成
 
 
[root@centos]# mkdir /home/****/blog
 
[root@centos]# chown ****. /home/****/blog  (所有者の指定)
 
[root@centos]# chmod 775 /home/****/       (アクセス権の変更)
 
[root@centos]# chmod 775 /home/****/blog/   (アクセス権の変更)
 
※ /home/****/は私の環境なので自分の環境に合わせる
apache の設定
サーバーネームとディレクトリに関する記述を全てコメント化
 
 
[root@centos]# vi /etc/httpd/conf/httpd.conf
 
 
途中略
 
 #
 # ServerName gives the name and port that the server uses to identify itself.
 # This can often be determined automatically, but we recommend you specify
 # it explicitly to prevent problems during startup.
 #
 # If your host doesn't have a registered DNS name, enter its IP address here.
 #
 #ServerName www.papa-net.info:80   <--- コメント化
 
 
途中略
 
 #
 # DocumentRoot: The directory out of which you will serve your
 # documents. By default, all requests are taken from this directory, but
 # symbolic links and aliases may be used to point to other locations.
 #
 
#DocumentRoot "/var/www/html"   <--- コメント化
 
 ## Relax access to content within /var/www.
 #
 
#<Directory "/var/www">   <--- コメント化
    
#AllowOverride None   <--- コメント化
     # Allow open access:
    
#Require all granted  <--- コメント化
 
#</Directory>             <--- コメント化

 # Further relax access to the default document root:
 
#<Directory "/var/www/html">   <--- コメント化
     #
     # Possible values for the Options directive are "None", "All",
     # or any combination of:
     # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
     #
     # Note that "MultiViews" must be named *explicitly* --- "Options All"
     # doesn't give it to you.
     #
     # The Options directive is both complicated and important. Please see
     # http://httpd.apache.org/docs/2.4/mod/core.html#options
     # for more information.
     #
    
#Options Indexes FollowSymLinks   <--- コメント化

     #
     # AllowOverride controls what directives may be placed in .htaccess files.
     # It can be "All", "None", or any combination of the keywords:
     # Options FileInfo AuthConfig Limit
     #
    
#AllowOverride None   <--- コメント化
     #
     # Controls who can get stuff from this server.
     #
    
#Require all granted   <--- コメント化
 
#</Directory>   <--- コメント化
 
 
途中略
 
 
#<IfModule alias_module>
     #
     # Redirect: Allows you to tell clients about documents that used to
     # exist in your server's namespace, but do not anymore. The client
     # will make a new request for the document at its new location.
     # Example:
     # Redirect permanent /foo http://www.example.com/bar

     #
     # Alias: Maps web paths into filesystem paths and is used to
     # access content that does not live under the DocumentRoot.
     # Example:
     # Alias /webpath /full/filesystem/path
     #
     # If you include a trailing / on /webpath then the server will
     # require it to be present in the URL. You will also likely
     # need to provide a <Directory> section to allow access to
     # the filesystem path.

     #
     # ScriptAlias: This controls which directories contain server scripts.
     # ScriptAliases are essentially the same as Aliases, except that
     # documents in the target directory are treated as applications and
     # run by the server when requested rather than as documents sent to the
     # client. The same rules about trailing "/" apply to ScriptAlias
     # directives as to Alias.
     #
    
#ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"   <--- コメント化

 
#</IfModule>   <--- コメント化

 #
 # "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
 # CGI directory exists, if you have that configured.
 #
 
#<Directory "/var/www/cgi-bin">   <--- コメント化
    
#AllowOverride None    <--- コメント化
    
#Options None          <--- コメント化
    
#Require all granted   <--- コメント化
 
#</Directory>              <--- コメント化
   
 
以下略
 
バーチャルドメイン・バーチャルホスト設定
バーチャルホスト用の設定ファイルは /etc/httpd/conf.d/ に作成
ここに作成された".conf" 拡張子がついた設定ファイルはApacheのメインファイル "httpd.conf" に自動で取り込まれる
名前は何でも構わないがわかりやすく "バーチャルドメイン" + ".conf"
上記の設定でサーバーネームとディレクトリ関係の記述を全てコメント化したので www・blog の両方のディレクトリ関係の記述を行う
  また、ドメインの頭部を変えてバーチャルドメインにしている関係上バーチャルドメインに設定されていないアクセスを拒否するファイルも作成
バーチャルホスト www.papa-net.info.conf の 作成と設定
 
 
[root@centos]# vi /etc/httpd/conf.d/www.papa-net.info.conf
 
 
以下新規作成 
 

 <VirtualHost *:80>
 
     DocumentRoot /home/****/www
     ServerName www.papa-net.info

     ErrorLog logs/www.papa-net.info-error_log
     CustomLog logs/www.papa-net.info-access_log combined env=!no_log

  
    
<Directory "/home/****/www">
         Options ExecCGI FollowSymLinks
         AllowOverride All
         Require all granted
     </Directory>

 

     <IfModule alias_module>
         ScriptAlias /cgi-bin/ "/home/****/www/cgi-bin/"
     </IfModule>
 
     <Directory "/home/****/www/cgi-bin">
         AllowOverride None
         Options Includes ExecCGI FollowSymLinks
         Require all granted
     </Directory>
 
 </VirtualHost>
 
 
バーチャルホスト blog.papa-net.info.conf の 作成と設定
 
 
[root@centos]# vi /etc/httpd/conf.d/blog.papa-net.info.conf
 
 以下新規記述
 

 <VirtualHost *:80>
 
     DocumentRoot /home/****/blog
     ServerName blog.papa-net.info

     ErrorLog logs/blog.papa-net.info-error_log
     CustomLog logs/blog.papa-net.info-access_log combined env=!no_log

  
    
<Directory "/home/****/blog">
         Options ExecCGI FollowSymLinks
         AllowOverride All
         Require all granted
     </Directory>

 

     <IfModule alias_module>
         ScriptAlias /cgi-bin/ "/home/****/blog/cgi-bin/"
     </IfModule>
 
     <Directory "/home/****/blog/cgi-bin">
         AllowOverride None
         Options Includes ExecCGI FollowSymLinks
         Require all granted
     </Directory>
 
 </VirtualHost>
 
 
アクセスを拒否するファイルの作成と設定
 
 
[root@centos]# vi /etc/httpd/conf.d/virtualhost-reject.conf
 
 以下新規記述
 

 <VirtualHost _default_:80>
     ServerName any
     <Location />
          Require all denied
      <--- 全て拒否
     </Location>
 </VirtualHost>

 
httpd ファイルの構文チェック及びバーチャルドメイン一覧表示
 
 [root@
centos]# httpd -t
 Syntax OK
      <--- Syntax OK  になっていればOK

 [root@
centos]# httpd -t -D DUMP_VHOSTS
 VirtualHost configuration:
 *:80                   is a NameVirtualHost
          default server blog.papa-net.info (/etc/httpd/conf.d/blog.papa-net.info.conf:1)
          port 80 namevhost blog.papa-net.info (/etc/httpd/conf.d/blog.papa-net.info.conf:1)
          port 80 namevhost www.papa-net.info (/etc/httpd/conf.d/www.papa-net.info.conf:1)

          port 80 namevhost any (/etc/httpd/conf.d/virtualhost-reject.conf:1)
 
バーチャルドメイン(バーチャルホスト)の設定ファイルは数値・アルファベット順に読み込まれる
そのため上記ではデフォルトサーバーは blog.papa-net.info になっている
apache を再起動
 
 [root@
centos]# systemctl restart httpd
 
hosts ファイルの変更
  バーチャルドメイン(バーチャルホスト)を使って複数のホームページを立ち上げた場合にネットワーク内のクライアントPCからIPアドレスで接続しようとするとメインのホームページ しか表示されない
DNSサーバーを構築すれば問題ないが、そこまでやる必要がない場合に hosts ファイルを変更してドメイン名で目的のホームページに行く事ができる

戻る