你的位置:首页 > Java教程

[Java教程]在虚拟机中配置FastDFS+Nginx模块


先上部署图

提示一下, ip 192.168.72.138 上面部署了两个group, 分别为 group1和g2. 另外, 同组之内的 port 要保持一致.


一.安装准备
1. #每台机器都添加两个用户 fastdfs 和 nginx
useradd fastdfs -M -s /sbin/nologin & useradd nginx -M -s /sbin/nologin
2. #为了方便测试 请关闭防火墙
service iptables stop && chkconfig iptables off
3.本次采用的架构
IP 安装组件 添加用户
tracker 192.168.72.140 libfastcommon fastdfs Nginx(用作反向代理) useradd fastdfs -M -s /sbin/nologin & useradd nginx -M -s /sbin/nologin
storage 192.168.72.138 libfastcommon fastdfs Nginx(用作接受请求) fastdfs-nginx-module useradd fastdfs -M -s /sbin/nologin & useradd nginx -M -s /sbin/nologin
192.168.72.139 libfastcommon fastdfs Nginx(用作接受请求) fastdfs-nginx-module useradd fastdfs -M -s /sbin/nologin & useradd nginx -M -s /sbin/nologin

 

二. 使用余庆的安装包, 进行 libfastcommon 和 FastDFS 安装
1. cd /usr/local/src/
2. unzip libfastcommon-master.zip
3. cd libfastcommon-master
4. ./make.sh
5. ./make.sh install
6. cd ..
7. tar xzf FastDFS_v5.05.tar.gz
8. cd FastDFS
9. ./make.sh
10. ./make.sh install

11.相关文件的下载链接: 

  nginx-1.7.8.tar.gz

  libfastcommon-master.zip

  fastdfs-nginx-module_v1.16.tar.gz

  FastDFS_v5.05.tar.gz

三. 安装 Nginx
1.在 tracker 上安装 Nginx
#安装 Nginx 依赖包, 可以先配置Nginx,如果配置不成功再安装依赖包
1. yum install –y openssl-devel pcre-devel zlib-devel
2. cd /usr/local/src/
3. tar xf nginx-1.7.8.tar.gz
4. cd nginx-1.7.8
5. ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx #配置Nginx
6. make && make install

2.在 storage 上安装 Nginx
#安装 Nginx 依赖包, 可以先配置Nginx,如果配置不成功再安装依赖包
1. yum install –y openssl-devel pcre-devel zlib-devel
2. cd /usr/local/src/
3. tar xzf fastdfs-nginx-module_v1.16.tar.gz
4. #默认prefix安装路径为/usr(可以从FastDFS下的make.sh中找到),需要删除多余的/local路径
#需要修改 fastdfs-nginx-module/src/config ,
#把 CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/" 改为 CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"

5. tar xzf nginx-1.7.8.tar.gz
6. cd nginx-1.7.8
7. ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --add-module=../fastdfs-nginx-module/src #配置Nginx
8. make
9. 如果第7步不成功, 就执行
yum groupinstall"Development Tools"
ldd $(which /usr/local/nginx/sbin/nginx)
10. make install
11. #修改 Nginx 配置文件,
vi /usr/local/nginx/conf/nginx.conf
添加:
location /group1/M00 {
alias /home/data/fdfs/data;
ngx_fastdfs_module;
}
12. 使用linx命令把 FastDFS/conf 中的 anti-steal.jpg/http.conf/mie.types/storage-ids.conf 复制到 /etc/fdfs/
使用linx命令把 fastdfs-nginx-module/src/mod_fastdfs.conf 复制到 /etc/fdfs/

13. 启动 Nginx
cd /usr/local/nginx/sbin
~./nginx

四. 配置 tracker 和 storage
1. 配置 tracker (192.168.72.140)
mkdir /home/data/fdfs -p
#复制一个配置文件
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
#修改 base_path=/mnt/fastdfs_tracker
vim /etc/fdfs/tracker.conf

