分类 Linux笔记 下的文章

充分利用自己服务器:linux实现离线下载与在线云播

首先,结合上一篇文章 lnmpa环境安装ownCloud环境补充,折腾了很久,最后发现几个问题,BT离线下载后BT文件夹目录下为空以及离线下载后的文件都不显示大小,或许有很多解决办法吧,但是继续研究下去就失去了简单方便的原则了,毕竟咱们也不是用于生产环境!
那么现在来一些比较简单方便的方案,这些方案目前转自一些折腾的朋友:
介绍下三个软件:

    aria2:一个轻量化多协议命令行下载工具
    yaaw:为aria2开发的web前端页面,还有很多类似的,可以随意选用。
    h5ai:目录列表程序,可以在线查看图片,文本文档等。

webdir可以替代:https://github.com/maysrp/webdir
通过安装ffmpeg可以实现在线视频播放(暂时只能mp4和mkv格式播放,其余格式并没有完全测试。rmvb不能播放,可以远程转码为mp4后播放)

安装 aria2 + ffmpeg

yum install -y epel-release 
#如果出现缺少Code提示,可以:
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
#安装完成之后,可以查看是否安装成功
yum repolist 
#安装Nux-Dextop源
#导入一个Code
rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
#安装nux-dextop 源
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
#查看repo源是否安装成功
yum repolist 
#update一下
yum update -y
#yum安装ffmpeg
yum install -y ffmpeg
#安装完成后检查ffmpeg 版本
ffmpeg -version
#yum安装aria2
yum install aria2
#安装完成后检查aria2 版本
aria2c --version

关于FFmpeg的安装,这里说明一下:centos7 64bit
然后大家可以尝试:

##################the codes##############
yum install -y vim
 yum install -y epel-release 
 sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
 yum repolist 
 sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro 
 sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-1.el7.nux.noarch.rpm
 yum repolist 
 yum update -y
 yum install -y ffmpeg
ffmpeg -version
###############end of the code##############

做一个这样的脚本chmod +x install-ffmpeg.sh,然后./install-ffmpeg.sh
yum安装ffmpeg是最简单的,如果要编译安装...那么...还不一定成功,我曾经 安装一个国外的视频程序,因为需要用到后台转码,曾经很详细的尝试编译安装ffmpeg,非常复杂,有动手能力的可以去编译安装,版本也比较新!

ffmpeg -version
ffmpeg version 2.6.8 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-4)
configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --enable-bzlib --disable-crystalhd --enable-gnutls --enable-ladspa --enable-libass --enable-libcdio --enable-libdc1394 --enable-libfaac --enable-nonfree --enable-libfdk-aac --enable-nonfree --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvorbis --enable-libv4l2 --enable-libx264 --enable-libx265 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --shlibdir=/usr/lib64 --enable-runtime-cpudetect
libavutil      54. 20.100 / 54. 20.100
libavcodec     56. 26.100 / 56. 26.100
libavformat    56. 25.101 / 56. 25.101
libavdevice    56.  4.100 / 56.  4.100
libavfilter     5. 11.102 /  5. 11.102
libavresample   2.  1.  0 /  2.  1.  0
libswscale      3.  1.101 /  3.  1.101
libswresample   1.  1.100 /  1.  1.100
libpostproc    53.  3.100 / 53.  3.100

先上传配置文件,后面有补充!
然后创建一个aria2.session文件:echo '' > /root/aria2.session;
最后关闭防火墙

service iptables stop
chkconfig iptables off

Nginx conf 配置目录列表,可省去 h5ai。

  location /down {
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
    }

Nginx 编译时最后加上

--with-http_mp4_module 
#可添加 MP4 流媒体支持。

这里补充一下关于aria2的启动,一定要带配置文件启动,否则yaaw等会出现一些问题!

/usr/local/bin/aria2c --conf-path=/etc/aria2.conf --enable-rpc --rpc-listen-all=true --rpc-allow-origin-all -c -D --check-certificate=false

或者:

aria2c --enable-rpc --rpc-listen-all=true --rpc-allow-origin-all -c

带用户:

