FastDFS的PHP支持

来源:互联网 发布:战术防身笔淘宝 编辑:程序博客网 时间:2024/06/05 06:55

本文参考http://www.jb51.net/article/109228.htm文章,并在安装过程中出现的问题加入解决方法。

系统CentOS6.5

软件fastdfs-nginx-module_v1.19.zip、fastdfs-nginx-module_v1.19.zip、libfastcommon-master.zip、nginx-1.13.0.tar.gz

测试发现fastdfs安装过程中对各个依赖环境的版本有要求,上面的几个可以搭配使用。

我直接从官网下载的PHP5.5.38的源码包,用工具上传的到centos服务器上,也可以直接用下面的命令下载

wget http://cn2.php.net/distributions/php-5.5.38.tar.gz

安装PHP依赖环境

yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel
解压
tar -zxvf php-5.5.38.tar.gzcd php-5.5.38
编译配置
./configure --prefix=/usr/local/php --with-config-file-path=/etc  --with-apxs2=/usr/sbin/apxs --with-mysql  --enable-fpm  --with-jpeg-dir --with-zlib --with-gd  --with-openssl --enable-soap --enable-mbstring --with-curl

报错

configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.checking whether to enable computed goto gcc extension with re2c... nochecking whether to force non-PIC code in shared modules... nochecking whether /dev/urandom exists... yeschecking for pthreads_cflags... -pthreadchecking for pthreads_lib... Configuring SAPI moduleschecking for AOLserver support... nochecking for Apache 1.x module support via DSO through APXS... nochecking for Apache 1.x module support... nochecking whether to enable Apache charset compatibility option... nochecking for Apache 2.0 filter-module support via DSO through APXS... nochecking for Apache 2.0 handler-module support via DSO through APXS... Sorry, I cannot run apxs.  Possible reasons follow:1. Perl is not installed2. apxs was not found. Try to pass the path using --with-apxs2=/path/to/apxs3. Apache was not built using --enable-so (the apxs usage page is displayed)

这个情况是因为没有装apache和apache的扩展,如果用不到apxs可以直接删除便已配置里的

--with-apxs2=/usr/sbin/apxs //删除这句

或者通过通过yum安装。

yum -y install httpd httpd-devel
安装完成之后通过命令查看apxs路径
[root@localhost php-5.5.38]# find / -name apxs/usr/sbin/apxs[root@localhost php-5.5.38]# 

我们重新配置PHP

./configure --prefix=/usr/local/php --with-config-file-path=/etc  --with-apxs2=/usr/sbin/apxs --with-mysql  --enable-fpm  --with-jpeg-dir --with-zlib --with-gd  --with-openssl --enable-soap --enable-mbstring --with-curl

配置完成

Generating filesconfigure: creating ./config.statuscreating main/internal_functions.ccreating main/internal_functions_cli.c+--------------------------------------------------------------------+| License:                                                           || This software is subject to the PHP License, available in this     || distribution in the file LICENSE.  By continuing this installation || process, you are bound by the terms of this license agreement.     || If you do not agree with the terms of this license, you must abort || the installation process at this point.                            |+--------------------------------------------------------------------+Thank you for using PHP.config.status: creating php5.specconfig.status: creating main/build-defs.hconfig.status: creating scripts/phpizeconfig.status: creating scripts/man1/phpize.1config.status: creating scripts/php-configconfig.status: creating scripts/man1/php-config.1config.status: creating sapi/cli/php.1config.status: creating sapi/fpm/php-fpm.confconfig.status: creating sapi/fpm/init.d.php-fpmconfig.status: creating sapi/fpm/php-fpm.serviceconfig.status: creating sapi/fpm/php-fpm.8config.status: creating sapi/fpm/status.htmlconfig.status: creating sapi/cgi/php-cgi.1config.status: creating ext/phar/phar.1config.status: creating ext/phar/phar.phar.1config.status: creating main/php_config.hconfig.status: executing default commands[root@localhost php-5.5.38]# 

