一、前言
使用xinetd+shell构建一个web服务器,由于xinetd很轻量,所以也适合部署在资源少的设备上。
1.1 原理
通过访问xinetd管理的端口触发执行脚本,而脚本返回的是模拟http响应数据,所以能在浏览器上看到内容。
二、实践
2.1 安装xinetd
ubuntu环境
复制代码
1
2sudo apt install xinetd
2.2 xinetd配置
- 新建一个执行脚本 http_test.sh
复制代码
1
2
3
4
5
6
7
8
9#!/bin/bash echo -e "HTTP/1.1 200 OK" echo -e "Content-Type:text/html" echo -e "" #响应头和内容要空一行 cat /root/index.html #指定绝对路径下的html文件 echo -e ""
- 新建一个网页文件 index.html
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22<html> <head lang="en"> <meta charset="UTF-8"> <title>xinetd web</title> <style> p { color:red; font-size:30px; background-color:blue; width:400px; height:100px; } </style> </head> <body> <p>this is a tag</p> <script> alert("hello world"); </script> </body> </html>
- 添加一个服务文件,文件名即是服务名 /etc/xinetd.d/httptest
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15service httptest { disable = no #不禁用 type = UNLISTED port = 9200 #开放端口 socket_type = stream protocol = tcp wait = no #并行不等待 user = root #执行服务的用户 server = /root/http_test.sh #要执行的脚本路径 server_args = test }
注意:直接在服务文件里添加端口要先确保端口未被使用,可以在服务列表查看 /etc/services
- 重启xinetd服务,及查看
复制代码
1
2
3
4
5
6sudo service xinetd restart #重启 sudo service xinetd status #查看状态
- 测试
复制代码
1
2
3
4
5# 使用nc工具 nc localhost 9200 # 或者telnet telnet localhost 9200
提示:也可以在浏览器打开 (http://localhost:9200)
三、参考
ubuntu14.04安装xinetd服务
利用xinetd实现简单web服务器(镜像站)
Linux中shell如何实现HTTP服务
Xinetd服务的安装与配置
最后
以上就是美好电脑最近收集整理的关于shell构建web服务一、前言二、实践三、参考的全部内容,更多相关shell构建web服务一、前言二、实践三、参考内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复