Nginx+fastDFS搭建图床服务端

准备工作

服务器:Linux(CentOS 7.x,Ubuntu)

Nginx安装

宝塔安装

通过宝塔面板编译安装 Nginx 1.15

Ubuntu源码安装

APT安装

1
sudo apt-get install nginx

源码安装

安装包:nginx-1.10.1.tar.gz

安装相关软件

安装gcc g++的依赖库(如果没有gcc)

1
2
3
sudo apt-get install build-essential
sudo apt-get install libtool
centos的话:yum install gcc-c++

安装pcre依赖库

1
2
3
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
centos的话:yum install -y pcre pcre-devel

安装zlib依赖库

1
2
sudo apt-get install zlib1g-dev
centos的话:yum install -y zlib zlib-devel

安装SSL依赖库(16.04默认已经安装了)

1
2
sudo apt-get install openssl
centos的话:yum install -y openssl openssl-devel
安装nginx

关于configure、make、make install

1
2
3
4
5
源码的安装一般由有这三个步骤:配置(configure)、编译(make)、安装(make install)

其中–prefix选项就是配置安装的路径,如果不配置该选项,安装后可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc,其它的资源文件放在/usr /local/share,比较分散。
为了便于集中管理某个软件的各种文件,可以配置–prefix,如:./configure –prefix=/usr/local 。可以把所有资源文件放在/usr/local的路径中,就不会分散了。
使用--prefix选项的另一个好处是方便卸载软件或移植软件;当某个安装的软件不再需要时,只须简单的删除该安装目录,就可以把软件卸载得干干净净;而移植软件只需拷贝整个目录到另外一个机器即可(相同的操作系统下)。

configure

1
2
首先检查机器的一些配置和环境,系统的相关依赖。如果缺少相关依赖,脚本会停止执行,软件安装失败
根据之前检查环境和依赖的结果,生产Makefile文件(main job)

make

1
2
make是Unix系统下的一个包。执行make命令需Makefile文件。make会根据Makefile文件中指令来安装软件
Makefile文件中有许多标签,来表示不同的section。一般的,make会编译源代码并生成可执行文件,其实Makefile主要就是描述文件编译的相互依赖关系

make install

1
2
3
当执行make命令不加任何参数,程序就会按照Makefile的指令在相应的section间跳转并且执行相应的命令
加上install参数即执行make install时,程序只会执行install section处的命令。install section的指令会将make阶段生产的可执行文件拷贝到相应的地方,例如/usr/local/bin
make clean 会删除上次make生产的obj文件以及可执行文件

配置软链接

1
sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

现在就可以不用路径直接输入nginx启动。

配置开机启动服务

在/etc/init.d/下创建nginx文件,sudo vim /etc/init.d/nginx,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/sh

### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/nginx/sbin/nginx
NAME=nginx
DESC=nginx

# Include nginx defaults if available
if [ -r /etc/default/nginx ]; then
. /etc/default/nginx
fi

STOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/5/TERM/5/KILL/5}"

test -x $DAEMON || exit 0

. /lib/init/vars.sh
. /lib/lsb/init-functions

# Try to extract nginx pidfile
PID=$(cat /usr/local/nginx/conf/nginx.conf | grep -Ev '^\s*#' | awk 'BEGIN { RS="[;{}]" } { if ($1 == "pid") print $2 }' | head -n1)
if [ -z "$PID" ]; then
PID=/run/nginx.pid
fi

if [ -n "$ULIMIT" ]; then
# Set ulimit if it is set in /etc/default/nginx
ulimit $ULIMIT
fi

start_nginx() {
# Start the daemon/service
#
# Returns:
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON -- \
$DAEMON_OPTS 2>/dev/null \
|| return 2
}

test_config() {
# Test the nginx configuration
$DAEMON -t $DAEMON_OPTS >/dev/null 2>&1
}

