在虛擬機(CentOS)上配置fastdfs+nginx

廢話不多說!用到的包如下:

FastDFS--tracker安裝

01 :如果沒有gcc環境,安裝:yum  install  gcc-c++

02:FastDFS依賴libevent庫,需要安裝:yum -y install libevent

03:libfastcommon是FastDFS官方提供的,libfastcommon包含了FastDFS運行所需要的一些基礎庫。

        將libfastcommonV1.0.7.tar.gz拷貝至/usr/local/下,執行以下命令

        cd /usr/local

        tar -zxvf libfastcommonV1.0.7.tar.gz

        cd libfastcommon-1.0.7

        ./make.sh

        ./make.sh install

04:注意libfastcommon安裝好後會自動將庫文件拷貝至/usr/lib64下,由於FastDFS程序引用usr/lib目錄所以需要將/usr/lib64下的庫文件拷貝至/usr/lib目錄下

       cp /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so

05:tracker編譯安裝,

       將FastDFS_v5.05.tar.gz拷貝至/usr/local下後,執行以下命令

       tar-zxvf FastDFS_v5.05.tar.gz

       cd FastDFS

       ./make.sh

       ./make.sh install

       安裝成功將安裝目錄下的conf下的文件拷貝到/etc/fdfs/下

06:配置

       安裝成功後進入/etc/fdfs目錄:

       cp tracker.conf.example tracker.conf

       修改tracker.conf

       vi tracker.conf

       base_path=/home/xxx/FastDFS

       改爲:

       base_path=/home/FastDFS

       配置http端口:

       http.server_port=80

 07:啓動  /usr/bin/fdfs_trackerd  /etc/fdfs/tracker.conf  restart

FastDFS--storage安裝

01--05步驟與tracker相同

06:配置 

       安裝成功後進入/etc/fdfs目錄,拷貝一份新的storage配置文件:cp  storage.conf.sample  storage.conf 

       修改storage.conf 

       vi storage.conf

       group_name=group1

       base_path=/home/xxx/FastDFS改爲:base_path=/home/FastDFS

       store_path0=/home/xxx/FastDFS改爲:store_path0=/home/FastDFS/fdfs_storage 

       #如果有多個掛載磁盤則定義多個store_path,如下

       #store_path1=.....

       #store_path2=......
       tracker_server=192.168.160.128:22122   #配置tracker服務器IP     

       #如果有多個則配置多個tracker

       #tracker_server=192.168.101.4:22122

       #配置http端口

       http.server_port=80

07:啓動 /usr/bin/fdfs_storaged  /etc/fdfs/storage.conf  restart

測試 通過fdfs_test程序上傳圖片

       FastDFS安裝成功可通過/usr/bin/fdfs_test測試上傳、下載等操作   

       修改/etc/fdfs/client.conf

       base_path=/home/fastdfs

       tracker_server=192.168.160.128:22122

       #tracker_server=192.168.101.4:22122     

       比如將/home下的圖片上傳到FastDFS中:/usr/bin/fdfs_test /etc/fdfs/client.conf  upload  /home/tomcat.png


      http://192.168.160.128/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png就是文件的下載路徑。

      對應storage服務器上的

      /home/fastdfs/fdfs_storage/data/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png文件。

      由於現在還沒有和nginx整合無法使用http下載。

FastDFS 和nginx整合

在tracker上安裝nginx

01:nginx安裝需要的環境

       gcc:yum install gcc-c++

       PCRE:yum install -y pcre pcre-devel

       zlib:yum install -y zlib zlib-devel

       openssl:yum install -y openssl openssl-devel

02:編譯安裝

       將nginx-1.8.0.tar.gz和ngx_cache_purge-2.3.tar.gz拷貝至/usr/local/下,執行

       tar -zxvf nginx-1.8.0.tar.gz

       tar -zxvf ngx_cache_purge-2.3.tar.gz

       cd nginx-1.8.0

03:configure 

       ./configure \

       --prefix=/usr/local/nginx \

       --pid-path=/var/run/nginx/nginx.pid \

       --lock-path=/var/lock/nginx.lock \

       --error-log-path=/var/log/nginx/error.log \

       --http-log-path=/var/log/nginx/access.log \

       --with-http_gzip_static_module \

       --http-client-body-temp-path=/var/temp/nginx/client\

       --http-proxy-temp-path=/var/temp/nginx/proxy\

       --http-fastcgi-temp-path=/var/temp/nginx/fastcgi\

       --http-uwsgi-temp-path=/var/temp/nginx/uwsgi\

       --http-scgi-temp-path=/var/temp/nginx/scgi

       --add-module=/usr/local/ngx_cache_purge-2.3  //添加緩存模塊

