在服務器根目錄(你也能夠在其餘地方下載)下載如下安裝包:
libfastcommon包下載: wget https://github.com/happyfish100/libfastcommon/archive/V1.0.39.tar.gz -O libfastcommon.tar.gz
fastdfs包下載:wget https://github.com/happyfish100/fastdfs/archive/V5.11.tar.gz -O fastdfs.tar.gz
nginx包下載:wget http://nginx.org/download/nginx-1.12.0.tar.gz -O nginx.tar.gz
fastdfs-nginx模塊包下載:wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz -O fastdfs-nginx-module.tar.gz
### -O 表示重命名下載包
### 除了nginx包,其餘包都在這裏下載:https://github.com/happyfish100
複製代碼
安裝gcc環境
yum -y install gcc-c++
安裝pcre-devel與openssl-devel
yum -y install pcre-devel openssl openssl-devel
這個命令是解決安裝nginx時出現的錯誤問題:./configure: error: the HTTP rewrite module requires the PCRE library.html
1.解壓libfastcommon包: tar -zxvf libfastcommon.tar.gz
2.進入解壓目錄: cd libfastcommon-1.0.39
3.安裝: ./make.sh
./make.sh install
複製代碼
1.解壓fastDFS包: tar -zxvf fastdfs.tar.gz
2.進入解壓目錄: cd fastdfs-5.11
3.安裝:./make.sh
./make.sh install
若是安裝成功,在/etc/fdfs目錄下會有四個文件:
-rw-r--r-- 1 root root 1461 Nov 13 09:54 client.conf.sample
-rw-r--r-- 1 root root 7927 Nov 13 09:54 storage.conf.sample
-rw-r--r-- 1 root root 105 Nov 13 09:54 storage_ids.conf.sample
-rw-r--r-- 1 root root 7389 Nov 13 09:54 tracker.conf.sample
進入目錄 cd /etc/fdfs,而後將這三個文件複製並去掉.sample:
cp client.conf.sample client.conf
cp storage.conf.sample storage.conf
cp tracker.conf.sample tracker.conf
複製代碼
1.建立保存tracker的data和log的目錄:mkdir -p /usr/local/fast-dfs/fastdfs_tracker
2.進入目錄 cd /etc/fdfs
3.修改tracker.conf文件:vim tracker.conf
修改以下:
disabled=false #默認開啓
port=22122 #默認端口號
base_path=/usr/local/fast-dfs/fastdfs_tracker #剛剛建立的目錄
http.server_port=6666 #默認端口是8080
修改完後,
啓動tracker: /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
查看tracker是否有端口:netstat -ntpul |grep fdfs (若是沒有出現端口,說明tracker啓動失敗)
其它命令:
重啓tracker: /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
中止tracker: /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop
添加開機啓動:
chmod +x /etc/rc.d/rc.local
echo '/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart' >> /etc/rc.d/rc.local
複製代碼
1.建立保存storage的data和log的目錄:mkdir /usr/local/fast-dfs/fastdfs_storage
再建立一個文件存儲目錄:mkdir /usr/local/fast-dfs/fastdfs_storage_data
2.進入目錄:cd /etc/fdfs
3.修改storage.conf文件:vim storage.conf
修改以下:
disabled=false #默認false
group_name=group1 #組名,根據實際狀況修改
port=23000 #設置storage的端口號,默認是23000,同一個組的storage端口號必須一致
base_path=/usr/local/fast-dfs/fastdfs_storage #設置storage數據文件和日誌目錄
store_path_count=1 #存儲路徑個數,須要和store_path個數匹配
store_path0=/usr/local/fast-dfs/fastdfs_storage_data #實際文件存儲路徑
tracker_server=服務器公網ip地址:22122
http.server_port=8888 #設置 http 端口號
修改保存後建立軟引用
ln -s /usr/bin/fdfs_storaged /usr/local/bin
啓動storage: /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
查看storage是否有端口:netstat -ntpul |grep fdfs (若是沒有出現端口,說明storage啓動失敗)
其它命令:
重啓storage: /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
中止storage: /usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop
添加開機啓動:
echo '/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart' >> /etc/rc.d/rc.local
複製代碼
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
以下圖表示成功註冊了 java
1.進入目錄:cd /etc/fdfs
2.修改client.conf: vim client.conf
修改以下:
base_path=/usr/local/fast-dfs/fastdfs_tracker #tracker服務器文件路徑
tracker_server=服務器公網ip地址:22122 #tracker服務器IP地址和端口號
http.tracker_server_port=6666 # tracker 服務器的 http端口號,必須和tracker的設置對應起來!
複製代碼
1.進入根目錄(下載包存放的目錄):cd ~
2.解壓fastdfs的Nginx模塊包:tar -zvxf fastdfs-nginx-module.tar.gz
3.重命名模塊目錄:mv fastdfs-nginx-module-1.12.0 fastdfs-nginx-module
4.修改模塊配置文件:vim /root/fastdfs-nginx-module/src/config
修改以下:
ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/"
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
CORE_LIBS="$CORE_LIBS -L/usr/lib -lfastcommon -lfdfsclient"
保存後,創建軟連接:ln -s /usr/include/fast* /usr/local/include/
5.1解壓nginx包: tar -zxvf nginx.tar.gz
5.2進入nginx解壓目錄並添加fastdfs的nginx模塊:
cd nginx-1.12.0
./configure --prefix=/usr/local/storage-nginx --add-module=/root/fastdfs-nginx-module/src
若是沒出錯,就依次執行:
make
make install
複製代碼
上面的nginx安裝完後,做爲storage的nginx,進行配置
1.修改nginx的配置文件:vim /root/nginx-1.12.0/conf/nginx.conf
修改監聽端口 listen 9999, 新增location(因爲nginx編碼嚴格,不要複製,手動輸入,不然會出現nginx: [emerg] unknown directive " " in /usr/local/nginx-1.12.0-storage/conf/nginx.conf:49):
server {
listen 9999;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location ~/group1/M00 {
root /usr/local/fast-dfs/fastdfs_storage_data/data;
ngx_fastdfs_module;
}
}
而後進入FastDFS安裝時的解壓過的目錄,將http.conf和mime.types拷貝到/etc/fdfs目錄下:
cd /root/fastdfs-5.11/conf
cp http.conf /etc/fdfs/
cp mime.types /etc/fdfs/
還須要把fastdfs-nginx-module安裝目錄中src目錄下的mod_fastdfs.conf也拷貝到/etc/fdfs目錄下:
cp /root/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
再對剛複製的mod_fastdfs配置:
vim /etc/fdfs/mod_fastdfs.conf
以下:
base_path=/usr/local/fast-dfs/fastdfs_storage #保存日誌目錄
tracker_server=服務器公網ip地址:22122 #tracker服務器的IP地址以及端口號
storage_server_port=23000 #storage服務器的端口號
url_have_group_name = true #文件 url 中是否有 group 名
store_path0=/usr/local/fast-dfs/fastdfs_storage_data #存儲路徑
group_count = 3 #設置組的個數,事實上此次只使用了group1
在文件的最後,設置group
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/usr/local/fast-dfs/fastdfs_storage_data
[group2]
group_name=group2
storage_server_port=23000
store_path_count=1
store_path0=/usr/local/fast-dfs/fastdfs_storage_data
[group3]
group_name=group3
storage_server_port=23000
store_path_count=1
store_path0=/usr/local/fast-dfs/fastdfs_storage_data
保存後,建立M00至storage存儲目錄的符號鏈接:
ln -s /usr/local/fast-dfs/fastdfs_storage_data/data/ /usr/local/fast-dfs/fastdfs_storage_data/data/M00
啓動nginx:
/usr/local/storage-nginx/sbin/nginx -c /root/nginx-1.12.0/conf/nginx.conf
查看端口是否啓動:
netstat -ntpul |grep nginx #出現9999端口的nignx説明啓動成功
複製代碼
1.再解壓一個nginx,而後進入解壓目錄添加fastdfs-nginx模塊:
./configure --prefix=/usr/local/tracker-nginx --add-module=/root/fastdfs-nginx-module/src
沒出現錯誤,就依次執行:
make
make install
而後修改nginx解壓目錄下的/conf/nginx.conf:
upstream fdfs_group1 {
server 127.0.0.1:9999;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /group1/M00 {
proxy_pass http://fdfs_group1;
}
#location / {
# root html;
# index index.html index.htm;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
保存後,啓動tracker的nginx:
/usr/local/tracker-nginx/sbin/nginx -c /root/tracker-nginx/nginx-1.12.0/conf/nginx.conf
查看端口是否有80和9999:
netstat -ntpul |grep nginx
至此FastDFS服務部署完成。
下面配置下防火牆:
1.開啓全部配置的端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent #開啓80端口號
firewall-cmd --zone=public --add-port=23000/tcp --permanent #開啓23000端口號
firewall-cmd --zone=public --add-port=22122/tcp --permanent #開啓22122端口號
firewall-cmd --zone=public --add-port=9999/tcp --permanent #開啓9999端口號
firewall-cmd --zone=public --add-port=6666/tcp --permanent #開啓6666端口號
firewall-cmd --zone=public --add-port=8888/tcp --permanent #開啓8888端口號
2.重啓防火牆
CentOS7 防火牆相關命令:
firewall-cmd --zone=public --list-ports #查看開啓的端口
firewall-cmd state #查看防火牆是否開啓
systemctl enable firewalld.service #開啓防火牆
systemctl stop firewalld.service #關閉防火牆(開機會仍會啓動)
systemctl disable firewalld.service #禁用防火牆(開機後再也不啓動)
複製代碼
在idea上測試,操做以下:
1.拉取fastdfs的java客戶端包:File-->New-->project from version control-->github
2.填上Url:https://github.com/happyfish100/fastdfs-client-java.git ,項目所在目錄、名稱,點擊clone
3.等項目初次化完後,設置下你的maven的安裝目錄:settings-->搜索maven-->改變maven home directory的目錄
4.打開右邊Maven Project窗口,進行安裝fastdfs的java客戶端:lifecycle-->install
5.至此你的fastdfs的maven依賴安裝完成,在你的測試項目的pom.xml文件加入依賴就能夠測試了
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
例子:
在resources下建立fdfs_client.conf配置文件,添加內容:tracker_server=服務器公網ip地址:22122
建立一個demo.java:
public class Demo {
public static void main(String[] args) throws Exception{
//加載配置文件,配置文件中的內容就是 tracker 服務的地址
ClientGlobal.init("D:/myWork/fasDFS-demo/src/main/resources/fdfs_client.conf");
//建立一個 TrackerClient 對象。直接 new 一個
TrackerClient trackerClient=new TrackerClient();
//使用 TrackerClient 對象建立鏈接,得到一個 TrackerServer 對象。
TrackerServer trackerServer=trackerClient.getConnection();
//建立一個 StorageServer 的引用,值爲 null
StorageServer storageServer=null;
//建立一個 StorageClient 對象,須要兩個參數 TrackerServer 對象、StorageServer 的引用
StorageClient storageClient=new StorageClient(trackerServer,storageServer);
//使用 StorageClient 對象上傳圖片。
//擴展名不帶「.」
String[] strings=storageClient.upload_file("C:/Users/Public/Pictures/Sample Pictures/test.jpg","jpg",null);
//遍歷數組打印包含組名和圖片的路徑。
for (String string:strings){
System.out.println(string);
}
}
}
最後在瀏覽器上訪問你上傳的圖片:
http://服務器公網ip地址/group1/M00/00/00/rBsABFvqfIyACR3WAAl5WIkOlj8480.jpg
複製代碼
blog.csdn.net/weixin_4037… cloud.tencent.com/developer/a…nginx