搭建单机版的FastDFS服务器

编程知识 更新时间:2023-05-03 01:28:58

参考文章 https://blog.csdn/u012453843/article/details/69951920#comments
https://blog.csdn/u012453843/article/details/69396434

        由于FastDFS集群搭建非常复杂,对于初期学习FastDFS来说,搭建个单机版的作为入门更为实际一些。

第一步:搭建虚拟环境

        FastDFS需要在Linux系统上安装,我们一般使用的都是Windows系统,这样就需要有虚拟环境,我这里使用Vmware12创建虚拟机,关于虚拟机最小化的安装大家可以参考:http://blog.csdn/u012453843/article/details/68947589这篇博客进行学习

第二步:配置静态IP而且要保证能上网

       大家可以参考:http://blog.csdn/u012453843/article/details/52839105这篇博客进行学习,需要提醒的是,由于最小化安装默认是没有vim命令的,因此在配置静态IP时使用vi命令即可。

第三步:安装vim命令

       之所以安装vim命令是因为在安装它的同时会自动帮我们安装perl,而perl在进行编译安装时是必须要有的。安装vim的命令是:yum install vim-enhanced

第四步:安装gcc

      使用命令:yum install make cmake gcc gcc-c++进行安装即可

第五步:上传安装文件到Linux服务器

      为了便于管理,我们在Linux服务器上新建一个/usr/local/software目录,专门用于存放安装包,如下所示


 
 
  1. [root@fastdfs ~]# mkdir /usr/local/software
  2. [root@fastdfs ~]#
       下面我们把需要用到的安装包都上传到/usr/local/software目录下,我们可以使用XShell和Xftp5相结合来方便的进行上传下载操作,大家可以参考http://blog.csdn/u012453843/article/details/68951776这篇博客进行学习。也可以通过安装rzsz命令来进行上传下载操作,安装rzsz的命令是yum install lrzsz,安装完之后,rz表示上传,sz 文件表示下载。

        大家可以到http://download.csdn/detail/u012453843/9802538这个地址下载需要的安装包,上传完之后,如下所示


 
 
  1. [root@fastdfs software]# ll
  2. 总用量 1248
  3. -rw-r--r--. 1 root root 17510 11月 28 00:19 fastdfs-nginx-module_v1.16.tar.gz
  4. -rw-r--r--. 1 root root 345400 11月 28 00:20 FastDFS_v5.05.tar.gz
  5. -rw-r--r--. 1 root root 102378 11月 28 00:19 libfastcommon-master.zip
  6. -rw-r--r--. 1 root root 804164 11月 28 01:11 nginx-1.6.2.tar.gz
  7. [root@fastdfs software]#

第六步:安装zip和unzip命令

        由于解压.zip结尾的文件需要用到unzip命令,因此我们需要安装,安装命令是yum install zip unzip

第七步:安装libfastcommon

       1、解压

[root@fastdfs software]# unzip libfastcommon-master.zip -d /usr/local/fast/
 
 
       2、进入目录


 
 
  1. [root@fastdfs software]# cd /usr/local/fast/libfastcommon-master/
  2. [root@fastdfs libfastcommon-master]# ll
  3. 总用量 28
  4. -rw-r--r--. 1 root root 2913 2月 27 2015 HISTORY
  5. -rw-r--r--. 1 root root 582 2月 27 2015 INSTALL
  6. -rw-r--r--. 1 root root 1342 2月 27 2015 libfastcommon.spec
  7. -rwxr-xr-x. 1 root root 2151 2月 27 2015 make.sh
  8. drwxr-xr-x. 2 root root 4096 2月 27 2015 php-fastcommon
  9. -rw-r--r--. 1 root root 617 2月 27 2015 README
  10. drwxr-xr-x. 2 root root 4096 2月 27 2015 src
  11. [root@fastdfs libfastcommon-master]#
      3、编译

[root@fastdfs libfastcommon-master]# ./make.sh
 
 
      4、安装

[root@fastdfs libfastcommon-master]# ./make.sh install
 
 

 第八步:创建软链接

       FastDFS主程序设置的目录为/usr/local/lib/,所以我们需要创建/ usr/lib64/下的一些核心执行程序的软连接文件。如下所示。


 
 
  1. [root@fastdfs libfastcommon-master]# ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
  2. [root@fastdfs libfastcommon-master]# ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
  3. [root@fastdfs libfastcommon-master]# ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
  4. [root@fastdfs libfastcommon-master]# ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
第九步:安装FastDFS

       1、进入到cd /usr/local/software下,解压FastDFS_v5.05.tar.gz文件 


 
 
  1. [root@fastdfs fast]# cd /usr/local/software/
  2. [root@fastdfs software]# ll
  3. 总用量 1248
  4. -rw-r--r--. 1 root root 17510 4月 11 03:28 fastdfs-nginx-module_v1.16.tar.gz
  5. -rw-r--r--. 1 root root 345400 4月 11 03:28 FastDFS_v5.05.tar.gz
  6. -rw-r--r--. 1 root root 102378 4月 11 03:28 libfastcommon-master.zip
  7. -rw-r--r--. 1 root root 804164 4月 11 03:29 nginx-1.6.2.tar.gz
  8. [root@fastdfs software]# tar -zxvf FastDFS_v5.05.tar.gz -C /usr/local/fast/
       2、编译安装


 
 
  1. [root@fastdfs software]# cd /usr/local/fast/FastDFS/
  2. [root@fastdfs FastDFS]# ./make.sh
  3. [root@fastdfs FastDFS]# ./make.sh install
      安装完后,服务脚本位置如下


 
 
  1. [root@fastdfs FastDFS]# cd /etc/init.d/ && ls | grep fdfs
  2. fdfs_storaged
  3. fdfs_trackerd
  4. [root@fastdfs init.d]#
       配置文件位置如下:


 
 
  1. [root@fastdfs init.d]# cd /etc/fdfs/
  2. [root@fastdfs fdfs]# ll
  3. 总用量 20
  4. -rw-r--r--. 1 root root 1461 4月 11 04:10 client.conf.sample
  5. -rw-r--r--. 1 root root 7829 4月 11 04:10 storage.conf.sample
  6. -rw-r--r--. 1 root root 7102 4月 11 04:10 tracker.conf.sample
  7. [root@fastdfs fdfs]#
       FastDFS一系列执行脚本如下,可以看到有上传文件脚本、下载文件脚本等等。


 
 
  1. [root@fastdfs fdfs]# cd /usr/bin/ && ls | grep fdfs
  2. fdfs_appender_test
  3. fdfs_appender_test1
  4. fdfs_append_file
  5. fdfs_crc32
  6. fdfs_delete_file
  7. fdfs_download_file
  8. fdfs_file_info
  9. fdfs_monitor
  10. fdfs_storaged
  11. fdfs_test
  12. fdfs_test1
  13. fdfs_trackerd
  14. fdfs_upload_appender
  15. fdfs_upload_file
  16. [root@fastdfs bin]#
      3、因为FastDFS服务脚本设置的bin目录为/usr/local/bin/下,但是实际我们安装在了/u sr/bin/下面。所以我们需要修改FastDFS配置文件中的路径,也就是需要修改两个配置文件

     使用命令vim /etc/init.d/fdfs_storaged进入编辑模式,然后直接输入":",光标会定位到最后一行,在":"后输入"%s+/usr/local/bin+/usr/bin",如下图所示。输入完之后回车,会提示修改了7处。为了确保所有的/usr/local/bin都被替换了,我们可以再打开文件确认一下。

       接着修改第二个配置文件,我们使用命令vim /etc/init.d/fdfs_trackerd进入编辑模式,接着按照上面那样输入":%s+/usr/local/bin+/usr/bin "并按回车,同样会提醒我们修改了7处。



第十步:配置跟踪器

     1、进入到/etc/fdfs目录并且复制一份tracker.conf.sample并命名为tracker.conf,如下所示。


 
 
  1. [root@fastdfs bin]# cd /etc/fdfs/
  2. [root@fastdfs fdfs]# ll
  3. 总用量 20
  4. -rw-r--r--. 1 root root 1461 4月 11 04:10 client.conf.sample
  5. -rw-r--r--. 1 root root 7829 4月 11 04:10 storage.conf.sample
  6. -rw-r--r--. 1 root root 7102 4月 11 04:10 tracker.conf.sample
  7. [root@fastdfs fdfs]# cp tracker.conf.sample tracker.conf
  8. [root@fastdfs fdfs]#
      2、使用命令vim /etc/fdfs/tracker.conf进入编辑模式,然后修改base_path的值为/fastdfs/tracker,如下图所示。