编译安装(等待时间较长)

make && make install
编译安装完成

Build complete.Don't forget to run 'make test'.Installing PHP SAPI module:       apache2handler/usr/lib64/httpd/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool' libphp5.la /usr/lib64/httpd/modules/usr/lib64/apr-1/build/libtool --mode=install cp libphp5.la /usr/lib64/httpd/modules/libtool: install: cp .libs/libphp5.so /usr/lib64/httpd/modules/libphp5.solibtool: install: cp .libs/libphp5.lai /usr/lib64/httpd/modules/libphp5.lalibtool: install: warning: remember to run `libtool --finish /home/software/php-5.5.38/libs'chmod 755 /usr/lib64/httpd/modules/libphp5.so[activating module `php5' in /etc/httpd/conf/httpd.conf]Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/Installing PHP CLI binary:        /usr/local/php/bin/Installing PHP CLI man page:      /usr/local/php/php/man/man1/Installing PHP FPM binary:        /usr/local/php/sbin/Installing PHP FPM config:        /usr/local/php/etc/Installing PHP FPM man page:      /usr/local/php/php/man/man8/Installing PHP FPM status page:      /usr/local/php/php/php/fpm/Installing PHP CGI binary:        /usr/local/php/bin/Installing PHP CGI man page:      /usr/local/php/php/man/man1/Installing build environment:     /usr/local/php/lib/php/build/Installing header files:          /usr/local/php/include/php/Installing helper programs:       /usr/local/php/bin/  program: phpize  program: php-configInstalling man pages:             /usr/local/php/php/man/man1/  page: phpize.1  page: php-config.1Installing PEAR environment:      /usr/local/php/lib/php/[PEAR] Archive_Tar    - installed: 1.4.0[PEAR] Console_Getopt - installed: 1.4.1[PEAR] Structures_Graph- installed: 1.1.1[PEAR] XML_Util       - installed: 1.3.0[PEAR] PEAR           - installed: 1.10.1Wrote PEAR system config file at: /usr/local/php/etc/pear.confYou may want to add: /usr/local/php/lib/php to your php.ini include_path/home/software/php-5.5.38/build/shtool install -c ext/phar/phar.phar /usr/local/php/binln -s -f phar.phar /usr/local/php/bin/pharInstalling PDO headers:          /usr/local/php/include/php/ext/pdo/[root@localhost php-5.5.38]# 
修改httpd.conf文件
[cpp] view plain copy
  1. vim /etc/httpd/conf/httpd.conf  
AddType application*后面加如下一行
[cpp] view plain copy
  1. AddType application/x-httpd-php .php .phtml  
DirectoryIndex index.html加上index.php
[cpp] view plain copy
  1. DirectoryIndex index.php index.html  
确保httd.conf文件中包含以下字段,如不包含则加入此字段
[cpp] view plain copy
  1. LoadModule php5_module        /usr/lib64/httpd/modules/libphp5.so  
重启HTTPD
[cpp] view plain copy
  1. service httpd restart  

配置PHP环境变量
[cpp] view plain copy
  1. vim /etc/profile  
在最下方加入
[cpp] view plain copy
  1. PATH=$PATH:/usr/local/php/bin  
  2. export PATH  
使配置立即生效
[cpp] view plain copy
  1. source /etc/profile  
配置php-fpm
[cpp] view plain copy
  1. cp php.ini-production /etc/php.ini  
  2. cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf  
  3. cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm  
  4. chmod +x /etc/init.d/php-fpm    
启动php-fpm
[cpp] view plain copy
  1. service php-fpm start  

[root@localhost php-5.3.38]# service php-fpm startStarting php-fpm  done

可以通过php-v查看PHP版本

