前言 对NAS总是抱有好奇?对NAS的价格望而却步? 但是,如果你有树莓派(或者其它arm64架构的开发板也行) ,或者是Linux服务器,为什么不试试让它们变成自己的小型NAS?
NAS NAS(Network Attached Storage:网络附属存储)按字面简单说就是连接在网络上,具备资料存储功能的装置,因此也称为“网络存储器”。它是一种专用数据存储服务器。
大型的NAS,比如:群晖。可以有App,对手机照片进行备份,还可以对数据进行灾变处理,但是价格就比较贵了。而本次搭建NAS,主要满足:
操作思路 1. 在线访问数据 比如:.flv、.mp4、.jpg等等,我们使用PHP实现网络浏览,这里我们使用_h5ai:
演示网站
官方文档
2. 下载站 下载站,我们使用Aria2这个多线程下载器实现,难点是挂载到电脑上,也就是目录映射。最好能满足:
3. 挂载到本地磁盘 在Windwos上挂载
其实,本地挂载树莓派、服务器磁盘的选择有很多。比如:SMB、FTP等。但是,考虑安全和利用情况下,我还是选择Webdav。
Windows和macOS都支持挂载Webdav,但是可能会出现无法写的情况。我建议使用第三方免费工具:
以上两款软件也都是免费的。
本文使用Webdav挂载,如果你使用宝塔,或者使用SMB,那么也完全可以选择非Webdav协议挂载磁盘。
(宝塔不可使用Nginx Webdav插件)
总结 综上,我们本次需要安装:
Nginx:用来提供Web目录访问
PHP:用来映射目录,实现_h5ai
Nginx模块:Webdav模块和Fancyindex模块,用来实现Webdav
前排提示 本次教程,适用所有架构的Linux(arm架构和x86都可以)。PHP、Nginx的配置,使用编译安装的方法。
不建议用宝塔安装Nginx和PHP,服务管理起来不方便,其它的懂的都懂
服务器要求 如果是树莓派(或者其它arm64架构的开发板也行) ,那么没什么具体要求,有个可以刷写SD卡的软件即可
但是,如果是服务器,最好选择高带宽的服务器,内存最好需要2G以上,否则编译PHP时,可能需要SWAP的支持。
我的是香橙派,也是arm64架构的开发板,系统用的Debian11,用其它linux系统的可以根据 命令自行修改一下即可
从下面开始我都是用root用户来进行,不想用root用户的可以在命令前面加上sudo
PHP PHP官方下载地址
安装依赖
为下面编译参数做准备
1 apt install build-essential pkg-config openssl libssl-dev libsqlite3-dev zlib1g-dev libcurl4-openssl-dev libpng-dev libjpeg-dev libonig-dev libzip-dev ffmpeg imagemagick -y
下载PHP 1 2 3 wget https://www.php.net/distributions/php-8.2.6.tar.gz -P /usr/local/src cd /usr/local/src && tar xf php-8.2.6.tar.gzcd php-8.2.6
配置用户和用户组
orangepi是我开发板自带的用户,不想用自带的用户也可以创建一个别的用户和用户组
如果你在服务器上操作,建议创建一个www用户和www用户组:
1 2 3 4 5 6 7 8 sudo useradd www sudo groupadd www sudo usermod -a -G www www
配置编译参数
这是我配置的编译参数,其他PHP版本可能略微不一样,需要自己判断),也可以参考我这篇文章部署LNMP教程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ./configure \ --prefix=/usr/local/php \ --with-curl \ --with-mysqli \ --with-openssl \ --with-pdo-mysql \ --with-jpeg \ --enable-gd \ --enable-fpm \ --with-fpm-user=orangepi \ --with-fpm-group=orangepi \ --enable-bcmath \ --enable-xml \ --with-zip \ --enable-mbstring \ --enable-sockets \ --with-zlib \ --enable-fileinfo \ --enable-exif
–with-fpm-user:fpm进程属于的用户
–with-fpm-group:fpm进程属于的用户组
编译安装
上面的configure
配置没有问题,接下来就是编译和安装了,我图方便就一条命令来执行,新手可以把两条命令分开执行,先make
在make install
1 make -j$(nproc ) && make install -j$(nproc )
复制配置文件 1 2 cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.confcp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
测试运行 1 /usr/local/php7/sbin/php-fpm
查看服务进程和端口 1 2 ps aux | grep php-fpm lsof -i:9000
Nginx Nginx官方下载地址
安装依赖
为下面编译参数做准备
1 apt install build-essential libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev libxml2-dev libxslt-dev -y
额外模块
如果我们需要进行目录映射,需要更好看的、更实用的目录映射,需要额外的Fancyindex模块支持。
Nginx自带的Webdav支持不完善,需要额外的模块进行支持。
1 2 3 mkdir /usr/local/src/NginxModule/ && cd /usr/local/src/NginxModulegit clone https://github.com/arut/nginx-dav-ext-module.git git clone https://github.com/aperezdc/ngx-fancyindex.git
下载Nginx 1 2 3 wget http://nginx.org/download/nginx-1.24.0.tar.gz -P /usr/local/src cd /usr/local/src && tar xf nginx-1.24.0.tar.gzcd nginx-1.24.0
配置用户和用户组
orangepi是我开发板自带的用户,不想用自带的用户也可以创建一个别的用户和用户组
如果你在服务器上操作,建议创建一个www用户和www用户组:
1 2 3 4 5 6 7 8 sudo useradd www sudo groupadd www sudo usermod -a -G www www
配置编译参数
这是我配置的编译参数,其他Nginx版本可能略微不一样,需要自己判断),也可以参考我这篇文章部署LNMP教程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 ./configure \ --prefix=/usr/local/nginx \ --user=orangepi \ --group=orangepi \ --sbin-path=/usr/local/nginx/sbin/nginx \ --conf-path=/usr/local/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --with-file-aio \ --with-threads \ --with-http_addition_module \ --with-http_auth_request_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_mp4_module \ --with-http_random_index_module \ --with-http_realip_module \ --with-http_secure_link_module \ --with-http_slice_module \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_sub_module \ --with-http_v2_module \ --with-mail \ --with-mail_ssl_module \ --with-stream \ --with-stream_realip_module \ --with-stream_ssl_module \ --with-stream_ssl_preread_module \ --add-module=/usr/local/src/NginxModule/nginx-dav-ext-module \ --add-module=/usr/local/src/NginxModule/ngx-fancyindex
–user:nginx进程属于的用户
–group:nginx进程属于的用户组
编译安装
上面的configure
配置没有问题,接下来就是编译和安装了,我图方便就一条命令来执行,新手可以把两条命令分开执行,先make
在make install
1 make -j$(nproc ) && make install -j$(nproc )
配置
因为上面配置参数指定缓存文件在/var/cache/nginx
,日志文件在/var/log/nginx
,所以需要创建
1 2 mkdir /var/cache/nginxmkdir /var/log/nginx
测试运行 1 /usr/local/nginx/sbin/nginx
查看服务进程和端口 1 2 3 ps aux | grep nginx lsof -i:80
配置_h5ai H5AI,其实全称是:HTML5 Apache Index。最初是用来在Apache Web服务器上,完成资源映射,但是后来适配到Nginx等其他平台。配置很简单:下载_h5ai
-修改Nginx配置文件
-开始使用
。 注意_h5ai的映射逻辑:
1 2 3 4 网站根目录(需要映射的目录) ├─ _h5ai ├─ 你的其他文件1 └─ 你的其他文件1
下载_h5ai 因为是开源的工具,所以网上有很多修改版本,包含官方的:
1 2 3 4 5 6 7 8 9 10 11 12 13 cd /home/orangepimkdir Downloadscd Downloadsgit clone https://github.com/Mintimate/h5ai_M cd h5ai_Mmv _h5ai ..cd /home/orangepichown -R orangpepi:orangepi Downloads
修改Nginx配置 备份原先的nginx配置文件
1 mv /usr/local/nginx/nginx.conf /usr/local/nginx/nginx.conf.bak
新建一个配置文件
1 vim /usr/local/nginx/nginx.conf
直接添加配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 user orangepi; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; root /home/orangepi/Downloads; location / { index index.html index.htm /_h5ai/public/index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ^~/webdav{ set $dest $http_destination ; if (-d \$request_filename ) { rewrite ^(.*[^/])$ $1 /; set $dest $dest /; } if ($request_method ~ MKCOL) { rewrite ^(.*[^/])$ $1 / break ; } alias /home/pi/Downloads/; autoindex on; dav_methods PUT DELETE MKCOL COPY MOVE; dav_ext_methods PROPFIND OPTIONS; create_full_put_path on; client_max_body_size 3G; dav_access user:rw group:rw all:rw; auth_basic "Authorized Users Only" ; auth_basic_user_file webdavpasswd; } location ~ [^/]\.php(/|$){ fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root /$fastcgi_script_name ; } } }
使用
如果装了宝塔的,使用前记得开放对应端口,80端口和9000端口
直接用浏览器访问香橙派的IP,就可以访问_h5ai
的映射目录
默认:
然后进入这个地址:http://服务器的IP/_h5ai/public/index.php,这个界面是没有密码的,直接点击login
看到配置全绿就OK了
配置下载器 如果只配置了_h5ai,距离NAS基本算完成一大步了。但是,我认为还需要配置下载器,能把网上资源,直接下载到服务器才算是NAS。 这里我们使用Aria2作为下载器。
配置Aria2 一键脚本搭建:
AutoInstallAria2
因为一键安装脚本会在家目录下创建目录,所以切换到orangepi用户
1 2 su orangepi wget "https://cdn.jsdelivr.net/gh/Mintimate/AutoInstallAria2@latest/AutoInstallAria2ForLinux.sh" && bash AutoInstallAria2ForLinux.sh
测试使用 这个时候,我们本地就可以使用Aria2的RPC进行下载了,首先Linux服务器上启动Aira2:
如果装了宝塔的,使用前记得开放对应端口,默认6800端口,可在配置中修改
上面修改的Nginx配置,已经设置Webdav参数和目录,还设置了验证用户 ,所以还需要创建webdavpasswd 这个验证文件。如果不需要验证,可以注释这一项,就可以使用本地的Webdav客户端访问了。
设置验证用户 1 2 3 4 cat << EOF > /usr/local/nginx/webdavpasswd #设置登录用户名和密码 admin:$(openssl passwd 123) EOF
用户名 和密码 可自行修改,之后重新加载配置文件和重启服务:
1 2 3 /usr/local/nginx/sbin/nginx -s reload killall nginx /usr/local/nginx/sbin/nginx
客户端登陆 macOS和Windows的文件管理器都可以登录,但是功能不全(无法上传),这里推荐:
以上软件,个人使用免费版本就可以了:
没用过mac,我用windows演示
nginx默认80端口,如果80端口要跑其它服务,也可以修改nginx配置文件把80端口改成其它端口,路径要加/webdav
,然后一定要取消勾选地址!!!
添加系统服务
还是可以参考我这篇文章部署LNMP教程
1 vim /usr/lib/systemd/system/nginx.service
1 2 3 4 5 6 7 8 9 10 11 12 13 [Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
1 sudo vim /usr/lib/systemd/system/php-fpm.service
1 2 3 4 5 6 7 8 9 10 11 [Unit] Description=php-fpm After=network.target [Service] Type=forking ExecStart=/usr/local/php7/sbin/php-fpm PrivateTmp=true [Install] WantedBy=multi-user.target
1 sudo vim /usr/lib/systemd/system/aria2.service
1 2 3 4 5 6 7 8 9 10 11 [Unit] Description=Aria2 After=network.target [Service] User=pi Type=simple ExecStart=/etc/aria2/aria2c --conf-path=/etc/aria2/aria2.conf [Install] WantedBy=multi-user.target
建议还是给予执行权限:
1 2 3 chmod +x nginx.servicechmod +x php-fpm.servicechmod +x aria2.service
允许开机启动并激活:
1 2 3 systemctl enable nginx.service systemctl enable php-fpm.service systemctl enable aria2.service
连接Aria2进程
AriaNG Native 个人推荐使用AriaNG Native连接服务器和本地的Aria2进程。
我们下载对应自己电脑版本的AriaNG Native: 之后,我们设置RPC:
设置的RPC要和在linux上aria2.conf配置文件中的RPC一致(rpc-secret:设置Aria2验证远程码)
刷新即可连接:
搭建网页前端——AriaNg 这个一般是部署在服务器 项目地址(GitHub): https://github.com/mayswind/AriaNg/releases
浏览器的扩展程序 这是Chrome浏览器的扩展程序:
https://chrome.google.com/webstore/detail/aria2-explorer/mpkodccbngfoacfalldjimigbofkhgjn?utm_source=ext_app_menu
我图方便用的也是这种,用的火狐浏览器
我下载的是第一个扩展
配置 打开管理扩展,点击选项
点击RPC服务器后再点击默认服务器
协议选http,填入服务器IP,密钥要和在linux上aria2.conf配置文件中的RPC一致,然后点保存
使用 固定到工具栏
点详情就可以使用了
连接到Aria2后就可以新建任务,把下载链接复制过去,就能把文件下载到Linux本地上了