CentOS8.4 Apache服务配置(HTTP协议)

该博客详细介绍了如何在CentOS8上更改Apache服务器的主页及文件目录,包括修改DocumentRoot、设置默认页面、创建目录和页面,并通过域名进行访问。此外,还讲解了基于IP和域名的虚拟主机配置,包括创建虚拟主机目录、编写主页内容、修改配置文件以及进行测试验证。
摘要由CSDN通过智能技术生成

目录

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

默认配置

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>

修改服务器域名

查看上一篇的配置过程:CentOS8.4 DNS解析(辅助DNS配置)

重启后测试

[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和域名的虚拟主机)

虚拟主机1192.168.89.101
虚拟主机2192.168.89.102
域名www.cqcet.net
同ipbbs.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值