搭建環境準備:
Traker: 192.168.124.84
Storage: 192.168.124.155
nginx(及其依賴包)+fastDFS(及其依賴包)
# 在Traker上上傳一系列包在/usr/local/src下
# 在Storage上上傳一系列包到/usr/local/src下
一、在Tracker上配置
關閉防火牆
systemctl stop firewalld
setenforce 0
1.安裝libfastcommon
# 進到/usr/local/src下
cd /usr/local/src
# 解壓
tar zxvf libfastcommon-1.0.35.tar.gz -C /data/server
# 進到目標文件目錄下
cd /data/server/libfastcommon-1.0.35/
# 編譯安裝
* ./make.sh
這裏會顯示報錯
./make.sh: line 14: gcc: command not found
./make.sh: line 15: ./a.out: No such file or directory
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o hash.o hash.c
make: cc: Command not found
make: *** [hash.o] Error 127
解決方法:
yum install -y gcc*(編譯時需要gcc環境)
* ./make.sh
* ./make.sh install
# 添加環境變量
vim /etc/profile
在末行添加
export LD_LIBRARY_PATH=/usr/lib64/
# 使環境變量生效
source /etc/profile
# 創建軟鏈接
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
2.安裝libevent
# 進到/usr/local/src目錄下
cd /usr/local/src
# 解壓
tar zxvf libevent-2.0.20-stable.tar.gz -C /data/server
# 進到目標文件目錄
cd /data/server/libevent-2.0.20-stable
# 編譯安裝
* ./configure --prefix=/data/server
* make && make install
#創建軟鏈接
ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
3.安裝tracker
#創建目錄
mkdir -p /fdfs/tracker
# 進到 /usr/local/src目錄下
cd /usr/local/src
# 解壓
tar zxvf FastDFS_v5.08.tar.gz -C /data/server
# 進到目標文件目錄
cd /data/server/FastDFS
# 編譯安裝
* ./make.sh
* ./make.sh install
4.配置文件
vim /data/server/FastDFS/conf/tracker.conf
修改如下內容
base_path=/fdfs/tracker #設置tracker的數據文件和日誌目錄
max_connections=300 #最大連接數
work_threads=1 #工作線程數,最好和cpu核數相同
store_lookup=0 #0爲輪詢模式
http.server_port=8080 #設置http端口號
5.啓動tracker
cd /data/server/FastDFS/tracker
fdfs_trackerd /data/server/FastDFS/conf/tracker.conf
6.查看是否啓動成功
cat /fdfs/tracker/logs/trackerd.log
++++++++++++++++++++++++++++++++++++++++++++
以下內容在在操作完Storage的第1步到第18步之後再操作
7.創建緩存目錄
mkdir -p /var/cache/nginx/proxy_cache
8.安裝nginx及其插件
# 回到/usr/local/src下
cd /usr/local/src
# 解壓這四個包
tar -zxvf nginx-1.11.5.tar.gz -C /data/server
tar zxvf fastdfs-nginx-module_v1.16.tar.gz -C /data/server
tar zxvf pcre-8.34.tar.gz -C /data/server
tar zxvf zlib-1.2.8.tar.gz -C /data/server
# 進到nginx目標文件目錄
cd /data/server/nginx-1.11.5/
# 編譯安裝
* ./configure --prefix=/data/server/nginx --add-module=/data/server/fastdfs-nginx-module/src/ --with-pcre=/data/server/pcre-8.34/ --with-zlib=/data/server/zlib-1.2.8
* make && make install
9.配置nginx文件
vim /data/server/nginx/conf/nginx.conf
添加補充下面內容
http {
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
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 /data/server/nginx/logs/access.log main; #寫自己的日誌路徑
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip 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;
#設置head的服務器
upstream fdfs_head {
server 192.168.124.155:8080 fail_timeout=30s; # storage的ip
}
#設置other的服務器
upstream fdfs_other {
server 192.168.124.155:8080 fail_timeout=30s; # storage的ip
}
server {
listen 8888;
server_name localhost;
#設置head的負載均衡參數
location /head/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_head;
expires 30d;
}
#設置other的負載均衡參數
location /other/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_other;
expires 30d;
}
#此location模塊要與server模塊對齊
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 192.168.124.0/24; #改成自己的網段
deny all;
proxy_cache_purge http-cache $1$is_args$args;
}
+++++++++++++++++++++++++++++++++++++++++
10.修改client.conf文件
vim /data/server/FastDFS/conf/client.conf
修改如下內容:
base_path=/fdfs/tracker #日誌存放路徑
tracker_server=192.168.124.84:22122
http.tracker_server_port=8080 #tracker服務器的http端口號
+++++++++++++++++++++++++++++++
去storage上操作第19步
+++++++++++++++++++++++++++++++
11.運行nginx
cd /data/server/nginx/sbin
./nginx
訪問網頁測試一下nginx是否搭好
12.上傳圖片
# 先上傳一張圖片到Moba上
# fdfs_upload_file /data/server/FastDFS/conf/client.conf /root/a.jpg # 路徑寫全
得到返回值
head/M00/00/00/wKh8m11WVYOAQKRwAAGWPojC1gk461.jpg
# 在瀏覽器上訪問
http://192.168.124.155:8080/head/M00/00/00/wKh8m11WVYOAQKRwAAGWPojC1gk461.jpg
# 再上傳一個圖片
# fdfs_upload_file /data/server/FastDFS/conf/client.conf /root/b.jpg # 路徑寫全
得到返回值
other/M00/00/00/wKh8m11WVguAbtNMAAKFVWAT42o693.jpg
# 在瀏覽器上訪問
http://192.168.124.155:8080/other/M00/00/00/wKh8m11WVguAbtNMAAKFVWAT42o693.jpg
# 再上傳的時候就會 一個上傳到head 一個上傳到other
輪詢模式實現
+++++++++++++++++++++++++++++++++++++++++
去storage上操作第30步(檢測數據是否存儲成功)
+++++++++++++++++++++++++++++++++++++++++
二、在Storage上配置
關閉防火牆
systemctl stop firewalld
setenforce 0
1.安裝libfastcommon
# 進到/usr/local/src下
cd /usr/local/src
# 解壓
tar zxvf libfastcommon-1.0.35.tar.gz -C /data/server
# 進到目標文件目錄下
cd /data/server/libfastcommon-1.0.35/
# 編譯安裝
* ./make.sh
這裏會顯示報錯
./make.sh: line 14: gcc: command not found
./make.sh: line 15: ./a.out: No such file or directory
cc -Wall -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -g -O3 -c -o hash.o hash.c
make: cc: Command not found
make: *** [hash.o] Error 127
解決方法:
yum install -y gcc*(編譯時需要gcc環境)
* ./make.sh
* ./make.sh install
# 添加環境變量
vim /etc/profile
在末行添加
export LD_LIBRARY_PATH=/usr/lib64/
# 使環境變量生效
source /etc/profile
# 創建軟鏈接
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
2.安裝libevent
# 進到/usr/local/src目錄下
cd /usr/local/src
# 解壓
tar zxvf libevent-2.0.20-stable.tar.gz -C /data/server
# 進到目標文件目錄
cd /data/server/libevent-2.0.20-stable
# 編譯安裝
* ./configure --prefix=/data/server
* make && make install
#創建軟鏈接
ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
3.安裝storage
# 創建對應存儲目錄
mkdir -p /fdfs/tracker #tracker目錄
mkdir -p /fdfs/head/data #存放頭像文件
mkdir -p /fdfs/other/data #存放其他文件
mkdir -p /fdfs/storage #storage數據節點
# 解壓
tar zxvf FastDFS_v5.08.tar.gz -C /data/server
# 進到目標文件目錄
cd /data/server/FastDFS
# 編譯安裝
* ./make.sh
* ./make.sh install
4.配置文件
vim /data/server/FastDFS/conf/storage.conf
修改以下內容
disabled=false #啓用配置文件
max_connections=300 #最大連接數
work_threads=1 #工作線程數,最好和cpu核數相同
group_name=head #組名,
port=23000 #設置storage的端口號
base_path=/fdfs/storage #設置storage的日誌目錄(事先創建好的那個)
store_path_count=1 #存儲路徑個數,需要和store_path個數匹配
store_path0=/fdfs/head #存儲路徑
tracker_server=192.168.124.84:22122 #tracker服務器的IP地址和端口號
http.server_port=8080 #設置http端口號
5.運行storage
cd /data/server/FastDFS/storage
fdfs_storaged /data/server/FastDFS/conf/storage.conf
6.確認是否運行成功
cat /fdfs/storage/logs/storaged.log
7.查看存儲目錄
cd /fdfs/head/data
ls
8.安裝nginx及其插件
# 回到/usr/local/src下
cd /usr/local/src
# 解壓這四個包
tar -zxvf nginx-1.11.5.tar.gz -C /data/server
tar zxvf fastdfs-nginx-module_v1.16.tar.gz -C /data/server
tar zxvf pcre-8.34.tar.gz -C /data/server
tar zxvf zlib-1.2.8.tar.gz -C /data/server
# 修改相關路徑參數
vim /data/server/fastdfs-nginx-module/src/config
在其中找到第4行,
將這行
CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
改成
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
# 進到nginx目標文件目錄
cd /data/server/nginx-1.11.5/
# 編譯安裝
* ./configure --prefix=/data/server/nginx --add-module=/data/server/fastdfs-nginx-module/src/ --with-pcre=/data/server/pcre-8.34/ --with-zlib=/data/server/zlib-1.2.8
* make && make install
9.配置nginx文件
vim /data/server/nginx/conf/nginx.conf
添加修改
keepalive_timeout 60; #keepalive超時時間
在server段中添加或者修改
listen 8080; #監聽storage的http端口
server_name 192.168.31.129; #storage的IP
location ~/head/M00 { #組head
root /fdfs/head/data;
ngx_fastdfs_module;
}
location ~/other/M00 { #組other
root /fdfs/other/data;
ngx_fastdfs_module;
}
10.創建other組
cd /data/server/FastDFS/conf/
cp storage.conf storage_other.conf
11.配置storage other組
vim /data/server/FastDFS/conf/storage_other.conf
在其中修改
group_name=other
port=23001
store_path0=/fdfs/other
12.啓動other的storage
cd /data/server/FastDFS/storage
fdfs_storaged /data/server/FastDFS/conf/storage_other.conf
13.查看存儲狀況
cd /fdfs/other/data
ls
14.配置相關文件
vim /data/server/fastdfs-nginx-module/src/mod_fastdfs.conf
修改
base_path=/fdfs/storage #保存日誌目錄
tracker_server=192.168.124.84:22122 #tracker的ip
url_have_group_name = true #文件url中是否有group名
group_name=group1 #把這行註釋掉
group_count = 2 #設置組的個數
在末尾增加2個組的具體信息:
[group1]
group_name=head
storage_server_port=23000
store_path_count=1
store_path0=/fdfs/head
[group2]
group_name=other
storage_server_port=23001
store_path_count=1
store_path0=/fdfs/other
15.創建軟鏈接
ln -s /fdfs/head/data /fdfs/head/data/M00
ln -s /fdfs/other/data /fdfs/other/data/M00
16.查看軟鏈接
ll /fdfs/head/data/M00
ll /fdfs/other/data/M00
17.運行nginx
cd /data/server/nginx/sbin
./nginx
18.訪問網頁
在瀏覽器輸入192.168.124.155:8080(storage的ip:8080)
會出現如下頁面
在這裏會出現無法訪問頁面的情況
解決方法:
# 查看錯誤日誌
cat /data/server/nginx/logs/error.log
我們會看到時這個報錯
ERROR - file: shared_func.c, line: 968, file /etc/fdfs/mod_fastdfs.conf not exist
ERROR - file: /data/server/fastdfs-nginx-module/src//common.c, line: 155, load conf file "/etc/fdfs/mod_fastdfs.conf" fail, ret code: 2
# cd /data/server/FastDFS/conf
# cp http.conf /etc/fdfs/
# cp mime.types /etc/fdfs
# cd /data/server/fastdfs-nginx-module/src
# cp mod_fastdfs.conf /etc/fdfs/
# cd /data/server/nginx/sbin
# ./nginx -s reload
# 訪問網頁
++++++++++++++++++++++++++++++++++++++++++++++++
此時,去tracker上操作第7到第9步
++++++++++++++++++++++++++++++++++++++++++++++++
19.修改client.conf文件
vim /data/server/FastDFS/conf/client.conf
修改如下內容:
base_path=/fdfs/storage #日誌存放路徑
tracker_server=192.168.124.84:22122
http.tracker_server_port=8080 #tracker服務器的http端口號
++++++++++++++++++++++++
去tracker上操作第11到第12步
++++++++++++++++++++++++
20.檢測
cd /fdfs/head/data/00/00
ls
cd /fdfs/other/data/00/00
ls