CentOS 6.4 i386 版本安裝 FastDFS、使用Nginx作為檔案訪問WEB伺服器

[曾恩]發表於2013-11-20

安裝環境:
1. CentOS-6.4-i386
2. FastDFS_v4.06
3. fastdfs-nginx-module_v1.15
4. Nginx-1.5.6(安裝見此
5. libevent-2.0.21-stable

tracker server  IP:192.168.1.11
storage1 server IP:192.168.1.12 group1
storage2 server IP:192.168.1.13 group2
storage3 server IP:192.168.1.14 group2
 
1. CentOS系統初始化:
    #yum預裝常用的伺服器軟體
      yum -y install gcc gcc-c++

2. 系統操作環境的設定
    #軟體安裝包儲存
      /usr/local/src
    #libevent安裝目錄
      /usr/local/libevent
    #fastdfs安裝目錄
      /usr/local/fastdfs
    #nginx安裝目錄
      /usr/local/nginx
 
3. 安裝libevent( fastdfs在編譯源程式時fastdfs內部呼叫libevent的處理機制,,需要用到libevent一些依賴檔案,否則編譯fastdfs會出錯)
    #解除安裝系統自帶libevent,自帶版本過低,安裝fastdfs會出錯
      1> rpm -qa|grep libevent
      2> yum remove libevent*
    #下載安裝libevent:
      1> wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
      2> tar -zxvf libevent-2.0.21-stable.tar.gz
      3> cd libevent-2.0.21-stable
      4> ./configure --prefix=/usr/local/libevent
      5> make && make install
    #為libevent建立軟連結到/lib庫下,64位系統對應/lib64
      ln -s /usr/local/libevent/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5
      ln -s /usr/local/libevent/lib/libevent-2.0.so.5 /usr/lib64/libevent-2.0.so.5
 
4. 安裝FastDFS
    1> wget http://fastdfs.googlecode.com/files/FastDFS_v4.06.tar.gz
    2> tar -zxvf FastDFS_v4.06.tar.gz
    3> cd FastDFS
    4> #由於定義/usr/local/fastdfs為fastdfs安裝目錄,所以需要修改make.sh
           vi make.sh
           #內容更改如下
           TARGET_PREFIX=/usr/local 修改為 /usr/local/fastdfs
           將/etc/fdfs 全部替換為 /usr/local/fastdfs/conf
    5> #安裝
           ./make.sh C_INCLUDE_PATH=/usr/local/libevent/include LIBRARY_PATH=/usr/local/libevent/lib
           ./make.sh install
 
5. 配置Tracker
   #建立tracker目錄儲存執行日誌
     mkdir -m 777 -p /home/fastdfs/tracker
   #修改tracker.conf配置
     vim /usr/local/fastdfs/conf/tracker.conf
     #修改內容如下所示
# the tracker server port
port=22122
# the base path to store data and log files
base_path=/home/yuqing/fastdfs -> base_path=/home/fastdfs/tracker #日誌目錄
#開啟自定義server ID取代ip形式,方便內部網路伺服器更換ip#**此方式要重點理解,4.0以後新特性
use_storage_id = true #使用server ID作為storage server標識
storage_ids_filename = storage_ids.conf #<id> <group_name> <ip_or_hostname>
id_type_in_filename = id #檔名反解析中包含server ID,以前是ip
View Code

   #移動storage_ids.conf檔案
     cp -r /usr/local/src/FastDFS/conf/storage_ids.conf /usr/local/fastdfs/conf/
   #編輯storage伺服器ID與IP地址的對應關係
     vim /usr/local/fastdfs/conf/storage_ids.conf
     #修改內容如下所示

#<id> <group_name> <ip_or_hostname>
 100001          group1           192.168.1.12
 100002          group2           192.168.1.13
 100003          group2           192.168.1.14
View Code

   #編輯啟動指令碼
     vim /etc/init.d/fdfs_trackerd
     #啟動指令碼內容如下

#!/bin/bash
#
# fdfs_trackerd Starts fdfs_trackerd
#
#
# chkconfig: 2345 99 01
# description: FastDFS tracker server
### BEGIN INIT INFO
# Provides: $fdfs_trackerd
### END INIT INFO
# Source function library.
. /etc/init.d/functions
FastDfs='/usr/local/fastdfs'
CONF="$FastDfs/conf/tracker.conf"
if [ ! -f $CONF ]; then
  echo "file $CONF does not exist!"
  exit 2
fi
PRG="$FastDfs/bin/fdfs_trackerd"
if [ ! -f $PRG ]; then
  echo "file $PRG does not exist!"
  exit 2
fi
Stop="$FastDfs/bin/stop.sh"
if [ ! -f $Stop ]; then
  echo "file $Stop does not exist!"
  exit 2
fi
Restart="$FastDfs/bin/restart.sh"
if [ ! -f $Restart ]; then
  echo "file $Restart does not exist!"
  exit 2
fi
RETVAL=0
start() {
    echo -n $"Starting FastDFS tracker server: "
    $PRG $CONF &
    RETVAL=$?
    echo
    return $RETVAL
}
stop() {
    echo -n $"Stop FastDFS tracker server: "
    $Stop $PRG $CONF
    RETVAL=$?
    return $RETVAL
}
rhstatus() {
    status fdfs_trackerd
}
restart() {
        $Restart $PRG $CONF &
}
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    rhstatus
    ;;
  restart|reload)
    restart
    ;;
  condrestart)
    restart
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|condrestart}"
    exit 1