sudo -u www aria2c --enable-rpc --rpc-listen-all=true --rpc-allow-origin-all -c -D --check-certificate=false

或者采用:

cd /usr/local/bin/
chown www.www aria2c (chown www aria2c)
chmod g+s aria2c     (chmod u+s aria2c)

连用户组一起改了,可能导致下面启动脚本无法启动,但是可以手动启动,所以只改用户也一样,测试可以用于php-ffmpeg直接转换下载的视频...
然后带参数启动也是可以的!
这种启动模式主要是一些网盘或者文件管理系统结合aria2,可以管理文件,否则aria2默认是root用户,下载的文件也是root用户的,来个www用户的WEB页面去管理文件,肯定不行,所以这里也推荐使用AriaNg,下载:https://github.com/mayswind/AriaNg/releases,带有后台配置功能,结合起来用非常不错,自己结合起来玩!
centos7可以参考:
vi /etc/init.d/aria2

#!/bin/bash
#
# aria2 - this script starts and stops the aria2 daemon
#
# chkconfig:   - 85 15
# description: Aria2 - Download Manager
# processname: aria2c
# config:      /etc/aria2/aria2.conf
# pidfile:     

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

aria2c="/usr/bin/aria2c"
ARIA2C_CONF_FILE="/etc/aria2/aria2.conf"
options=" --conf-path=$ARIA2C_CONF_FILE -D "

RETVAL=0

start() {
        # code here to start the program
        echo -n "Starting aria2c daemon."
        ${aria2c} ${options}
        RETVAL=$?
        echo
}

stop() {
        echo -n "Shutting down aria2c daemon."
        /usr/bin/killall aria2c
        RETVAL=$?
        echo
}

status() {
        ID=$(/bin/ps -ef | grep 'aria2c' | grep -v 'grep' | awk '{print $2}')
        if [[ "x$ID" != "x" ]]; then
                echo "Aria2 is running."
        else
                echo "Aria2 is not running."
        fi
}

restart() {
        stop
        sleep 3
        start
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        status) 
                status
                ;;
        restart)
                restart
                ;;
        *)
                echo "Usage: service aria2c {start|stop|restart}"
                RETVAL=1                             
esac                                                 

exit $RETVAL

给脚本权限:chmod +x /etc/init.d/aria2
启动:/etc/init.d/aria2 start
可能会提示:Starting aria2 (via systemctl): Warning: aria2.service changed on disk. Run 'systemctl daemon-reload' to reload units.
那么按照提示输入systemctl daemon-reload试试.../etc/init.d/aria2 restart试试,yaaw看看能否OK,或者ps -aux | grep aria2c看看...

systemctl enable aria2

防火墙:systemctl stop firewalld.service systemctl disable firewalld.service
等等方法,参考之前文章亦可!
配置文件参考:aria2.zip 修改好上传到/etc//aria2/下
参考:原文是这里