/
      3、我们在上图配置文件中配置的/fastdfs/tracker目前是不存在的,因此我们需要创建一下该目录


 
 
  1. [root@fastdfs fdfs]# mkdir -p /fastdfs/tracker
  2. [root@fastdfs fdfs]#

      4、配置防火墙,放开tracker使用的端口22122,使用命令vim /etc/sysconfig/iptables进入编辑模式,添加一行内容-A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT,如下图所示。

      5、重启防火墙


 
 
  1. [root@fastdfs fdfs]# service iptables restart
  2. iptables:将链设置为政策 ACCEPT:filter [确定]
  3. iptables:清除防火墙规则: [确定]
  4. iptables:正在卸载模块: [确定]
  5. iptables:应用防火墙规则: [确定]
  6. [root@fastdfs fdfs]#
      6、在启动tracker之前,/fastdfs/tracker目录下是没有任何文件的,如下所示

 
 
  1. [root@fastdfs tracker]# cd /fastdfs/tracker/ && ll
  2. 总用量 0
  3. [root@fastdfs tracker]#
       启动tracker,启动完之后,可以看到这个目录下多了两个目录data和logs。如下所示。


 
 
  1. [root@fastdfs tracker]# /etc/init.d/fdfs_trackerd start
  2. Starting FastDFS tracker server:
  3. [root@fastdfs tracker]# cd /fastdfs/tracker/ && ll
  4. 总用量 8
  5. drwxr-xr-x. 2 root root 4096 4月 11 05:01 data
  6. drwxr-xr-x. 2 root root 4096 4月 11 05:01 logs
  7. [root@fastdfs tracker]#
      7、设置开机自启动,在rc.local文件中添加/etc/init.d/fdfs_trackerd start,如下所示。


 
 
  1. [root@fastdfs tracker]# vim /etc/rc.d/rc.local
  2. #!/bin/sh
  3. #
  4. # This script will be executed *after* all the other init scripts.
  5. # You can put your own initialization stuff in here if you don't
  6. # want to do the full Sys V style init stuff.
  7. touch /var/lock/subsys/local
  8. /etc/init.d/fdfs_trackerd start
第十一步:配置FastDFS存储

     1、进入/etc/fdfs目录,复制一份storage.conf.sample文件并命名为storage.conf,如下所示。


 
 
  1. [root@fastdfs tracker]# cd /etc/fdfs/
  2. [root@fastdfs fdfs]# ll
  3. 总用量 28
  4. -rw-r--r--. 1 root root 1461 4月 11 04:10 client.conf.sample
  5. -rw-r--r--. 1 root root 7829 4月 11 04:10 storage.conf.sample
  6. -rw-r--r--. 1 root root 7098 4月 11 04:45 tracker.conf
  7. -rw-r--r--. 1 root root 7102 4月 11 04:10 tracker.conf.sample
  8. [root@fastdfs fdfs]# cp storage.conf.sample storage.conf
  9. [root@fastdfs fdfs]# ll
  10. 总用量 36
  11. -rw-r--r--. 1 root root 1461 4月 11 04:10 client.conf.sample
  12. -rw-r--r--. 1 root root 7829 4月 11 05:07 storage.conf
  13. -rw-r--r--. 1 root root 7829 4月 11 04:10 storage.conf.sample
  14. -rw-r--r--. 1 root root 7098 4月 11 04:45 tracker.conf
  15. -rw-r--r--. 1 root root 7102 4月 11 04:10 tracker.conf.sample
  16. [root@fastdfs fdfs]#
     2、修改storage.conf文件 ,我们使用命令vim /etc/fdfs/storage.conf进入编辑模式,对以下四项进行修改,192.168.156.13是我的虚拟机的IP,大家根据自己虚拟机的IP自行设置。


 
 
  1. base_path=/fastdfs/storage
  2. store_path0=/fastdfs/storage
  3. tracker_server=192.168.156.13:22122
  4. http.server_port=8888
      3、创建存储目录,如下所示。


 
 
  1. [root@fastdfs fdfs]# mkdir -p /fastdfs/storage
  2. [root@fastdfs fdfs]#
     4、配置防火墙,允许外界访问storage的默认端口23000,如下所示。


 
 
  1. [root@fastdfs fdfs]# vim /etc/sysconfig/iptables
  2. # Firewall configuration written by system-config-firewall
  3. # Manual customization of this file is not recommended.
  4. *filter
  5. :INPUT ACCEPT [0:0]
  6. :FORWARD ACCEPT [0:0]
  7. :OUTPUT ACCEPT [0:0]
  8. -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  9. -A INPUT -p icmp -j ACCEPT
  10. -A INPUT -i lo -j ACCEPT
  11. -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
  12. -A INPUT -m state --state NEW -m tcp -p tcp --dport 22122 -j ACCEPT
  13. -A INPUT -m state --state NEW -m tcp -p tcp --dport 23000 -j ACCEPT
  14. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  15. -A FORWARD -j REJECT --reject-with icmp-host-prohibited
  16. COMMIT
       添加完之后,重启防火墙,如下所示。


 
 
  1. [root@fastdfs fdfs]# service iptables restart
  2. iptables:将链设置为政策 ACCEPT:filter [确定]
  3. iptables:清除防火墙规则: [确定]
  4. iptables:正在卸载模块: [确定]
  5. iptables:应用防火墙规则: [确定]
  6. [root@fastdfs fdfs]#
       5、在启动storage之前,/fastdbf/storage目录下是没有任何文件的


 
 
  1. [root@fastdfs fdfs]# cd /fastdfs/storage/ && ll
  2. 总用量 0
  3. [root@fastdfs storage]#
       启动storage,启动后再看/fastdfs/storage目录,可以看到多了data和logs。


 
 
  1. [root@fastdfs storage]# /etc/init.d/fdfs_storaged start
  2. Starting FastDFS storage server:
  3. [root@fastdfs storage]# cd /fastdfs/storage/ && ll
  4. 总用量 8
  5. drwxr-xr-x. 68 root root 4096 4月 11 05:21 data
  6. drwxr-xr-x. 2 root root 4096 4月 11 05:20 logs
  7. [root@fastdfs storage]#
      6、查看FastDFS tracker和storage 是否启动成功,当看到如下所示信息时说明都启动成功了。


 
 
  1. [root@fastdfs storage]# ps -ef | grep fdfs
  2. root 2124 1 0 05:01 ? 00:00:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
  3. root 2228 1 0 05:21 ? 00:00:00 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf
  4. root 2238 1360 0 05:22 pts/0 00:00:00 grep fdfs
  5. [root@fastdfs storage]#
      7、我们进入到 /fastdfs/storage/data/目录下,可以看到两级共256*256个目录,每级都是从00到FF,如下只是列出了第一级的目录,点进去每个目录都还有00到FF共256个目录。


 
 
  1. [root@fastdfs storage]# cd /fastdfs/storage/data/ && ls
  2. 00 09 12 1B 24 2D 36 3F 48 51 5A 63 6C 75 7E 87 90 99 A2 AB B4 BD C6 CF D8 E1 EA F3 FC
  3. 01 0A 13 1C 25 2E 37 40 49 52 5B 64 6D 76 7F 88 91 9A A3 AC B5 BE C7 D0 D9 E2 EB F4 FD
  4. 02 0B 14 1D 26 2F 38 41 4A 53 5C 65 6E 77 80 89 92 9B A4 AD B6 BF C8 D1 DA E3 EC F5 fdfs_storaged.pid
  5. 03 0C 15 1E 27 30 39 42 4B 54 5D 66 6F 78 81 8A 93 9C A5 AE B7 C0 C9 D2 DB E4 ED F6 FE
  6. 04 0D 16 1F 28 31 3A 43 4C 55 5E 67 70 79 82 8B 94 9D A6 AF B8 C1 CA D3 DC E5 EE F7 FF
  7. 05 0E 17 20 29 32 3B 44 4D 56 5F 68 71 7A 83 8C 95 9E A7 B0 B9 C2 CB D4 DD E6 EF F8 storage_stat.dat
  8. 06 0F 18 21 2A 33 3C 45 4E 57 60 69 72 7B 84 8D 96 9F A8 B1 BA C3 CC D5 DE E7 F0 F9 sync
  9. 07 10 19 22 2B 34 3D 46 4F 58 61 6A 73 7C 85 8E 97 A0 A9 B2 BB C4 CD D6 DF E8 F1 FA
  10. 08 11 1A 23 2C 35 3E 47 50 59 62 6B 74 7D 86 8F 98 A1 AA B3 BC C5 CE D7 E0 E9 F2 FB
  11. [root@fastdfs data]#
     8、设置storage开机自启动,添加一行/etc/init.d/fdfs_storaged start,如下所示。


 
 
  1. [root@fastdfs data]# vim /etc/rc.local
  2. #!/bin/sh
  3. #
  4. # This script will be executed *after* all the other init scripts.
  5. # You can put your own initialization stuff in here if you don't
  6. # want to do the full Sys V style init stuff.
  7. touch /var/lock/subsys/local
  8. /etc/init.d/fdfs_trackerd start
  9. /etc/init.d/fdfs_storaged start