使用linx命令把 FastDFS/conf 中的 anti-steal.jpg/http.conf/mie.types/storage-ids.conf 复制到 /etc/fdfs/

#启动 tracker
fdfs_trackerd /etc/fdfs/tracker.conf
#错误日志保存在:
/mnt/fastdfs_tracker/logs/tracker.log

配置 Nginx, 使用 tracker 上的 nginx 作反向代理
user nginx nginx; //此处为已经建立好的用户 和分组
worker_processes 2;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 1024;
events {
use epoll; // epoll是Linux内核为处理大批量文件描述符而作了改进的poll
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /usr/local/nginx/logs/access.log main;

upstream server_g1{
server 192.168.72; //这里配置的是storage的IP 可以配多台
server 192.168.80.102:80;
}

server {
listen 80;
server_name localhost;
location /g1 {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://server_g1;
}
}
}

 



2. 配置 storage (192.168.72.138 和 192.168.72.139)
mkdir /home/data/fdfs -p
#复制一个配置文件
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
#修改配置 /etc/fdfs/storage.conf
group_name=group1
port=23000 #端口号, 注意,每个组内的port要使用同一个端口号
base_path=/home/data/fdfs
store_path0=/home/data/fdfs
tracker_server=192.168.72.140:22122

修改 /etc/fdfs/mod_fastdfs.conf
base_path=/home/data/fdfs
tracker_server=192.168.72.140:22122
group_name=group1
url_have_group_name = true
store_path0=/home/data/fdfs
ln -s /home/data/fdfs/data /home/data/fdfs/data/M00


#启动 storage
fdfs_storaged /etc/fdfs/storage.conf
#错误日志保存在:
/mnt/fastdfs_storage_info/logs/storage.log

vim /etc/rc.d/rc.local
添加一行
fdfs_storaged /etc/fdfs/storage.conf

3. 如果同一台服务器上配置添加一个组, 需要在 /etc/fdfs/mod_fastdfs.conf 增加一个 group 节
如:
[group2]
group_name=g2
storage_server_port=23001
store_path_count=1
store_path0=/home/data/fdfs_g2
4. /etc/fdfs/clinet.conf 里面一般配置 tracker_server=192.168.72.140:22122 和 load_fdfs_parameters_from_tracker=true 就可以了

5. 启动
启动 tracker: fdfs_trackerd /etc/fdfs/tracker.conf
/usr/local/nginx/sbin/nginx
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT && /etc/init.d/iptables save

逐个启动 storage: fdfs_storaged /etc/fdfs/storage
/usr/local/nginx/sbin/nginx
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT && /etc/init.d/iptables save
6.添加启动项:
修改 /etc/rc.d/rc.local,添加内容:
192.168.72.140: fdfs_trackerd /etc/fdfs/tracker.conf
192.168.72.138: fdfs_storaged /etc/fdfs/storage.conf
fdfs_storaged /etc/fdfs/storage_g2.conf
192.168.72.139: fdfs_storaged /etc/fdfs/storage.conf
192.168.72.141: fdfs_storaged /etc/fdfs/storage.conf

#调整防火墙的规则,开放端口:23000
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 23001 -j ACCEPT && /etc/init.d/iptables save

五. 测试
1. 在 tracker(192.168.72.140) 上启动 tracker,nginx
service fdfs_trackerd start
/usr/local/nginx/sbin/nginx

2. 在 storage(192.168.72.138, 139)上面分别启动storage和nginx
service fdfs_storaged start
/usr/local/nginx/sbin/nginx

3. 在tracker上配置一个client
vim /etc/fdfs/client.conf
base_path=/mnt/fastdfs_tracker
tracker_server=192.168.70.140:22122

4. 查看集群详细
fdfs_monitor /etc/fdfs/client.conf

5. 测上传
fdfs_upload_file /etc/fdfs/client.conf aa.jpg