stop_nginx() {
# Stops the daemon/service
#
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=$STOP_SCHEDULE --pidfile $PID --name $NAME
RETVAL="$?"
sleep 1
return "$RETVAL"
}

reload_nginx() {
# Function that sends a SIGHUP to the daemon/service
start-stop-daemon --stop --signal HUP --quiet --pidfile $PID --name $NAME
return 0
}

rotate_logs() {
# Rotate log files
start-stop-daemon --stop --signal USR1 --quiet --pidfile $PID --name $NAME
return 0
}

upgrade_nginx() {
# Online upgrade nginx executable
# http://nginx.org/en/docs/control.html
#
# Return
# 0 if nginx has been successfully upgraded
# 1 if nginx is not running
# 2 if the pid files were not created on time
# 3 if the old master could not be killed
if start-stop-daemon --stop --signal USR2 --quiet --pidfile $PID --name $NAME; then
# Wait for both old and new master to write their pid file
while [ ! -s "${PID}.oldbin" ] || [ ! -s "${PID}" ]; do
cnt=`expr $cnt + 1`
if [ $cnt -gt 10 ]; then
return 2
fi
sleep 1
done
# Everything is ready, gracefully stop the old master
if start-stop-daemon --stop --signal QUIT --quiet --pidfile "${PID}.oldbin" --name $NAME; then
return 0
else
return 3
fi
else
return 1
fi
}

case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
start_nginx
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
stop_nginx
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
;;
restart)
log_daemon_msg "Restarting $DESC" "$NAME"

# Check configuration before stopping nginx
if ! test_config; then
log_end_msg 1 # Configuration error
exit $?
fi

stop_nginx
case "$?" in
0|1)
start_nginx
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
reload|force-reload)
log_daemon_msg "Reloading $DESC configuration" "$NAME"

# Check configuration before stopping nginx
#
# This is not entirely correct since the on-disk nginx binary
# may differ from the in-memory one, but that's not common.
# We prefer to check the configuration and return an error
# to the administrator.
if ! test_config; then
log_end_msg 1 # Configuration error
exit $?
fi

reload_nginx
log_end_msg $?
;;
configtest|testconfig)
log_daemon_msg "Testing $DESC configuration"
test_config
log_end_msg $?
;;
status)
status_of_proc -p $PID "$DAEMON" "$NAME" && exit 0 || exit $?
;;
upgrade)
log_daemon_msg "Upgrading binary" "$NAME"
upgrade_nginx
log_end_msg $?
;;
rotate)
log_daemon_msg "Re-opening $DESC log files" "$NAME"
rotate_logs
log_end_msg $?
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}" >&2
exit 3
;;
esac

-

1
2
3
4
5
#设置服务脚本有执行权限
sudo chmod +x /etc/init.d/nginx
#注册服务
cd /etc/init.d/
sudo update-rc.d nginx defaults

现在基本上就可以开机启动了,常用的命令如下:

1
sudo service nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}

fastDFS软件准备

fastdfs-5.10.tar.gz

libfastcommon-1.36.zip

fastdfs-nginx-module_v1.16.tar.gz

阿里云服务器准备

参考

https://www.cnblogs.com/guigujun/p/7804670.html

本人使用的是阿里云服务器,配置上和上面教程略有不同

阿里防火墙 开放端口 TCP 22122 23000 等,记得开放要用的端口,否则后面操作失败

由于有了阿里云防火墙,所以服务器上的防火墙基本都不想开了,因为如果开放一个端口,要改阿里防火墙和服务器防火墙 2个地方,太繁琐了,所以可以使用以下命令禁用服务器自身的防火墙

1
2
3
4
#关闭防火墙
systemctl stop firewalld.service
#禁止防火墙开机自启
systemctl disable firewalld.service

fastDFS安装

安装libfastcommon

使用软件上传所有FastDFS软件到服务器的 /home/package/fdfs目录上(目录自己建立)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cd /home/package/fdfs

