关于apach的web服务配置实现基本访问

目录

1.安装httpd,并将访问apache服务器的首页修改为hello.html, 且内容为: "My Home Page is hello"

2.虚拟主机:虚拟两台主机ip为100,200, 对应访问目录:/www/ip/100, /www/ip/200并创建首页文件index.html 

3.配置不同端口的虚拟主机访问apache服务器


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

俗人不俗鸭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值