創業型的互聯網公司,所以用開源軟件自己搭建圖片服務器,用來上傳下載以及nginx轉發負載均衡
wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
2.解壓libevent:
tar -zxvpf libevent-2.0.21-stable.tar.gz
3.進入解壓目錄:
cd libevent-2.0.21-stable
4.指定安裝目錄:
yum install -y libtool*
yum install -y autoconf*
yum install -y automake
./autogen.sh
./configure –prefix=/usr/local/libevent-2.0.21
5.安裝libevent:
make
make install
二、安裝FastDFS步驟如下:
1.下載FastDFS:
wget http://fastdfs.googlecode.com/files/FastDFS_v4.06.tar.gz
2.解壓FastDFS:
tar -xvf FastDFS_v4.06.tar.gz
3.進入解壓目錄:
cd FastDFS
4.修改FastDFS的安裝文件:
vim make.sh
TARGET_PREFIX=/usr/local/fastdfs —安裝路徑
TARGET_CONF_PATH=/etc/fdfs —配置文件路徑
WITH_LINUX_SERVICE=1 —是否為linux服務
5.指定libevent的lib目錄:
./make.sh C_INCLUDE_PATH=/usr/local/libevent-2.0.21/include LIBRARY_PATH=/usr/local/libevent-2.0.21/lib
6.安裝FastDFS:
./make.sh install
7.設置軟連接(否則編譯nginx時會出錯)
[root@zhitianstatic2 FastDFS]# ln -sf /usr/local/fastdfs/lib/libfastcommon.so /usr/local/lib/libfastcommon.so
[root@zhitianstatic2 FastDFS]# ln -sf /usr/local/fastdfs/lib/libfastcommon.so /usr/lib/libfastcommon.so
[root@zhitianstatic2 FastDFS]# ln -sf /usr/local/fastdfs/lib/libfastcommon.so /usr/lib64/libfastcommon.so
[root@zhitianstatic2 FastDFS]# ln -s /usr/local/fastdfs/lib/libfdfsclient.so /usr/local/lib/libfdfsclient.so
[root@zhitianstatic2 FastDFS]# ln -s /usr/local/fastdfs/lib/libfdfsclient.so /usr/lib/libfdfsclient.so
[root@zhitianstatic2 FastDFS]# ln -s /usr/local/fastdfs/lib/libfdfsclient.so /usr/lib64/libfdfsclient.so
[root@zhitianstatic2 FastDFS]#
三,安裝依賴組件包
yum install -y gcc gcc-c++ autoconf automake bzip2-devel ncurses-devel
yum install -y openssl libtool* zlib-devel libxml-devel libjpeg-devel ligpng-devel libtiff-devel
yum install -y openssl fontconfig-devel freetype-devel libXpm-devel gettext-devel curl curl-devel pam-devel e2fsprogs-devel krb5-devel libidn libidn-devel
四,開始安裝nginx Nginx和fastdfs-nginx-module
1,安裝pcre
yum install -y pcre-devel
PS:有些yum安裝成功,有些失敗,如果失敗了,在下一步驟直接源碼方式安裝pcre
准備組件 fastdfs-nginx-module
tar -xvf fastdfs-nginx-module_v1.15.tar.gz
2,安裝nginx
yum install -y openssl*
tar -xvf pcre-8.31.tar.gz -C /usr/local/
tar -xvf fastdfs-nginx-module_v1.15.tar.gz
tar -xvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure –with-http_stub_status_module –with-http_realip_module –with-http_ssl_module –prefix=/usr/local/nginx –add-module=/soft/fastdfs-nginx-module/src –with-pcre=/usr/local/pcre-8.31
或者
./configure –prefix=/usr/local/nginx –conf-path=/usr/local/nginx/conf/nginx.conf –add-module=/soft/fastdfs-nginx-module/src
make
make install
PS: –with-pcre=/usr/local/pcre-8.31,加這個會報錯 make[1]: * [objs/nginx] Error 1
原文章地址:http://blog.csdn.net/mchdba/article/details/50933405,未經過原作者mchdba(黃杉)允許,謝絕轉載。
3,nginx編譯包報錯:
/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c: In function ‘ngx_http_fastdfs_handler’:
/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:842: error: ‘struct fdfs_http_context’ has no member named ‘arg’
/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:843: error: ‘struct fdfs_http_context’ has no member named ‘header_only’
/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:844: error: ‘struct fdfs_http_context’ has no member named ‘url’
/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:845: error: ‘struct fdfs_http_context’ has no member named ‘output_headers’
/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:846: error: ‘struct fdfs_http_context’ has no member named ‘send_file’
/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:847: error: ‘struct fdfs_http_context’ has no member named ‘send_reply_chunk’
/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:847: error: ‘fdfs_send_reply_chunk’ undeclared (first use in this function)
/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:848: error: ‘struct fdfs_http_context’ has no member named ‘proxy_handler’
/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:854: error: ‘struct fdfs_http_context’ has no member named ‘if_modified_since’
/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:857: error: ‘struct fdfs_http_context’ has no member named ‘if_modified_since’
/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:887: error: ‘struct fdfs_http_context’ has no member named ‘range’
/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:893: error: ‘struct fdfs_http_context’ has no member named ‘if_range’
/soft/fastdfs-nginx-module/src/ngx_http_fastdfs_module.c:893: error: ‘true’ undeclared (first use in this function)
make[1]: * [objs/addon/src/ngx_http_fastdfs_module.o] Error 1
make[1]: Leaving directory `/soft/nginx-1.4.2’
make: * [install] Error 2
解決方案:
cd fastdfs-nginx-module/src
vi config
去掉/local
CORE_INCS="$CORE_INCS /usr/local/fastdfs/include/fastdfs /usr/local/fastdfs/include/fastcommon/"
CORE_LIBS="$CORE_LIBS -L/usr/lib -lfastcommon -lfdfsclient"
改成這樣是因為我們在安裝fastdfs的時候,指定的目錄是/usr/local/fastdfs而不是/usr/local/,所以要修改為我們安裝的fastdfs的目錄/usr/local/fastdfs/include/fastdfs
或者軟連接:
ln -s /usr/local/fastdfs/lib/libfastcommon.so /usr/lib64/libfastcommon.so
ln -s /usr/local/fastdfs/lib/libfastcommon.so /usr/lib/libfastcommon.so
ln -s /usr/local/fastdfs/lib/libfdfsclient.so /usr/lib64/libfdfsclient.so
ln -s /usr/local/fastdfs/lib/libfdfsclient.so /usr/lib/libfdfsclient.so
然後重新進行解壓縮編譯問題就解決了。
4,去啟動nginx
將libfastcommon.so、libfastcommon.so.1、libfdfsclient.so、libfdfsclient.so.1復制到/lib64/,否則啟動會報錯
啟動:/usr/local/nginx/sbin/nginx
啟動報錯:
[root@zhitianstatic1 nginx-1.4.2]# /usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx: error while loading shared libraries: libfastcommon.so: cannot open shared object file: No such file or directory
[root@zhitianstatic1 nginx-1.4.2]#
訪問:在浏覽器中輸入nginx服務器ip地址即可。
5,配置nginx.conf
location /group1/M00 {
root /home/storage/fastdfs/data;
ngx_fastdfs_module;
}
6,啟動nginx
[root@mall_hc_244 nginx-1.4.2]# /usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=15554
[root@mall_hc_244 nginx-1.4.2]#
五,配置tracker server 在static2 10.250.11.32上面:
1. vim /etc/fdfs/tracker.conf
bind_addr=192.168.227.128
base_path=/home/tracker/fastdfs
2.配置防火牆:
_
iptables -I INPUT -p tcp -m state –state NEW -m tcp –dport 22122 -j ACCEPT
3.保存配置:
/etc/init.d/iptables save
啟動 tracker
/usr/local/fastdfs/bin/fdfs_trackerd /etc/fdfs/tracker.conf
在啟動時報錯:
/usr/local/bin/fdfs_trackerd: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
執行下面的命令即可正常啟動:
echo ‘/usr/local/libevent-2.0.21/include/’ >> /etc/ld.so.conf
echo ‘/usr/local/libevent-2.0.21/lib/’ >> /etc/ld.so.conf
ldconfig
[root@mall_hc_244 nginx-1.4.2]# echo ‘/usr/local/libevent-2.0.21/include/’ >> /etc/ld.so.conf
[root@mall_hc_244 nginx-1.4.2]# echo ‘/usr/local/libevent-2.0.21/lib/’ >> /etc/ld.so.conf
[root@mall_hc_244 nginx-1.4.2]# ldconfig
[root@mall_hc_244 nginx-1.4.2]# /usr/local/fastdfs/bin/fdfs_trackerd /etc/fdfs/tracker.conf
[2016-01-22 07:10:35] ERROR - file: tracker_func.c, line: 191, “/home/tracker/fastdfs” can’t be accessed, error info: No such file or directory
[2016-01-22 07:10:35] INFO - local_host_ip_count: 2, 127.0.0.1 192.168.121.244
[2016-01-22 07:10:35] CRIT - exit abnormally!
[root@mall_hc_244 nginx-1.4.2]# mkdir -p /home/tracker/fastdfs
[root@mall_hc_244 nginx-1.4.2]#
5. 查看監聽程序:
netstat -ntpl | grep fdfs
6.添加開機啟動命令行:
vim /etc/rc.d/rc.local
/usr/local/fastdfs/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
六、配置 storage server 在static1 10.250.11.31上面
1. vim /etc/fdfs/storage.conf
group_name=group1
bind_addr=10.250.11.31
base_path=/home/storage/fastdfs
store_path0=/home/storage/fastdfs
tracker_server=10.250.11.32:22122
http.server.port=8080
2. 復制一份
[root@mall_hc_244 nginx-1.4.2]# cp /soft/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
[root@mall_hc_244 nginx-1.4.2]#
3. vim /etc/fdfs/mod_fastdfs.conf
base_path=/home/storage/fastdfs
group_name=group1
store_path0=/home/storage/fastdfs
tracker_server=10.250.11.32:22122:
url_have_group_name = true
group_count = 0
4. vim /usr/local/nginx/conf/nginx.conf
location /group1/M00 {
root /home/storage/fastdfs/data;
ngx_fastdfs_module;
}
5.配置防火牆:
iptables -I INPUT -p tcp -m state –state NEW -m tcp –dport 23000 -j ACCEPT
6.保存配置:
/etc/init.d/iptables save
7.啟動 storage
/usr/local/fastdfs/bin/fdfs_storaged /etc/fdfs/storage.conf
[root@mall_hc_244 nginx-1.4.2]# /usr/local/fastdfs/bin/fdfs_storaged /etc/fdfs/storage.conf
[2016-01-22 07:21:41] ERROR - file: trunk_mgr/trunk_shared.c, line: 177, "No such file or directory" can't be accessed, error info: /home/storage/fastdfs
[2016-01-22 07:21:41] CRIT - exit abnormally!
[root@mall_hc_244 nginx-1.4.2]# ll /home/storage/fastdfs
ls: cannot access /home/storage/fastdfs: No such file or directory
[root@mall_hc_244 nginx-1.4.2]# mkdir -p /home/storage/fastdfs
[root@mall_hc_244 nginx-1.4.2]# /usr/local/fastdfs/bin/fdfs_storaged /etc/fdfs/storage.conf
data path: /home/storage/fastdfs/data, mkdir sub dir...
mkdir data path: 00 ...
mkdir data path: 01 ...
mkdir data path: 02 ...
mkdir data path: 03 ...
mkdir data path: 04 ...
mkdir data path: 05 ...
mkdir data path: 06 ...
mkdir data path: 07 ...
mkdir data path: 08 ...
mkdir data path: 09 ...
mkdir data path: 0A ...
mkdir data path: 0B ...
mkdir data path: 0C ...
.......................................................
mkdir data path: FD ...
mkdir data path: FE ...
mkdir data path: FF ...
data path: /home/storage/fastdfs/data, mkdir sub dir done.
[root@mall_hc_244 nginx-1.4.2]#
8. 查看監聽程序:
netstat -ntpl | grep fdfs
查看日志文件:
cat /home/storage/fastdfs/logs/storaged.log
[root@mall_hc_244 nginx-1.4.2]# cat /home/storage/fastdfs/logs/storaged.log
[2016-01-22 07:22:04] INFO - FastDFS v4.06, base_path=/home/storage/fastdfs, store_path_count=1, subdir_count_per_path=256, group_name=group1, run_by_group=, run_by_user=, connect_timeout=30s, network_timeout=60s, port=23000, bind_addr=192.168.121.244, client_bind=1, max_connections=256, work_threads=4, disk_rw_separated=1, disk_reader_threads=1, disk_writer_threads=1, buff_size=256KB, heart_beat_interval=30s, stat_report_interval=60s, tracker_server_count=1, sync_wait_msec=50ms, sync_interval=0ms, sync_start_time=00:00, sync_end_time=23:59, write_mark_file_freq=500, allow_ip_count=-1, file_distribute_path_mode=0, file_distribute_rotate_count=100, fsync_after_written_bytes=0, sync_log_buff_interval=10s, sync_binlog_buff_interval=10s, sync_stat_file_interval=300s, thread_stack_size=512 KB, upload_priority=10, if_alias_prefix=, check_file_duplicate=0, file_signature_method=hash, FDHT group count=0, FDHT server count=0, FDHT key_namespace=, FDHT keep_alive=0, HTTP server port=80, domain name=, use_access_log=0, rotate_access_log=0, access_log_rotate_time=00:00, rotate_error_log=0, error_log_rotate_time=00:00, rotate_access_log_size=0, rotate_error_log_size=0, file_sync_skip_invalid_record=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s
[2016-01-22 07:22:09] INFO - file: storage_param_getter.c, line: 187, use_storage_id=0, id_type_in_filename=ip, storage_ip_changed_auto_adjust=1, store_path=0, reserved_storage_space=10.00%, use_trunk_file=0, slot_min_size=256, slot_max_size=16 MB, trunk_file_size=64 MB, trunk_create_file_advance=0, trunk_create_file_time_base=02:00, trunk_create_file_interval=86400, trunk_create_file_space_threshold=20 GB, trunk_init_check_occupying=0, trunk_init_reload_from_binlog=0, store_slave_file_use_link=0
[2016-01-22 07:22:09] INFO - file: storage_func.c, line: 174, tracker_client_ip: 192.168.121.244, my_server_id_str: 192.168.121.244, g_server_id_in_filename: -193353536
[2016-01-22 07:22:09] INFO - local_host_ip_count: 2, 127.0.0.1 192.168.121.244
[2016-01-22 07:22:09] INFO - file: tracker_client_thread.c, line: 308, successfully connect to tracker server 192.168.121.244:22122, as a tracker client, my ip is 192.168.121.244
七、測試:
[root@mall_hc_244 nginx-1.4.2]# /usr/local/fastdfs/bin/fdfs_test /etc/fdfs/client.conf upload /etc/passwd
This is FastDFS client test program v4.06
Copyright (C) 2008, Happy Fish / YuQing
FastDFS may be copied only under the terms of the GNU General
Public License V3, which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.csource.org/
for more detail.
[2016-01-22 07:23:18] ERROR - file: ../client/client_func.c, line: 257, “/home/yuqing/fastdfs” can’t be accessed, error info: No such file or directory
[root@mall_hc_244 nginx-1.4.2]#
修改配置文件:/etc/fdfs/client.conf:
base_path=/home/storage/fastdfs
tracker_server=192.168.121.244:22122
關閉防火牆:
iptables -I INPUT -p tcp -m state –state NEW -m tcp –dport 8080 -j ACCEPT
再次開始測試:
[root@mall_hc_244 nginx-1.4.2]# /usr/local/fastdfs/bin/fdfs_test /etc/fdfs/client.conf upload /tmp/1.png
This is FastDFS client test program v4.06
Copyright (C) 2008, Happy Fish / YuQing
FastDFS may be copied only under the terms of the GNU General
Public License V3, which may be found in the FastDFS source kit.
Please visit the FastDFS Home Page http://www.csource.org/
for more detail.
[2016-01-22 07:27:29] DEBUG - base_path=/home/storage/fastdfs, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0
tracker_query_storage_store_list_without_group:
server 1. group_name=, ip_addr=192.168.121.244, port=23000
group_name=group1, ip_addr=192.168.121.244, port=23000
storage_upload_by_filename
group_name=group1, remote_filename=M00/00/00/wKh59FahaWGAQIobAAADWBdhqfs1165318
source ip address: 192.168.121.244
file timestamp=2016-01-22 07:27:29
file size=856
file crc32=392276475
file url: http://192.168.121.244:8080/group1/M00/00/00/wKh59FahaWGAQIobAAADWBdhqfs1165318
storage_upload_slave_by_filename
group_name=group1, remote_filename=M00/00/00/wKh59FahaWGAQIobAAADWBdhqfs1165318_big
source ip address: 192.168.121.244
file timestamp=2016-01-22 07:27:29
file size=856
file crc32=392276475
file url: http://192.168.121.244:8080/group1/M00/00/00/wKh59FahaWGAQIobAAADWBdhqfs1165318_big
[root@mall_hc_244 nginx-1.4.2]#
啟動命令:
/usr/local/fastdfs/bin/restart.sh /usr/local/fastdfs/bin/fdfs_trackerd /etc/fdfs/tracker.conf
/usr/local/fastdfs/bin/restart.sh /usr/local/fastdfs/bin/fdfs_storaged /etc/fdfs/storage.conf
/usr/local/nginx/sbin/nginx -s reload
測試圖片上傳分發:
/usr/local/fastdfs/bin/fdfs_test /etc/fdfs/client.conf upload /tmp/1.png
做軟連接:
ln -s /home/storage/fastdfs/data /home/storage/fastdfs/data/M00
八,同步開發的svn的靜態資源到nginx
安裝svn客戶端:yum install -y svn
然後開始同步:svn checkout https://192.168.121.206/svn/Fusion/trunk/yueworld_static /usr/local/nginx/yueworld_static/
這個要輸入p同意checkout然後輸入連接svn的用戶名密碼
[root@mall_hc_244 yueworld_static]# svn checkout https://192.168.121.206/svn/Fusion/trunk/yueworld_static /usr/local/nginx/yueworld_static/
九,問題
1,問題1:
libtool: compile: unrecognized option `-DHAVE_CONFIG_H'
libtool: compile: Try `libtool --help' for more information.
make[1]: *** [pcrecpp.lo] Error 1
make[1]: Leaving directory `/home/guangbo/work/pcre-8.12'
make: *** [all] Error 2
缺少gcc-c++和libtool,也就是c++編譯包
解決方法:需要先安裝libtool和gcc-c++
#yum -y install libtool
#yum -y install gcc-c++
2,問題2error log:
2016/01/23 04:20:49 [error] 7875#0: *4 open() “/usr/local/nginx/html/group1/M00/00/00/wKh59FaijDCAF92yAAAWHOdjXIg765_big.png” failed (2: No such file or directory), client: 192.168.120.249, server: localhost, request: “GET /group1/M00/00/00/wKh59FaijDCAF92yAAAWHOdjXIg765_big.png HTTP/1.1”, host: “192.168.121.244”
access log:
192.168.120.249 - - [23/Jan/2016:04:20:49 +0800] “GET /group1/M00/00/00/wKh59FaijDCAF92yAAAWHOdjXIg765_big.png HTTP/1.1” 404 570 “-” “Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36”
有如下2個原因:
1),軟連接沒有創建
創建M00目錄的軟連接
[root@storage1]# ln -s /home/storage/fastdfs/data /home/storage/fastdfs/data/M00
2),nginx沒有生效,需要重新安裝
十,配置nginx.conf
worker_processes 8;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
server {
listen 80;
server_name www.nginxtest.com 192.168.121.102;
index index.html index.htm index.jsp index.do;
root /usr/local/nginx/html/myloan;
#配置Nginx動靜分離,定義的靜態頁面直接從Nginx發布目錄讀取。
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /usr/local/nginx/html/myloan;
#expires定義用戶浏覽器緩存的時間為7天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力
expires 7d;
}
#所有jsp、do的動態請求都交給後面的tomcat處理
location ~ (\.jsp)|(\.do)$
{
#tomcat地址
proxy_pass http://192.168.121.102:8080;
proxy_redirect off;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /usr/local/nginx/logs/access.log access;
}
server
{
listen 80;
server_name www.server.nginxtest.com;
location / {
stub_status on;
access_log off;
}
}
}