首页 > 代码库 > FastDFS的php和nginx模块配置

FastDFS的php和nginx模块配置

一、FastDFS和php整合

1、安装php

# 安装依赖包
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel  openssl openssl-devel openldap openldap-devel nss_ldap openldap_clients

# 安装epel,为了安装libmcrypt等包
yum  install epel-release 
yum install libmcrypt libmcrypt-devel mcrypt mhash -y

#下载并解压php源码包
cd /opt/tools
wget http://cn2.php.net/get/php-5.6.26.tar.gz/from/this/mirror
tar -zxf php-5.6.26.tar.gz

#编译安装
cd php-5.6.26
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc  --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear --with-zlib --enable-pdo --with-pdo-mysql
make && make install

2、生成并配置fastdfs的php模块

# 进入fastdfs源码解压后的目录
cd /opt/tools/fastdfs-5.05/php_client/
/usr/local/php/bin/phpize

# 测试php安装结果
[root@mylinux3 php_client]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226

# 编译并安装fastdfs的php模块
./configure --with-php-config=/usr/local/php/bin/php-config 
make && make install
[root@mylinux3 php_client]# ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
total 1992
-rwxr-xr-x 1 root root  340037 Oct  5 17:26 fastdfs_client.so
-rwxr-xr-x 1 root root 1105024 Oct  5 17:13 opcache.a
-rwxr-xr-x 1 root root  586352 Oct  5 17:13 opcache.so

3、编辑php.ini

cp /opt/tools/php-5.6.26/php.ini-production /usr/locni-production /usr/local/php/etc/php.ini
cd /opt/tools/fastdfs-5.05/php_client
[root@mylinux3 php_client]# ll fastdfs_client.ini 
-rw-rw-r-- 1 root root 1403 Nov 22  2014 fastdfs_client.ini
cat fastdfs_client.ini >> /usr/local/php/etc/php.ini

4、测试通过php操作fastdfs

/usr/local/php/bin/php fastdfs_test.php 