# 解压所有的压缩包
unzip libfastcommon-1.0.36.zip
tar -zxvf fastdfs-5.10.tar.gz
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz

# 安装fastdfs的环境
cd libfastcommon-1.0.36
./make.sh
./make.sh install


#建立软链接(可省略)
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so

安装fastDFS

1
2
3
4
5
6
7
8
9
10
# 安装FastDFS
cd /home/package/fdfs/fastdfs-5.10
./make.sh
./make.sh install

#从样本中拷贝配置文件
cd /etc/fdfs/
cp client.conf.sample client.conf
cp storage.conf.sample storage.conf
cp tracker.conf.sample tracker.conf

配置tracker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 一并把后面要的所有目录创建
mkdir /home/fastdfs/tracker
mkdir /home/fastdfs/storage
mkdir /home/fastdfs/client


******************************************
# 配置文件 tracker
vim /etc/fdfs/tracker.conf
# 关注以下配置
disabled=false
port=22122 #默认端口号
base_path=/home/fastdfs/tracker #自己刚创建的目录
#http.server_port=8080 #默认端口是8080
#bind_addr 留空即可,不写则(外网内网)都可以访问
bind_addr=
#bind_addr=192.168.133.128 内网网址


# 查看支持命令 ,常用的有 start,stop,status
service fdfs_trackerd
# 启动tracker,这里显示ok还不行,还得看下面的监听端口看不看得到
service fdfs_trackerd start
#fdfs_trackerd /etc/fdfs/tracker.conf 同样可以启动

# 开机自启动
echo "service fdfs_trackerd start" |tee -a /etc/rc.d/rc.local

#查看tracker监听端口
# 这里必须看到 tracker的监听端口,否则请检查上面的操作
netstat -unltp|grep fdfs

配置storage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
*****************************
# 配置文件 storage
vim /etc/fdfs/storage.conf


# 注意下列配置
disabled=false
group_name=group1 #组名,根据实际情况修改
port=23000 #设置storage的端口号,默认是23000,同一个组的storage端口号必须一致
base_path=/home/fastdfs/storage #设置storage数据文件和日志目录
store_path_count=1 #存储路径个数,需要和store_path个数匹配
store_path0=/home/fastdfs/storage_data #实际文件存储路径
tracker_server=公网IP:22122 #阿里云服务器的公网IP
#bind_addr 留空即可,不写则(外网内网)都可以访问
bind_addr=
#bind_addr=192.168.133.128 内网网址
#http.server_port=8888 #设置 http 端口号


# 创建软引用
ln -s /usr/bin/fdfs_storaged /usr/local/bin

#启动storage ,这里ok还不行,必须看到storage的监听端口才行
service fdfs_storaged start
#fdfs_storaged /etc/fdfs/storage.conf

#开机自启动 storage
echo "service fdfs_storaged start" |tee -a /etc/rc.d/rc.local

# 看一下是否有tracker和storage的2个监听端口,否则请检查上面操作
netstat -unltp | grep fdfs

配置client

1
2
3
4
5
6
vim /etc/fdfs/client.conf

# 注意以下配置
base_path=/home/fastdfs/client #tracker服务器文件路径
tracker_server=公网IP:22122 #tracker服务器IP地址和端口号
#http.tracker_server_port=8080 # tracker 服务器的 http端口号,必须和tracker的设置对应起来

检验

检验测试

1
fdfs_moniter /etc/fdfs/client.conf

检验上传下载

1
2
3
4
5
6
7
8
9
10
11
12
13
# 图片 /home/temp/test.jpg

#上传图片
fdfs_upload_file /etc/fdfs/client.conf /home/temp/test.jpg

# 看到group1/M00/00/00字眼即成功了,这个信息后面有用
# group1/M00/00/00/rBEx3l5k7cmAW6eiAAGyE0VtDsM290.jpg

#下载图片
fdfs_download_file group1/M00/00/00/rBEx3l5k7cmAW6eiAAGyE0VtDsM290.jpg

