把mod_xsendfile导入到Apache中
这样做了之后,Apache的公开的目录下面的任何文件都可能被下载。
比如:
Apache的公开目录默认是
/var/www/html
但是相对于mod_xsendfile
/data/samba/share/xx
上面的这个目录里面的文件都可以被下载。
导入的步骤
事先确认事项
事先,确认Apache中是否已经导入好了
[root@cent01 ~]# yum list installed | grep httpd
httpd.x86_64 2.2.3-53.el5.centos.3 installed
system-config-httpd.noarch 5:1.3.3.3-1.el5 installed
[root@cent01 ~]#
httpd-devel的安装
作为mod_xsendfile的前提,事先需要安装http-devel
(一执行这一步,xsendfile导入的时候就会有error出现)
[root@cent01 ~]# yum -y install httpd-devel.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.iij.ad.jp
* extras: ftp.iij.ad.jp
* updates: ftp.iij.ad.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package httpd-devel.x86_64 0:2.2.22-jason.1 set to be updated
--> Processing Dependency: httpd = 2.2.22-jason.1 for package: httpd-devel
--> Processing Dependency: apr-util-devel for package: httpd-devel
--> Processing Dependency: apr-devel for package: httpd-devel
--> Running transaction check
---> Package apr-devel.x86_64 0:1.4.5-1.jason.1 set to be updated
--> Processing Dependency: apr = 1.4.5-1.jason.1 for package: apr-devel
---> Package apr-util-devel.x86_64 0:1.3.12-1.jason.1 set to be updated
--> Processing Dependency: apr-util = 1.3.12-1.jason.1 for package: apr-util-devel
---> Package httpd.x86_64 0:2.2.22-jason.1 set to be updated
--> Processing Dependency: apr-util-ldap for package: httpd
--> Running transaction check
---> Package apr.x86_64 0:1.4.5-1.jason.1 set to be updated
---> Package apr-util.x86_64 0:1.3.12-1.jason.1 set to be updated
---> Package apr-util-ldap.x86_64 0:1.3.12-1.jason.1 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================================================
Package Arch Version Repository Size
====================================================================================================
Installing:
httpd-devel x86_64 2.2.22-jason.1 utterramblings 150 k
Installing for dependencies:
apr-devel x86_64 1.4.5-1.jason.1 utterramblings 181 k
apr-util-devel x86_64 1.3.12-1.jason.1 utterramblings 70 k
apr-util-ldap x86_64 1.3.12-1.jason.1 utterramblings 20 k
Updating for dependencies:
apr x86_64 1.4.5-1.jason.1 utterramblings 260 k
apr-util x86_64 1.3.12-1.jason.1 utterramblings 201 k
httpd x86_64 2.2.22-jason.1 utterramblings 3.2 M
Transaction Summary
====================================================================================================
Install 4 Package(s)
Upgrade 3 Package(s)
Total download size: 4.0 M
Downloading Packages:
(1/7): apr-util-ldap-1.3.12-1.jason.1.x86_64.rpm | 20 kB 00:00
(2/7): apr-util-devel-1.3.12-1.jason.1.x86_64.rpm | 70 kB 00:00
(3/7): httpd-devel-2.2.22-jason.1.x86_64.rpm | 150 kB 00:00
(4/7): apr-devel-1.4.5-1.jason.1.x86_64.rpm | 181 kB 00:01
(5/7): apr-util-1.3.12-1.jason.1.x86_64.rpm | 201 kB 00:01
(6/7): apr-1.4.5-1.jason.1.x86_64.rpm | 260 kB 00:01
(7/7): httpd-2.2.22-jason.1.x86_64.rpm | 3.2 MB 00:07
----------------------------------------------------------------------------------------------------
Total 273 kB/s | 4.0 MB 00:15
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Updating : apr 1/10
Updating : apr-util 2/10
Installing : apr-devel 3/10
Installing : apr-util-ldap 4/10
Updating : httpd 5/10
warning: /etc/httpd/conf/httpd.conf created as /etc/httpd/conf/httpd.conf.rpmnew
Installing : apr-util-devel 6/10
Installing : httpd-devel 7/10
Cleanup : httpd 8/10
Cleanup : apr 9/10
Cleanup : apr-util 10/10
Installed:
httpd-devel.x86_64 0:2.2.22-jason.1
Dependency Installed:
apr-devel.x86_64 0:1.4.5-1.jason.1 apr-util-devel.x86_64 0:1.3.12-1.jason.1
apr-util-ldap.x86_64 0:1.3.12-1.jason.1
Dependency Updated:
apr.x86_64 0:1.4.5-1.jason.1 apr-util.x86_64 0:1.3.12-1.jason.1 httpd.x86_64 0:2.2.22-jason.1
Complete!
[root@cent01 ~]#
确认「httpd-devel.x86」已经被安装
[root@cent01 ~]# yum list installed | grep httpd
httpd.x86_64 2.2.22-jason.1 installed
httpd-devel.x86_64 2.2.22-jason.1 installed
system-config-httpd.noarch 5:1.3.3.3-1.el5 installed
[root@cent01 ~]#
mod_xsendfile的安装
x-sendfile的tar包可以从下面知道网站去下载
https://tn123.org/mod_xsendfile/
(我使用过的下载命令: wget https://tn123.org/mod_xsendfile/mod_xsendfile-0.12.tar.gz)
[root@cent01 tmp]# tar zxvf mod_xsendfile-0.12.tar.gz
mod_xsendfile-0.12/
mod_xsendfile-0.12/docs/
mod_xsendfile-0.12/docs/Readme.html
mod_xsendfile-0.12/mod_xsendfile.c
[root@cent01 tmp]#
[root@cent01 tmp]# cd mod_xsendfile-0.12
[root@cent01 mod_xsendfile-0.12]# ll
合計 20
drwxr-xr-x 2 1001 1001 4096 10月 12 2010 docs
-rw-r--r-- 1 1001 1001 15992 10月 12 2010 mod_xsendfile.c
[root@cent01 mod_xsendfile-0.12]#
[root@cent01 mod_xsendfile-0.12]# apxs -cia mod_xsendfile.c
/usr/lib64/apr-1/build/libtool --silent --mode=compile gcc -prefer-pic -O2 -g -m64
-mtune=generic -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -pthread -I/usr/include/httpd
-I/usr/include/apr-1 -I/usr/include/apr-1 -c -o mod_xsendfile.lo mod_xsendfile.c &&
touch mod_xsendfile.slo
/usr/lib64/apr-1/build/libtool --silent --mode=link gcc -o mod_xsendfile.la
-rpath /usr/lib64/httpd/modules -module -avoid-version mod_xsendfile.lo
/usr/lib64/httpd/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool'
mod_xsendfile.la /usr/lib64/httpd/modules
/usr/lib64/apr-1/build/libtool --mode=install cp mod_xsendfile.la /usr/lib64/httpd/modules/
cp .libs/mod_xsendfile.so /usr/lib64/httpd/modules/mod_xsendfile.so
cp .libs/mod_xsendfile.lai /usr/lib64/httpd/modules/mod_xsendfile.la
cp .libs/mod_xsendfile.a /usr/lib64/httpd/modules/mod_xsendfile.a
chmod 644 /usr/lib64/httpd/modules/mod_xsendfile.a
ranlib /usr/lib64/httpd/modules/mod_xsendfile.a
PATH="$PATH:/sbin" ldconfig -n /usr/lib64/httpd/modules
----------------------------------------------------------------------
Libraries have been installed in:
/usr/lib64/httpd/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 755 /usr/lib64/httpd/modules/mod_xsendfile.so
[activating module `xsendfile' in /etc/httpd/conf/httpd.conf]
[root@cent01 mod_xsendfile-0.12]#
mod_xsendfile的设定
在mod_xsendfile中指定可以从经过Apache下载的公开目录
在httpd的配置目录中做成新的xsendfile.conf的文件
[root@cent01 mod_xsendfile-0.12]# cd /etc/httpd/conf.d/
[root@cent01 conf.d]#
[root@cent01 conf.d]# ll
合計 16
-rw-r--r-- 1 root root 392 2月 24 02:29 README
-rw-r--r-- 1 root root 560 9月 2 2011 php.conf
-rw-r--r-- 1 root root 566 12月 6 2005 proxy_ajp.conf
-rw-r--r-- 1 root root 299 9月 9 2004 welcome.conf
[root@cent01 conf.d]#
[root@cent01 conf.d]# vi xsendfile.conf
比如;
/data/samba/lib
记载这个目录下面的文件为经由Apache公开情况的设定
<IfModule mod_xsendfile.c>
XsendFile on
XsendFilePath /data/samba/lib
</IfModule>
[root@cent01 conf.d]# ll
合計 20
-rw-r--r-- 1 root root 392 2月 24 02:29 README
-rw-r--r-- 1 root root 560 9月 2 2011 php.conf
-rw-r--r-- 1 root root 566 12月 6 2005 proxy_ajp.conf
-rw-r--r-- 1 root root 299 9月 9 2004 welcome.conf
-rw-r--r-- 1 root root 83 6月 5 22:43 xsendfile.conf
[root@cent01 conf.d]#
Apache重新启动之后,把xsendfile的配置读进来
[root@cent01 conf.d]# /sbin/service httpd restart
httpd を停止中: [ OK ]
httpd を起動中: [ OK ]
[root@cent01 conf.d]#
(配置到此就结束了)
通过Apache来公开文件的方法
(Php 方法)
xsendfile导入之后,实际中下载文件的场合下,为了返回以下的header,在Web app中追加下面的变更
header('X-Sendfile: '.ファイルのパス);
比如:
/data/samba/lib/book1.pdf
被下载的场合,变成下面的
header('X-Sendfile: /data/samba/lib/book1.pdf');
(Rails的方法)
send_file(path, :filename => "/data/samba/lib/book1.pdf", :type => "application/pdf", :disposition => "inline",:x_sendfile =>true)
http://kowloonet.org/memo/tech/index.php?Apache%20mod_xsendfile%A4%CE%C6%B3%C6%FE