软件下载nginx
wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gzgit
wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gzgithub
wget http://zlib.net/zlib-1.2.8.tar.gzvim
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz浏览器
wget http://nginx.org/download/nginx-1.8.0.tar.gz服务器
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gzapp
将下载的软件拷贝到 /usr/local/src/负载均衡
cp /root/Downloads/libfastcommonV1.0.7 /usr/local/src/tcp
cp /root/Downloads/fastdfsV5.05 /usr/local/src/
cp /root/Downloads/zlib-1.2.8.tar.gz /usr/local/src/
cp /root/Downloads/pcre-8.36.tar.gz /usr/local/src/
cp /root/Downloads/ngx_cache_purge-2.3.tar.gz /usr/local/src/
cp /root/Downloads/fastdfs-nginx-module_v1.16.tar.gz /usr/local/src/
cp /root/Downloads/nginx-1.8.0.tar.gz /usr/local/src/
安装FastDFS
首先安装libfastcommon ,由于FastDFS依赖libfastcommon包
tar -zxvf libfastcommonV1.0.7
cd libfastcommon-1.0.7/
./make.sh
./make.sh install
因为libfastcommon.so默认安装到了/usr/lib64/libfastcommon.so,而FastDFS主程序设置的lib目录是/usr/lib,因此设置软链接
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
libfastcommon安装完成后,安装FastDFS
tar -zxvf fastdfsV5.05
cd fastdfs-5.05/
./make.sh
./make.sh install
FastDFS安装完成后,全部可执行文件在/usr/bin/,全部配置文件在/etc/fdfs/。
其中配置文件各拷贝一份:
cp tracker.conf.sample tracker.conf
cp storage.conf.sample storage.conf
cp client.conf.sample client.conf
若是须要启动tracker服务则修改tracker.conf
若是须要启动 storage服务则修改storage.conf
若是须要使用client 服务则修改 client.conf
配置tracker.conf 并启动 tracker服务
建立文件夹用于存储tracker 的日志和数据
mkdir -p /fdfs/tracker
编辑配置文件目录下的/etc/fdfs/tracker.conf,设置相关信息并保存。
vi tracker.conf
通常只需改动如下几个参数便可:
disabled=false #启用配置文件
port=22122 #设置tracker的端口号
base_path=/fdfs/tracker #设置tracker的数据文件和日志目录(需预先建立)
http.server_port=8080 #设置http端口号
如须要进行性能调优,能够仔细阅读配置文件的详细说明
启动tracker服务
运行tracker以前,先要把防火墙中对应的tracker端口打开,默认端口为22122
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 22122 -j ACCEPT
保存防火墙配置
/etc/init.d/iptables save
启动tracker服务
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
检查fdfs_trackerd服务是否启动正常,查看fdfs是否在监听22122端口
netstat -unltp | grep fdfs
查看tracker日志中是否有错误信息
cat /fdfs/tracker/logs/trackerd.log | more
设置开机启动,将启动命令追加到启动文件中
vi /etc/rc.d/rc.local ---add--> /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
配置storageconf 并启动 storage服务
建立文件夹用于存储storage 的日志和数据
mkdir -p /fdfs/storage
编辑配置文件目录下的storage.conf,设置相关信息并保存
通常只需改动如下几个参数便可:
disabled=false #启用配置文件
group_name=group1 #组名,根据实际状况修改
port=23000 #设置storage的端口号
base_path=/fdfs/storage #设置storage的日志目录(需预先建立)
store_path_count=1 #存储路径个数,须要和store_path个数匹配
store_path0=/fdfs/storage #存储路径
tracker_server=10.64.2.171:22122
tracker_server=10.64.2.172:22122
#tracker服务器的IP地址和端口号,可配置多个tracker
http.server_port=8080 #设置http端口号
如须要进行性能调优,能够仔细阅读配置文件的详细说明
启动storage服务
运行storage以前,先要把防火墙中对应的storage端口打开,默认端口为23000
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 23000 -j ACCEPT
保存防火墙配置
/etc/init.d/iptables save
启动storage服务
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
启动storage,会根据配置文件的设置自动建立多级存储目录,检查storage服务是否启动正常,查看fdfs_storaged是否在监听23000端口
netstat -unltp | grep fdfs
查看storage日志中是否有错误信息
cat /fdfs/storage/logs/storaged.log | more
确认启动成功后,能够运行fdfs_monitor查看storage服务器是否已经登记到tracker服务器
看到“172.16.1.203 ACTIVE”便可确认storage运行正常
设置开机启动,将启动命令追加到启动文件中
vi /etc/rc.d/rc.local ---add--> /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
附加说明:中止storage 执行/usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop
测试FastDFS安装是否成功
首先配置/etc/fdfs/client.conf主要修改一下几个参数
base_path=/fdfs/tracker
tracker_server=10.64.2.172:22122
http.tracker_server_port=8080
使用自带的客户端测试是否上传成功
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/Downloads/nginx-1.8.0.tar.gz
若有相应路径返回则说明安装成功例:
group1/M00/00/00/CkACqlYzHvCAZYGgAAyyaB1JZ1k.tar.gz
在storage上安装nginx
在storage上安装的nginx主要为了提供http的访问服务,同时解决group中storage服务器的同步延迟问题
安装前先修改fastdfs-nginx-module中的config配置文件
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
cd fastdfs-nginx-module/src/
vi config
修改以下配置,这里原来是
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
改为
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
这个是很重要的,否则在nginx编译的时候会报错的,我看网上不少在安装nginx的fastdfs的插件报错,都是这个缘由,而不是版本不匹配。
因为nginx 的安装依赖pcre 、zlib 包,因此先安装这两个包
pcre安装
tar -zxvf pcre-8.36.tar.gz
cd pcre-8.36
./configure
make
make install
配置软链接
ln -s /usr/local/lib/libpcre.so.1 /lib64/
zlib安装:
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install
nginx安装:
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/fastdfs-nginx-module/src
make
make install
将FastDFS的nginx插件模块的配置文件copy到FastDFS配置文件目录
拷贝配置文件
cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
cp /usr/local/src/fastdfs-5.05/conf/http.conf /etc/fdfs/
cp /usr/local/src/fastdfs-5.05/conf/mime.types /etc/fdfs/
安装完成后,nginx全部文件在/usr/local/nginx下
配置
编辑/etc/fdfs配置文件目录下的mod_fastdfs.conf,设置storage信息并保存
通常只需改动如下几个参数便可:
base_path=/fdfs/storage #保存日志目录
tracker_server=172.16.1.202:22122 #tracker服务器的IP地址以及端口号,多个tracker可写多行
storage_server_port=23000 #storage服务器的端口号
group_name=group1 #当前服务器的group名
url_have_group_name= true #文件url中是否有group名
store_path_count=1 #存储路径个数,须要和store_path个数匹配
store_path0=/fdfs/storage #存储路径
http.need_find_content_type=true #从文件扩展名查找文件类型(nginx时为true)
group_count= 2 #设置组的个数
在末尾增长2个组的具体信息:
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/fdfs/storage
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/fdfs/storage
编辑/usr/local/nginx/conf配置文件目录下的nginx.conf,设置添加storage信息并保存。
将server段中的listen端口号改成8080:
listen 8080;
在server段中添加:
location ~/group[1-2]/M00{
root /fdfs/storage/data;
ngx_fastdfs_module;
}
创建M00至存储目录的符号链接
ln -s /fdfs/storage/data /fdfs/storage/data/M00
至此,nginx以及FastDFS插件模块设置完成
启动nginx
运行nginx以前,先要把防火墙中对应的端口打开(本例中为8080)
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
/etc/init.d/iptables save
启动nginx,确认启动是否成功。(查看是否对应端口8080是否开始监听)
/usr/local/nginx/sbin/nginx
netstat -unltp | grep nginx
也可查看nginx的日志是否启动成功或是否有错误
cat /usr/local/nginx/logs/error.log
在error.log中没有错误,既启动成功。能够打开浏览器,直接访问http://10.64.2.167:8080,查看是否弹出nginx欢迎页面
设置开机自动启动。
将运行命令行添加进文件:/usr/local/nginx/sbin/nginx
在tracker上安装nginx
在tracker上安装的nginx主要为了提供http访问的反向代理、负载均衡以及缓存服务
因为nginx 的安装依赖pcre 、zlib 包,因此先安装这两个包
pcre安装
tar -zxvf pcre-8.36.tar.gz
cd pcre-8.36
./configure
make
make install
配置软链接
ln -s /usr/local/lib/libpcre.so.1 /lib64/
zlib安装:
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install
nginx安装
./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/ngx_cache_purge-2.3
make
make install
配置
编辑/usr/local/nginx/conf配置文件目录下的nginx.conf,设置负载均衡以及缓存
vim /usr/local/nginx/conf/nginx.conf
worker_processes 4; #根据CPU核心数而定
events {
worker_connections 65535; #最大连接数
use epoll; #新版本的Linux可以使用epoll加快处理性能
}
http { 中
#设置缓存参数
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
#设置缓存存储路径、存储方式、分配内存大小、磁盘最大空间、缓存期限
proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=http-cache:500m max_size=10g inactive=30d;
proxy_temp_path /var/cache/nginx/proxy_cache/tmp;
#设置group1的服务器
upstream fdfs_group1 {
server 10.64.2.170:8080 weight=1 max_fails=2 fail_timeout=30s;
server 10.64.2.169:8080 weight=1 max_fails=2 fail_timeout=30s;
}
#设置group2的服务器
upstream fdfs_group2 {
server 10.64.2.168:8080 weight=1 max_fails=2 fail_timeout=30s;
server 10.64.2.167:8080 weight=1 max_fails=2 fail_timeout=30s;
}
server { 中
#设置服务器端口
listen 8080;
#设置group1的负载均衡参数
location /group1/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_group1;
expires 30d;
}
#设置group2的负载均衡参数
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;
}
#设置清除缓存的访问权限
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 172.16.1.0/24;
deny all;
proxy_cache_purge http-cache $1$is_args$args;
}
至此,nginx以及nginx cache purge插件模块设置完成
启动nginx
运行nginx以前,先要把防火墙中对应的端口打开(本例中为8080)
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
/etc/init.d/iptables save
建立缓存文件夹
mkdir -p /var/cache/nginx/proxy_cache
启动nginx,确认启动是否成功。(查看是否对应端口8080是否开始监听)
/usr/local/nginx/sbin/nginx
netstat -unltp | grep nginx
也可查看nginx的日志是否启动成功或是否有错误
cat /usr/local/nginx/logs/error.log
在error.log中没有错误,既启动成功。能够打开浏览器,直接访问http://10.64.2.171:8080,查看是否弹出nginx欢迎页面
设置开机自动启动。
将运行命令行添加进文件:/usr/local/nginx/sbin/nginx
总体测试
尝试上传一个文件到FastDFS,而后访问试试。先配置client.conf文件
vim /etc/fdfs/client.conf
修改如下参数:
base_path=/fdfs/tracker #日志存放路径
tracker_server=10.64.2.171:22122 #tracker服务器IP地址和端口号
http.tracker_server_port=8080 #tracker服务器的http端口号
使用/usr/local/bin/fdfs_upload_file上传一个文件,程序会自动返回文件的URL
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf /root/Downloads/dt.jpg
返回:group2/M00/00/00/CkACqFY2zTGAQFuvAAiQfKHDaaQ932.jpg
而后使用浏览器访问, 成功则能够看到文件被正确读取出来了
查看nginx的access.log日志,能够看到访问返回200成功
cat /usr/local/nginx/logs/access.log
查看nginx的cache目录,能够看到已经生成了缓存文件
ll /var/cache/nginx/proxy_cache/ -R
若是要手动清除缓存,能够在文件URL以前加上purge:
至此,tracker服务器上的http反向代理+负载均衡+缓存已经安装完成
附加说明
1. 上传文件
FastDFS提供了多种方式上传文件:
使用fdfs_upload_file上传(以前已经有演示)
使用C语言客户端接口上传
使用PHP客户端接口上传
使用Java客户端接口上传
使用Python客户端接口上传
使用.NET客户端接口上传
根据具体状况选择使用。上传均支持断点续传
2. 下载文件
使用fdfs_download_file经过tracker服务器下载
使用nginx经过http方式直接从storage下载(支持断点续传,做者推荐)
3. 监视服务器资源
使用fdfs_monitor查看tracker和全部group的运行状况。
/usr/bin/fdfs_monitor /etc/fdfs/client.conf
4. 其余功能
使用fdfs_crc32获取文件CRC
使用fdfs_delete_file删除文件
使用fdfs_file_info查看文件属性信息
以上全部功能都可使用接口在代码中调用