一、安装 Nginx 和 fastdfs-nginx-module
1.下载 Nginx 和 fastdfs-nginx-module ,这里是通过wget下载(我喜欢这种方式)。
wget -c https://nginx.org/download/nginx-1.10.1.tar.gzwget https://github.com/happyfish100/fastdfs-nginx-module/archive/master.zip
2.解压 fastdfs-nginx-module ,记着这时候别用tar解压了,因为是 .zip 文件,正确命令:
unzip master.zip
3.配置 nginx 安装,加入fastdfs-nginx-module模块。这是和普通 Nginx 安装不一样的地方,因为加载了模块。
./configure --add-module=../fastdfs-nginx-module-master/src/但是,万万没想到,报错啦!有些文件显示not found错误。更多看下图,都是错误:
gcc、PCRE这些没找到,说明我们服务器并未安装,所以,我们进行下安装即可了,我在 从零开始学 Java - CentOS 下安装 Nginx 有gcc、 PCRE pcre-devel、 zlib、 OpenSSL的安装命令,都安装一下之后我们再次进行 Nginx 的安装,显示下面的界面就是安装成功啦!
使用以下命令进行安装
makemake install
这时候,我们可以看一下 Nginx 下安装成功的版本及模块,命令:
/usr/local/nginx/sbin/nginx -V
二、配置 fastdfs-nginx-module 和 Nginx
1.配置mod-fastdfs.conf,并拷贝到/etc/fdfs文件目录下。
cd /software/fastdfs-nginx-module-master/src/vim mod_fastdfs.confcp mod_fastdfs.conf /etc/fdfs
修改mod-fastdfs.conf配置只需要修改我标注的这三个地方就行了,其他不需要也不建议改变。
# 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 truetracker_server=192.168.198.129:22122# 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 falseurl_have_group_name = true# store_path#, based 0, if store_path0 not exists, it's value is base_path# the paths must be exist# must same as storage.confstore_path0=/data/fastdfs/storage#store_path1=/home/yuqing/fastdfs1
接着我们需要把fastdfs-5.05下面的配置中还没有存在/etc/fdfs中的拷贝进去
cd /software/fastdfs-5.05/confcp anti-steal.jpg http.conf mime.types /etc/fdfs/
2.配置 Nginx。编辑nginx.conf文件:
cd /usr/local/nginx/confvi nginx.conf
在配置文件中加入:
location /group1/M00 {root /data/fastdfs/storage/;ngx_fastdfs_module;}
由于我们配置了group1/M00的访问,我们需要建立一个group1文件夹,并建立M00到data的软链接。
mkdir /data/fastdfs/storage/data/group1ln -s /data/fastdfs/storage/data /data/fastdfs/storage/data/group1/M00
启动 Nginx ,会打印出fastdfs模块的pid,看看日志是否报错,正常不会报错的
/usr/local/nginx/sbin/nginx
开放80端口访问权限。在iptables中加入重启就行,或者你直接关闭防火墙,本地测试环境可以这么干,但到线上万万不能关闭防火墙的。
vi /etc/sysconfig/iptables-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
重启防火墙,使设置生效:
service iptables restart
我最后说一下怎么在已经安装过 Nginx 的服务器上安装配置 fastdfs-nginx-module 模块? 因为,一般我们线上服务器都是已经安装过 Nginx 的,所以这个时候,我们就直接进入 Nginx 的存放目录,进行配置后编译,就不需要执行最后安装make install这一步了,接着重启就行了。
上传测试
完成上面的步骤后,我们已经安装配置完成了全部工作,接下来就是测试了。因为执行文件全部在/usr/bin目录下,我们切换到这里,并新建一个test.txt文件,随便写一点什么,我写了This is a test file. by:mafly这句话在里边。然后测试上传:
cd /usr/binvim test.txtfdfs_test /etc/fdfs/client.conf upload test.txt
很不幸,并没有成功,报错了。
ERROR - file: shared_func.c, line: 960, open file /etc/fdfs/client.conf fail, errno: 2, error info: No such file or directoryERROR - file: ../client/client_func.c, line: 402, load conf file "/etc/fdfs/client.conf"fail, ret code: 2
一般什么事情第一次都不是很顺利,这很正常,通过错误提示我看到,好像没有找到client.conf这个文件,现在想起来的确没有配置这个文件,那我们现在去配置一下图中的两个参数:
cd /etc/fdfscp client.conf.sample client.confvim client.conf
怎么还依然报错阿???
upload file fail, error no: 2, error info: No such file or directory
哈哈,你是不是测试上传命令中要上传的test.txt文件路径有问题,嗯,那我改一下命令:
/usr/bin/fdfs_test /etc/fdfs/client.conf upload /usr/bin/test.txt
本文详细介绍了如何在CentOS环境下安装配置Nginx和fastdfs-nginx-module模块,并结合fastdfs实现文件的上传与访问。文章涵盖依赖安装、模块配置、防火墙设置等关键步骤。
2712

被折叠的 条评论
为什么被折叠?



