在CentOS7上进行VirtualHost设置

环境

    • macOS Monterey v12.1

 

    • Vagrant v2.3.1

 

    • CentOS 7

 

    Apache 2.4.6 (CentOS)

VirtualHost是什么?

在一台计算机上配置能够处理两个或更多个网站的设置。需要创建并确定每个网站的目录位置的工作。※设置文件→/etc/httpd/conf/httpd.conf

什么是DocumentRoot?

在网上公开的目录。
基本上,显示的是位于DocumentRoot下的文件。
※文档根目录→/var/www/html

准备文件

确认DocumentRoot目录

$ vagrant up
$ vagrant ssh

$ grep -i 'DocumentRoot' /etc/httpd/conf/httpd.conf
# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/var/www/html"
# access content that does not live under the DocumentRoot.

在DocumentRoot目录的/var/www/html内创建目录和文件。
在每个index.html文件中使用vi编辑器编写

Site A

或类似的内容,以便易于理解。
*稍后在浏览器中显示。

var/www/html
         ├ index.html
         ├ site_a
         │  └ index.html
         └ site_b
            └ index.html

主机配置

在Mac上打开终端,然后修改hosts文件设置。
* 将IP地址与字符串连接起来,以区分显示相同IP地址的页面。

$ sudo vi /private/etc/hosts

请描述以下内容。
※请使用您虚拟环境的私有IP地址。

192.168.56.10 a.test
192.168.56.10 b.test
192.168.56.10 hoge.test

VirtualHost(虚拟主机)的配置(httpd.conf)

请在httpd.conf文件中编写VirtualHosts的配置。
*也要备份以防万一。

$ sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bk
$ sudo vi /etc/httpd/conf/httpd.conf

请添加以下VirtualHost的设置。

略)...

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

# --------------------------------
# VirtualHost setting

<VirtualHost *:80>
  DocumentRoot "/var/www/html"
  ServerName hoge.test
</VirtualHost>
<VirtualHost *:80>
  DocumentRoot "/var/www/html/site_a"
  ServerName a.test
</VirtualHost>
<VirtualHost *:80>
  DocumentRoot "/var/www/html/site_b"
  ServerName b.test
</VirtualHost>
# --------------------------------

略)...

重新启动Apache

$ sudo systemctl restart httpd

检查虚拟主机

请尝试通过浏览器访问以下网站链接,确认每个链接是否显示不同的页面。

    • a.test

 

    • b.test

 

    hoge.test

尽管IP地址相同,但我们能够显示不同的页面。
请记得尽快删除hosts的设置。

请依照此进行参考。

    • 【Apache】VirtualHostを使ってみよう

 

    • Vagrant内でVirtualHostを設定する

 

    CentOS 7 の Apache httpd 2.4 に VirutalHost の設定をする手順
bannerAds