第十二步:测试图片上传

      1、进入到/etc/fdfs目录下并复制一份client.conf.sample并更名为client.conf,如下所示。


 
 
  1. [root@fastdfs data]# cd /etc/fdfs
  2. [root@fastdfs fdfs]# ll
  3. 总用量 36
  4. -rw-r--r--. 1 root root 1461 4月 11 04:10 client.conf.sample
  5. -rw-r--r--. 1 root root 7820 4月 11 05:12 storage.conf
  6. -rw-r--r--. 1 root root 7829 4月 11 04:10 storage.conf.sample
  7. -rw-r--r--. 1 root root 7098 4月 11 04:45 tracker.conf
  8. -rw-r--r--. 1 root root 7102 4月 11 04:10 tracker.conf.sample
  9. [root@fastdfs fdfs]# cp client.conf.sample client.conf
  10. [root@fastdfs fdfs]# ll
  11. 总用量 40
  12. -rw-r--r--. 1 root root 1461 4月 11 05:40 client.conf
  13. -rw-r--r--. 1 root root 1461 4月 11 04:10 client.conf.sample
  14. -rw-r--r--. 1 root root 7820 4月 11 05:12 storage.conf
  15. -rw-r--r--. 1 root root 7829 4月 11 04:10 storage.conf.sample
  16. -rw-r--r--. 1 root root 7098 4月 11 04:45 tracker.conf
  17. -rw-r--r--. 1 root root 7102 4月 11 04:10 tracker.conf.sample
  18. [root@fastdfs fdfs]#
     2、使用命令vim /etc/fdfs/client.conf进入编辑模式并修改如下两项内容,如下所示。


 
 
  1. base_path=/fastdfs/tracker
  2. tracker_server=192.168.156.13:22122
     3、我们找到命令的脚本位置,并且使用命令,进行文件的上传。


 
 
  1. [root@fastdfs bin]# cd /usr/bin/ && ls | grep fdfs
  2. fdfs_appender_test
  3. fdfs_appender_test1
  4. fdfs_append_file
  5. fdfs_crc32
  6. fdfs_delete_file
  7. fdfs_download_file
  8. fdfs_file_info
  9. fdfs_monitor
  10. fdfs_storaged
  11. fdfs_test
  12. fdfs_test1
  13. fdfs_trackerd
  14. fdfs_upload_appender
  15. fdfs_upload_file
  16. [root@fastdfs bin]#
       下面使用fdfs_upload_file脚本进行文件上传操作,如下所示。可以看到已经上传成功了,返回的是图片的保存位置:group1/M00/00/00/wKicDVjr_ayAE4VVAAHk-VzqZ6w020.jpg


 
 
  1. [root@fastdfs bin]# /usr/bin/fdfs_upload_file /etc/fdfs/client.conf /usr/local/software/3.jpg
  2. group1/M00/00/00/wKicDVjr_ayAE4VVAAHk-VzqZ6w020.jpg
  3. [root@fastdfs bin]#
第十三步:FastDFS与nginx相结合

     1、先安装nginx,大家可以参考http://blog.csdn/u012453843/article/details/69396434这篇博客的第四步Nginx安装(我们已经安装过的vim、gcc等就不需要重复安装了)。

一、Nginx概念        

       Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

二、负载均衡策略

       1、使用硬件复杂均衡策略实现,如使用F5、Array等负载均衡器

       2、使用软件进行负载均衡

            2.1 使用阿里云服务器均衡负载SLB

           2.2 使用Nginx+Keepalived

            2.3 其它软件负载均衡如LVS(Linux Virtual Server)、haproxy等技术。

三、Nginx的优点

      1、Nginx 可以在大多数 UnixLinux OS 上编译运行,并有 Windows 移植版。 Nginx 的1.4.0稳定版已经于2013年4月24日发布,一般情况下,对于新建站点,建议使用最新稳定版作为生产版本,已有站点的升级急迫性不高。Nginx 的源代码使用 2-clause BSD-like license。
      2、Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性:
在连接高并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型。

四、Nginx安装

       如果大家是刚新建的虚拟机并且最小化安装的话,需要先配置静态IP并且要能上网,大家可以参考:http://blog.csdn/u012453843/article/details/52839105这篇博客进行学习,编辑文章用vi即可。配置好静态IP并且能上网之后需要先安装wget、vim和gcc。


 
 
  1. yum install wget
  2. yum install vim-enhanced
  3. yum install make cmake gcc gcc-c++

       1、下载nginx安装包


 
 
  1. [root@nginx1 ~]# wget http://nginx/download/nginx-1.6.2.tar.gz
  2. --2017-04-07 01:44:55-- http://nginx/download/nginx-1.6.2.tar.gz
  3. 正在解析主机 nginx... 206.251.255.63, 95.211.80.227, 2606:7100:1:69::3f, ...
  4. 正在连接 nginx|206.251.255.63|:80... 已连接。
  5. 已发出 HTTP 请求,正在等待回应... 200 OK
  6. 长度:804164 (785K) [application/octet-stream]
  7. 正在保存至: “nginx-1.6.2.tar.gz”

      2、安装依赖,其中pcre(perl compatible regular expressions)是一个pert库,包括perl兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。pcre-devel是使用pcre开发的一个二次库,nginx也需要此库。zlib库提供了很多种压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。openssl是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的秘钥和证书封装管理功能及SSL协议,并提供丰富的应用程序提供测试或其它目的的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

yum install -y pcre pcre-devel
 
 
yum install -y zlib zlib-devel

 
 
yum install -y openssl openssl-devel
 
 
         3、解压nginx-1.6.2.tar.gz到/usr/local/目录下

[root@nginx1 ~]# tar -zxvf nginx-1.6.2.tar.gz -C /usr/local/
 
 

         4、进入到/usr/local目录下,可以看到我们解压后的nginx-1.6.2文件夹了,然后我们进行configure配置,命令:cd nginx-1.6.2 && ./configure --prefix=/usr/local/nginx。可以看出,这条命令是组合命令,先进入nginx-1.6.2目录然后在执行./configure命令。如下图所示。

          5、编译安装

[root@nginx1 nginx-1.6.2]# make && make install
 
 
          6、启动Nginx,启动完之后检查nginx是否已经正常启动,看到如下信息说明正常启动。


 
 
  1. [root@nginx1 nginx-1.6.2]# /usr/local/nginx/sbin/nginx
  2. [root@nginx1 nginx-1.6.2]# ps -ef | grep nginx
  3. root 3640 1 0 04:40 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  4. nobody 3641 3640 0 04:40 ? 00:00:00 nginx: worker process
  5. root 3643 1368 0 04:40 pts/0 00:00:00 grep nginx
  6. [root@nginx1 nginx-1.6.2]#
          如果要关闭nginx,我们可以使用如下命令:

[root@nginx1 nginx-1.6.2]# /usr/local/nginx/sbin/nginx -s stop
 
 
          如果想要重新热启动nginx,则使用如下命令:

