fastdfs-安裝fastdfs-nginx-module和配置使用(二)

在上一篇文章中,介紹了fasfdfs的安裝過程,下面將介紹一下關於fastdfs安裝nginx模塊的功能。
fastdfs-nginx-module這個模塊主要有什麼作用呢?
1、爲了實現下載和方便訪問文件。
2、訪問源文件,由於上傳一個大文件到一個組的一個stoage1服務器後,這裏的同組的storage2將會進行進行通部複製,如果複製沒有完成,用戶訪問值storage2的時候,那麼這個將會沒有辦法完整訪問的,所以fastdfs-nginx-module這個模塊,將會查找一個源文件,也就是存在storage1的服務器上,可以訪問storage1上的文件。

下面分爲兩種情況
第一種:沒有安裝nginx,需要重新安裝這個過程的
第二種,已經安裝好nginx,現在是添加模塊的。

首先下載fastdfs-nginx-module模塊
github地址:https://github.com/happyfish100/fastdfs-nginx-module

針對第一種情況:
進入nginx的源碼目錄(nginx自行下載)

./configure --prefix=... --add-module=/home/bing/fastdfs-nginx-module/src/ ...
make 
make install

這裏安裝就不寫nginx的其他模塊了,比如
--prefix=path :定義一個目錄,存放服務器上的文件 ,也就是nginx的安裝目錄。默認使用 /usr/local/nginx。
--sbin-path=path :設置nginx的可執行文件的路徑,默認爲  prefix/sbin/nginx.
--conf-path=path :設置在nginx.conf配置文件的路徑。nginx允許使用不同的配置文件啓動,通過命令行中的-c選項。默認爲prefix/conf/nginx.conf.
--pid-path=path :設置nginx.pid文件,將存儲的主進程的進程號。安裝完成後,可以隨時改變的文件名 , 在nginx.conf配置文件中使用 PID指令。默認情況下,文件名 爲prefix/logs/nginx.pid.
--error-log-path=path :設置主錯誤,警告,和診斷文件的名稱。安裝完成後,可以隨時改變的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默認情況下,文件名 爲prefix/logs/error.log.

在這裏就直接寫--add-module,這裏指向的就是fastdfs-nginx-module的源碼路徑,一定要進入到src目錄下,然後執行make ,再執行make 
install

make:主要是用來編譯的,它從Makefile中讀取指令,然後編譯生成一個可執行文件在nginx源碼目錄下的objs,這個可執行文件時nginx

make install:主要是實現安裝,它也從Makefile中讀取指令,安裝到指定的位置。

下面繼續說一下如果安裝完成nginx,現在在安裝一個模塊的問題,該如何解決,很多時候,我們是不能說,再重新安裝nginx的,所以必須想一個辦法實現動態安裝module。

首先查看nginx已經安裝了哪些模塊

# nginx -V
nginx version: nginx/1.10.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module

可以看到列出了一些nginx的信息,包括已經安裝的一些模塊。

那麼我們可以重新編譯這些模塊,同時在通過--add-module添加模塊

例如上面的,重新進入nginx的源碼目錄

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module--add-module=/home/bing/fastdfs-nginx-module/src/

make 

可以看到紅色部分,重新添加一個--add-module,指定需要添加的模塊的源碼目錄路徑,然後執行make,特別注意,這裏不需要執行make install

編譯之後,將會在objs的目錄下重新生成一個nginx的可執行文件,可以複製,然後用來覆蓋原來安裝nginx時生成的nginx文件,這個文件默認在/usr/local/nginx/sbin/下,爲了安全起見,向備份原來的文件

cd /usr/local/nginx/sbin

cp nginx nginx.back
cp ..../objs/nginx /usr/local/nginx/sbin

然後重新啓動nginx

那麼查看是否安裝成功這個模塊

/usr/local/nginx/sbin/nginx -V

在這裏要注意,我們也可以複製nginx 到/usr/local/sbin下面,這個樣子就可以直接nginx -V,不需要輸入完整路徑。

安裝完成這個fastdfs-nginx-module後,接下來,將是如何配置這個模塊
在fastdfs的源碼安裝包下面的conf下面,我們需要複製兩個文件至/etc/fdfs/下

cp http.conf mime.types /etc/fdfs/

同時把fastdfs-nginx-modul源碼包的src目錄下的mod_fastdfs.conf複製至/etc/fdfs/

那麼可以看到/etc/fdfs下有的文件

-rwxrwxrwx 1 root root  1443 2月  10 03:33 client.conf
-rwxrwxrwx 1 root root   859 2月   8 23:44 http.conf
-rwxrwxrwx 1 root root 31172 2月   8 23:24 mime.types
-rwxrwxrwx 1 root root  3693 2月   9 17:14 mod_fastdfs.conf
-rwxrwxrwx 1 root root  7923 2月  10 04:07 storage.conf
-rwxrwxrwx 1 root root    71 2月  10 02:46 storage_ids.conf
-rwxrwxrwx 1 root root  7318 2月  10 01:51 tracker.conf

然後配置mod_fastdfs.conf這個文件,主要以下幾個
# the base path to store log files
base_path=/tmp

tracker_server=192.168.1.67:22122

#include http.conf

group_name=group1
url_have_group_name = true 
store_path0=/home/wwwroot/default


# group settings for group #1
# since v1.14
# when support multi-group on this storage server, uncomment following section
[group1]
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/home/wwwroot/default
# group settings for group #2
# since v1.14
# when support multi-group, uncomment following section as neccessary
#[group2]
#group_name=group2
#storage_server_port=23000
#store_path_count=1
#store_path0=/home/wwwroot/default
如果這裏有多個group,測配置多個group,這裏只有一個group,所以只配置group1
配置完成這個之後,我們將在nginx的配置文件中配置一下
server {
    listen 8080 ;
    server_name 192.168.1.67;
    index index.html index.htm index.php;
    
    root /home/wwwroot/default;

    location /group1/M00 {
        
        ngx_fastdfs_module;
    }
}

建立軟連接
ln -s /home/wwwroot/default/data  /home/wwwroot/default/data/M00

重新啓動nginx,將會如下圖出現,關鍵是紅色部分

Starting nginx... ngx_http_fastdfs_set pid=18283
done

上傳之後測試:

http://192.168.1.67:8080/group1/M00/00/00/wKgBQ1ihmzuAIjAMAAAPBAI7S14151.png

如果可以看到圖片,說明成功了。

如果不能訪問的話,可能是8080端口沒有開放,執行一下命令開放端口

iptables -A INPUT -ptcp --dport 8080 -j ACCEPT



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章