[root@mylinux3 php_client]# /usr/local/php/bin/php fastdfs_test.php 
5.05
fastdfs_tracker_make_all_connections result: 1
array(1) {
  ["group1"]=>
  array(13) {
    ["total_space"]=>
    int(17944)
    ["free_space"]=>
    int(12476)
    ["trunk_free_space"]=>
    int(0)
    ["server_count"]=>
    int(2)
    ["active_count"]=>
    int(2)
    ["storage_port"]=>
    int(23000)
    ["storage_http_port"]=>
    int(8888)
    ["store_path_count"]=>
    int(1)
    ["subdir_count_per_path"]=>
    int(256)
    ["current_write_server"]=>
    int(0)
    ["current_trunk_file_id"]=>
    int(0)
    ["192.168.100.181"]=>
    array(61) {
      ["ip_addr"]=>
      string(15) "192.168.100.181"
      ["join_time"]=>
      int(1475602195)
      ["up_time"]=>
      int(1475646224)
      ["http_domain"]=>
      string(0) ""
      ["version"]=>
      string(4) "5.05"
      ["src_storage_id"]=>
      string(0) ""
      ["if_trunk_server"]=>
      bool(false)
      ["upload_priority"]=>
      int(10)
      ["store_path_count"]=>
      int(1)
      ["subdir_count_per_path"]=>
      int(256)
      ["storage_port"]=>
      int(23000)
      ["storage_http_port"]=>
      int(8888)
      ["current_write_path"]=>
      int(0)
      ["status"]=>
      int(7)
      ["total_space"]=>
      int(17944)
      ["free_space"]=>
      int(12476)
      ["connection.alloc_count"]=>
      int(256)
      ["connection.current_count"]=>
      int(1)
      ["connection.max_count"]=>
      int(2)
      ["total_upload_count"]=>
      int(1)
      ["success_upload_count"]=>
      int(1)
      ["total_append_count"]=>
      int(0)
      ["success_append_count"]=>
      int(0)
      ["total_modify_count"]=>
      int(0)
      ["success_modify_count"]=>
      int(0)
      ["total_truncate_count"]=>
      int(0)
      ["success_truncate_count"]=>
      int(0)
      ["total_set_meta_count"]=>
      int(0)
      ["success_set_meta_count"]=>
      int(0)
      ["total_delete_count"]=>
      int(1)
      ["success_delete_count"]=>
      int(1)
      ["total_download_count"]=>
      int(2)
      ["success_download_count"]=>
      int(2)
      ["total_get_meta_count"]=>
      int(0)
      ["success_get_meta_count"]=>
      int(0)
      ["total_create_link_count"]=>
      int(0)
      ["success_create_link_count"]=>
      int(0)
      ["total_delete_link_count"]=>
      int(0)
      ["success_delete_link_count"]=>
      int(0)
      ["total_upload_bytes"]=>
      int(13)
      ["success_upload_bytes"]=>
      int(13)
      ["total_append_bytes"]=>
      int(0)
      ["success_append_bytes"]=>
      int(0)
      ["total_modify_bytes"]=>
      int(0)
      ["success_modify_bytes"]=>
      int(0)
      ["total_download_bytes"]=>
      int(26)
      ["success_download_bytes"]=>
      int(26)
      ["total_sync_in_bytes"]=>
      int(12)
      ["success_sync_in_bytes"]=>
      int(12)
      ["total_sync_out_bytes"]=>
      int(0)
      ["success_sync_out_bytes"]=>
      int(0)
      ["total_file_open_count"]=>
      int(5)
      ["success_file_open_count"]=>
      int(5)
      ["total_file_read_count"]=>
      int(2)
      ["success_file_read_count"]=>
      int(2)
      ["total_file_write_count"]=>
      int(3)
      ["success_file_write_count"]=>
      int(3)
      ["last_heart_beat_time"]=>
      int(1475660266)
      ["last_source_update"]=>
      int(1475647676)
      ["last_sync_update"]=>
      int(1475648117)
      ["last_synced_timestamp"]=>
      int(1475648114)
    }
    ["192.168.100.182"]=>
    array(61) {
      ["ip_addr"]=>
      string(15) "192.168.100.182"
      ["join_time"]=>
      int(1475602286)
      ["up_time"]=>
      int(1475648692)
      ["http_domain"]=>
      string(0) ""
      ["version"]=>
      string(4) "5.05"
      ["src_storage_id"]=>
      string(15) "192.168.100.181"
      ["if_trunk_server"]=>
      bool(false)
      ["upload_priority"]=>
      int(10)
      ["store_path_count"]=>
      int(1)
      ["subdir_count_per_path"]=>
      int(256)
      ["storage_port"]=>
      int(23000)
      ["storage_http_port"]=>
      int(8888)
      ["current_write_path"]=>
      int(0)
      ["status"]=>
      int(7)
      ["total_space"]=>
      int(18717)
      ["free_space"]=>
      int(15808)
      ["connection.alloc_count"]=>
      int(256)
      ["connection.current_count"]=>
      int(1)
      ["connection.max_count"]=>
      int(1)
      ["total_upload_count"]=>
      int(1)
      ["success_upload_count"]=>
      int(1)
      ["total_append_count"]=>
      int(1)
      ["success_append_count"]=>
      int(1)
      ["total_modify_count"]=>
      int(0)
      ["success_modify_count"]=>
      int(0)
      ["total_truncate_count"]=>
      int(0)
      ["success_truncate_count"]=>
      int(0)
      ["total_set_meta_count"]=>
      int(0)
      ["success_set_meta_count"]=>
      int(0)
      ["total_delete_count"]=>
      int(0)
      ["success_delete_count"]=>
      int(0)
      ["total_download_count"]=>
      int(2)
      ["success_download_count"]=>
      int(2)
      ["total_get_meta_count"]=>
      int(0)
      ["success_get_meta_count"]=>
      int(0)
      ["total_create_link_count"]=>
      int(0)
      ["success_create_link_count"]=>
      int(0)
      ["total_delete_link_count"]=>
      int(0)
      ["success_delete_link_count"]=>
      int(0)
      ["total_upload_bytes"]=>
      int(6)
      ["success_upload_bytes"]=>
      int(6)
      ["total_append_bytes"]=>
      int(6)
      ["success_append_bytes"]=>
      int(6)
      ["total_modify_bytes"]=>
      int(0)
      ["success_modify_bytes"]=>
      int(0)
      ["total_download_bytes"]=>
      int(25)
      ["success_download_bytes"]=>
      int(25)
      ["total_sync_in_bytes"]=>
      int(13)
      ["success_sync_in_bytes"]=>
      int(13)
      ["total_sync_out_bytes"]=>
      int(0)
      ["success_sync_out_bytes"]=>
      int(0)
      ["total_file_open_count"]=>
      int(5)
      ["success_file_open_count"]=>
      int(5)
      ["total_file_read_count"]=>
      int(2)
      ["success_file_read_count"]=>
      int(2)
      ["total_file_write_count"]=>
      int(3)
      ["success_file_write_count"]=>
      int(3)
      ["last_heart_beat_time"]=>
      int(1475660272)
      ["last_source_update"]=>
      int(1475648114)
      ["last_sync_update"]=>
      int(1475647685)
      ["last_synced_timestamp"]=>
      int(0)
    }
  }
}
array(3) {
  ["ip_addr"]=>
  string(15) "192.168.100.182"
  ["port"]=>
  int(22122)
  ["sock"]=>
  int(4)
}
array(3) {
  ["ip_addr"]=>
  string(15) "192.168.100.182"
  ["port"]=>
  int(22122)
  ["sock"]=>
  int(5)
}
bool(true)
array(3) {
  ["ip_addr"]=>
  string(15) "192.168.100.182"
  ["port"]=>
  int(22122)
  ["sock"]=>
  int(-1)
}
array(2) {
  [0]=>
  array(4) {
    ["ip_addr"]=>
    string(15) "192.168.100.181"
    ["port"]=>
    int(23000)
    ["sock"]=>
    int(-1)
    ["store_path_index"]=>
    int(0)
  }
  [1]=>
  array(4) {
    ["ip_addr"]=>
    string(15) "192.168.100.182"
    ["port"]=>
    int(23000)
    ["sock"]=>
    int(-1)
    ["store_path_index"]=>
    int(0)
  }
}
array(2) {
  ["group_name"]=>
  string(6) "group1"
  ["filename"]=>
  string(44) "M00/00/00/wKhktVf0yf2AVFhSAAB7UNInADc94328.h"
}
array(5) {
  ["source_id"]=>
  int(0)
  ["create_timestamp"]=>
  int(1475660285)
  ["file_size"]=>
  int(31568)
  ["source_ip_addr"]=>
  string(15) "192.168.100.181"
  ["crc32"]=>
  int(-769195977)
}
file exist: 1
array(2) {
  ["group_name"]=>
  string(6) "group1"
  ["filename"]=>
  string(50) "M00/00/00/wKhktVf0yf2AVFhSAAB7UNInADc94328.part1.h"
}
delete slave file return: 1
delete file return: 1
string(57) "group1/M00/00/00/wKhktVf0yf2AE52YAAB7UNInADc60554.part2.h"
delete file group1/M00/00/00/wKhktVf0yf2AE52YAAB7UNInADc60554.part2.h return: 1
delete file group1/M00/00/00/wKhktVf0yf2AE52YAAB7UNInADc60554.h return: 1
array(2) {
  ["group_name"]=>
  string(6) "group1"
  ["filename"]=>
  string(44) "M00/00/00/wKhktlf0yf2AZjIKAAAAD5Ij_SY680.txt"
}
array(5) {
  ["source_id"]=>
  int(0)
  ["create_timestamp"]=>
  int(1475660285)
  ["file_size"]=>
  int(15)
  ["source_ip_addr"]=>
  string(15) "192.168.100.182"
  ["crc32"]=>
  int(-1843135194)
}
file exist: 1
token=984fd906825dec9b3e7cd560a44a97aa
file content: this is a test.(15)
storage_download_file_to_file result: 1
fastdfs_storage_set_metadata result: 1
array(3) {
  ["color"]=>
  string(0) ""
  ["font"]=>
  string(8) "MS Serif"
  ["size"]=>
  string(2) "32"
}
array(2) {
  ["group_name"]=>
  string(6) "group1"
  ["filename"]=>
  string(50) "M00/00/00/wKhktlf0yf2AZjIKAAAAD5Ij_SY680.part1.txt"
}
delete slave file return: 1
delete file return: 1
file content: thisisatest.(15)
storage_download_file_to_file1 result: 1
fastdfs_storage_set_metadata1 result: 1
array(5) {
  ["color"]=>
  string(6) "yellow"
  ["font"]=>
  string(8) "MS Serif"
  ["height"]=>
  string(3) "768"
  ["size"]=>
  string(10) "1234567890"
  ["width"]=>
  string(4) "1024"
}
string(57) "group1/M00/00/00/wKhktVf0yf6AOjCBAAAAD_4E1BM090.part2.txt"
delete file group1/M00/00/00/wKhktVf0yf6AOjCBAAAAD_4E1BM090.part2.txt return: 1
delete file group1/M00/00/00/wKhktVf0yf6AOjCBAAAAD_4E1BM090.bin return: 1
array(3) {
  ["ip_addr"]=>
  string(15) "192.168.100.182"
  ["port"]=>
  int(23000)
  ["sock"]=>
  int(-1)
}
array(3) {
  ["ip_addr"]=>
  string(15) "192.168.100.182"
  ["port"]=>
  int(23000)
  ["sock"]=>
  int(-1)
}
array(1) {
  [0]=>
  array(3) {
    ["ip_addr"]=>
    string(15) "192.168.100.182"
    ["port"]=>
    int(23000)
    ["sock"]=>
    int(-1)
  }
}
array(3) {
  ["ip_addr"]=>
  string(15) "192.168.100.181"
  ["port"]=>
  int(23000)
  ["sock"]=>
  int(-1)
}
array(3) {
  ["ip_addr"]=>
  string(15) "192.168.100.181"
  ["port"]=>
  int(23000)
  ["sock"]=>
  int(-1)
}
array(1) {
  [0]=>
  array(3) {
    ["ip_addr"]=>
    string(15) "192.168.100.181"
    ["port"]=>
    int(23000)
    ["sock"]=>
    int(-1)
  }
}
fastdfs_tracker_close_all_connections result: 1
tracker_make_all_connections result: 1
array(3) {
  ["ip_addr"]=>
  string(15) "192.168.100.181"
  ["port"]=>
  int(22122)
  ["sock"]=>
  int(3)
}
array(3) {
  ["ip_addr"]=>
  string(15) "192.168.100.181"
  ["port"]=>
  int(22122)
  ["sock"]=>
  int(8)
}
bool(true)
array(2) {
  [0]=>
  array(4) {
    ["ip_addr"]=>
    string(15) "192.168.100.181"
    ["port"]=>
    int(23000)
    ["sock"]=>
    int(-1)
    ["store_path_index"]=>
    int(0)
  }
  [1]=>
  array(4) {
    ["ip_addr"]=>
    string(15) "192.168.100.182"
    ["port"]=>
    int(23000)
    ["sock"]=>
    int(-1)
    ["store_path_index"]=>
    int(0)
  }
}
array(3) {
  ["ip_addr"]=>
  string(15) "192.168.100.181"
  ["port"]=>
  int(23000)
  ["sock"]=>
  int(-1)
}
array(3) {
  ["ip_addr"]=>
  string(15) "192.168.100.181"
  ["port"]=>
  int(23000)
  ["sock"]=>
  int(-1)
}
array(1) {
  [0]=>
  array(3) {
    ["ip_addr"]=>
    string(15) "192.168.100.181"
    ["port"]=>
    int(23000)
    ["sock"]=>
    int(-1)
  }
}
array(2) {
  ["group_name"]=>
  string(6) "group1"
  ["filename"]=>
  string(44) "M00/00/00/wKhktVf0yf6ADI75AAB7UNInADc29189.h"
}
array(5) {
  ["source_id"]=>
  int(0)
  ["create_timestamp"]=>
  int(1475660286)
  ["file_size"]=>
  int(31568)
  ["source_ip_addr"]=>
  string(15) "192.168.100.181"
  ["crc32"]=>
  int(-769195977)
}
file exist: 1
array(2) {
  ["group_name"]=>
  string(6) "group1"
  ["filename"]=>
  string(50) "M00/00/00/wKhktVf0yf6ADI75AAB7UNInADc29189.part1.h"
}
delete slave file return: 1
delete file return: 1
string(57) "group1/M00/00/00/wKhktlf0yf6AGc7zAAB7UNInADc71931.part2.c"
delete file group1/M00/00/00/wKhktlf0yf6AGc7zAAB7UNInADc71931.part2.c return: 1
delete file group1/M00/00/00/wKhktlf0yf6AGc7zAAB7UNInADc71931.c return: 1
array(2) {
  ["group_name"]=>
  string(6) "group1"
  ["filename"]=>
  string(44) "M00/00/00/wKhktVf0yf6AFavJAAAAAAAAAAA794.txt"
}
file content: (0)
storage_download_file_to_file result: 1
storage_set_metadata result: 1
array(2) {
  ["color"]=>
  string(6) "yellow"
  ["size"]=>
  string(2) "32"
}
array(2) {
  ["group_name"]=>
  string(6) "group1"
  ["filename"]=>
  string(50) "M00/00/00/wKhktVf0yf6AFavJAAAAAAAAAAA794.part1.txt"
}
delete slave file return: 1
delete file return: 1
array(5) {
  ["source_id"]=>
  int(0)
  ["create_timestamp"]=>
  int(1475660286)
  ["file_size"]=>
  int(15)
  ["source_ip_addr"]=>
  string(15) "192.168.100.182"
  ["crc32"]=>
  int(-1385915893)
}
file exist: 1
token=1c19dc1cf1702db27bc956dd92827e41
file content: thisisatest.(15)
storage_download_file_to_file1 result: 1
storage_set_metadata1 result: 1
string(57) "group1/M00/00/00/wKhktlf0yf6AKcjvAAAAD61kmgs561.part2.txt"
delete file group1/M00/00/00/wKhktlf0yf6AKcjvAAAAD61kmgs561.part2.txt return: 1
array(3) {
  ["color"]=>
  string(6) "yellow"
  ["font"]=>
  string(4) "Aris"
  ["size"]=>
  string(2) "32"
}
delete file group1/M00/00/00/wKhktlf0yf6AKcjvAAAAD61kmgs561.bin return: 1
bool(true)
tracker_close_all_connections result: 1

