企业项目管理、ORK、研发管理与敏捷开发工具平台

网站首页 > 精选文章 正文

最好用的最简单的HTTP文件服务器(http 服务软件)

wudianyun 2025-05-08 21:38:05 精选文章 12 ℃

Gohttpserver


  • Goal: Make the best HTTP File Server.
  • Features: Human-friendly UI, file uploading support, direct QR-code generation for Apple & Android install package.
  • 目标: 做最好的HTTP文件服务器
  • 功能: 人性化的UI体验,文件的上传支持,安卓和苹果安装包的二维码直接生成。
  • 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
    最近发表
    标签列表