esac
exit $?
View Code

   #給啟動指令碼增加許可權
     chmod 777 /etc/init.d/fdfs_trackerd
   #啟動tracker
     service fdfs_trackerd restart
   #啟動成功,加入開機啟動
     vim /etc/rc.d/rc.local
     #加入內容如下
     service fdfs_trackerd start
   #防火牆開啟tracker埠22122
     vim /etc/sysconfig/iptables
     #加入內容如下
     -A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT
   #重啟防火牆
     service iptables restart

6. 配置Storage
   #建立Storage目錄儲存執行日誌及其data資料
     mkdir -m 777 -p /home/fastdfs/storage
   #修改storage.conf配置
     vim /usr/local/fastdfs/conf/storage.conf
   #修改內容如下所示
vim /usr/local/fastdfs/conf/storage.conf
# the name of the group this storage server belongs to
group_name=group1 #設定組名
# the name of the group this storage server belongs to
# the storage server port #the storage server port
port=23000
# the base path to store data and log files #日誌目錄
base_path=/home/yuqing/fastdfs -> /home/fastdfs/storage
# store_path#, based 0, if store_path0 not exists, it's value is base_path #data資料儲存目錄
# the paths must be exist
store_path0=/home/fastdfs/storage
# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
tracker_server=192.168.209.121:22122 ->192.168.1.11:22122
View Code

   #編輯啟動指令碼
     vim /etc/init.d/fdfs_storaged
     #啟動指令碼內容如下

#!/bin/bash
#
# fdfs_storaged Starts fdfs_storaged
#
#
# chkconfig: 2345 99 01
# description: FastDFS storage server
### BEGIN INIT INFO
# Provides: $fdfs_storaged
### END INIT INFO
# Source function library.
. /etc/init.d/functions
FastDfs='/usr/local/fastdfs'
CONF="$FastDfs/conf/storage.conf"
if [ ! -f $CONF ]; then
  echo "file $CONF does not exist!"
  exit 2
fi
PRG="$FastDfs/bin/fdfs_storaged"
if [ ! -f $PRG ]; then
  echo "file $PRG does not exist!"
  exit 2
fi
Stop="$FastDfs/bin/stop.sh"
if [ ! -f $Stop ]; then
  echo "file $Stop does not exist!"
  exit 2
fi
Restart="$FastDfs/bin/restart.sh"
if [ ! -f $Restart ]; then
  echo "file $Restart does not exist!"
  exit 2
fi
RETVAL=0
start() {
    echo -n $"Starting FastDFS storage server: "
    $PRG $CONF &
    RETVAL=$?
    echo
    return $RETVAL
}
stop() {
    echo -n $"Stop FastDFS storage server: "
    $Stop $PRG $CONF
    RETVAL=$?
    return $RETVAL
}
rhstatus() {
    status fdfs_storaged
}
restart() {
        $Restart $PRG $CONF &
}
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    rhstatus
    ;;
  restart|reload)
    restart
    ;;
  condrestart)
    restart
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|condrestart}"
    exit 1
esac
exit $?
View Code

   #給啟動指令碼增加許可權
     chmod 777 /etc/init.d/fdfs_storaged
   #啟動storage
     service fdfs_storaged restart
   #接下來會出現很多mkdir data path,這是系統在建立資料目錄,如下圖所示
   
   #啟動成功,加入開機啟動
     vim /etc/rc.d/rc.local
     #加入內容如下
     service fdfs_storaged start

7. 安裝nginx(僅Storage)
    #建立nginx日誌目錄
      mkdir -m 777 -p /home/www/logs
    #安裝nginx必需的庫:zlib-devel openssl-devel pcre
      yum -y install zlib-devel openssl-devel
      #手動安裝pcre
      1> wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
      2> tar -zxvf pcre-8.33.tar.gz
      3> cd pcre-8.33
      4> ./configure
      5> make && make install
      6> ln -s /usr/local/lib/libpcre.so.1 /lib
    #安裝nginx
      1> wget http://nginx.org/download/nginx-1.5.6.tar.gz
      2> tar -zxvf nginx-1.5.6.tar.gz
      3> cd nginx-1.5.6
      4> ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
      5> make && make install
     #檢查nginx配置是否正確
       /usr/local/nginx/sbin/nginx -t
     #出現以下類似資訊表示配置正確
     
     #檢視nginx編譯選項
       /usr/local/nginx/sbin/nginx -V
     #編輯啟動指令碼
       vim /etc/init.d/fdfs_storaged
       #啟動指令碼內容如下

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/nginx.pid
# admin        chenai
# Last Updated 20120.6.1

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