二、fastdfs的nginx模块部署和配置

1、下载pcre

cd /opt/tools
wget https://sourceforge.net/projects/pcre/files/pcre/8.37/pcre-8.37.tar.gz/download --no-check-certificate
tar -zxf pcre-8.37.tar.gz

2、下载fastdfs nginx模块

cd /opt/tools/
git clone https://github.com/happyfish100/fastdfs-nginx-module.git 
[root@mylinux3 tools]# tree fastdfs-nginx-module/
fastdfs-nginx-module/
├── HISTORY
├── INSTALL
└── src
    ├── common.c
    ├── common.h
    ├── config
    ├── mod_fastdfs.conf
    └── ngx_http_fastdfs_module.c
1 directory, 7 files

3、编译安装nginx

wget http://nginx.org/download/nginx-1.10.1.tar.gz
tar -zxf nginx-1.10.1.tar.gz 
cd nginx-1.10.1
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-pcre=/opt/tools/pcre-8.37 --add-module=/opt/tools/fastdfs-nginx-module/src/
make && make install 
useradd -s /sbin/nologin -M nginx

4、准备配置文件

cd /opt/tools/fastdfs-nginx-module/src/
cp mod_fastdfs.conf /etc/fdfs/  #拷贝fastdfs nginx模块目录下的mod_fastdfs.conf到/etc/fdfs下
cp /opt/tools/fastdfs-5.05/conf/{anti-steal.jpg,http.conf,mime.types} /etc/fdfs/  #把fastdfs安装源文件中/conf目录下的这三个文件拷贝到/etc/fdfs下
touch /var/log/mod_fastdfs.log  #创建fastdfs nginx模块日志,方便后面排错
chown nginx:nginx /var/log/mod_fastdfs.log