#查看当前路径下有没有图片 rBEx3l5k7cmAW6eiAAGyE0VtDsM290.jpg
ls

fastdfs-nginx-module安装

Nginx相关

nginx安装

源码安装路径

1
cd /home/package/nginx/nginx-1.10.1/

安装

1
2
3
4
5
6
7
8
9
10
11
./configure --add-module=/home/package/fdfs_nginx_module/fastdfs-nginx-module/src

#跳转objs目录编辑Makefile添加头文件
vim ./objs/Makefile
# 添加以下语句
-I usr/include/fastdfs \
-I usr/include/fastcommon \

#最后编译安装
make
make install

宝塔nginx安装

默认安装脚本路径

1
/www/server/panel/install/nginx.sh

nginx源码位置

1
/www/server/nginx/src/

重新编译宝塔nginx

1
cd /www/server/nginx/src/
查看nginx编译参数
1
nginx -V

返回结果(我的)

1
--user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module
添加代码
1
--add-module=/home/package/fdfs_nginx_module/fastdfs-nginx-module/src
添加fastDFS模块(以上两部分整合)
1
--user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module --add-module=/home/package/fdfs_nginx_module/fastdfs-nginx-module/src
nginx重新编译
1
./configure --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module --add-module=/home/package/fdfs_nginx_module/fastdfs-nginx-module/src

make

1
2
fatal error: fdfs_define.h: No such file or directory 
fatal error: common_define.h: No such file or directory

vim 打开makefile目录下边的 objs/Makefile

在这个makefile中添加两个头文件

1
2
3
4
5
vim /www/server/nginx/src/objs/Makefile

# 添加以下语句
-I usr/include/fastdfs \
-I usr/include/fastcommon \

make install

未安装nginx

直接make install

已安装

复制编译后产生的nginx文件,替换旧的nginx文件(自己需提前备份)

1
cp /www/server/nginx/src/objs/nginx /www/server/nginx/sbin/

nginx启动

启动nginx, 只有一个master没有worker

查看日志文件

1
2
3
4
5
6
7
8
9
/usr/local/nginx/logs/error.log 

ERROR - file: shared_func.c, line: 968, file /etc/fdfs/mod_fastdfs.conf not exist
#拷贝fastdfs-nginx-module/src中的mod_fastdfs.conf到/etc/fdfs/中


ERROR - file: ini_file_reader.c, line: 631, include file "http.conf" not exists, line: "#include http.conf"
#从fastDFS的源码安装包中的conf中 将http.conf 拷贝到 /etc/fdfs/
#从nginx的源码安装包中的conf中mime.types拷贝到 /etc/fdfs/

mod_fastdfs.conf 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
base_path=/home/fastDFS/storage #log日志目录
tracker_server=公网:22122 #追踪器的地址
storage_server_port=23000 #当前存储节点的端口
group_name=group1 #当前存储节点所属的组
url_have_group_name = true #浏览器访问的时候, url中是否包含组名
store_path_count=1 #当前存储节点存储路径的个数
store_path1 store_path2 #如果有多个, 需要全部写到配置文件中
store_path0=/home/robin/fastDFS/storage #当前存储节点的存储路径
group_count = 1 #整个的fastDFS文件系统一共有多少个组
[group1] #每个组信息
group_name=group1
storage_server_port=23000
store_path_count=1
store_path0=/home/fastDFS/storage

nginx配置文件添加location

1
2
3
4
location /group1/M00{
root /home/fastdfs/storage/data;
ngx_fastdfs_module;
}

之前上传的图片路径为

1
group1/M00/00/00/rBEx3l5k7cmAW6eiAAGyE0VtDsM290.jpg

访问链接

http://39.96.209.253/group1/M00/00/00/rBEx3l5k7cmAW6eiAAGyE0VtDsM290.jpg


文章结束了,但我们的故事还在继续
坚持原创技术分享,您的支持将鼓励我继续创作!