[root@nginx1 nginx-1.6.2]# /usr/local/nginx/sbin/nginx -s reload
 
 
          7、nginx默认的端口是80,我们需要在防火墙配置中添加80端口:"-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT"从而可以让外界访问80端口,否则防火墙将禁止外界访问80端口。如下所示。


 
 
  1. [root@nginx1 nginx-1.6.2]# vim /etc/sysconfig/iptables
  2. # Firewall configuration written by system-config-firewall
  3. # Manual customization of this file is not recommended.
  4. *filter
  5. :INPUT ACCEPT [0:0]
  6. :FORWARD ACCEPT [0:0]
  7. :OUTPUT ACCEPT [0:0]
  8. -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  9. -A INPUT -p icmp -j ACCEPT
  10. -A INPUT -i lo -j ACCEPT
  11. -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
  12. -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
  13. -A INPUT -j REJECT --reject-with icmp-host-prohibited
  14. -A FORWARD -j REJECT --reject-with icmp-host-prohibited
  15. COMMIT
          添加完80端口之后,我们重启防火墙,如下所示。


 
 
  1. [root@nginx1 nginx-1.6.2]# service iptables restart
  2. iptables:将链设置为政策 ACCEPT:filter [确定]
  3. iptables:清除防火墙规则: [确定]
  4. iptables:正在卸载模块: [确定]
  5. iptables:应用防火墙规则: [确定]

          8、通过浏览器访问nginx欢迎页,我们在地址栏输入:http://192.168.156.11/(80端口不用输也可以)或http://192.168.156.11:80/,如下图所示。


