主要參考了以下連接配接:https://www.yuque.com/docs/share/e84780c7-6322-4cd6-aa22-d0350ac5ad22?#
1. 主機環境與系統:
伺服器:阿裡雲
作業系統:Ubuntu 18.04 64位
本機已經開啟root權限,是以省去了執行su等指令.
2. gcc、g++編譯器安裝
apt-get install gcc
apt-get install g++
apt-get install build-essential
apt-get install libtool
3. Nginx的安裝和配置
3.1 使用Nginx的必備軟體
PCRE庫
PCRE庫源碼包的下載下傳位址:https://sourceforge.net/projects/pcre/files/pcre/8.44/pcre-8.44.tar.gz
編譯和安裝PCRE庫的相關指令:
wget https://sourceforge.net/projects/pcre/files/pcre/8.44/pcre-8.44.tar.gz
tar -zxvf pcre-8.44.tar.gz
cd pcre-8.44/
./configure
make
make install
PCRE的安裝的一點小坑 在Windows10 WSL2 Ubuntu18下:
問題:在已經安裝了PCRE庫,openssl,zlib的環境中無法正常啟動nginx 提示: [email protected]:/usr/local/nginx/sbin# ./nginx ./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory 解決方法: 首先檢視依賴: [email protected]:/usr/local/nginx/sbin# ldd $(which /usr/local/nginx/sbin/nginx) linux-vdso.so.1 (0x00007ffd4e7c5000) libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f12d4bb3000) libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f12d4994000) libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007f12d475c000) libpcre.so.1 => not found libz.so.1 => /usr/local/lib/libz.so.1 (0x00007f12d453e000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f12d414d000) /lib64/ld-linux-x86-64.so.2 (0x00007f12d5099000) 可以看到确實沒有找到libpcre.so.1 可是實際上在安裝完PCRE庫以後,該檔案會被拷貝到/usr/local/lib/libpcre.so.1下 這時候需要修改預設的$LD_LIBRARY_PATH 執行sudo vim /etc/profile 然後在最下面添加這行: 然後執行 source /etc/profile 再次檢視可以發現已經找到了libpcre.so.1 [email protected]:/usr/local/nginx/sbin# source /etc/profile [email protected]:/usr/local/nginx/sbin# ldd $(which /usr/local/nginx/sbin/nginx) linux-vdso.so.1 (0x00007ffd163e2000) libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f406c786000) libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f406c567000) libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007f406c32f000) libpcre.so.1 => /usr/local/lib/libpcre.so.1 (0x00007f406c111000) libz.so.1 => /usr/local/lib/libz.so.1 (0x00007f406bef3000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f406bb02000) /lib64/ld-linux-x86-64.so.2 (0x00007f406cc6c000) |
zlib庫
zlib源碼包下載下傳位址:https://nchc.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz
編譯和安裝zlib庫相關指令:
wget https://nchc.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11/
./configure
make
make install
OpenSSL開發庫
OpenSSL源碼包下載下傳位址:https://www.openssl.org/source/openssl-1.1.1g.tar.gz
編譯和安裝OpenSSL開發庫的相關指令:
wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz
tar -zxvf openssl-1.1.1g.tar.gz
cd openssl-1.1.1g/
./config
make
make install
3.2 Nginx的安裝與啟動
Nginx源碼包下載下傳位址:http://nginx.org/download/nginx-1.16.1.tar.gz
編譯和安裝Nginx相關指令:
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxvf nginx-1.16.1.tar.gz
nginx-1.16.1/
./configure
make
make install
Nginx的啟動與關閉
預設情況下,nginx呗安裝在/usr/local/nginx目錄中:
cd usr/local/nginx
ls
顯示:conf html logs sbin
其中,Nginx的配置檔案放置于conf/nginx.conf, bin檔案是位于sbin目錄下的nginx檔案。
1)預設方式啟動Nginx伺服器
/usr/local/nginx/sbin/nginx
這時候會讀取配置檔案:/usr/local/nginx/conf/nginx.conf
2)檢視nginx程序
ps -ef|grep nginx
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLzczN2EzNxYTM2AjNwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
可以通過浏覽器直接通路此機器的IP,如果出現Welcome to nginx!表明Nginx已經安裝并運作成功:
3)可以指定ngxin的配置檔案并啟動服務
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
4)測試配置資訊
# /usr/local/nginx/sbin/nginx -t
4 FastDFS安裝與配置
版本:
libfastcommon 1.0.50
fastdfs 6.0.7
4.1 安裝 libfastcommon
libfastcommon的git下載下傳位址:https://github.com/happyfish100/libfastcommon
安裝fastdfs之前,需要先安裝libfastcommon
git clone https://github.com/happyfish100/libfastcommon.git
cd libfastcommon
git checkout V1.0.50
./make.sh
./make.sh install
4.2 安裝FastDFS
git clone https://github.com/happyfish100/fastdfs.git
cd fastdfs
git checkout V6.07
./make.sh && ./make.sh install
關于fastdfs的架構參考這篇文章:
https://blog.csdn.net/sesiria/article/details/117388280?spm=1001.2014.3001.5501
4.3 配置Tracker
# 建立 Tracker 的存儲日志和資料的根目錄
mkdir -p /home/fastdfs/tracker
cd /etc/fdfs
cp tracker.conf.sample tracker.conf
# 配置 tracker.conf
vim tracker.conf
在這裡,tracker.conf 隻是修改以下Tracker存儲日志和資料的路徑,主要修改base_path路徑
# 啟用配置檔案(預設為 false,表示啟用配置檔案)
disabled=false
# Tracker 服務端口(預設為 22122)
port=22122
# 存儲日志和資料的根目錄
base_path=/home/fastdfs/tracker
4.4 配置Storage
# 建立 Storage 的存儲日志和資料的根目錄
mkdir -p /home/fastdfs/storage
cd /etc/fdfs
cp storage.conf.sample storage.conf
# 配置 storage.conf
vim storage.conf
這裡storage.conf隻是修改以下storage存儲日志和資料的路徑
# 啟用配置檔案(預設為 false,表示啟用配置檔案)
disabled=false
# Storage 服務端口(預設為 23000)
port=23000
# 資料和日志檔案存儲根目錄
base_path=/home/fastdfs/storage
# 存儲路徑,通路時路徑為 M00
# store_path1 則為 M01,以此遞增到 M99(如果配置了多個存儲目錄的話,這裡隻指定 1 個)
store_path0=/home/fastdfs/storage
# Tracker 伺服器 IP 位址和端口,單機搭建時也不要寫 127.0.0.1
# tracker_server 可以多次出現,如果有多個,則配置多個
tracker_server = 118.31.8.xxx:22122 # 這裡修改成自己的主機IP
# 設定 HTTP 通路檔案的端口。這個配置已經不用配置了,配置了也沒什麼用
# 這也是為何 Storage 伺服器需要 Nginx 來提供 HTTP 通路的原因
http.server_port=8888
主要修改:base_path、store_path0、tracker_server
4.5 啟動Tracker和Storage服務
# 啟動 Tracker 服務
# 其它操作則把 start 改為 stop、restart、reload、status 即可。Storage 服務相同
/etc/init.d/fdfs_trackerd start
# 啟動 Storage 服務
/etc/init.d/fdfs_storaged start
# 可以通過 fdfs_monitor 檢視叢集的情況
# 檢視 Storage 是否已經注冊到 Tracker 伺服器中
# 當檢視到 ip_addr = 120.27.131.197 (localhost.localdomain) ACTIVE
# ACTIVE 表示成功
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
通過以下指令可以檢視storage服務是否已經添加到tracker服務中
4.6 測試上傳檔案
# 修改 Tracker 伺服器用戶端配置檔案
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
vim /etc/fdfs/client.conf
client.conf 中修改 base_path 和 Tracker 伺服器的 IP 位址與端口号即可
# 存儲日志檔案的基本路徑
base_path=/home/fastdfs/tracker
# Tracker 伺服器 IP 位址與端口号
tracker_server = 118.31.8.112:22122
主要修改base_path和tracker_server
測試上傳檔案:
[email protected]:~# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf ./test.txt
group1/M00/00/00/dh8IcGC8jB-ASA6kAAAADc5qlig067.txt
當傳回檔案ID号,比如group1/M00/00/00/xxxxxxx.txt則表示上傳成功
如果報錯:tracker_query_storage fail, error no: 2, error info: No such file or directory,一般都是因為路徑沒有設定對。可以
tail -f /home/fastdfs/storage/logs/storaged.log
檢視日志
4.7 下載下傳檔案測試
檢視檔案路徑
在client所在的機器完成下載下傳,删除測試
下載下傳:
[email protected]:~/test# /usr/bin/fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/dh8IcGC8jB-ASA6kAAAADc5qlig067.txt
[email protected]:~/test# ls
dh8IcGC8jB-ASA6kAAAADc5qlig067.txt
[email protected]:~/test#
删除:
[email protected]:~/test# /usr/bin/fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/dh8IcGC8jB-ASA6kAAAADc5qlig067.txt
[email protected]:~/test# ls /home/fastdfs/storage/data/00/00
[email protected]:~/test#
可以看到目錄為空,檔案已經被删除。
以上則完成了fastdfs的安裝與配置,可以使用api來完成檔案的上傳,同步和下載下傳。
接下來還會安裝Nginx,目的如下:
* Storage安裝Nginx, 為了提供http的通路和下載下傳服務,同時解決group中Storage伺服器的同步延遲問題
* Tracker安裝Nginx, 主要是為了提供http通路的方向代裡、負載均衡以及緩存服務
5 fastdfs-nginx-module安裝
5.1 子產品包的安裝
注意:全部安裝條件在確定之前的FastDFS的tracker、storage和client可以正常使用。
子產品包源碼包本地下載下傳路徑:https://github.com/happyfish100/fastdfs-nginx-module
5.1.1 下載下傳fastdfs-nginx-module
git clone https://github.com/happyfish100/fastdfs-nginx-module.git
cd fastdfs-nginx-module
git checkout V1.22
cd fastdfs-nginx-module/ 會發現裡面有個INSTALL和src目錄,這個不需要make而是需要重新編譯以下storage的Nginx子產品
5.1.2 檢視fastdfs-nginx-module子產品src路徑
[email protected]:~/packages/fastdfs-nginx-module# pwd
/root/packages/fastdfs-nginx-module
5.1.3 安裝和編譯Nginx并添加FastDFS子產品
#進入到nginx源碼目錄
cd nginx-1.16.1/ (本人路徑:/root/packages/nginx-1.16.1)
./configure --add-module=/root/packages/fastdfs-nginx-module/src
其中/root/packages/fastdfs-nginx-module/src是剛才下載下傳的
fastdfs_nginx_module子產品的絕對路徑,就是在編譯Nginx時候,聯通這個子產品一起編譯。
出現以下資訊則表示添加成功:
5.1.4 給Nginx目錄下的objs/Makefile檔案中增加頭檔案目錄
vim objs/Makefile
添加
ALL_INCS = -I src/core \
-I /usr/include/fastdfs \
-I /usr/include/fastcommon \
-I src/event \
-I src/event/modules \
特别要注意加入兩行 後 -I 和 \ 的顔色要和原來一緻,否則報錯:
Makefile:8: recipe for target 'build' failedmake: *** [build] Error 2
5.1.5 重新編譯及安裝nginx
關閉所有nginx服務
接着編譯
make
make install
5.2 fastdfs-nginx-module的配置
5.2.1 fastdfs-nginx-module配置檔案
切換到fastdfs-nginx-module/src路徑
[email protected]:~/packages/fastdfs-nginx-module/src# pwd
/root/packages/fastdfs-nginx-module/src
[email protected]:~/packages/fastdfs-nginx-module/src# ls
common.c common.h config mod_fastdfs.conf ngx_http_fastdfs_module.c
将fastdfs-nginx-module/src/mod_fastdfs.conf 拷貝到/etc/fdfs/下:
cp mod_fastdfs.conf /etc/fdfs/
fastdfs-nginx-module 和 FastDFS 配置檔案修改# 複制 FastDFS 的部配置設定置檔案到 /etc/fdfs
cd /root/0voice/cloud-drive/fastdfs
cp conf/http.conf /etc/fdfs/
cp conf/mime.types /etc/fdfs/
修改vim /etc/fdfs/mod_fastdfs.conf
# Tracker 伺服器IP和端口修改
tracker_server=120.27.131.197:22122
# url 中是否包含 group 名稱,改為 true,包含 group
url_have_group_name = true
# 配置 Storage 資訊,修改 store_path0 的資訊
store_path0=/home/fastdfs/storage
# 其它的一般預設即可,例如
base_path=/tmp
group_name=group1
storage_server_port=23000
store_path_count=1
主要修改tracker_server、url_have_group_name、store_path0。
5.2.2 配置nginx
vim /usr/local/nginx/conf/nginx.conf
配置nginx支援group0-group9, 以及M00-M99,以便後續擴容
location ~/group([0-9])/M([0-9])([0-9]) {
ngx_fastdfs_module;
}
5.2.3 重新開機nginx
配置完fastdfs-nginx-module後需要重新開機nginx
/usr/local/nginx/sbin/nginx -s reload
如果出現如下錯誤。需要先停止nginx再啟動nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx: [emerg] unknown directive "ngx_fastdfs_module" in /usr/local/nginx/conf/nginx.conf:92
5.2.4 測試環境
伺服器中測試上傳
[email protected]:~# touch test.txt
[email protected]:~# echo "You are not strong, no one brave for you" > test.txt
[email protected]:~# fdfs_upload_file /etc/fdfs/client.conf test.txt
group1/M00/00/00/dh8IcGC8l7yAArNnAAAAKTIQHvk523.txt
[email protected]:~#
接着在浏覽器中輸入:
http://118.31.8.112:80/group1/M00/00/00/dh8IcGC8l7yAArNnAAAAKTIQHvk523.txt
測試成功