网站首页 > 精选文章 正文
Gohttpserver
Binaries can be downloaded from this repo Releases · codeskyblue/gohttpserver · GitHub
发现一个最好用且很简单和http服务器工具:gohttpserver,怎么个好用法呢,下载好后,直接运行即可,默认端口为8000,支持Linux,Windows,MacOS系统。
gohttpserver参数:
./gohttpserver -h
usage: gohttpserver [<flags>]
Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
--version Show application version.
--conf=CONF config file path, yaml format
-r, --root=ROOT root directory, default ./
--prefix=PREFIX url prefix, eg /foo
--port=PORT listen port, default 8000
-a, --addr=ADDR listen address, eg 127.0.0.1:8000
--cert=CERT tls cert.pem path
--key=KEY tls key.pem path
--auth-type=AUTH-TYPE Auth type <http|openid>
--auth-http=AUTH-HTTP ... HTTP basic auth (ex: user:pass)
--auth-openid=AUTH-OPENID OpenID auth identity url
--theme=THEME web theme, one of <black|green>
--upload enable upload support
--delete enable delete support
--xheaders used when behide nginx
--debug enable debug mode
-p, --plistproxy=PLISTPROXY plist proxy when server is not https
--title=TITLE server title
--google-tracker-id=GOOGLE-TRACKER-ID
set to empty to disable it
--deep-path-max-depth=DEEP-PATH-MAX-DEPTH
set to -1 to not combine dirs
--no-index disable indexing
接下来看看如何把gohttpserver配置成系统服务方便使用。
在 CentOS 7 中,将 gohttpserver 添加为系统服务需要创建一个 systemd 服务文件并进行相应配置。以下是具体步骤:
创建服务文件
使用文本编辑器(如 vim)创建一个新的服务文件:
sudo vim /etc/systemd/system/gohttpserver.service
编辑服务文件
将以下内容添加到文件中,根据需要修改路径和参数:
[Unit]
Description=Go HTTP File Server
After=network.target
[Service]
WorkingDirectory=/opt/gohttpserver
ExecStart=/opt/gohttpserver/gohttpserver --root /xData/wwwroot --port 8080
Restart=on-abnormal
User=root
Group=root
[Install]
WantedBy=multi-user.target
说明
- Description:服务的描述。
- After:指定该服务在网络服务之后启动。
- WorkingDirectory:指定 gohttpserver 的工作目录。
- ExecStart:指定 gohttpserver 的启动命令,包括共享目录路径和端口号。
- Restart:设置服务在异常退出时自动重启。
- User 和 Group:指定运行服务的用户和组。
- WantedBy:指定该服务在多用户目标下启动。
保存并退出
:wq
保存文件并退出编辑器。
重载 systemd 配置
使新的服务文件生效:
sudo systemctl daemon-reload
启动并启用服务
启动 gohttpserver 服务并设置为开机自启:
sudo systemctl start gohttpserverbrsudo systemctl enable gohttpserver
验证服务状态
检查服务是否成功启动:
sudo systemctl status gohttpserver
开放防火墙端口
gohttpserver默认使用8000端口,在linux中开放该端口
sudo systemctl status firewalld # 查看防火墙状态
● firewalld.service - firewalld - dynamic firewall daemon # 防火墙正在运行
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2025-04-15 09:24:49 CST; 5h 27min ago
Docs: man:firewalld(1)
Main PID: 724 (firewalld)
CGroup: /system.slice/firewalld.service
└─724 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent # 开放端口8000
[sudo] password for xiaoqiang: # 输入密码
sudo firewall-cmd --reload # 重载防火墙
sudo firewall-cmd --list-all # 查看防火墙开放的所有端口
通过以上步骤,gohttpserver 就会被添加为 CentOS 7 的系统服务,并能够使用 systemctl 命令进行管理,打开Chrome,输入服务器IP:8000,即可实现http文件服务器。
在 Windows 中,可以通过 nssm 手动注册服务 来添加系统服务。以下是详细步骤:
1. 下载 nssm
从 nssm.cc 下载并解压 nssm.exe,将nssm.exe拷贝到system32目录下。
cppy x64\nssm.exe C:\Windows\System32\
2. 以管理员身份运行 CMD
nssm install GoHttpServer
会弹出 GUI 界面:
- Path:选择你的脚本/exe文件(如 D:\gohttpserver\gohttpserver.exe)。
- Startup directory:脚本所在目录(如 D:\wwwroot)。
- Service name:输入服务名称(如 GoHttpServer)。
或静默执行:
nssm install GoHttpServer "D:\gohttpserver\gohttpserver.exe" -r D:\wwwroot\ --upload --delete --port 8090
3. 启动服务
nssm start GoHttpServer
常见问题
1. 服务启动失败?
- 检查路径是否正确(建议使用绝对路径)。
- 确保程序能独立运行(先手动测试)。
- 查看事件日志(Win + R → eventvwr.msc → Windows 日志 → 系统)。
2. 如何修改服务配置?
nssm edit GoHttpServer
3. 删除服务
nssm stop GoHttpServer
nssm remove GoHttpServer
4. nssm更多帮助
nssm --help
猜你喜欢
- 2025-05-08 jQuery文件下载方法及引入HTML语法
- 2025-05-08 python编程实践:下载文件模块wget的使用
- 2025-05-08 快速将网站从HTTP升级到HTTPS教程
- 2025-05-08 python原始套接字socket下载https网页文件到txt
- 2025-05-08 应用层协议HTTP和HTTPS,一篇文章学会
- 2025-05-08 百度网盘与360云盘哪个好(百度网盘与360网盘的区别)
- 2025-05-08 SPRINGBOOT 实现大文件上传下载、分片、断点续传教程
- 2025-05-08 SPRING BOOT实现文件上传和下载(spring boot 文件上传下载)
- 2025-05-08 YouTube应用下载全攻略:安卓、iOS及视频下载指南
- 2025-05-08 Burp Suite显示HTTP消息中的中文字符设置(附字体下载)
- 最近发表
- 标签列表
-
- 向日葵无法连接服务器 (32)
- git.exe (33)
- vscode更新 (34)
- dev c (33)
- git ignore命令 (32)
- gitlab提交代码步骤 (37)
- java update (36)
- vue debug (34)
- vue blur (32)
- vscode导入vue项目 (33)
- vue chart (32)
- vue cms (32)
- 大雅数据库 (34)
- 技术迭代 (37)
- 同一局域网 (33)
- github拒绝连接 (33)
- vscode php插件 (32)
- vue注释快捷键 (32)
- linux ssr (33)
- 微端服务器 (35)
- 导航猫 (32)
- 获取当前时间年月日 (33)
- stp软件 (33)
- http下载文件 (33)
- linux bt下载 (33)