fastdfs(文件服务器)安装教程及php扩展安装
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fastdfs(文件服务器)安装教程及php扩展安装相关的知识,希望对你有一定的参考价值。
在安装fastdfs之前已经安装了lnmp集成包,所以直接安装fastdfs,之后再安装fastdfs_nginx_model(nginx的扩展)
准备工作:下载fastdif安装包到 /home/soft 目录 ,下载地址:http://pan.baidu.com/s/1jH59oO2
1、安装fastdfs
# cd /home/softs/fastdfs
# tar xzf FastDFS_v4.06.tar.gz
# cd FastDFS
# vi make.sh
去掉注释
WITH_LINUX_SERVICE=1
# ./make.sh
# ./make.sh install
2 配置tracker
2.1 创建与配置tracker数据目录
# mkdir -p /home/fastdfs/tracker
# vi /etc/fdfs/tracker.conf
base_path=/home/fastdfs/tracker
2.2 启动tracker
# service fdfs_trackerd start
2.3 查看启动
# netstat -anp | grep 22122
3 配置storage
3.1 创建与配置storage目录
# mkdir -p /home/fastdfs/storage/storage0
# vi /etc/fdfs/storage.conf
需要修改的部分:
http.server_port=80
group_name=group1
base_path=/home/fastdfs
store_path0=/home/fastdfs/storage/storage0
tracker_server=175.22.14.205:22122
3.2 启动storage
# service fdfs_storaged start
3.3 查看启动
# netstat -anp | grep 23000
4 安装fastdfs_nginx_model
4.1、首先下载好fastdfs_nginx_model扩展
4.2 配置mod_fastdfs.conf
# cp /home/softs/fastdfs/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
# vi /etc/fdfs/mod_fastdfs.conf
修改:
base_path=/home/fastdfs
tracker_server=175.22.14.205:22122
group_name=group1
url_have_group_name = true
store_path0=/home/fastdfs/storage/storage0
4.3、更新nginx
cd /home/softs/lnmp1.4
vi lnmp.conf (主要修改加黑的行就可以了)
Download_Mirror=‘https://soft.vpser.net‘
Nginx_Modules_Options=‘--add-module=/home/softs/fastdfs/fastdfs-nginx-module/src‘
php_Modules_Options=‘‘
##mysql/MariaDB database directory##
MySQL_Data_Dir=‘/usr/local/mysql/var‘
MariaDB_Data_Dir=‘/usr/local/mariadb/var‘
##Default website home directory##
Default_Website_Dir=‘/home/wwwroot/default‘
Enable_Nginx_Openssl=‘y‘
~
更新一下nginx
cd /home/softs/lnmp1.4
./upgrade.sh nginx 【运行之后会要求输入一个nginx版本,当前版本是1.12.1】
4.5 配置nginx.conf
# vi /usr/local/nginx/conf/nginx.conf
location /group2/M00 {
root /home/fastdfs/storage/storage0/data;
ngx_fastdfs_module;
}
4.6、启动nginx
lnmp nginx reload
5、 配置client.conf
# vi /etc/fdfs/client.conf
base_path=/home/tmp
tracker_server=175.22.14.205:22122
http.tracker_server_port=80
6、测试
/usr/local/bin/fdfs_test /etc/fdfs/client.conf upload test.txt
上传图片
/usr/local/bin/fdfs_test /etc/fdfs/client.conf upload 111.png
上传mp3
/usr/local/bin/fdfs_test /etc/fdfs/client.conf upload 123.mp3
第二部分,php扩展fastdfs_client安装
1、安装,进入/home/softs/fastdfs/FastDFS/php_client目录
执行:
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
执行完之后文件变化
2、测试
cp ../conf/client.conf /etc/fdfs/
cat fastdfs_client.ini >> /usr/local/php/etc/php.ini
cp fastdfs_test.php /home/wwwroot/default/ (测试php扩展是否安装成功)
至此php扩展fastdfs_client安装完毕
可以尽情的玩耍了.....
php测试fastdfs上传 附带php上传代码
表单提交html
<html>
<body>
<form action="upload.php" method="post" target="_blank" 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>
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‘], $file[‘type‘], $file[‘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] : ‘‘;
}
/* }}} */
//上传文件到fastdfs
function 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();
if (!empty($ret[‘fileId‘])) {
$host_file = ‘http://175.22.14.205/‘ . $ret[‘fileId‘];
header(‘location:‘ . $host_file);
}
print_r($ret);
}
start();
?>
fastdfs文件服务器集群配置(简版配置)
文件服务器:
主:175.22.14.205 【group1】
从:175.22.14.208 【group2】
从:175.22.14.211 【group3】
主服务器nginx配置
nginx.cof文件中http区域中加入如下代码
upstream fdfs_group2 {
server 175.22.14.208;
}
server配置项增加:
location /group2/M00 {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
#proxy_cache http-cache;
#proxy_cache_valid 200 304 12h;
#proxy_cache_key $uri$is_args$args;
proxy_pass http://fdfs_group2;
expires 30d;
}
从服务器配置,增加一个server配置即可
测试
以上是关于fastdfs(文件服务器)安装教程及php扩展安装的主要内容,如果未能解决你的问题,请参考以下文章