或者:
安装配置好 aira2 后[http://aria2c.com/usage.html]
再添加一个 webdir
https://github.com/maysrp/webdir
可以设置一个登入密码:

define("PASS", "admin");

设置禁止显示文件

$this->notex=array("php","js","tgz");//不允许显示的后缀名文件 
$this->notdir=array("a","phpmyadmin");//不允许显示的文件夹 

然后继续吧,一次性说完,下面说说 芒果云KodExplorer手动添加Aria2c

rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.i686.rpm
rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
yum -y install aria2
iptables -I INPUT -p tcp --dport 6800 -jACCEPT 
iptables -I INPUT -p tcp --dport 6976 -jACCEPT 
iptables -I INPUT -p udp --dport 6991 -jACCEPT 
iptables -I INPUT -p tcp --dport 6969 -jACCEPT 
service iptables save
service iptables restart
aria2c --event-poll=select --enable-rpc --rpc-listen-all=true --rpc-allow-origin-all --file-allocation=none -c -D --bt-tracker=http://mgtracker.org:2710/announce,http://tracker.istole.it/announce,http://tracker.coppersurfer.tk:6969/announce
echo “aria2c --enable-rpc --rpc-listen-all=true --rpc-allow-origin-all -c” >> /etc/rc.local
cd /xxx //切换到web环境目录
git clone https://github.com/ghostry/webui-aria2.git //下载webui-aria2汉化版

集成版:看看这里

好了,使劲折腾吧,看哪种适合你,用哪种!现在网盘都靠不住,还是自己玩吧!需要服务器的可以联系我,便宜的配置高的,速度快的都有,适合自己做网盘的也有!

lnmpa环境安装ownCloud环境补充

PHP模块'文件信息'丢失. 我们强烈建议启用此模块以便mime类型检测取得最佳结果.
/dev/urandom 无法被 PHP 读取,出于安全原因,这是强烈不推荐的。请查看文档了解详情。

PHP的fileinfo:

cd /lnmp**/src/php-5.*.*/ext/fileinfo/
/usr/local/php/bin/phpize

返回:

    Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525

./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

返回如下信息:

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/

表示OK了!
最后在/usr/local/php/etc/php.ini 添加扩展:

extension=fileinfo.so

/dev/urandom:
/usr/local/apache/conf/vhost/*.conf
找到你网站对应的配置文件,在php_admin_value open_basedir这行后面添加/dev/urandom即可,例如:

"/home/wwwroot/***:/tmp/:/var/tmp/:/proc/:/dev/urandom"

好了,先说到这里,再补充!

继续补充离线下载:ocDownloader(ownCloud 8.0-8.1版本适用,我使用的是ownCloud 9.1)
https://apps.owncloud.com/content/show.php/ocDownloader+%28NG%29?content=169974
https://github.com/DjazzLab/ocdownloader
首先安装Aria2:
centos6 64bit
编译安装Aria2
依赖环境
Aria2 1.17.1以上版本要求gcc >= 4.8.3 or clang >= 3.4
安装clang


$ yum install clang   #epel源

安装GCC
通过SCL安装GCC
CentOS 6

https://copr.fedoraproject.org/coprs/rhscl/devtoolset-3/

$ wget https://copr.fedoraproject.org/coprs/rhscl/devtoolset-3/repo/epel-6/rhscl-devtoolset-3-epel-6.repo   -O /etc/yum.repos.d/rhscl-devtoolset-3-epel-6.repo
$ yum install devtoolset-3-gcc devtoolset-3-gcc-c++ devtoolset-3-binutils devtoolset-3-gcc-gfortran 
$ scl enable devtoolset-3 bash   #启用SCL环境中新版本GCC 
$ gcc --version

编译Aria2

$ wget https://github.com/aria2/aria2/releases/download/release-1.30.0/aria2-1.30.0.tar.gz
$ tar xzvf aria2-1.30.0.tar.gz
$ cd aria2-1.30.0
$ ./configure
$ make
$ make install
$ man aria2c //查看aria2c manual

验证Aria2版本

$ aria2c --version

下面作为一个参考吧,作者的博客已经无法访问wiki也访问不了,内容就无从查起了,只能说作为一个参考看看...

接着直接把它放到usr/local/bin/目录下。
启动aria2的话,根据
https://wiki.sgc-univ.net/index.php/OCDownloader:Run_ARIA2C_Daemon_as_www-data
使用www-data用户开启aria2,不建议开启日志,尤其是挂bt的时候,日志会很快涨到G数量级的。
不过这个wiki提供的方法还真是蛋疼,没忍住还是用supervisor了。贴个配置。

[program:aria2-ocdownloader]
command=/usr/local/bin/aria2c --enable-rpc --rpc-allow-origin-all -c -D --check-certificate=false
autostart=true
autorestart=true
user=www-data
重新载入配置, supervisorctl reload。

下面我们就临时启动:

/usr/local/bin/aria2c --enable-rpc --rpc-allow-origin-all -c -D --check-certificate=false

然后下载:ocdownloader,https://github.com/DjazzLab/ocdownloader,传到apps目录下,记得把目录权限修改好,例如chown -R www.www apps
好了,测试下载了一个http文件和一些bt,http当然直接秒下了,bt还在继续,只是问题是下载后文件在ownCloud看不到大小,但是通过SFTP等管理文件都可以看到此文件以及大小,具体在/data/**/files/Downloads/ 用户对应的目录下!就这样,还有待解决一系列问题才能正式使用,这只是临时测试,下一步看看是否部署一下,充分利用空闲的服务器,同时也摈弃那些所谓的网盘了!
https://github.com/rg3/youtube-dl

# 下载YouTube-DL 到 /usr/local/bin/ 目录
wget https://yt-dl.org/downloads/latest/youtube-dl -O /usr/local/bin/youtube-dl
# 设置可执行权限
chmod a+x /usr/local/bin/youtube-dl

继续,因为上述启动aria2c后,下载bt文件会发现,bt文件夹目录里面都是root权限的文件,OW里面无法显示!
PS:N久后再次补充更新,解决了aria2c用户组问题,下载的文件所属权限也没问题,但是BT离线后文件依然无法显示!那么临时通过这个方法试试吧:在ownCloud根目录下执行

# 假如web用户为www
# 重新扫描所有用户的文件
sudo -u www php occ files:rescan --all
# 重新扫描admin用户的文件
sudo -u www php occ files:rescan admin

sudo -u www php occ files:scan admin
Starting scan for user 1 out of 1 (admin)

+---------+-------+--------------+
| Folders | Files | Elapsed time |
+---------+-------+--------------+
| 14      | 51    | 00:00:11     |
+---------+-------+--------------+

然后通过OW能看到文件显示了,具体的以后再研究吧,反正ocdownloader真心太麻烦了,作者也没更新了,这里仅供大家参考吧,有成功的请指教一下吧...
下面来试试看supervisorctl:
根据服务器上的python版本下载对应的setuptools

python -V
Python 2.6.6

wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg#md5=bfa92100bd772d5a213eedd356d64086

直接安装

sh setuptools-0.6c11-py2.6.egg

下载并安装supervisor

wget http://pypi.python.org/packages/source/s/supervisor/supervisor-3.2.3.tar.gz
tar -zxvf supervisor-3.2.3.tar.gz
cd supervisor-3.2.3
python setup.py install

安装setuptools后也可以

easy_install supervisor

设定supervisor配置文件

echo_supervisord_conf  >/etc/supervisord.conf
vi /etc/supervisord.conf

取消以下的注释,并修改IP为0.0.0.0

[inet_http_server]         ; inet (TCP) server disabled by default
port=0.0.0.0:9001        ; (ip_address:port specifier, *:port for all iface)
username=user              ; (default is no username (open server))
password=123               ; (default is no password (open server))

增加自定义的后台进程(注意进程名之间用一个:分隔)

[program:aria2-ocdownloader]
command=/usr/local/bin/aria2c --enable-rpc --rpc-listen-all=true --rpc-allow-origin-all -c -D --check-certificate=false
autostart=true
autorestart=true
user=www

设定supervisor启动文件

    vi /etc/init.d/supervisord

 

#! /bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

PROGNAME=supervisord

DAEMON=/usr/bin/$PROGNAME

CONFIG=/etc/$PROGNAME.conf

PIDFILE=/tmp/$PROGNAME.pid

DESC="supervisord daemon"

SCRIPTNAME=/etc/init.d/$PROGNAME

# Gracefully exit if the package has been removed.

test -x $DAEMON || exit 0

 

start()

{

        echo -n "Starting $DESC: $PROGNAME"

        $DAEMON -c $CONFIG

        echo "..."

}

stop()

{

        echo -n "Stopping $DESC: $PROGNAME"

        supervisor_pid=$(cat $PIDFILE)

        kill -15 $supervisor_pid

        echo "..."

}

case "$1" in

  start)

        start

        ;;

  stop)

        stop

        ;;

  restart)

        stop

        start

        ;;

  *)

        echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2

        exit 1

        ;;

esac

exit 0

再来看看:

ps -ef | grep aria2c
www      11234     1  0 15:36 ?        00:00:00 /usr/local/bin/aria2c --enable-rpc --rpc-allow-origin-all -c -D --check-certificate=false
root     11246 10638  0 15:36 pts/0    00:00:00 grep aria2c