美文网首页
Aria2+Nginx在VPS搭建离线下载服务器

Aria2+Nginx在VPS搭建离线下载服务器

作者: hdfg159 | 来源:发表于2018-02-11 17:09 被阅读0次
    • 此文章是自己搭建简易的VPS离线下载服务器的简易教程
    • 目前我做了一个docker file,用docker的同学可以试试:aria2-nginx-alpine-webui [强烈推荐使用,不需要这么麻烦配置]
    • 不定时更新更新此文......

    系统环境

    • Ubuntu 16.04
    • 以下内容系统登录后操作用户默认为ROOT用户

    下载安装Aria2 & Nginx

    • 更新资源库
      apt update
    • 安装Aria2
      apt install aria2
    • 安装Nginx服务器
      apt install nginx

    配置Aria2

    • 创建Aria2配置文件
      cd ~
      mkdir .aria2
      cd aria2
      touch aria2.conf
      touch aria2.session
    • 编辑Aria2配置文件
      创建下载文件夹路径
      mkdir ~/Download
      编辑Aria2配置文件
      vi ~/.aria2/aria2.conf
      加入以下内容,部分设置根据需要自行修改:
    ##### RPC Options ##### 
    # 开启JSON-RPC/XML-RPC服务,从而能够通过其接口控制aria2,默认为true 
    enable-rpc=true 
    # 指定访问端口号,默认6800 
    rpc-listen-port=6800 
    # 允许所有访问来源,web控制界面跨域需要,默认false 
    rpc-allow-origin-all=true 
    # 允许除local loopback以外的网卡访问,默认false,远程控制需要 
    rpc-listen-all=true 
    # 外部访问安全令牌,强烈建议设置token并记住 (修改成自己的安全令牌)
    rpc-secret=hdfg159 
    # 若不设置token,也可通过用户名密码访问,现版本不建议使用 
    # rpc-user=<username> 
    # rpc-passwd=<passwd> 
    ##### Advance Options ##### 
    #以守护进程方式后台运行,默认为false,也可在启动aria2c时加上-D选项达到相同效果 
    daemon=true
    # 磁盘缓存,可设为0禁用,默认16M。 
    disk-cache=16M 
    # 磁盘空间分配模式,可选none,prealloc,trunc,falloc,默认prealloc 
    # 若完整分配,官方建议ext4、NTFS使用falloc快速分配,可以瞬间完成分配 
    #FAT32、ext3建议使用prealloc,如果此时使用falloc分配时间和prealloc相当,分配时会造成aria2卡顿 
    file-allocation=falloc 
    #使用会话文件保存信息,并能够从意外错误(断电等)错误中恢复(上一步骤创建的Session文件) 
    save-session=/root/.aria2/aria2.session 
    # 指定开启时读取会话文件的位置 (上一步骤创建的Session文件)
    input-file=/root/.aria2/aria2.session 
    # 定期保存会话,默认0为只在退出时保存 
    save-session-interval=60 
    ##### Basic Options ##### 
    # 下载路径 (注意:此目录不存在需要自行去创建)
    dir=/root/Download 
    # 最大同时下载任务数量,默认为5 
    max-concurrent-downloads=20
    #若下载速度低于此值(Byte/s),会被自动暂停,速度可以有K或M等后缀,对BT下载无效 #lowest-speed-limit=0 
    # 每个下载任务对单个服务器最大的链接数量,默认为1 
    max-connection-per-server=10
    # 任务分块大小,当下载文件大小大于两倍于此设置时,会被分块下载,默认20M 
    min-split-size=20M 
    # 任务分块数量,默认为5 
    split=10
    
    • 创建Aria2开机脚本(Ubuntu适用)
      打开vi文本编辑器
      vi
      插入以下内容:
    [Unit]
    Description=Aria2
    After=syslog.target
    After=network.target
    
    [Service]
    Type=forking
    ExecStart=/usr/bin/aria2c
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    

       保存路径:
       /etc/systemd/system/aria2.service
       启用并运行:
       systemctl enable aria2.service && systemctl start aria2.service

    • 下载 webui-aria2
      放置到合适的路径,我的是/root/webui-aria2
      或者git clone下来:
      apt install git
      git clone https://github.com/ziahamza/webui-aria2.git

    • 配置Nginx
      下载后需要找到相应的离线文件下载到本地,自然要用Nginx配置一个可以浏览下载服务器文件夹的路径(当然你也可以用其他服务器)。
      vi /etc/nginx/nginx.conf
      在http段添加以下内容

        server {
            listen 80;
            server_name <你的服务器ip地址>;
            
            location /aria2/download {
                #这是你离线文件存放的位置
                alias /root/Download;
                autoindex on;
                autoindex_exact_size off;
                autoindex_localtime on; 
                charset utf-8,gbk;
            }
            
            location /aria2/home {
                #这是你刚才下载webui-aria2解压的位置
                alias /root/webui-aria2/docs;
                index index.html
                charset utf-8,gbk;
            }
        }
    

      我的完整配置如下:

    user www-data;
    worker_processes auto;
    pid /run/nginx.pid;
    
    events {
        worker_connections 768;
        # multi_accept on;
    }
    
    http {
    
        ##
        # Basic Settings
        ##
    
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;
    
        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;
    
        include /etc/nginx/mime.types;
        default_type application/octet-stream;
    
        ##
        # SSL Settings
        ##
    
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;
    
        ##
        # Logging Settings
        ##
    
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
    
        ##
        # Gzip Settings
        ##
    
        gzip on;
        gzip_disable "msie6";
    
        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    
        ##
        # Virtual Host Configs
        ##
    
        server {
            listen 80;
            server_name 服务器ip;
            
            location /aria2/download {
                alias /root/Download;
                autoindex on;
                autoindex_exact_size off;
                autoindex_localtime on; 
                charset utf-8,gbk;
            }
            
            location /aria2/home {
                alias /root/webui-aria2/docs;
                index index.html
                charset utf-8,gbk;
            }
        }
    
        include /etc/nginx/conf.d/*.conf;
        #include /etc/nginx/sites-enabled/*;
    }
    
    
    #mail {
    #   # See sample authentication script at:
    #   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
    # 
    #   # auth_http localhost/auth.php;
    #   # pop3_capabilities "TOP" "USER";
    #   # imap_capabilities "IMAP4rev1" "UIDPLUS";
    # 
    #   server {
    #       listen     localhost:110;
    #       protocol   pop3;
    #       proxy      on;
    #   }
    # 
    #   server {
    #       listen     localhost:143;
    #       protocol   imap;
    #       proxy      on;
    #   }
    #}
    
    

      保存文件并重启Nginx
      systemctl stop nginx.service && systemctl start nginx.service

    设置定时任务

    因为不提供文件删除功能,只能够手动清理离线下载的问题。
    现在简单说一下定时任务的配置,具体用法请看:
    http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html

    • 编辑定时任务
      crontab -e
    • 在末尾添加一下内容(意思是:设置每两天的0点0分清空文件夹内容)
      0 0 */2 * * rm -rf /root/Download/*
      -查看定时任务配置
      crontab -l
      显示我的配置如下:
    # Edit this file to introduce tasks to be run by cron.
    #
    # Each task to run has to be defined through a single line
    # indicating with different fields when the task will be run
    # and what command to run for the task
    #
    # To define the time you can provide concrete values for
    # minute (m), hour (h), day of month (dom), month (mon),
    # and day of week (dow) or use '*' in these fields (for 'any').#
    # Notice that tasks will be started based on the cron's system
    # daemon's notion of time and timezones.
    #
    # Output of the crontab jobs (including errors) is sent through
    # email to the user the crontab file belongs to (unless redirected).
    #
    # For example, you can run a backup of all your user accounts
    # at 5 a.m every week with:
    # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
    #
    # For more information see the manual pages of crontab(5) and cron(8)
    #
    # m h  dom mon dow   command
    0 0 */2 * * rm -rf /root/Download/*
    

    最后

    访问你的服务器ip/aria2/home就可以访问aria2 webui了,例如127.0.0.1/aria2/home;
    访问服务器离线下载文件地址:你的服务器ip/aria2/download,例如127.0.0.1/aria2/download

    相关文章

      网友评论

          本文标题:Aria2+Nginx在VPS搭建离线下载服务器

          本文链接:https://www.haomeiwen.com/subject/fojktftx.html