五、学习nginx配置

       1、当启动过nginx之后,我们到/usr/local/nginx目录下,可以看到有conf、html、logs、sbin四个文件这四个文件是刚解压后就有的,是nginx重要的文件,还可以看到几个_temp结尾的文件,这些都是启动后生成的文件,我们暂且不用去管它们。我们使用cd conf命令进入conf目录,该目录下有个nginx.conf文件,这是nginx最重要的文件,我们修改nginx就是修改该文件,如下所示。


 
 
  1. [root@nginx1 nginx-1.6.2]# cd /usr/local/nginx
  2. [root@nginx1 nginx]# ll
  3. 总用量 36
  4. drwx------. 2 nobody root 4096 4月 7 05:16 client_body_temp
  5. drwxr-xr-x. 2 root root 4096 4月 7 04:37 conf
  6. drwx------. 2 nobody root 4096 4月 7 05:16 fastcgi_temp
  7. drwxr-xr-x. 2 root root 4096 4月 7 04:37 html
  8. drwxr-xr-x. 2 root root 4096 4月 7 05:16 logs
  9. drwx------. 2 nobody root 4096 4月 7 05:16 proxy_temp
  10. drwxr-xr-x. 2 root root 4096 4月 7 04:37 sbin
  11. drwx------. 2 nobody root 4096 4月 7 05:16 scgi_temp
  12. drwx------. 2 nobody root 4096 4月 7 05:16 uwsgi_temp
  13. [root@nginx1 nginx]# cd conf
  14. [root@nginx1 conf]# ll
  15. 总用量 60
  16. -rw-r--r--. 1 root root 1034 4月 7 04:37 fastcgi.conf
  17. -rw-r--r--. 1 root root 1034 4月 7 04:37 fastcgi.conf.default
  18. -rw-r--r--. 1 root root 964 4月 7 04:37 fastcgi_params
  19. -rw-r--r--. 1 root root 964 4月 7 04:37 fastcgi_params.default
  20. -rw-r--r--. 1 root root 2837 4月 7 04:37 koi-utf
  21. -rw-r--r--. 1 root root 2223 4月 7 04:37 koi-win
  22. -rw-r--r--. 1 root root 3957 4月 7 04:37 mime.types
  23. -rw-r--r--. 1 root root 3957 4月 7 04:37 mime.types.default
  24. -rw-r--r--. 1 root root 2656 4月 7 04:37 nginx.conf
  25. -rw-r--r--. 1 root root 2656 4月 7 04:37 nginx.conf.default
  26. -rw-r--r--. 1 root root 596 4月 7 04:37 scgi_params
  27. -rw-r--r--. 1 root root 596 4月 7 04:37 scgi_params.default
  28. -rw-r--r--. 1 root root 623 4月 7 04:37 uwsgi_params
  29. -rw-r--r--. 1 root root 623 4月 7 04:37 uwsgi_params.default
  30. -rw-r--r--. 1 root root 3610 4月 7 04:37 win-utf
  31. [root@nginx1 conf]#
         nginx.conf文件的内容如下( 注:我把注释给加上了):


 
 
  1. #user nobody;
  2. #开启进程数 <=CPU数
  3. worker_processes 1;
  4. #错误日志保存位置
  5. #error_log logs/error.log;
  6. #error_log logs/error.log notice;
  7. #error_log logs/error.log info;
  8. #进程号保存文件
  9. #pid logs/nginx.pid;
  10. #每个进程最大连接数(最大连接=连接数x进程数)每个worker允许同时产生多少个链接,默认1024
  11. events {
  12. worker_connections 1024;
  13. }
  14. http {
  15. #文件扩展名与文件类型映射表
  16. include mime.types;
  17. #默认文件类型
  18. default_type application/octet-stream;
  19. #日志文件输出格式 这个位置相于全局设置
  20. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  21. # '$status $body_bytes_sent "$http_referer" '
  22. # '"$http_user_agent" "$http_x_forwarded_for"';
  23. #请求日志保存位置
  24. #access_log logs/access.log main;
  25. #打开发送文件
  26. sendfile on;
  27. #tcp_nopush on;
  28. #keepalive_timeout 0;
  29. #连接超时时间
  30. keepalive_timeout 65;
  31. #打开gzip压缩
  32. #gzip on;
  33. server {
  34. #监听端口,默认是80端口
  35. listen 80;
  36. #监听域名
  37. server_name localhost;
  38. #charset koi8-r;
  39. #nginx访问日志放在logs/host.access.log下,并且使用main格式(还可以自定义格式)
  40. #access_log logs/host.access.log main;
  41. #如果没有location更明确的匹配访问路径的话,访问请求都会被该location处理。
  42. location / {
  43. #root指定nginx的根目录为/usr/local/nginx/html
  44. root html;
  45. #默认访问文件,欢迎页先去html目录下找index.html,如果找不到再去找index.htm
  46. index index.html index.htm;
  47. }
  48. #error_page 404 /404.html;
  49. # redirect server error pages to the static page /50x.html
  50. #
  51. #错误页面及其返回地址,错误码为500502503504都会返回50.html错误页面。
  52. error_page 500 502 503 504 /50x.html;
  53. #location后面是"="的话,说明是精确匹配
  54. location = /50x.html {
  55. root html;
  56. }
  57. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  58. #
  59. #location ~ \.php$ {
  60. # proxy_pass http://127.0.0.1;
  61. #}
  62. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  63. #
  64. #location ~ \.php$ {
  65. # root html;
  66. # fastcgi_pass 127.0.0.1:9000;
  67. # fastcgi_index index.php;
  68. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  69. # include fastcgi_params;
  70. #}
  71. # deny access to .htaccess files, if Apache's document root
  72. # concurs with nginx's one
  73. #
  74. #location ~ /\.ht {
  75. # deny all;
  76. #}
  77. }
  78. # another virtual host using mix of IP-, name-, and port-based configuration
  79. #
  80. #server {
  81. # listen 8000;
  82. # listen somename:8080;
  83. # server_name somename alias another.alias;
  84. # location / {
  85. # root html;
  86. # index index.html index.htm;
  87. # }
  88. #}
  89. # HTTPS server
  90. #
  91. #server {
  92. # listen 443 ssl;
  93. # server_name localhost;
  94. # ssl_certificate cert.pem;
  95. # ssl_certificate_key cert.key;
  96. # ssl_session_cache shared:SSL:1m;
  97. # ssl_session_timeout 5m;
  98. # ssl_ciphers HIGH:!aNULL:!MD5;
  99. # ssl_prefer_server_ciphers on;
  100. # location / {
  101. # root html;
  102. # index index.html index.htm;
  103. # }
  104. #}
  105. }
       其实那些注释掉的信息我们都可以删掉,这样看起来就会很简洁明了了。配置文件里可以添加多个server,server监听的端口不同,我们可以根据需要让nginx代理多个端口,当访问某个端口的时候,指定去做某些事情。我这里添加了一个server,这个server监听的端口为1234,server_name我指定为了test,也就是域名为test,当访问1234端口时会自动导航到/usr/local/nginx/tester/tester111.html页面,如下所示。


 
 
  1. #user nobody;
  2. #开启进程数 <=CPU数
  3. worker_processes 1;
  4. #错误日志保存位置
  5. #error_log logs/error.log;
  6. #error_log logs/error.log notice;
  7. #error_log logs/error.log info;
  8. #进程号保存文件
  9. #pid logs/nginx.pid;
  10. #每个进程最大连接数(最大连接=连接数x进程数)每个worker允许同时产生多少个链接,默认1024
  11. events {
  12. worker_connections 1024;
  13. }
  14. http {
  15. #文件扩展名与文件类型映射表
  16. include mime.types;
  17. #默认文件类型
  18. default_type application/octet-stream;
  19. #日志文件输出格式 这个位置相于全局设置
  20. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  21. # '$status $body_bytes_sent "$http_referer" '
  22. # '"$http_user_agent" "$http_x_forwarded_for"';
  23. #请求日志保存位置
  24. #access_log logs/access.log main;
  25. #打开发送文件
  26. sendfile on;
  27. #tcp_nopush on;
  28. #keepalive_timeout 0;
  29. #连接超时时间
  30. keepalive_timeout 65;
  31. #打开gzip压缩
  32. #gzip on;
  33. server {
  34. #监听端口
  35. listen 80;
  36. #监听域名
  37. server_name localhost;
  38. #charset koi8-r;
  39. #nginx访问日志放在logs/host.access.log下,并且使用main格式(还可以自定义格式)
  40. #access_log logs/host.access.log main;
  41. #如果没有location更明确的匹配访问路径的话,访问请求都会被该location处理。
  42. location / {
  43. #root指定nginx的根目录为/usr/local/nginx/html
  44. root html;
  45. #默认访问文件,欢迎页先去html目录下找index.html,如果找不到再去找index.htm
  46. index index.html index.htm;
  47. }
  48. #error_page 404 /404.html;
  49. # redirect server error pages to the static page /50x.html
  50. #
  51. #错误页面及其返回地址,错误码为500502503504都会返回50.html错误页面。
  52. error_page 500 502 503 504 /50x.html;
  53. #location后面是"="的话,说明是精确匹配
  54. location = /50x.html {
  55. root html;
  56. }
  57. server {
  58. listen 1234;
  59. server_name test;
  60. location / {
  61. #正则表达式匹配uri方式:在/usr/local/nginx/tester下 建立一个tester111.html 然后使用正则匹配
  62. root tester;
  63. index tester111.html;
  64. }
  65. }
  66. }
  67. }
       我们需要在/usr/local/nginx/目录下新建tester目录,在tester目录下新建一个tester111.html文件,在文件中随便写段html代码主要是为了判断是否访问的是我们写的html页面。如下所示。


 
 
  1. [root@nginx1 nginx]# mkdir tester
  2. [root@nginx1 nginx]# ll
  3. 总用量 40
  4. drwx------. 2 nobody root 4096 4月 7 05:16 client_body_temp
  5. drwxr-xr-x. 2 root root 4096 4月 7 06:32 conf
  6. drwx------. 2 nobody root 4096 4月 7 05:16 fastcgi_temp
  7. drwxr-xr-x. 2 root root 4096 4月 7 04:37 html
  8. drwxr-xr-x. 2 root root 4096 4月 7 05:16 logs
  9. drwx------. 2 nobody root 4096 4月 7 05:16 proxy_temp
  10. drwxr-xr-x. 2 root root 4096 4月 7 04:37 sbin
  11. drwx------. 2 nobody root 4096 4月 7 05:16 scgi_temp
  12. drwxr-xr-x. 2 root root 4096 4月 7 06:37 tester
  13. drwx------. 2 nobody root 4096 4月 7 05:16 uwsgi_temp
  14. [root@nginx1 nginx]# cd tester/
  15. [root@nginx1 tester]# vim tester111.html
  16. <html>
  17. <body>Hello This is Nginx Test! </body>
  18. </html>
         下面我们需要在防火墙中添加1234端口从而让浏览器可以访问1234端口,这个步骤在上面写的清楚了,这里就不再啰嗦了。

         在防火墙配置中添加完1234端口后,我们需要重启nginx,如下所示。


 
 
  1. [root@nginx1 nginx]# /usr/local/nginx/sbin/nginx -s reload
  2. [root@nginx1 nginx]# ps -ef|grep nginx
  3. root 3654 1 0 05:16 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  4. nobody 3870 3654 0 06:47 ? 00:00:00 nginx: worker process
  5. root 3872 1368 0 06:47 pts/0 00:00:00 grep nginx
  6. [root@nginx1 nginx]#
          由于我们在配置文件中配置的域名test并不是真实存在的,我们要想让windows系统识别该“域名”,我们需要修改下HOST文件,如下图所示。


          下面我们在地址栏输入:http://test:1234/便可以访问到我们创建的tester111.html文件的内容了,如下图所示,这说明我们配置的server是生效的。



          2.下面我们再来说下logs,nginx的log文件存在的目录为/usr/local/nginx/logs目录下,在logs目录下有三个文件,分别是access.log,error.log,nginx.pid,access.log很明显,就是记录浏览器访问的记录,error.log就是记录错误日志,nginx.pid则是记录当前的进程号,如下所示。


 
 
  1. [root@nginx1 conf]# cd /usr/local/nginx/
  2. [root@nginx1 nginx]# ll
  3. 总用量 40
  4. drwx------. 2 nobody root 4096 4月 7 05:16 client_body_temp
  5. drwxr-xr-x. 2 root root 4096 4月 7 07:00 conf
  6. drwx------. 2 nobody root 4096 4月 7 05:16 fastcgi_temp
  7. drwxr-xr-x. 2 root root 4096 4月 7 04:37 html
  8. drwxr-xr-x. 2 root root 4096 4月 7 05:16 logs
  9. drwx------. 2 nobody root 4096 4月 7 05:16 proxy_temp
  10. drwxr-xr-x. 2 root root 4096 4月 7 04:37 sbin
  11. drwx------. 2 nobody root 4096 4月 7 05:16 scgi_temp
  12. drwxr-xr-x. 2 root root 4096 4月 7 06:45 tester
  13. drwx------. 2 nobody root 4096 4月 7 05:16 uwsgi_temp
  14. [root@nginx1 nginx]# cd logs/
  15. [root@nginx1 logs]# ll
  16. 总用量 16
  17. -rw-r--r--. 1 root root 4121 4月 7 07:01 access.log
  18. -rw-r--r--. 1 root root 3435 4月 7 07:01 error.log
  19. -rw-r--r--. 1 root root 5 4月 7 05:16 nginx.pid
  20. [root@nginx1 logs]#
          我们可以查看下nginx.pid文件的内容,然后再查看当前nginx的进程,发现两个进程号都是3654。这个文件的一个用处就是当需要杀掉当前nginx进程的时候,可以使用shell脚本去nginx.pid文件中获取到该进程号然后再使用kill -9 pid进程号命令杀掉nginx。


 
 
  1. [root@nginx1 logs]# cat nginx.pid
  2. 3654
  3. [root@nginx1 logs]# ps -ef | grep nginx
  4. root 3654 1 0 05:16 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  5. nobody 4085 3654 0 07:01 ? 00:00:00 nginx: worker process
  6. root 4090 1368 0 07:23 pts/0 00:00:00 grep nginx
  7. [root@nginx1 logs]#
          logs目录下的access.log和error.log两个文件都是在nginx启动的时候自动生成的, 我们不能删掉nginx.pid否则启动nginx会报错!!如果你人为删掉access.log和error.log文件,需要重启nginx才能重新生成,访问页面是无法生成这两个文件的。

 
 
  1. [root@nginx1 logs]# ls
  2. nginx.pid
  3. [root@nginx1 logs]# ls
  4. nginx.pid
  5. [root@nginx1 logs]# /usr/local/nginx/sbin/nginx -s reload
  6. [root@nginx1 logs]# ll
  7. 总用量 8
  8. -rw-r--r--. 1 root root 0 4月 7 07:30 access.log
  9. -rw-r--r--. 1 root root 60 4月 7 07:30 error.log
  10. -rw-r--r--. 1 root root 5 4月 7 05:16 nginx.pid
  11. [root@nginx1 logs]#
        我们可以在不同的server配置不同的log文件,我在80端口所在的server还是使用默认的这个access.log,在1234端口所在的server使用test.log。如下图所示。



       为了不让原来log文件中的内容影响我们的结果,我们先把access.log和error.log文件删除,由于修改了配置文件并且要生成log文件,因此我们需要重启nginx,重启后,我们到/usr/local/nginx/logs/目录下查看生成的日志文件,如下所示。可以看到已经生成我说的几个日志文件了,这时候日志文件都是空的。


 
 
  1. [root@nginx1 conf]# /usr/local/nginx/sbin/nginx -s reload
  2. [root@nginx1 conf]# ps -ef | grep nginx
  3. root 3654 1 0 05:16 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
  4. nobody 4141 3654 0 08:01 ? 00:00:00 nginx: worker process
  5. root 4143 1368 0 08:02 pts/0 00:00:00 grep nginx
  6. [root@nginx1 conf]# cd /usr/local/nginx/logs/
  7. [root@nginx1 logs]# ll
  8. 总用量 8
  9. -rw-r--r--. 1 root root 0 4月 7 08:01 access.log
  10. -rw-r--r--. 1 root root 60 4月 7 08:01 error.log
  11. -rw-r--r--. 1 root root 5 4月 7 05:16 nginx.pid
  12. -rw-r--r--. 1 root root 0 4月 7 08:01 test.log
  13. [root@nginx1 logs]#
         我们可以在浏览器访问一下1234端口对应的server,访问之后,我们查看test.log文件,可以看到一条访问记录,提示IP为192.168.156.100(我本机配置与虚拟机同一网段的IP)访问了一次该服务。


 
 
  1. [root@nginx1 logs]# cat test.log
  2. 192.168.156.100 - - [07/Apr/2017:08:04:23 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; LCTE)" "-"
  3. [root@nginx1 logs]#
         我们再访问下80端口服务,然后查看access.log文件,如下图所示,也查看到了我们的访问记录。由于浏览器有缓存,当我们第二次或更多次刷新页面的时候日志不会累加。想要每次刷新页面都有日志信息的话,可以设置让浏览器不保存缓存信息,大家可以参考http://blog.csdn/u012453843/article/details/69499563这篇博客进行设置。


         下面来说下日志切割,我们在日常生活中,对nginx日志的分析非常的重要,通常需要运维去对nginx的日志进行切割和分析处理。比如实现一个定时任务,去处理nginx日志等。为什么要进行日志拆分?这是因为对于一个访问量大的网站来说,日志的增长是非常快的,如果都放在一个文件当中,查看将非常不方便,不利于问题的解决,我们根据公司的实际情况定时对日志进行拆分,像天猫、京东这样的大型互联网站,每隔多少分钟可能就需要进行一次拆分,对于一般的公司来说,可以每小时或者每天拆分一次。

          分割日志,我们肯定不能靠人工去分割,我们采用的是shell脚本来进行分割,做法就是先把日志文件移动到另一个地方并根据时间命名,然后再重新生成一个新的日志文件。由于需要定时做分割,因此我们在contab里面定时去调用shell脚本

          首先我们在/usr/local/nginx/sbin/目录下创建一个log.sh的脚本,如下所示。


 
 
  1. [root@nginx1 sbin]# pwd
  2. /usr/local/nginx/sbin
  3. [root@nginx1 sbin]# vim log.sh

           log.sh脚本内容如下:


 
 
  1. #基础路径:/usr/local/nginx
  2. BASE_DIR=/usr/local/nginx
  3. #原文件名称:test.access.log
  4. BASE_FILE_NAME=test.access.log
  5. #当前日志文件路径:/usr/local/nginx/logs
  6. CURRENT_PATH=$BASE_DIR/logs
  7. #备份日志文件地址:/usr/local/nginx/datalogs
  8. BAK_PATH=$BASE_DIR/datalogs
  9. #当前日志文件:/usr/local/nginx/logs/test.access.log
  10. CURRENT_FILE=$CURRENT_PATH/$BASE_FILE_NAME
  11. #备份今天的日志,为了方便测试我们的定时器功能,这里把时间精确到分钟,名称如:201704071720
  12. BAK_TIME=`/bin/date -d today +%Y%m%d%H%M`
  13. #备份文件:/usr/local/nginx/datalogs/201704071720-test.access.log
  14. BAK_FILE=$BAK_PATH/$BAK_TIME-$BASE_FILE_NAME
  15. #输出备份文件信息
  16. echo $BAK_FILE
  17. #备份之前先关闭nginx,关闭命令:/usr/local/nginx/sbin/nginx -s stop
  18. $BASE_DIR/sbin/nginx -s stop
  19. #将/usr/local/nginx/logs/test.access.log移动到/usr/local/nginx/datalogs/201704071720-test.access.log
  20. mv $CURRENT_FILE $BAK_FILE
  21. #启动nginx,命令:/usr/local/nginx/sbin/nginx,重启之后会自动再生成一个test.access.log文件
  22. $BASE_DIR/sbin/nginx
       由于我们在脚本中设置了日志备份目录datalogs,而该目录目前还没有创建,因此我们需要创建该目录,如下所示。


 
 
  1. [root@nginx1 nginx]# mkdir datalogs
  2. [root@nginx1 nginx]# ll
  3. 总用量 40
  4. drwx------. 2 nobody root 4096 4月 7 02:59 client_body_temp
  5. drwxr-xr-x. 2 root root 4096 4月 7 02:58 conf
  6. drwxr-xr-x. 2 root root 4096 4月 7 08:41 datalogs
  7. drwx------. 2 nobody root 4096 4月 7 02:59 fastcgi_temp
  8. drwxr-xr-x. 2 root root 4096 4月 7 02:58 html
  9. drwxr-xr-x. 2 root root 4096 4月 7 02:59 logs
  10. drwx------. 2 nobody root 4096 4月 7 02:59 proxy_temp
  11. drwxr-xr-x. 2 root root 4096 4月 7 02:58 sbin
  12. drwx------. 2 nobody root 4096 4月 7 02:59 scgi_temp
  13. drwx------. 2 nobody root 4096 4月 7 02:59 uwsgi_temp
  14. [root@nginx1 nginx]#
         在定时调用log.sh脚本文件之前,我们需要给予该脚本文件执行权限,我们先看看我们创建的log.sh默认是什么权限,如下所示,可以卡到,只有读的权限。


 
 
  1. [root@nginx1 sbin]# ll
  2. 总用量 3048
  3. -rw-r--r--. 1 root root 1016 4月 7 09:11 log.sh
  4. -rwxr-xr-x. 1 root root 3115344 4月 7 02:58 nginx
  5. [root@nginx1 sbin]#

        我们给log.sh文件赋予所有权限,如下所示。


 
 
  1. [root@nginx1 sbin]# chmod 777 log.sh
  2. [root@nginx1 sbin]# ll
  3. 总用量 3048
  4. -rwxrwxrwx. 1 root root 1016 4月 7 10:12 log.sh
  5. -rwxr-xr-x. 1 root root 3115344 4月 7 02:58 nginx
  6. [root@nginx1 sbin]#
 

       下面我们便使用crontab -e命令去设置定时任务,如下所示。


 
 
  1. #每天凌晨两点备份前一天的日志
  2. #* 2 * * * sh /usr/local/nginx/sbin/log.sh
  3. #为了测试方便,我们暂且设置成每分钟执行一次备份
  4. */1 * * * * sh /usr/local/nginx/sbin/log.sh
         设置好定时任务之后,保存退出,这样每隔一分钟便会执行一次log.sh脚本,同时会生成一个备份文件,我们到/usr/local/nginx/datalogs目录下进行查看,如下所示,可以看到,每隔一分钟便会有一个备份文件产生,命名规则与我们设置的完全一致。


 
 
  1. [root@nginx1 sbin]# cd /usr/local/nginx/datalogs/
  2. You have new mail in /var/spool/mail/root
  3. [root@nginx1 datalogs]# ll
  4. 总用量 0
  5. -rw-r--r--. 1 root root 0 4月 7 10:12 201704071013-test.access.log
  6. -rw-r--r--. 1 root root 0 4月 7 10:13 201704071014-test.access.log
  7. -rw-r--r--. 1 root root 0 4月 7 10:14 201704071015-test.access.log
  8. -rw-r--r--. 1 root root 0 4月 7 10:15 201704071016-test.access.log
  9. -rw-r--r--. 1 root root 0 4月 7 10:16 201704071017-test.access.log
  10. -rw-r--r--. 1 root root 0 4月 7 10:17 201704071018-test.access.log
  11. -rw-r--r--. 1 root root 0 4月 7 10:18 201704071019-test.access.log
  12. -rw-r--r--. 1 root root 0 4月 7 10:19 201704071020-test.access.log
  13. -rw-r--r--. 1 root root 0 4月 7 10:20 201704071021-test.access.log
  14. -rw-r--r--. 1 root root 0 4月 7 10:21 201704071022-test.access.log
  15. -rw-r--r--. 1 root root 0 4月 7 10:22 201704071023-test.access.log
  16. -rw-r--r--. 1 root root 0 4月 7 10:23 201704071024-test.access.log
  17. [root@nginx1 datalogs]#
          如果发现没有备份文件产生,那肯定是有错误了,那么怎么看错误信息呢?其实系统已经告诉我们了,如下所示,提示我们去/var/spool/mail/root文件中进行查看。

 
 
  1. [root@nginx1 sbin]# cd /usr/local/nginx/datalogs/
  2. You have new mail in /var/spool/mail/root

          比如我在刚开始的时候把获取日期的那行的`搞成'了,可以看到,该文件中明确告诉我们是日期那行出了错误。

          

      3.nginx配置实现动静分离

       对于nginx来说,实现动静分离是件非常容易的事,我们只需要配置多个location便可以达到这个目的。location的基础语法有三种:

1.location = pattern {}精确匹配

2.location pattern {} 一般匹配

3.location ~ pattern {} 正则匹配

        下面我们就使用正则表达式举个例子,我们打开nginx.conf配置文件,并修改端口为1234的server的location,如下所示,用来匹配以tester开头的文件。


 
 
  1. server {
  2. listen 1234;
  3. server_name test;
  4. location ~ tester {
  5. root tester;
  6. index tester111.html;
  7. }
  8. access_log logs/test.log main;
  9. }
       修改完之后,我们重启nginx。

[root@nginx1 nginx]# /usr/local/nginx/sbin/nginx -s reload
 
 
       重启nginx之后,我们这时再访问http://test:1234的话,将不能访问到tester111.html了,而是访问到index.html(即nginx欢迎页了),如下图所示。出现这种情况的原因就是我们输入http://test:1234后,它会去匹配location /{},结果发现在端口为1234的server当中没有location /{},于是便会去试着找其它有location /{}的server,发现80端口可以匹配成功,因此便去访问80端口所对应的欢迎页了。      

           当我们在地址栏输入http://test:1234/tester111.html时,页面如下图所示。这时之所以可以访问到/usr/local/nginx/tester目录下的tester111.thtml,就是因为我们在端口为1234的server当中设置了正则表达式即:location ~ tester {},它可以匹配以tester开头的请求,而且必须注意的是,访问的文件必须真实存在,比如访问tester111.html文件可以,这是因为在tester目录下确实有该文件,如果访问tester123.html就会报404,因为找不到这么一个文件。那么为什么这时不去访问80端口的欢迎页了呢?这是因为此时地址栏输入的请求信息是要具体访问某个文件了,不适合再导向欢迎页。


      下面再说下在nginx中if条件的使用,if(条件为:=~~*)、return、break、rewrite。-f是否为文件、-d是否为目录、-e是否存在。我们就举个if条件的例子,我们在nginx.conf文件的1234所对应的server的location当中添加if条件,如下所示,添加的判断是如果发起请求的IP地址是192.168.156.100(我本地的IP地址)的话,就返回401错误。


 
 
  1. server {
  2. listen 1234;
  3. server_name test;
  4. location ~ tester {
  5. if ($remote_addr = 192.168.156.100) {
  6. return 401;
  7. }
  8. root tester;
  9. index tester111.html;
  10. }
  11. access_log logs/test.log main;
  12. }
       修改完配置文件,我们重启nginx

[root@nginx1 conf]# /usr/local/nginx/sbin/nginx -s reload
 
 
       重启完nginx之后,我们再访问http://test:1234/tester111.html这个地址就会出现如下图所示的结果。这说明我们配置的拦截条件生效了。

       下面再举个if条件的例子,我们把if条件改成如下所示的样子,它的意思是访问的浏览器如果是chrome浏览器的话,不管你是什么请求都定位到chrome.html页面,然后break跳出(如果不加break的话,每次请求过来都会重新让浏览器发起重定向请求,而且永远不会结束,从而无法真正访问到chrome.html页面,导致提示404错误)。


 
 
  1. server {
  2. listen 1234;
  3. server_name test;
  4. location ~ tester {
  5. if ($http_user_agent ~* chrome) {
  6. rewrite ^.*$ /chrome.html;
  7. break;
  8. }
  9. root tester;
  10. index tester111.html;
  11. }
  12. access_log logs/test.log main;
  13. }
       既然要重定向到chrome.html页面,那么我们便需要先创建该文件,如下所示。


 
 
  1. [root@nginx1 nginx]# cd tester/
  2. [root@nginx1 tester]# pwd
  3. /usr/local/nginx/tester
  4. [root@nginx1 tester]# ll
  5. 总用量 4
  6. -rw-r--r--. 1 root root 57 4月 7 06:45 tester111.html
  7. [root@nginx1 tester]# vim chrome.html
  8. <html>
  9. <body>Chrome Page!!!! </body>
  10. </html>
       既然修改了nginx.conf文件,我们便需要重启nginx

[root@nginx1 tester]# /usr/local/nginx/sbin/nginx -s reload
 
 
      重启之后,我们再访问http://test:1234/tester111.html,这时访问结果如下图所示,可以看到请求被成功重定向到chrome.html页面了。


      那么为何可以达到这个效果呢?我们来看看谷歌浏览器的请求日志信息,如下图所示,可以看到谷歌浏览器的请求信息中有"Chrome"信息,我们的if ($http_user_agent ~* chrome)条件判断中使用了正则表达式,* chrome可以忽略大小写,所以"Chrome"也可以匹配"chrome",既然浏览器匹配,就进入到了if条件当中,rewrite ^.*$ /chrome.html;这句话的意思是不论你要请求什么资源,都给你定向到chrome.html文件,因此我们才看到了chrome.html文件的内容。


      下面再举个例子,我们在端口为1234的server当中再加一个location,匹配goods,如下所示。


 
 
  1. server {
  2. listen 1234;
  3. server_name test;
  4. location ~ tester {
  5. if ($http_user_agent ~* chrome) {
  6. rewrite ^.*$ /chrome.html;
  7. break;
  8. }
  9. root tester;
  10. index tester111.html;
  11. }
  12. location /goods {
  13. rewrite "goods-(\d{1,5})\.html" /goods-ctrl.html;
  14. root tester;
  15. index tester111.html;
  16. }
  17. access_log logs/test.log main;
  18. }
       我们还是得在/usr/local/nginx/tester目录下创建一下goods-ctrl.html文件,如下所示。


 
 
  1. [root@nginx1 conf]# cd /usr/local/nginx/tester/
  2. [root@nginx1 tester]# ll
  3. 总用量 8
  4. -rw-r--r--. 1 root root 44 4月 8 05:52 chrome.html
  5. -rw-r--r--. 1 root root 57 4月 7 06:45 tester111.html
  6. [root@nginx1 tester]# vim goods-ctrl.html
  7. <html>
  8. <body>Goods Page!!!! </body>
  9. </html>
         由于修改了nginx.conf文件,因此需要重启nginx

[root@nginx1 tester]# /usr/local/nginx/sbin/nginx -s reload
 
 

         重启之后,我们在浏览器输入goods-后面跟0到5位的数字然后是.html,这样才能匹配重定向条件,会被重定向到goods-ctrl.html页面。这个例子在真实项目中是非常常见的,Rewrite最主要的作用就是对URL进行重写,即重定向。举个简单的例子,我们用电脑打开淘宝显示出的页面与手机打开显示出的页面,或者是IE与Chrome浏览器打开的页面,有着特别大的差别,这就是使用了Rewrite模块,为用户提供最合适的页面。


      nginx还可以对数据进行压缩,对一些图片、html、css、js等文件进行缓存、从而实现动静分离等等优化功能,在网站做优化的时候非常的有用。

      所谓的动静分离,通过上面的例子,我们完全可以将动态的请求都交给tomcat处理,静态的请求都交给nginx来处理,这是非常容易做到的事情。

        </div>
            </div>

     2、安装fastdfs-nginxmodule_v1.16.tar.gz(fast与nginx相结合的模块安装包), 进入 /usr/local/software目录并解压,如下所示。


 
 
  1. [root@fastdfs nginx-1.6.2]# cd /usr/local/software/
  2. [root@fastdfs software]# tar -zxvf fastdfs-nginx-module_v1.16.tar.gz -C /usr/local/fast/
  3. fastdfs-nginx-module/
  4. fastdfs-nginx-module/src/
  5. fastdfs-nginx-module/src/ngx_http_fastdfs_module.c
  6. fastdfs-nginx-module/src/mod_fastdfs.conf
  7. fastdfs-nginx-module/src/config
  8. fastdfs-nginx-module/src/common.h
  9. fastdfs-nginx-module/src/common.c
  10. fastdfs-nginx-module/INSTALL
  11. fastdfs-nginx-module/HISTORY
  12. [root@fastdfs software]#
     3、进入到/usr/local/fast目录下可以看到解压的fastdfs-nginx-module目录,然后进入到fastdfs-nginx-module/src/目录下,可以看到config文件。


 
 
  1. [root@fastdfs software]# cd /usr/local/fast/
  2. [root@fastdfs fast]# ll
  3. 总用量 12
  4. drwxr-xr-x. 10 8980 users 4096 4月 11 04:10 FastDFS
  5. drwxrwxr-x. 3 500 500 4096 5月 4 2014 fastdfs-nginx-module
  6. drwxr-xr-x. 4 root root 4096 4月 11 03:30 libfastcommon-master
  7. [root@fastdfs fast]# cd fastdfs-nginx-module/src/
  8. [root@fastdfs src]# ll
  9. 总用量 76
  10. -rw-rw-r--. 1 500 500 33207 8月 30 2013 common.c
  11. -rw-rw-r--. 1 500 500 3479 1月 3 2012 common.h
  12. -rw-rw-r--. 1 500 500 447 11月 4 2010 config
  13. -rw-rw-r--. 1 500 500 3679 3月 30 2013 mod_fastdfs.conf
  14. -rw-rw-r--. 1 500 500 28542 5月 4 2014 ngx_http_fastdfs_module.c
  15. [root@fastdfs src]#
       修改该conf文件,我们把文件的第四行配置中的/usr/local/include都改为/usr/include,共两处。


      4、fastdfs与nginx进行结合,由于我们刚才安装过nginx了,因此在/usr/local目录下已经生成了一个nginx目录了,如下图所示。


       为了将nginx与fastdfs相结合,我们先把这个nginx目录删除掉,如下图所示,可以看到已经没有nginx目录了。


       进入到nginx-1.6.2/目录下并执行配置和编译安装,如下所示。


 
 
  1. [root@fastdfs local]# cd nginx-1.6.2/
  2. [root@fastdfs nginx-1.6.2]# ./configure --add-module=/usr/local/fast/fastdfs-nginx-module/src/
  3. [root@fastdfs nginx-1.6.2]# make && make install
       复制fastdfs-nginx-module中的配置文件,到/etc/fdfs目录中,如下所示。


 
 
  1. [root@fastdfs fdfs]# cd /usr/local/fast/fastdfs-nginx-module/src/
  2. [root@fastdfs src]# ll
  3. 总用量 76
  4. -rw-rw-r--. 1 500 500 33207 8月 30 2013 common.c
  5. -rw-rw-r--. 1 500 500 3479 1月 3 2012 common.h
  6. -rw-rw-r--. 1 500 500 435 4月 11 06:09 config
  7. -rw-rw-r--. 1 500 500 3679 3月 30 2013 mod_fastdfs.conf
  8. -rw-rw-r--. 1 500 500 28542 5月 4 2014 ngx_http_fastdfs_module.c
  9. [root@fastdfs src]# cp /usr/local/fast/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
  10. [root@fastdfs src]#
        我们到 /etc/fdfs/ 目录下,修改我们刚copy过来的mod_fastdfs.conf 文件,需要修改的项如下,其中第一项是超时时长,第三项是允许外界通过http方式访问资源。


 
 
  1. connect_timeout=10
  2. tracker_server=192.168.156.13:22122
  3. url_have_group_name = true
  4. store_path0=/fastdfs/storage
        复制FastDFS里的2个文件,到/etc/fdfs目录中,如下所示。


 
 
  1. [root@fastdfs fdfs]# cd /usr/local/fast/FastDFS/conf/
  2. [root@fastdfs conf]# ll
  3. 总用量 84
  4. -rw-r--r--. 1 8980 users 23981 12月 2 2014 anti-steal.jpg
  5. -rw-r--r--. 1 8980 users 1461 12月 2 2014 client.conf
  6. -rw-r--r--. 1 8980 users 858 12月 2 2014 http.conf
  7. -rw-r--r--. 1 8980 users 31172 12月 2 2014 mime.types
  8. -rw-r--r--. 1 8980 users 7829 12月 2 2014 storage.conf
  9. -rw-r--r--. 1 8980 users 105 12月 2 2014 storage_ids.conf
  10. -rw-r--r--. 1 8980 users 7102 12月 2 2014 tracker.conf
  11. [root@fastdfs conf]# cp http.conf mime.types /etc/fdfs/
  12. [root@fastdfs conf]#
       创建一个软连接,在/fastdfs/storage文件存储目录下创建软连接,将其链接到实际存放数据 的目录,如下所示。


 
 
  1. [root@fastdfs conf]# ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00
  2. [root@fastdfs conf]#
       进入到/usr/local/nginx/conf/目录下,修改nginx.conf文件,如下图所示。


        修改的内容如下图示


        可以直接复制下面的内容。


 
 
  1. listen 8888;
  2. location ~/group([0-9])/M00 {
  3. ngx_fastdfs_module;
  4. }

       设置nginx开机自启动,这样下次重启设备之后,tracker、storage、nginx都自动启动了,直接就可以使用服务,如下所示。


 
 
  1. [root@fastdfs ~]# vim /etc/rc.d/rc.local
  2. #!/bin/sh
  3. #
  4. # This script will be executed *after* all the other init scripts.
  5. # You can put your own initialization stuff in here if you don't
  6. # want to do the full Sys V style init stuff.
  7. touch /var/lock/subsys/local
  8. /etc/init.d/fdfs_trackerd start
  9. /etc/init.d/fdfs_storaged start
  10. /usr/local/nginx/sbin/nginx

       启动nginx,如下所示。


 
 
  1. [root@fastdfs conf]# /usr/local/nginx/sbin/nginx
  2. ngx_http_fastdfs_set pid=6809
  3. [root@fastdfs conf]#
      5、在通过8888端口访问图片之前先配置下防火墙,允许外界访问8888端口,添加的一行是-A INPUT -m state --state NEW -m tcp -p tcp --dport 8888 -j ACCEPT,如下图所示。


      配置完防火墙后重启防火墙


 
 
  1. [root@fastdfs conf]# service iptables restart
  2. iptables:将链设置为政策 ACCEPT:filter [确定]
  3. iptables:清除防火墙规则: [确定]
  4. iptables:正在卸载模块: [确定]
  5. iptables:应用防火墙规则: [确定]
  6. [root@fastdfs conf]#
      6、现在我们便可以通过http的方式访问我们刚才上传的图片了(我们刚才上传图片返回的地址是group1/M00/00/00/wKicDVjr_ayAE4VVAAHk-VzqZ6w020.jpg),如下图所示。



     至此,一个单机版的FastDFS便搭建完毕了!!
   
    
































 









更多推荐

搭建单机版的FastDFS服务器

本文发布于:2023-04-29 21:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/b98c3ccf0825bc907d48d558f9d5ce5e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单机版   服务器   FastDFS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 112257文章数
  • 28552阅读数
  • 0评论数