通过 Apache httpd 提供简单的文件下载服务

注意
本文最后更新于 2023-12-28,文中内容可能已过时。

通过 Apache httpd 提供简单的文件下载服务

介绍 httpd

官方文档:Apache HTTP 服务器 2.4 文档 - Apache HTTP 服务器 版本 2.4

httpd 全名 Apache HTTP Server,即 Apache HTTP 服务器,简而言之就是用于处理简单的 http 请求的服务器,在学习 HTTP 协议的时候,我们知道 URL 实际上指定的就是服务器上特定的文件,所以使用 httpd 这个轻量的 HTTP server 实现一个简单的文件下载服务是再合适不过的了。

安装教程

参考博客:Centos7 使用 httpd 搭建下载文件界面_NUAA 丶无痕的博客-CSDN 博客

httpd 的安装非常简单,下载,启动,除此之外,还需要配置文件名列宽度中文编码

注意

阿里云服务器需要为 httpd 中监听的端口配置防火墙规则

最终的下载页面类似于

点击文件即可下载。

右键文件打开右键菜单,复制链接地址即为获取下载链接:

自定义文件根路径

在指定位置创建好路径,这里以/publicFolderForDownload为例

编辑配置文件

1
2
cd /etc/httpd/conf
vim httpd.conf

开始修改

1
2
3
4
5
6
7
8
DocumentRoot "/publicFolderForDownload"

<Directory "/publicFolderForDownload">
    Options Indexes FollowSymLinks
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

如图

然后重启服务器

1
systemctl  restart httpd

为 httpd 配置 SSL/TSL

在网上找了很多教程,感觉都太过复杂,其实通过一个 Nginx 转发即可实现,非常简单,而且这种方式也可以作为一种为 http 协议配置 https 的通用方法,首先要准备好证书。然后安装好 Nginx,然后修改配置文件

1
vim /etc/nginx/nginx.conf

然后加上这一段配置,注意,阿里云服务器需要为 HTTPS 使用的端口配置防火墙规则。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
http {
    ......
    server {

        # 对外以 9090 提供 HTTPS 服务
        listen       9090 ssl ;
        listen       [::]:9090 ssl ;
        server_name  _;

        #  证书地址
        ssl_certificate "/etc/host_ssl/xiashuo.xyz.pem";
        ssl_certificate_key "/etc/host_ssl/xiashuo.xyz.key";
        #  SSL 配置
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;

        location / {
            # 被代理的,原本的 HTTP 服务
            proxy_pass http://localhost:90;
        }
    }

}

然后启动 Nginx 或者重启 Nginx

1
2
3
4
// 启动 
nginx
// 重启
nginx -s reload
0%