5、修改配置文件

1)修改nginx配置文件

cd /usr/local/nginx/conf/
cp nginx.conf nginx.conf.bak
vi nginx.conf
[root@mylinux3 conf]# diff nginx.conf.bak  nginx.conf
37c37
<         server_name  localhost;
---
>         server_name  192.168.100.181;
41a42,45
>         location /group1/M00 {     #这段内容是新增的,需要放在默认location / 段的前面
>             root /data/fdfs_storage/store/;
>             ngx_fastdfs_module;
>         }

2)修改fastdfs nginx模块的配置文件

cp /etc/fdfs/mod_fastdfs.conf /etc/fdfs/mod_fastdfs.conf.bak
vi /etc/fdfs/mod_fastdfs.conf
[root@mylinux3 conf]# diff /etc/fdfs/mod_fastdfs.conf.bak /etc/fdfs/mod_fastdfs.conf
40c40,41
< tracker_server=tracker:22122
---
> tracker_server=192.168.100.181:22122   #修改tracker的IP地址
> tracker_server=192.168.100.182:22122
53c54
< url_have_group_name = false
---
> url_have_group_name = true    #允许包含组名,这个与nginx的配置对应,因为nginx上的配置没有把路径写全,所以这里要允许包含组名
62c63
< store_path0=/home/yuqing/fastdfs
---
> store_path0=/data/fdfs_storage/store  #修改存储路径为实际的文件路径
78c79
< log_filename=
---
> log_filename=/var/log/mod_fastdfs.log  #指定fastdfs nginx的日志路径