#必填
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
#必填
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    #service php-fpm start
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    $nginx -s stop
    echo_success
    retval=$?
    echo
    #service php-fpm stop
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    stop
    start
}
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    $nginx -s reload
    RETVAL=$?
    echo
}
force_reload() {
    restart
}
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
version() {
  $nginx -V
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest|version)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest|version}"
        exit 2
esac
View Code

     #給啟動指令碼增加許可權
       chmod 777 /etc/init.d/nginxd
     #啟動nginx
       service nginxd restart
     #啟動成功,加入開機啟動
       vim /etc/rc.d/rc.local
       #加入內容如下
       service nginxd start

 

8. 安裝nginx-module模組(僅Storage)
    1> wget http://fastdfs.googlecode.com/files/fastdfs-nginx-module_v1.15.tar.gz
    2> tar -zxvf fastdfs-nginx-module_v1.15.tar.gz;
    #修改外掛配置檔案
      vim /usr/local/src/fastdfs-nginx-module/src/config
      #修改內容如下

ngx_addon_name=ngx_http_fastdfs_module
HTTP_MODULES="$HTTP_MODULES ngx_http_fastdfs_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_fastdfs_module.c"
CORE_INCS="$CORE_INCS /usr/local/fastdfs/include/fastdfs /usr/local/fastdfs/include/fastcommon/"
CORE_LIBS="$CORE_LIBS -L/usr/local/fastdfs/lib -lfastcommon -lfdfsclient"
CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 -DFDFS_OUTPUT_CHUNK_SIZE='256*1024' -DFDFS_MOD_CONF_FILENAME='\"/usr/local/fastdfs/conf/mod_fastdfs.conf\"'"
View Code

    #複製mod_fastdfs.conf到/usr/local/fastdfs/conf/目錄下
      cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /usr/local/fastdfs/conf/
    #將/usr/local/fastdfs/lib 加入系統檔案/etc/ld.so.conf中(編譯時使用的動態連結庫)
      vim /etc/ld.so.conf
      #修改內容如下
      /usr/local/fastdfs/lib
    #更新庫檔案快取ld.so.cache
      /sbin/ldconfig
    #編譯fastdfs-nginx-module模組
      1> cd nginx-1.1.19/
      2> ./configure 此處加上nginx之前的編譯引數(使用 /usr/local/nginx/sbin/nginx -V 命令檢視) --add-module=/usr/local/src/fastdfs-nginx-module/src
      3> make && make install
    #修改mod_fastdfs.conf配置
      vim /usr/local/fastdfs/conf/mod_fastdfs.conf
      #修改內容如下

# if load FastDFS parameters from tracker server
# since V1.12
# default value is false
load_fdfs_parameters_from_tracker=true
# FastDFS tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
# valid only when load_fdfs_parameters_from_tracker is true
tracker_server=192.168.25.11:22122
# the port of the local storage server
# the default value is 23000
storage_server_port=23000
# the group name of the local storage server
group_name=group1 #當前storage機器組名
# if the url / uri including the group name
# set to false when uri like /M00/00/00/xxx
# set to true when uri like ${group_name}/M00/00/00/xxx, such as group1/M00/xxx
# default value is false
url_have_group_name = true
# path(disk or mount point) count, default value is 1
# must same as storage.conf
store_path_count=1
# store_path#, based 0, if store_path0 not exists, it's value is base_path
# the paths must be exist
# must same as storage.conf
store_path0=/home/fastdfs/storage
# set the log filename, such as /usr/local/apache2/logs/mod_fastdfs.log
# empty for output to stderr (apache and nginx error_log file)
log_filename=/home/www/logs/mod_fastdfs.log
View Code

    #修改nginx.conf配置
      vim /usr/local/nginx/conf/nginx.conf
      #修改內容如下

worker_processes  2;
error_log  /home/www/logs/error.log  notice;
pid        /home/www/logs/nginx.pid;

worker_rlimit_nofile 5120;
events {
    use epoll;
    worker_connections  5120;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  60;
    tcp_nodelay on;

    server {
        listen       80;
        server_name  localhost;
        location /組名/M00 {
            alias /home/fastdfs/storage/data;
            ngx_fastdfs_module;
        }
    }
}
View Code

    #重啟nginx服務
    service nginxd restart

9. 測試FastDFS
    


      


 

相關文章