6. 通过浏览器访问
http://192.168.80.101/g1/M00/AC/2F/wKgKDVMppoGAMCFNAAIFvJcyojY165.jpg

http://192.168.80.102/g1/M00/AC/2F/wKgKDVMppoGAMCFNAAIFvJcyojY165.jpg

六. 命令详解
1.Tracker server启动
/usr/local/FastDFS/bin/fdfs_trackerd /etc/fdfs/tracker.conf

2. Storage server启动
/usr/local/FastDFS/bin/fdfs_storaged /etc/fdfs/storage.conf

3. 查看是否启动成功
netstat -ntpl | grep fdfs

4. Storage server 上面启动 Nginx
cd /usr/local/nginx/sbin
~./nginx

5. 不打开直接下载文件:
Nginx.conf
location /
{
if($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
add_headerContent-Disposition: 'attachment;';
}
}

#调整防火墙的规则,开放端口:22122
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT && /etc/init.d/iptables save

#调整防火墙的规则,开放端口:23000
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT && /etc/init.d/iptables save

#启动tracker进程:
/etc/init.d/fdfs_trackerd start

#查看trackerd状态:
/etc/init.d/fdfs_trackerd status

#查看trackerd监听的端口:
netstat -plantu | grep tracker

#启动Storage进程:
/etc/init.d/fdfs_storaged start

#查看Storage进程:
ps -ef | grep storaged | grep -v grep


七. 到此安装结束
IP 配置文件 base_path store_path0 port 启动命令 group_name tracker_server
tracker CentOS_64_1 192.168.72.140 /etc/fdfs/tracker.conf /home/data/fdfs /home/data/fdfs 22122 fdfs_storaged 配置文件
storage CentOS_64_2 192.168.72.138 /etc/fdfs/storage.conf /home/data/fdfs /home/data/fdfs 23000 fdfs_storaged 配置文件 group1 192.168.70.140:22122
CentOS_64_4 192.168.72.139 /etc/fdfs/storage.conf /home/data/fdfs /home/data/fdfs 23000 fdfs_storaged 配置文件 group1 192.168.70.140:22122

CentOS_64_2 192.168.72.138 /etc/fdfs/storage_g2.conf /home/data/fdfs_g2 /home/data/fdfs_g2 23001 fdfs_storaged 配置文件 g2 192.168.70.140:22122
CentOS_64_5 192.168.72.141 /etc/fdfs/storage.conf /home/data/fdfs /home/data/fdfs 23001 fdfs_storaged 配置文件 g2 192.168.70.140:22122
Nginx 安装在各自机器的目录:/usr/local/nginx/


八. 其它
将tracker交给service管理并且设置开机启动
echo fdfs_trackerd /etc/fdfs/tracker.conf >> /etc/rc.d/rc.local

去掉 #号注释
grep -v '^#' /etc/fdfs/tracker.conf |grep -v '^$' > a.txt


user nginx nginx; //此处为已经建立好的用户 和分组
worker_processes 2;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 1024;
events {
use epoll; // epoll是Linux内核为处理大批量文件描述符而作了改进的poll
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /usr/local/nginx/logs/access.log main;
upstream server_g1{
server 192.168.80.101:80; //这里配置的是storage的IP 可以配多台
server 192.168.80.102:80;
}

server {
listen 80;
server_name localhost;
location /g1 {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://server_g1;
}
}
}

 

本机复制到远程机器
scp [可选参数] file_source file_target -r 复制目录
scp /usr/java/jdk1.6.0_22/jre/lib/fonts/fallback/* root@192.168.4.29:/usr/java/jdk16/jre/lib/fonts/fallback/
从远程机器复制到本机
scp 本地用户名 @IP 地址 : 文件名 1 远程用户名 @IP 地址 : 文件名 2

 

 

大家如果在配置过程中有疑问,可以问我 qq: wujialongcn@126.com