[root@localhost php-5.5.38]# php -vPHP 5.5.38 (cli) (built: Oct  6 2017 05:55:48) Copyright (c) 1997-2015 The PHP GroupZend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies[root@localhost php-5.5.38]# 
通过php -m查看已加载的模块
[root@localhost php-5.5.38]# php -m[PHP Modules]bcmathbz2calendarCorectypecurldatedbadomeregexiffileinfofilterftpgdgettextgmphashiconvjsonlibxmlmbstringmcryptmhashmysqlimysqlndopensslpcrePDOpdo_mysqlpdo_sqlitePharposixreadlineReflectionsessionshmopSimpleXMLsoapsocketsSPLsqlite3standardsysvmsgsysvsemsysvshmtokenizerwddxxmlxmlreaderxmlrpcxmlwriterxslzipzlib[Zend Modules][root@localhost php-5.5.38]# php -v
好了PHP我们安装完成,下一步看看FastDFS该如何调整。

此处我们参考http://www.bubuko.com/infodetail-1722873.html这篇文章。

FastDFS已经提前安装好了。我们直接进行操作。进入fastdfs的php_client目录

cd /home/software/fastdfs-5.05/php_client

执行phpize

/usr/local/php/bin/phpize

配置编译

./configure --with-php-config=/usr/local/php/bin/php-config
编译安装

make && make install
完成

checking the maximum length of command line arguments... 1966080checking command to parse /usr/bin/nm -B output from cc object... okchecking for objdir... .libschecking for ar... archecking for ranlib... ranlibchecking for strip... stripchecking if cc supports -fno-rtti -fno-exceptions... nochecking for cc option to produce PIC... -fPICchecking if cc PIC flag -fPIC works... yeschecking if cc static flag -static works... nochecking if cc supports -c -o file.o... yeschecking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yeschecking whether -lc should be explicitly linked in... nochecking dynamic linker characteristics... GNU/Linux ld.sochecking how to hardcode library paths into programs... immediatechecking whether stripping libraries is possible... yeschecking if libtool supports shared libraries... yeschecking whether to build shared libraries... yeschecking whether to build static libraries... nocreating libtoolappending configuration tag "CXX" to libtoolconfigure: creating ./config.statusconfig.status: creating config.hconfig.status: config.h is unchanged[root@localhost php_client]# 
编译安装
make && make install

完成

----------------------------------------------------------------------Libraries have been installed in:   /home/software/fastdfs-5.05/php_client/modulesIf you ever happen to want to link against installed librariesin a given directory, LIBDIR, you must either use libtool, andspecify 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 formore information, such as the ld(1) and ld.so(8) manual pages.----------------------------------------------------------------------Build complete.Don't forget to run 'make test'.Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/

查看一下生成的so文件

[root@localhost php_client]# ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/total 860-rwxr-xr-x 1 root root 339077 Oct  6 06:02 fastdfs_client.so-rwxr-xr-x 1 root root 538161 Oct  6 05:56 opcache.so
看到除了fastdfs相关的so文件意外还多了个opcache.so的文件,这应该是因为我们编译配置时候添加了--enable-opcache导致的,我们无视就可以

接下来,将fastdfs解压目录下的fastdfs_client.ini的内容导入到php.ini中

cat /home/software/fastdfs-5.05/php_client/fastdfs_client.ini >> /etc/php.ini
检查fastdfs 里的php是否正常
/usr/local/php/bin/php /home/software/fastdfs-5.05/php_client/fastdfs_test.php
php客户端测试

新建一个test.txt内容为三个test

echo "test test test" > test.txt
新建一个PHP文件 名称为test.php
vim test.php
写入如下内容
<?php$ret = fastdfs_storage_upload_by_filename('./test.txt');var_dump($ret);
保存退出后用PHP运行test.php文件
[root@localhost php_client]#  /usr/local/php/bin/php test.phparray(2) {  ["group_name"]=>  string(6) "group1"  ["filename"]=>  string(44) "M00/00/00/wKgB6VnYPdKAUFWSAAAADxtaRO4483.txt"}[root@localhost php_client]# 
返回如上就证明成功了,现在我们可以测试使用trackerIP+group信息+路径来访问