6、启动nginx

[root@mylinux3 conf]# /usr/local/nginx/sbin/nginx -t
ngx_http_fastdfs_set pid=8782
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@mylinux3 conf]# /usr/local/nginx/sbin/nginx 
ngx_http_fastdfs_set pid=8783
[root@mylinux3 conf]# lsof -i :80
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   8784  root    6u  IPv4  44777      0t0  TCP *:http (LISTEN)
nginx   8785 nginx    6u  IPv4  44777      0t0  TCP *:http (LISTEN)
[root@mylinux3 conf]# netstat -lnt|grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      
[root@mylinux3 conf]# ps -ef|grep nginx
root       8784      1  0 23:07 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      8785   8784  0 23:07 ?        00:00:00 nginx: worker process      
root       8791   1793  0 23:07 pts/0    00:00:00 grep nginx

7、上传文件

[root@mylinux3 tools]# cat /root/a.txt 
Hello,world.
[root@mylinux3 tools]# fdfs_upload_file /etc/fdfs/client.conf /root/a.txt 
group1/M00/00/00/wKhktVf1GFaATxhVAAAADUwC04E708.txt
[root@mylinux3 tools]# fdfs_upload_file /etc/fdfs/client.conf /root/test.gif 
group1/M00/00/00/wKhktlf1GcKALtFFAAAlbU5-NyE685.gif

8、使用浏览器测试

技术分享

技术分享


本文出自 “IT小二郎” 博客,请务必保留此出处http://jerry12356.blog.51cto.com/4308715/1858887

FastDFS的php和nginx模块配置