注意:上邊將臨時文件目錄指定爲/var/temp/nginx,需要在/var下創建tempnginx目錄   mkdir -p /var/temp/nginx

04:編譯安裝 

       make

       make  install

05:啓動nginx 

       cd /usr/local/nginx/sbin/

       ./nginx

06:關閉防火牆,否則http訪問不了

       /etc/init.d/iptables stop

       在瀏覽器訪問虛擬機ip地址,出現歡迎頁面,nginx安裝成功。

07:編輯/usr/local/nginx/conf配置文件目錄下的nginx.conf,設置負載均衡以及緩存     

worker_processes  4;                  #根據CPU核心數而定

events{

    worker_connections  65535;       #最大鏈接數

    use epoll;#新版本的Linux可使用epoll加快處理性能

}

http{

#設置緩存參數

    server_names_hash_bucket_size 128;

    client_header_buffer_size 32k;

    large_client_header_buffers 4 32k;

    client_max_body_size 300m;

    sendfile on;

    tcp_nopush 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;

#storage羣group1組

upstream storage_server_group1{

             server192.168.160.129:80 weight=10 max_fails=2 fail_timeout=30s;

             #server192.168.101.6:80 weight=10 max_fails=2 fail_timeout=30s;

    }

#storage羣group2組

#upstream storage_server_group2{

#            server192.168.101.7:80 weight=10 max_fails=2 fail_timeout=30s;

#            server 192.168.101.8:80weight=10 max_fails=2 fail_timeout=30s;

    }

server {

         listen 80;

         server_name mytracker;

         location /group1/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://storage_server_group1;

            expires 30d;

         }

         #location /group2{

         #         proxy_pass http://storage_server_group2;

         }     

#設置清除緩存的訪問權限

    location~ /purge(/.*) {

        allow 127.0.0.1;

        allow 172.16.1.0/24;

        deny all;

        proxy_cache_purge  http-cache  $1$is_args$args;

      } 

    }

}

 在Storage上安裝nginx

01:將FastDFS-nginx-module_v1.16.tar.gz傳至/usr/local/下,執行以下命令:

      cd /usr/local

      tar -zxvf FastDFS-nginx-module_v1.16.tar.gz

      cd FastDFS-nginx-module/src

      修改config文件將/usr/local/路徑改爲/usr/

      將FastDFS-nginx-module/src下的mod_FastDFS.conf拷貝至/etc/fdfs/下:cp mod_FastDFS.conf /etc/fdfs/

      並修改mod_FastDFS.conf的內容:

      vi /etc/fdfs/mod_FastDFS.conf

      base_path=/home/FastDFS

      tracker_server=192.168.160.128:22122

      #tracker_server=192.168.101.4:22122

      url_have_group_name=true            #url中包含group名稱

      store_path0=/home/FastDFS/fdfs_storage   #指定文件存儲路徑

      #如果有多個... 

      將libfdfsclient.so拷貝至/usr/lib下:cp /usr/lib64/libfdfsclient.so /usr/lib/

      創建nginx/client目錄:mkdir -p /var/temp/nginx/client

02:nginx安裝

      步驟同上。 

      添加FastDFS-nginx-module模塊 

./configure \

--prefix=/usr/local/nginx \

--pid-path=/var/run/nginx/nginx.pid\

--lock-path=/var/lock/nginx.lock\

--error-log-path=/var/log/nginx/error.log\

--http-log-path=/var/log/nginx/access.log\

--with-http_gzip_static_module\

--http-client-body-temp-path=/var/temp/nginx/client\

--http-proxy-temp-path=/var/temp/nginx/proxy\

--http-fastcgi-temp-path=/var/temp/nginx/fastcgi\

--http-uwsgi-temp-path=/var/temp/nginx/uwsgi\

--http-scgi-temp-path=/var/temp/nginx/scgi\

--add-module=/usr/local/fastdfs-nginx-module/src

03:編譯安裝

    make

    make install

04:編輯/usr/local/nginx/conf配置文件目錄下的nginx.conf

server {

        listen       80;

        server_name  192.168.160.129;

 

        location /group1/M00/{

                root /home/fastdfs/fdfs_storage/data;

                ngx_fastdfs_module;

        }

}

location /group1/M00/:group1爲nginx 服務FastDFS的分組名稱,M00是FastDFS自動生成編號,對應store_path0=/home/FastDFS/fdfs_storage,如果FastDFS定義store_path1,這裏就是M01

測試

保證nginx、tracker、storage都開啓,使用瀏覽器訪問http://192.168.160.128/group1/M00/00/00/wKhlBVVY2M-AM_9DAAAT7-0xdqM485_big.png






























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