下面是几个成品代码,参考http://blog.csdn.net/clevercode/article/details/52334597进行一定更改,实现直接从WEB服务器往文件服务器上传图片的功能

test.php

<html>  <body>    <form action="curlupload.php" method="post" enctype="multipart/form-data">      <label for="file">Filename:</label>      <input type="file" name="upFile" id="upFile" />       <br />      <input type="submit" name="submit" value="Submit" />  </form>    </body>  </html>  
curlupload.php
<?php  function curl_file(){        $upload = $_FILES['upFile']["tmp_name"];        $type = $_FILES['upFile']['type'];        $name = $_FILES['upFile']['name'];                $data = array('upFile'=>'@'. $upload,'name'=>$name,'type'=>$type);        $ch = curl_init();        curl_setopt($ch,CURLOPT_URL,"http://192.168.1.230:88/upload.php");        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);        curl_setopt($ch,CURLOPT_POST,true);        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);        $result = curl_exec($ch);         var_dump(curl_error($ch));          curl_close($ch);        return $result;    }    echo curl_file();?>
upload.php
<?php//上传附件function uploadAttach()                                                                            {/*{{{*/                                                                                                      $ret = array();    $ret['errorcode'] = 0;    $ret['errormsg'] = '';    if(!$_FILES || false == isset($_FILES["upFile"]))    {        $ret['errorcode'] = 1;        $ret['errormsg'] = "ERROR:upFile is not set";        return $ret;    }    $file = $_FILES["upFile"];    if (false == isset($file['tmp_name']) || false == is_file($file['tmp_name']))    {        $ret['errorcode'] = 2;        $ret['errormsg'] = "tmp_name is not file";        return $ret;    }    if (0 == filesize($file['tmp_name']))    {        $ret['errorcode'] = 3;        $ret['errormsg'] = "tmp_name filesize is 0";        return $ret;    }    $curlFile = new CurlFile($file['tmp_name'], $_POST['type'], $_POST['name']);      $fileSuffix = getSuffix($curlFile->getPostFilename());                                                      $ret['file'] = $file;    $ret['fileId'] = uploadToFastdfs($curlFile, $fileSuffix);                                                            return $ret;}/*}}}*/                                                                                                  //获取后缀 function getSuffix($fileName)  {/*{{{*/     preg_match('/\.(\w+)?$/', $fileName, $matchs);     return isset($matchs[1])?$matchs[1]:''; }/*}}}*///上传文件到fastdfsfunction uploadToFastdfs(CurlFile $file, $fileSuffix)                                                  {/*{{{*/                                                                                                      $fdfs = new FastDFS();     $tracker = $fdfs->tracker_get_connection();      $fileId = $fdfs->storage_upload_by_filebuff1(file_get_contents($file->getFilename()), $fileSuffix);      $fdfs->tracker_close_all_connections();        return $fileId;}/*}}}*/                                                                                                  function start(){    $ret = uploadAttach();      print_r($ret);}start();?>

假设你的fastdfs服务器是A、WEB服务器是B,将test.php和curlupload.php放到B机器,将upload.php放到A机器。然后可以实现访问B服务器上传文件至A服务器。需要注意的一点是要在curl.php文件里把http://192.168.1.230:88替换成自己A服务器的IP和端口。我这里因为只有一台机器,所以同时在一台机器上装了A和B,所以测试图地址是同一IP,不同电脑已经实测过没有问题。

访问test.php上传


得到返回路径

根据返回路径通过服务器AIP+路径实现读取。

http://192.168.1.230/group1/M00/00/00/wKgB6VnZRNKAYz4IAATBl8xofxY473.jpg


好了,大概就到这里,后期有什么补充的我会添进来,如果有什么疑问请评论留言。