目录

题目一(更改主页及文件目录)

默认配置

apache服务主配置文件

修改主配置文件

修改DocumentRoot

修改默认目录

修改默认主页文件类型

创建默认目录和默认页面

创建财务部目录和财务部主页

修改服务器域名

重启后测试

题目二(基于IP和域名的虚拟主机)

基于IP部分

修改网络配置文件

创建虚拟主机目录

编写虚拟主机主页内容

修改主配置文件

重启后测试

基于域名部分

修改域名

创建主页文件及目录

编辑虚拟主机配置文件

重启并测试


题目一(更改主页及文件目录)

默认目录

/tengyi/web

财务部目录

/tengyi/web/cwb

默认页面

tengyi.html

财务部页面

cwb.html

域名

www.tengyi.com.cn


默认配置

apache服务主配置文件

[root@CentOS8 ~]# vim /etc/httpd/conf/httpd.conf
  •  站点根目录
DocumentRoot "/var/www/html"

 在vim编辑器里按下":122"到达122行

  • 默认内容目录及权限
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

 在vim编辑器里按下":134"到达134行

  • 默认主页文件类型
<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

  在vim编辑器里按下":166"到达166行

修改主配置文件

[root@CentOS ~]# vim /etc/httpd/conf/httpd.conf

修改DocumentRoot

 在vim里输入":122"回车

DocumentRoot "/tengyi/web"

修改默认目录<Directory "/var/www/html">

 在vim里输入":134"回车

# Further relax access to the default document root:
<Directory "/tengyi/web">
    #
    # 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>

修改默认主页文件类型

在vim里输入":166"回车

<IfModule dir_module>
    DirectoryIndex index.html tengyi.html
</IfModule>

创建默认目录和默认页面

[root@CentOS ~]# mkdir -p /tengyi/web
[root@CentOS ~]# vim /tengyi/web/tengyi.html
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>This is our company's homepage</title>
	</head>
	<body>
		<h1>path:/tengyi/web/tengyi.html</h1>
		<a href="./cwb/cwb.html">cwb.html</a>
	</body>
</html>

创建财务部目录和财务部主页

[root@CentOS ~]# mkdir /tengyi/web/cwb
[root@CentOS ~]# vim /tengyi/web/cwb/cwb.html
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>This is our company's webpage</title>
	</head>
	<body>
		<h1>path:/tengyi/web/cwb/cwb.html</h1>
	</body>
</html>


重启后测试

[root@CentOS8 ~]# curl http://www.tengyi.com.cn
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>This is our company's homepage</title>
	</head>
	<body>
		<h1>path:/tengyi/web/tengyi.html</h1>
		<a href="./cwb/cwb.html">cwb.html</a>
	</body>
</html>

[root@CentOS8 ~]# curl http://www.tengyi.com.cn/cwb/cwb.html
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>This is our company's webpage</title>
	</head>
	<body>
		<h1>path:/tengyi/web/cwb/cwb.html</h1>
	</body>
</html>
[root@CentOS8 ~]# curl 192.168.89.241
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>This is our company's homepage</title>
	</head>
	<body>
		<h1>path:/tengyi/web/tengyi.html</h1>
		<a href="./cwb/cwb.html">cwb.html</a>
	</body>
</html>

[root@CentOS8 ~]# curl 192.168.89.241/cwb/cwb.html
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>This is our company's webpage</title>
	</head>
	<body>
		<h1>path:/tengyi/web/cwb/cwb.html</h1>
	</body>
</html>

题目二(基于IP和域名的虚拟主机)

虚拟主机1

192.168.89.101

虚拟主机2

192.168.89.102

域名

www.cqcet.net

同ip

bbs.cqcet.net


基于IP部分

修改网络配置文件

[root@CentOS8 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=192.168.89.101
NETMASK=255.255.255.0
PREFIX=24
IPADDR1=192.168.89.102
PREFIX1=24
NETMASK1=255.255.255.0
#尾部增加这六行
#然后重启连接
[root@CentOS8 ~]# nmcli c reload
[root@CentOS8 ~]# nmcli c up ens33
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/8)

创建虚拟主机目录

[root@CentOS8 ~]# mkdir -p /www/vir1
[root@CentOS8 ~]# mkdir -p /www/vir2

编写虚拟主机主页内容

[root@CentOS8 ~]# vim /www/vir1/index.html
<h1>This is 101 page</h1>
[root@CentOS8 ~]# vim /www/vir2/index.html
<p>This is 102 page</p>

修改主配置文件

#在文件底部添加两段代码
<VirtualHost 192.168.89.101>
    DocumentRoot /www/vir1
    ErrorLog "logs/vir1-error_log"
    Customlog "logs/vir1-access_log" combined
    DirectoryIndex default.html index.html
    <Directory /www/vir1>
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost 192.168.89.102>
    DocumentRoot /www/vir2
    ErrorLog "logs/vir1-error_log"
    Customlog "logs/vir1-access_log" combined
    DirectoryIndex default.html index.html
    <Directory /www/vir2>
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

重启后测试

[root@CentOS8 ~]# curl 192.168.89.101
<h1>This is 101 page</h1>
[root@CentOS8 ~]# curl 192.168.89.102
<p>This is 102 page</p>

基于域名部分

修改域名

[root@CentOS8 ~]# vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.89.100 www.cqcet.net
192.168.89.100 bbs.cqcet.net

创建主页文件及目录

[root@CentOS8 ~]# mkdir -p /web/www
[root@CentOS8 ~]# mkdir -p /web/bbs
[root@CentOS8 ~]# echo "WWW.cqcet.net" > /web/www/index.html
[root@CentOS8 ~]# echo "BBS.cqcet.net" > /web/bbs/index.html
[root@CentOS8 web]# pwd
/web
[root@CentOS8 web]# tree
.
├── bbs
│   └── index.html
└── www
    └── index.html

2 directories, 2 files

编辑虚拟主机配置文件

[root@CentOS8 ~]# vim /etc/httpd/conf.d/www.conf
<VirtualHost 192.168.89.100>
    DocumentRoot "/web/www"
    ServerName "www.cqcet.net"
    <Directory "/web/www">
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>
[root@CentOS8 ~]# vim /etc/httpd/conf.d/bbs.conf
<VirtualHost 192.168.89.100>
    DocumentRoot /web/bbs
    ServerName "bbs.cqcet.net"
    <Directory "/web/bbs">
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

重启并测试

[root@CentOS8 ~]# curl http://www.cqcet.net
WWW.cqcet.net
[root@CentOS8 ~]# curl http://bbs.cqcet.net
BBS.cqcet.net