目录
1.安装httpd,并将访问apache服务器的首页修改为hello.html, 且内容为: "My Home Page is hello"
2.虚拟主机:虚拟两台主机ip为100,200, 对应访问目录:/www/ip/100, /www/ip/200并创建首页文件index.html
1.安装httpd,并将访问apache服务器的首页修改为hello.html, 且内容为: "My Home Page is hello"
(1)安装httpd
[root@server ~]# yum -y install httpd
(2)创建首页hello.html,并添加内容
[root@client ~]# cd /var/www/html/
[root@client html]# echo "My Home Page is hello" > hello.html
[root@client html]# ll
total 4
-rw-r--r--. 1 root root 22 Jul 26 22:22 hello.html
(3)修改/etc/httpd/conf/httpd.conf配置文件中的默认首页为hello.html
[root@client conf]# vim /etc/httpd/conf/httpd.conf
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
#DirectoryIndex index.html
DirectoryIndex hello.html #将默认index.html改为hello.html
</IfModule>
(4)重启httpd服务
[root@client conf]# systemctl restart httpd
(5)访问测试
[root@client conf]# curl 192.168.5.129
My Home Page is hello
2.虚拟主机:虚拟两台主机ip为100,200, 对应访问目录:/www/ip/100, /www/ip/200并创建首页文件index.html
(1)为ens160网卡增加两个ip
[root@client conf]# nmcli c modify ens160 +ipv4.addresses 192.168.5.100/24
[root@client conf]# nmcli c modify ens160 +ipv4.addresses 192.168.5.200/24
[root@client conf]# nmcli c up ens160
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/10)
[root@client conf]# ip -4 addr show ens160
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
inet 192.168.5.129/24 brd 192.168.5.255 scope global dynamic noprefixroute ens160
valid_lft 1784sec preferred_lft 1784sec
inet 192.168.5.100/24 brd 192.168.5.255 scope global secondary noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.5.200/24 brd 192.168.5.255 scope global secondary noprefixroute ens160
valid_lft forever preferred_lft forever
(2)新建目录/www/ip/100和/www/ip/200,并分别添加index.html
[root@client ~]# mkdir -p /www/ip/{100,200}
[root@client ~]# cd /www/ip/
[root@client ip]# echo "this is 100" > /www/ip/100/index.html
[root@client ip]# echo "this is 200" > /www/ip/200/index.html
(3)在/etc/httpd/conf.d/目录下添加配置文件myhosts_ip.conf
[root@client ip]# cd /etc/httpd/conf.d/
[root@client conf.d]# vim myhosts_ip.conf
<Directory "/www/ip">
AllowOverride None
Require all granted
</Directory>
<VirtualHost 192.168.5.100:80>
DocumentRoot "/www/ip/100"
</VirtualHost>
<VirtualHost 192.168.5.200:80>
DocumentRoot "/www/ip/200"
</VirtualHost>
~
(4)重启httpd服务
[root@client conf.d]# systemctl restart httpd
(5)将selinux调为permissive模式
[root@client conf.d]# getenforce
Enforcing
[root@client conf.d]# setenforce 0
[root@client conf.d]# getenforce
Permissive
(6)访问测试
[root@client conf.d]# curl 192.168.5.100
this is 100
[root@client conf.d]# curl 192.168.5.200
this is 200
3.配置不同端口的虚拟主机访问apache服务器
(1)新建目录/www/port/8080和/www/port/9090,并分别添加index.html
[root@client conf.d]# mkdir -p /www/port/{8080,9090}
[root@client conf.d]# echo "this is page for port 8080" > /www/port/8080/index.html
[root@client conf.d]# echo "this is page for port 9090" > /www/port/9090/index.html
(2)在/etc/httpd/conf.d/目录下添加配置文件myhosts_port.conf
[root@client conf.d]# cd /etc/httpd/conf.d/
[root@client conf.d]# vim myhosts_port.conf
Listen 8080
Listen 9090
<Directory "/www/port/">
AllowOverride None
Require all granted
</Directory>
<VirtualHost 192.168.5.100:8080>
DocumentRoot "/www/port/8080"
</VirtualHost>
<VirtualHost 192.168.5.100:9090>
DocumentRoot "/www/port/9090"
</VirtualHost>
~
(3)重启httpd服务
[root@client conf.d]# systemctl restart httpd
(4)将selinux调为permissive模式
[root@client conf.d]# getenforce
Enforcing
[root@client conf.d]# setenforce 0
[root@client conf.d]# getenforce
Permissive
(5)访问测试
[root@client conf.d]# curl 192.168.5.100:8080
this is page for port 8080
[root@client conf.d]# curl 192.168.5.100:9090
this is page for port 9090