linux:http服务器搭建及实验案例

准备工作

1,安装http服务
2,将 /etc/selinux/config 文件下面的 SELINUX值改为 disabled 或者 permissive
3,关闭防火墙 systemctl stop firewalld
做上面的工作是为了http在提供服务时让其不会阻止读取一些文件。

http服务器各个配置文件大概说明

/etc/httpd/ 里面是http的主要的配置文件
tree /etc/httpd/ 可以看到这个文件的结构一目了然
在这里插入图片描述
/etc/httpd/conf/httpd.conf 文件里面是主要配置文件,conf.d文件里面所有的.conf文件都会被加载。

/etc/httpd/conf/httpd.conf 里面的 DocumentRoot 是用户访问时的主目录
welcome.conf文件是默认的index.html文件的所在

实验1:访问不同ip获得不同网页

要求:
1,该网站ip地址的主机位为100,设置DocumentRoot为/www/ip/100,网页内容为:100。,
2、该网站ip地址主机位为200,设置DocumentRoot为/www/ip/200,网页内容为:200。

开始:
1,根据查看自己本机地址可以创建2个ip地址:

nmcli c modify ens160 +ipv4.addresses 192.168.107.100/24 ipv4.method manual
nmcli c modify ens160 +ipv4.addresses 192.168.107.200/24 ipv4.method 

2, 创建DocumentRoot文件和写入网页内容

mkdir /www/ip/{100,200} -p   递归创建文件
echo 100 > /www/ip/100/index.html    写入对应的内容
echo 200 > /www/ip/200/index.html 

3,写配置文件
自己写的配置文件在 conf.d 里面,必须要为 .conf 结尾才能加载
自己写的配置文件参考示例文档为:/usr/share/doc/httpd/httpd-vhosts.conf

如何知道示例文档在哪里?
一般示例文档是有 doc 标志的,可以通过grep过滤

[root@localhost conf.d]# touch 100.conf   创建名字为 100.conf 的自定义配置文件
[root@localhost conf.d]# touch 200.conf 

[root@localhost conf.d]# vim 100.conf    参考示例文件编辑这个文件,写入下面内容
<VirtualHost 192.168.107.100:80>
    #ServerAdmin webmaster@dummy-host.example.com   现在阶段不需要写
    DocumentRoot "/www/ip/100"     用户访问的主文件
    ServerName 192.168.107.100  必须写
    #ServerAlias www.dummy-host.example.com    服务器别名
    ErrorLog "/var/log/httpd/100.com-error_log"     错误日志文件,也可以不要
    CustomLog "/var/log/httpd/200.com-access_log" common
</VirtualHost>
<Directory /www/ip/100>    给 /www/ip/100 目录赋予权限,不写服务器就读取不到该文件下的
    AllowOverride none
    Require all granted
</Directory>
[root@localhost conf.d]# vim 200.conf     与上面 操作相似,将ip100 换成200 即可,如下
<VirtualHost 192.168.107.200:80>
    #ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/www/ip/200"
    ServerName 192.168.107.200
    #ServerAlias www.dummy-host.example.com
    ErrorLog "/var/log/httpd/200.com-error_log"
    CustomLog "/var/log/httpd/200.com-access_log" common
</VirtualHost>

<Directory /www/ip/200>
    AllowOverride none
    Require all granted
</Directory>

4, 最后重新启动一下http服务并且访问

[root@localhost conf.d]# systemctl restart httpd

可以看到访问成功(也可以在浏览器里面访问)
在这里插入图片描述

实验2:同一ip访问不同端口获得不同网页

要求:
1、建立一个使用10000端口的网站,设置DocumentRoot为/www/port/10000,网页内容为:the port is 10000。默认为80端口,网页内容为100

开始:与上面实验基本差不多
1 创建一个 DocumentRoot 文件

[root@localhost conf.d]# mkdir /www/port/10000 -p
[root@localhost port]# echo 10000 > 10000/index.html
[root@localhost conf.d]# vim 100.conf   再次编辑这个文件,输入以下内容
<VirtualHost 192.168.107.100:10000>   
   #ServerAdmin webmaster@dummy-host.example.com
   DocumentRoot "/www/port/10000"
   ServerName 192.168.107.100
   #ServerAlias www.dummy-host.example.com
   ErrorLog "/var/log/httpd/port10000.com-error_log"
   CustomLog "/var/log/httpd/port10000.com-access_log" common
</VirtualHost>

<Directory /www/port>   给这个文件赋予权限
    AllowOverride none
    Require all granted
</Directory>

2,建立监听端口
因为默认是监听的80端口,所以需要手动添加端口10000
可以在/etc/httpd/conf/httpd.conf 编辑或者在conf.d 文件中编辑,这里在主配置文件中写监听端口

[root@localhost conf.d]# vim /etc/httpd/conf/httpd.conf   进入文件在Listen 80位置写上如下
Listen 10000

2,最后重新启动http服务

[root@localhost httpd]# systemctl restart httpd

再访问不同的端口可以看到请求到了不同的页面
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员Fy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值