inotify+rsync实战演练

试验目的:

演练rsync结合inotify实现服务端目录内文件有变动(包括修改,删除,创建)时,自动立即同步到客户端

试验环境:

centos6.5 192.168.10.89 —–角色:文件同步服务器.原始文件服务器.rsync客户端,inotify服务器
centos 6.5 192.168.10.103—–角色:文件同步客户端,由文件服务器自动向客户端同步

关于rsync和inotify介绍和具体用法.请参考其他笔记内容

实战步骤

一.在inotify服务器安装inotify-tools工具

下载链接https://sourceforge.net/projects/inotify-tools/?source=typ_redirect

安装过程简单:

tar zxvf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13
./configure --prefix=/usr/local/inotify
make && make install 

vim /etc/profile
在结尾处加上:
export PATH=$PATH:/usr/local/inotify/bin

应用profile文件:
source /etc/profile

二.演示inotify使用方法:

执行命令:

inotifywait -rm --format '%Xe %w%f' -e modify,create,delete,attrib /tmp/data

命令输出:

[root@oracle inotify-tools-3.13]# inotifywait -rm --format '%Xe %w%f' -e modify,create,delete,attrib /tmp/data
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.

解析:

inotifywait : 持续监控文件的状态变化

-r : 递归监控目录下的所有文件,包括子目录.

Note:如果要监控的目录中文件数量巨大,则通常需要修改/proc/sys/fs/inotify/max_users_watchs内核参数,因为其默认值为8192.

-m: 实现持续监控

–format 显示格式.

  • %X—-事件以”X”分隔.
  • %e—-显示事件(比如CREATE,MODIFY等),
  • %w—-显示文件名
  • %f—–显示目录
  • -e: 表示检测哪些事件

/tmp/data——-监测的目录路径

再开启一个终端,然后在/tmp/data目录下新建一些文件:

[root@localhost data]# touch {x,y,z,u,v,w}.txt
inotify输出如下:

检测到了文件变化.第一列是事件类型.有CREATE,ATTRIB. 第二列是文件的完整路径

[root@oracle inotify-tools-3.13]# inotifywait -rm –format ‘%Xe %w%f’ -e modify,create,delete,attrib /tmp/data
Setting up watches. Beware: since -r was given, this may take a while!
Watches established.
CREATE /tmp/data/x.txt
ATTRIB /tmp/data/x.txt
CREATE /tmp/data/y.txt
ATTRIB /tmp/data/y.txt
CREATE /tmp/data/z.txt
ATTRIB /tmp/data/z.txt
CREATE /tmp/data/u.txt
ATTRIB /tmp/data/u.txt
CREATE /tmp/data/v.txt
ATTRIB /tmp/data/v.txt
CREATE /tmp/data/w.txt
ATTRIB /tmp/data/w.txt


再试着删除所有文件:

[root@localhost data]# rm -rf {x,y,z,u,v,w}.txt
inotify检测到DELETE事件:

DELETE /tmp/data/x.txt
DELETE /tmp/data/y.txt
DELETE /tmp/data/z.txt
DELETE /tmp/data/u.txt
DELETE /tmp/data/v.txt
DELETE /tmp/data/w.txt

试试创建和删除目录检测到CREATE和DELETE的目录事件:

CREATEXISDIR /tmp/data/test

试试修改文件内容

[root@localhost data]# echo "haha" > 1.txt

检测到MODIFY事件:

CREATE /tmp/data/1.txt
MODIFY /tmp/data/1.txt

基本用法就介绍到这里.下面实战演练inotify+rsync结合做目录文件同步

在inotify编写脚本文件:

以下是工作在相对路径下

[root@localhost ~]# vim inotify_rsync.sh
#!/bin/bash
src=/tmp/data
des="/" #由于工作在相对路径下,会同步目录名.所以目的路径为/根
ip=192.168.10.103
user=root
cd $src #切换进工作目录
#inotify监测目录下文件是否有改动.主要监测:文件名或者目录修改,创建,删除,移动,文件内容修改.(这里我没有监测文件权限属性发送变化).
#将监测到的文件重定向到while循环.
inotifywait -mr --format '%Xe %w%f' -e modify,create,delete,close_write,move $src | while read file;do
   #获取Inotify的监测事件.有CREATE,MODIFY,DELETE等
   ino_event=$(echo $file | awk '{print $1}')
   #获取inotify监测到的变化文件
   ino_file=$(echo $file | awk '{print $2}')
   echo $file
   #if的正则匹配.如果ino_event变量的内容匹配CREATE开头.那么就条件为true.其实就等于 $ino_event == "CREATE*"
   if [[ $ino_event =~ "CREATE" ]] || [[ $ino_event =~ "MODIFY" ]] || [[ $ino_event =~ "CLOSE_WRITE" ]] || [[ $ino_event =~ "MOVED_TO" ]];then
         echo "CREATE or MODIFY or CLOSE_WRITE or MOVED_TO"
         #如果是文件有变化,则利用rsync同步该文件的父目录到远程主机相关目录下.这里使用了ssh协议.需要提前复制本机公钥到目的主机
         echo $(dirname $ino_file)
         /usr/bin/rsync -avzR -e ssh $(dirname $ino_file) $user@$ip:$des
   elif [[ $ino_event =~ "DELETE" ]] || [[ $ino_event =~ "MOVED_FROM" ]];then
         echo "Delete or Moved_From"
        #如果是文件删除,或者移动到其他地方.则利用rsync删除远程主机上的该文件
        /usr/bin/rsync -avzR --delete $(dirname $ino_file) $user@$ip:$des
   fi
done
以下是工作在绝对路径下:

[root@localhost ~]# vim inotify_rsync.sh
#!/bin/bash
src=/tmp/data
des=/tmp  #由于会同步/tmp/data目录.所以目的路径只需要指定/tmp目录
ip=192.168.10.103
user=root
#inotify监测目录下文件是否有改动.主要监测:文件名或者目录修改,创建,删除,移动,文件内容修改.(这里我没有监测文件权限属性发送变化).
#将监测到的文件重定向到while循环.
inotifywait -mr --format '%Xe %w%f' -e modify,create,delete,close_write,move $src | while read file;do
   #获取Inotify的监测事件.有CREATE,MODIFY,DELETE等
   ino_event=$(echo $file | awk '{print $1}')
   #获取inotify监测到的变化文件
   ino_file=$(echo $file | awk '{print $2}')
   echo $file
   #if的正则匹配.如果ino_event变量的内容匹配CREATE开头.那么就条件为true.其实就等于 $ino_event == "CREATE*"
   if [[ $ino_event =~ "CREATE" ]] || [[ $ino_event =~ "MODIFY" ]] || [[ $ino_event =~ "CLOSE_WRITE" ]] || [[ $ino_event =~ "MOVED_TO" ]];then
         echo "CREATE or MODIFY or CLOSE_WRITE or MOVED_TO"
         #如果是文件有变化,则利用rsync同步该文件的父目录到远程主机相关目录下.这里使用了ssh协议.需要提前复制本机公钥到目的主机
         /usr/bin/rsync -avz -e ssh $ino_file $user@$ip:$des
   elif [[ $ino_event =~ "DELETE" ]] || [[ $ino_event =~ "MOVED_FROM" ]];then
         echo "Delete or Moved_From"
        #如果是文件删除,或者移动到其他地方.则利用rsync删除远程主机上的该文件
        /usr/bin/rsync -avz --delete $ino_file $user@$ip:$des
   fi
done

Note:此脚本中的rsync使用的是ssh协议传输.而不是守护模式.所以需要实现传输本地的公钥到远程主机相关用户下

运行脚本:

[root@localhost ~]# ./inotify_rsync.sh

在/tmp/data目录内创建文件:

[root@localhost data]# touch {1,2,3,4,5,6}.txt

脚本输出:

[root@oracle ~]# ./inotify_rsync.sh
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
CLOSE_WRITEXCLOSE /tmp/data/1.txt
CREATE or MODIFY or CLOSE_WRITE or MOVED_TO
/tmp/data
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list
/tmp/
/tmp/data/
/tmp/data/1.txt
/tmp/data/2.txt
/tmp/data/3.txt
/tmp/data/4.txt
/tmp/data/5.txt
/tmp/data/6.txt
/tmp/data/test/

sent 388 bytes  received 138 bytes  350.67 bytes/sec
total size is 5  speedup is 0.01
CREATE /tmp/data/2.txt
CREATE or MODIFY or CLOSE_WRITE or MOVED_TO
/tmp/data
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 154 bytes  received 15 bytes  338.00 bytes/sec
total size is 5  speedup is 0.03
CLOSE_WRITEXCLOSE /tmp/data/2.txt
CREATE or MODIFY or CLOSE_WRITE or MOVED_TO
/tmp/data
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 154 bytes  received 15 bytes  338.00 bytes/sec
total size is 5  speedup is 0.03
CREATE /tmp/data/3.txt
CREATE or MODIFY or CLOSE_WRITE or MOVED_TO
/tmp/data
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 154 bytes  received 15 bytes  112.67 bytes/sec
total size is 5  speedup is 0.03
CLOSE_WRITEXCLOSE /tmp/data/3.txt
CREATE or MODIFY or CLOSE_WRITE or MOVED_TO
/tmp/data
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 154 bytes  received 15 bytes  338.00 bytes/sec
total size is 5  speedup is 0.03
CREATE /tmp/data/4.txt
CREATE or MODIFY or CLOSE_WRITE or MOVED_TO
/tmp/data
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 154 bytes  received 15 bytes  112.67 bytes/sec
total size is 5  speedup is 0.03
CLOSE_WRITEXCLOSE /tmp/data/4.txt
CREATE or MODIFY or CLOSE_WRITE or MOVED_TO
/tmp/data
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 154 bytes  received 15 bytes  338.00 bytes/sec
total size is 5  speedup is 0.03
CREATE /tmp/data/5.txt
CREATE or MODIFY or CLOSE_WRITE or MOVED_TO
/tmp/data
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 154 bytes  received 15 bytes  338.00 bytes/sec
total size is 5  speedup is 0.03
CLOSE_WRITEXCLOSE /tmp/data/5.txt
CREATE or MODIFY or CLOSE_WRITE or MOVED_TO
/tmp/data
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 154 bytes  received 15 bytes  112.67 bytes/sec
total size is 5  speedup is 0.03
CREATE /tmp/data/6.txt
CREATE or MODIFY or CLOSE_WRITE or MOVED_TO
/tmp/data
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 154 bytes  received 15 bytes  338.00 bytes/sec
total size is 5  speedup is 0.03
CLOSE_WRITEXCLOSE /tmp/data/6.txt
CREATE or MODIFY or CLOSE_WRITE or MOVED_TO
/tmp/data
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 154 bytes  received 15 bytes  338.00 bytes/sec
total size is 5  speedup is 0.03

在172.16.1.120客户端的/tmp/data目录下查看文件: 文件已成功复制:

[root@www ~]$ll /tmp/data
total 8
-rw-r--r--. 1 root root    5 Jun 24 13:27 1.txt
-rw-r--r--. 1 root root    0 Jun 24 13:27 2.txt
-rw-r--r--. 1 root root    0 Jun 24 13:27 3.txt
-rw-r--r--. 1 root root    0 Jun 24 13:27 4.txt
-rw-r--r--. 1 root root    0 Jun 24 13:27 5.txt
-rw-r--r--. 1 root root    0 Jun 24 13:27 6.txt

演示在Inotify服务上删除刚创建的文件: 监测到文件删除

[root@oracle ~]# ./inotify_rsync.sh
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
DELETE /tmp/data/1.txt
Delete or Moved_From
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list
/tmp/data/
deleting tmp/data/6.txt
deleting tmp/data/5.txt
deleting tmp/data/4.txt
deleting tmp/data/3.txt
deleting tmp/data/2.txt
deleting tmp/data/1.txt

sent 87 bytes  received 18 bytes  70.00 bytes/sec
total size is 0  speedup is 0.00
DELETE /tmp/data/2.txt
Delete or Moved_From
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 84 bytes  received 15 bytes  198.00 bytes/sec
total size is 0  speedup is 0.00
DELETE /tmp/data/3.txt
Delete or Moved_From
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 84 bytes  received 15 bytes  66.00 bytes/sec
total size is 0  speedup is 0.00
DELETE /tmp/data/4.txt
Delete or Moved_From
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 84 bytes  received 15 bytes  198.00 bytes/sec
total size is 0  speedup is 0.00
DELETE /tmp/data/5.txt
Delete or Moved_From
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 84 bytes  received 15 bytes  198.00 bytes/sec
total size is 0  speedup is 0.00
DELETE /tmp/data/6.txt
Delete or Moved_From
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list

sent 84 bytes  received 15 bytes  66.00 bytes/sec
total size is 0  speedup is 0.00

在172.16.1.120服务器上查看/tmp/data目录.下面没有任何文件

[root@www ~]$ll /tmp/data
total 4
drwxr-xr-x. 2 root root 4096 Jun 24 13:17 test

演示:新建一个目录.且在该目录下创建内容 脚本输出:

CREATEXISDIR /tmp/data/haha
CREATE or MODIFY or CLOSE_WRITE or MOVED_TO
/tmp/data
Nasty PTR record "172.16.1.120" is set up for 172.16.1.120, ignoring
sending incremental file list
/tmp/data/
/tmp/data/haha/

sent 100 bytes  received 22 bytes  244.00 bytes/sec
total size is 0  speedup is 0.00

目录已经被同步

[root@www ~]$ll /tmp/data
total 8
drwxr-xr-x. 2 root root 4096 Jun 24 13:33 haha
drwxr-xr-x. 2 root root 4096 Jun 24 13:17 test

rsync实战演练

rsync是一个远程数据同步工具,可以快速同步多台主机的文件,且只同步有差异的部分.非常强大的工具

实战环境:

  • 服务端:192.168.10.89
  • 客户端:192.168.10.103

rsync不需要安装,默认就自带.

关于rsync的命令想法,可以参考其他笔记.

note: 以下教程都是讲述客户端从远程服务器同步数据到本地.类似于下载行为. 如果需要将本地的文件同步到远程服务器.有点类似于上传行为.则需要改变命令.

下列命令表示了上传和下载的使用区别.

note:在使用rsyn同步前必须要千万小心.因为如果命令搞反.可能会出现意外的严重后果.例如将对方的文件同步到本地数据目录

1.下列的命令将对方(192.168.10.89)的/var/www/abc目录同步到本地的/root/rsync目录下:

rsync -avz --progress -e ssh [email protected]:/var/www/abc /root/rsync

2.下列命令表示将本地的/tmp/backups目录同步到对方(192.168.10.89)的/var/www/abc目录下

rsync -avz --progress /tmp/backups -e ssh [email protected]:/var/www/abc

3.另外.如果远程服务器的ssh不是默认22端口.则需要改成:

rsync -avz --progress /root/rsync -e "ssh -p 端口 " [email protected]:/var/www/abc

Rync的使用方法介绍:

一、以服务端的方式启动rsync进程

1.编辑/etc/rsyncd.conf配置文件——-这个文件默认没有,需要自己写入

vim /etc/rsyncd.conf
#全局参数.所有模块生效配置#
uid = nobody
gid = nobody
use chroot = no
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
#模块参数.一个模块代表一个路径#
[www]
path = /var/www/abc/   #路径目录,注意必须是一个/结尾的目录
ignore errors          #忽略错误信息
read only = yes       #服务端只读,客户端只能和服务端同步,不能上传文件
list = no          #是否允许客户端列出服务端此路径下的文件
hosts allow = 192.168.10.0/24  #允许哪个网络上的客户端同步
auth users = backup    #认证用户名
secrets file = /etc/rsync_server.pas #指定一个密码文件路径.此文件内容为username:password. 而且此文件必须和启动rsync的用户是同一个用户.且权限为600

2.编辑密码文件:

[root@localhost ~]# cat /etc/rsync_server.pas
backup:jesse
[root@localhost ~]# ll /etc/rsync_server.pas
-rw------- 1 root root 13 Dec 27 16:46 /etc/rsync_server.pas

Note:此文件必须为600权限.且和rsync进程的用户相同.比如如果是root启动的rsync服务.则此文件属主也必须是root

3.启动rsync服务,以daemon方式启动

rsync --daemon

rsync –daemon默认监控在873端口

默认监控在873端口:
[root@localhost ~]# netstat -tulnp | grep rsync
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      2034/rsync
tcp        0      0 :::873                      :::*                        LISTEN      2034/rsync

4.在/var/www/abc目录下写入一个测试文件夹

[root@localhost ~]# cat /var/www/abc/rsync.test
haha
this is for test rsync
the nginx02 is rsync server.the nginx 01 (ip:192.168.10.103) is a client
I am going to see whether this file will be sync to the client or not!

5.客户端同步文件 先定义密码.改成600权限

[root@localhost ~]# cat /etc/rsync_server.pas
jesse
[root@localhost ~]# ll /etc/rsync_server.pas
-rw------- 1 root root 6 Dec 27 16:47 /etc/rsync_server.pas

Note:我在这里踩到一个大坑.客户端的密码文件只需要包含密码.不能像服务端一样指定username:password.不然会提示验证失败

6.执行rsync命令

/usr/bin/rsync -avz  --progress [email protected]::www /var/www/abc/ --password-file=/etc/rsync_server.pas

-a ----保持文件权限
-v ----详细显示
-z -----启用压缩
--progress --显示备份过程
backup@ ----表示用backup用户认证
::www  --------注意这里有2个冒号.表示同步服务器上的www模块
/var/www/abc ---表示同步到本地这个目录下
--password-file ---表示用这个文件内的密码去认证

可是遇到和上面一样的坑:

[root@localhost ~]# /usr/bin/rsync -avz  --progress [email protected]::www /var/www/abc/ --password-file=/etc/rsync_server.pas
@ERROR: auth failed on module www

这个坑,至少坑了我5个小时.反复的确认selinux是否关闭,配置文件是否错误,密码文件和密码文件权限等最后才发现,原来配置文件的配置语句不能用注释

注释内容只能单独一行存在.修改配置文件:

[root@localhost ~]# vim /etc/rsyncd.conf
#全局参数.所有模块生效配置#
uid = nobody
gid = nobody
use chroot = no
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
#模块参数.一个模块代表一个路径#
[www]
path = /var/www/abc/
ignore errors
read only = yes
list = no
hosts allow = 192.168.10.0/24
auth users = backup
secrets file = /etc/rsync.secrets

客户端重新执行命令:

[root@localhost ~]# /usr/bin/rsync -avz  --progress [email protected]::www /var/www/abc/ --password-file=/etc/rsync_server.pas
receiving incremental file list
./
index.html
          91 100%   88.87kB/s    0:00:00 (xfer#1, to-check=2/4)
rsync.test
         173 100%  168.95kB/s    0:00:00 (xfer#2, to-check=1/4)
test.jpg
      185883 100%   25.32MB/s    0:00:00 (xfer#3, to-check=0/4)
sent 115 bytes  received 186303 bytes  372836.00 bytes/sec
total size is 186147  speedup is 1.00
[root@localhost ~]#
可以看到同步了rsync.test文件过来了.另外其他2个文件也一并同步过来

演示: 服务端文件名不修改.往文件内新增文件.观察rsync是否能实行增量同步

1.在rsync服务端内的/var/www/abc/index.html文件新增一行内容:

[root@rabbitmqnode0 abc]# vim index.html
hello.This is nginx server for www.abc.com
hello world!
add a new line to test rsync   #新增一行

2.客户端在原有同步后的基础上再次执行命令:可以看见.只同步了Index.html文件.其他文件并没有复制

[root@rabbitmqnode1 ~]# /usr/bin/rsync -avz  --progress [email protected]::www /var/www/abc/ --password-file=/etc/rsync_server.pas
receiving incremental file list
./
index.html
          85 100%   83.01kB/s    0:00:00 (xfer#1, to-check=1/3)
sent 83 bytes  received 251 bytes  668.00 bytes/sec
total size is 106558  speedup is 319.04

查看文件:

[root@rabbitmqnode1 ~]# cat /var/www/abc/index.html
hello.This is nginx server for www.abc.com
hello world!
add a new line to test rsync

可见.即便文件名一致.只要文件内容有变化,仍然会同步到客户端.

二、以ssh方式同步文件

1.关掉服务端的rsync进程

ps -ef | grep rsync
root       2014      1  0 21:28 ?        00:00:00 rsync --daemon
root       6334   2588  0 22:37 pts/1    00:00:00 grep rsync
[root@localhost ~]# kill -9 2014
[root@localhost ~]# rm -rf /var/run/rsyncd.pid

2.客户端直接执行命令:

[root@localhost ~]# rsync -avz --progress -e ssh [email protected]:/var/www/abc /root
[email protected]'s password:
receiving incremental file list
abc/
abc/index.html
          91 100%   88.87kB/s    0:00:00 (xfer#1, to-check=2/4)
abc/rsync.test
         173 100%  168.95kB/s    0:00:00 (xfer#2, to-check=1/4)
abc/test.jpg
      185883 100%   25.32MB/s    0:00:00 (xfer#3, to-check=0/4)
sent 72 bytes  received 186254 bytes  53236.00 bytes/sec
total size is 186147  speedup is 1.00
[root@localhost ~]#

-e 表示 使用ssh协议root@ 表示服务器的本地用户(注意,这里是服务器本地真实用户)..这里的用法和普通的scp命令一致

可以看到文件已经同步过来了.且权限保持一致

[root@localhost ~]# ll /root/abc
total 192
-rw-r--r-- 1 root root     91 Dec 26 19:21 index.html
-rw-r--r-- 1 root root    173 Dec 27 15:07 rsync.test
-rw-r--r-- 1 root root 185883 Dec 26 19:21 test.jpg

注意:ssh模式不能像rsync daemon模式那样指定一个密码文件.如果想不输入密码.只能复制本机的公钥到rsync服务端相关用户下.

Note:

1.一般在生产中 在客户端同步的时候还需要加入个 –delete参数.表示如果本机相关目录下有某个文件.而这个文件在服务端上没有.那么就删除.这是为了保持和服务端完全同步

2.一般需要写一个crontab定时任务,每5分钟同步一次

/5 * /usr/bin/rsync -avz –progress [email protected]::www /var/www/abc/ –password-file=/etc/rsync_server.pas > /dev/null 2>&1

Tomcat调优指南

摘要: Tomcat安装:wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.31/bin/apache-tomcat-8.5.31.tar.gz 解压缩后直接在apache/bin目录下./startup.sh 启动小技巧:当出现Tomcat一直卡在启动页面时,可以是因为Java.security配置文件里写的是/dev/random,/dev/random和/dev/urandom是Linux系统中提供的随机伪设备,这两个设备的任务,是提供永不为空的随机字节数据流。

Tomcat安装:

wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.31/bin/apache-tomcat-8.5.31.tar.gz

未分类

解压缩后直接在apache/bin目录下./startup.sh

启动小技巧:
当出现Tomcat一直卡在启动页面时,可以是因为Java.security配置文件里写的是/dev/random,/dev/random和/dev/urandom是Linux系统中提供的随机伪设备,这两个设备的任务,是提供永不为空的随机字节数据流。很多解密程序与安全应用程序(如SSH Keys,SSL Keys等)需要它们提供的随机数据流。当使用/dev/random因为需要生成随机数如果没有完成这个随机数的创建就会一直卡在启动页面。建议找到jdk1.x.x_xx/jre/lib/security/Java.security文件,在文件中找到securerandom.source这个设置项,将其改为:
securerandom.source=file:/dev/unrandom

在 apache/conf/server.xml目录下 tomcat默认参数设置:

  • maxThreads:tomcat可用于请求处理的最大线程数,默认是200 —-连接数
  • minSpareThreads:tomcat初始线程数,即最小空闲线程数 == minProcessors相同
  • maxSpareThreads:tomcat最大空闲线程数,超过的会被关闭==maxThreads==maxProcessors
  • acceptCount:当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,超过这个数的请求将不予处理.默认100— 最大排队数

  • minProcessors:最小连接线程数,用于提高系统处理性能,默认值为10

  • maxProcessors:最大连接线程数,即:并发处理的最大请求数,默认值为75
  • enableLookups:是否反查域名,取值为:true或false。为了提高处理能力,应设置为false
  • connectionTimeout:网络连接超时,单位:毫秒。设置为0表示永不超时,这样设置有隐患的。通常可设置为30000毫秒。

其中和最大连接数相关的参数为maxProcessors和acceptCount。如果要加大并发连接数,应同时加大这两个参数。

最大连接数 maxThreads
tomcat同时处理的线程数。

配置依据:

(1)、部署的程序偏计算型,主要利用cpu资源,应该将该参数设置小一点,减小同一时间抢占cpu资源的线程个数。
(2)、部署的程序对io、数据库占用时间较长,线程处于等待的时间较长,应该将该参数调大一点,增加处理个数。
最大排队数 acceptCount
当tomcat的线程数达到maxThreads后,新的请求就会排队等待,超过排队数的请求会被拒绝。
我一般设置和maxThreads相同。

“maxPostSize”该参数限制了post方式上传文件的大小,当maxPostSize<=0时,POST方式上传的文件大小不会被限制,maxPostSize参数只有当request的Content-Type为“application/x-www-form-urlencoded”时起作用。
“maxHttpHeaderSize”来自于客户端请求的Request和Response的HTTP,http请求头信息的最大程度,超过此长度的部分不予处理,一般8K。
“maxThreads”客户请求最大线程数,Tomcat使用线程来处理接收的每个请求。这个值表示Tomcat可创建的最大的线程数。
“minSpareThreads”最小空闲线程数,Tomcat初始化时创建的 socket 线程数.
“maxSpareThreads”最大连接线程数,即:并发处理的最大请求数,默认值为75,一旦创建的线程超过这个值,Tomcat就会关闭不再需要的socket线程。
“minProcessors”最小空闲连接线程数,用于提高系统处理性能,默认值为 10。
“acceptCount”允许的最大连接数,应大于等于 maxProcessors ,默认值为 100。
“enableLookups”若设为true, 则支持域名解析,可把 ip 地址解析为主机名,为了提高处理能力,应设置为false。
“compression”打开压缩功能。
“compressionMinSize “启用压缩的输出内容大小,这里面默认为2KB
“compressableMimeType”压缩类型。
“connectionTimeout”网络连接超时,单位:毫秒。设置为 0 表示永不超时,这样设置有隐患的。通常可设置为 30000 毫秒。
“URIEncoding”URL统一编码 。
“redirectPort”这里系统默认的,它指定转发端口,如果当前只支持non-SSL请求,在需要安全通信的场所,将把客户请求转发至SSL的redirectPort端口。
“disableUploadTimeout”上传时是否使用超时机制,如果不指定,该属性为“false”。

上述配置读者可通过实际业务需求进行调整,达到tomcat性能最优,关于更多tomcat详细部署读者可参考笔者的该篇文章,希望能有所帮助。

vultr云主机ssh连接异常分析

vultr云主机突发莫名其妙的连接异常,ssh无法登陆,通过tcpdump抓包进行分析,特记录之。

实验准备

  • vultr云主机
  • ssh客户端
  • tcpdump抓包工具

实验步骤

客户端发起ssh连接

未分类

服务端抓包

未分类

实验分析

客户端发起TCP连接,服务端收到了三次握手的seq 1966603666包
服务器端把seq +1 返回ack 1966603667包
重点在于客户端没有收到服务器的ack确认,又重发了Flags [S], seq 1966603666
循环往复,直至SSH连接超时

那么问题来了,ACK包为啥会丢失呢?或许有一个神秘的东方力量在起作用

奇怪的是在境外的服务器可以实现SSH登录vultr主机

未分类

只有先Across the great firewall,你才能Reach every corner in the world.

后续

最初出现问题,本来计划提交工单,要求vultr解决。经过理性分析之后,放弃了这个做法,因为这个问题不是vultr能解决的。最后通过迁移主机更换IP恢复了访问。

rsync+inotify实现实时同步acgred.cn

一、无差异同步数据

1.首先,在实践实时同步之前我们先来了解一下rsync无差异同步
无差异推送数据加–delete

1)备份 –delete风险
本地有,远端就有。本地没有远端有也会删除,服务器端的目录数据可能会丢失

无差异拉取数据 www.gaimor.cn

2)代码发布、下载 –delete风险
远端有,本地就有。远端没有的本地有也会删除,本地的目录数据可能丢失

未分类

ps:上图会将远程端的/hzftp/test里的文件完全克隆到客户端的/data1目录中


多模块共享配置,只需新增模块目录地址即可,其他 www.acgred.cn


inotify实施(切记在客户端上安装)

前提:需要rsync daemon已经搭建完成的情况下,可以在服务端上推拉数据
inotifu安装:
检查服务端的rsync服务是正常启用的,并且可以在客户端往服务端推送文件
检查内核版本:uname -r

[root@localhost inotify]# ls 
max_queued_events  max_user_instances  max_user_watches
[root@localhost inotify]# ll /proc/sys/fs/inotify
总用量 0
-rw-r--r--. 1 root root 0 6月  27 22:58 max_queued_events   
-rw-r--r--. 1 root root 0 6月  27 22:58 max_user_instances  
-rw-r--r--. 1 root root 0 6月  27 22:58 max_user_watches

然后从互联网上下载inotify源码安装包
下载好后,解压:tar xf inotify-tools-3.14.tar.gz

cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify-tools-3.14
make && make install
ln -s /usr/local/inotify-tools-3.14 /usr/local/inotify
[root@localhost inotify]# ll /usr/local/inotify
总用量 4
drwxr-xr-x. 2 root root   43 6月  27 23:10 bin  #inotify执行命令
drwxr-xr-x. 3 root root   25 6月  27 23:10 include  #inotify所需头文件
drwxr-xr-x. 2 root root 4096 6月  27 23:10 lib  #动态链接库文件
drwxr-xr-x. 4 root root   26 6月  27 23:10 share   #帮助文档

使用inotify来监控目录文件,当被监控的目录发生变化会产生输出:
监听创建:create
监听删除:delete
监听写入:close_write
ps:当create和close_write同时使用时,创建文件会被监控两遍
======create,delete,close_write同时使用用逗号隔开=======
/usr/local/inotify-tools-3.14/bin/inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f' -e create /backup

未分类

开发inotify数据同步脚本
创建脚本存放路径:mkdir -p /server/scripts
进入脚本存放目录:cd /server/scripts

CentOS安装和配置Rsync进行文件同步

Liunx系统实现文件同步不需要搭建FTP这类的工具,只需要按照Rsync配置下文件就可以。

本文以Centos7.0为例。

1.首先关闭SELINUX(不关闭无法同步,权限太高了)

vi /etc/selinux/config #编辑防火墙配置文件
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存,退出
setenforce 0  #立即生效

2.服务端和客户端同时安装Rsync

yum install rsync xinetd #安装

3.客户端和服务端同时新增配置文件(centos7 默认没有了,得单独手工建,否则无法启动)

vim /etc/xinetd.d/rsync
service rsync
{
        disable              = no
        flags                  = IPv6
        socket_type      = stream
        wait                   = no
        user                   = root
        server                = /usr/bin/rsync
        server_args       = --daemon
        log_on_failure  += USERID
}

4.修改服务端配置给客户端调用

vim /etc/rsyncd.conf
log file = /var/log/rsyncd.log #日志文件位置,启动rsync后自动产生这个文件,无需提前创建

pidfile = /var/run/rsyncd.pid  #pid文件的存放位置

lock file = /var/run/rsync.lock  #支持max connections参数的锁文件

secrets file = /etc/rsync.pass  #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件

motd file = /etc/rsyncd.Motd  #rsync启动时欢迎信息页面文件位置(文件内容自定义)

[test] #自定义名称

path = /data/ #rsync服务端数据目录路径

comment =rsync data comment #对那个文件夹进行描述

uid = root #设置rsync运行权限为root 推荐使用 nobody

gid = root #设置rsync运行权限为root 推荐使用 nobody

port=873  #默认端口

use chroot = no #默认为true,修改为no,增加对目录文件软连接的备份

read only = no  #设置rsync服务端文件为读写权限

list = no #不显示rsync服务端资源列表

max connections = 200 #最大连接数

timeout = 600  #设置超时时间

auth users = test #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开

hosts allow = 192.168.21.129  #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

hosts deny = 192.168.21.254 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

5.新增同步用户的配置文件保存密码

vim /etc/rsync.pass
test:123456

6.对配置文件进行授权

chmod 600 /etc/rsyncd.conf  #设置文件所有者读取、写入权限
chmod 600 /etc/rsync.pass  #设置文件所有者读取、写入权限

7.重启Rsync 是用软件生效

systemctl restart xinetd

8.客户端开始同步

首先telnet端口:
telnet 172.16.120.18 83 

服务端同步文件到客户端:
rsync -avz  [email protected]::ftp  /data


客户端同步文件到服务端

rsync -av /data/  [email protected]:ftp 

其中/data/ 若后面不加”/” 那/data 就是表示本身同步过去,切记!

Rsync 数据同步工具应用指南

Rsync 简介

Rsync 是一款开源的,快速的,多功能的,可实现全量及增量(差异化备份)的本地或远程数据同步备份的优秀工具。

Rsync软件适用于Unix、Linux、Windows等多种操作系统。

1)可使本地和远程两台主机之间的数据快速复制同步镜像,远程备份的功能,这个功能类似ssh带scp命令,但又优于scp命令的功能,scp每次都是全量拷贝,而rsync可以增量拷贝。

2)rsync还可以在本地主机的不同分区或目录之间全量及增量的复制数据,

3)利用rsync还可以实现删除文件和目录的功能。相当于rm

4)rsync相当于scp,cp.rm但是还优于他们每一个命令。

在同步备份数据时,默认情况下rsync通过独特的“quick check” 算法,它仅同步大小或者最后修改时间发生变化的文件或目录,当然也可以是根据权限,属主等属性的变化同步,但需要指定相应的参数,甚至可以实现只同步一个文件里有变化的内容部分,所以可以实现快速的同步备份数据。

  • CentOS 5 rsync2.x 比对方法,把所有的文件比对一遍,然后进行同步。

  • CentOS 6 rsync3.x 比对方法,一边比对差异,一边对差异的部分进行同步。

Rsync 特性

1)支持拷贝特殊文件如链接文件,设备等。

2)可以有排除指定文件或目录同步的功能,相当于打包命令tar的排除功能。

3)可以做到保持源文件或目录的权限,时间,软硬链接,属主,组等属性均不改变 -p.

4)可以实现增量同步,即只同步发生变化的数据,因此数据传输的效率很高,tar -N.

5)可以使用rcp,rsh,ssh,等方式来配合传输文件(rsync本身不对数据加密)

6)可以通过soket(进程方式)传输文件和数据(服务端和客户端)*****

7)支持匿名的或认证的(无需系统用户)的进程模式传输,可实现方便安全的进程数据备份及镜像。

实时同步(解决存储服务器等单点问题)

利用rsync结合inotify的功能做实时的数据同步,根据存储服务器上目录的变化,把变化的数据通过inotify或sersync结合rsync命令,同步到备份服务器,还可以通过drbd方案以及双写的方案实现双机数据同步。

Rsync的工作方式

大致使用三种主要的传输数据的方式。

1)单个主机本地之间的数据传输(此时类似于cp命令的功能)

2)借助rcp,ssh等通道来传输数据(此时类似于scp命令的功能)

3)以守护进程(socket)的方式传输数据(这个是rsync自身的重要功能)

服务端与客户端安装 Rsync

修改主机名

hostname backup
vi /etc/sysconfig/network

安装 rsync 与 依赖

yum -y install rsync xinetd

vi /etc/xinetd.d/rsync

将yes 修改为no IPV6修改为IPV4

rsync 命令同步参数详解

local(本地)模式的命令参数

-v --verbose 详细模式输出,传输时的进度等信息。

-z --compress 传输时进行压缩以提高传输效率,--compress-level=NUM可按级别压缩

重要的命令

-a --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等价于-rtopgDl

-r 对子目录以递归模式,即目录下的所有目录都同样传输,注意是小写的r.

-o 保持文件属性信息

-p 保持文件权限

-g 保持文件属组信息

-P 显示同步的过程及传输时的进度等信息

-D 保持设备文件信息

-l 保持软连接


-avzP 提示:这里的 相当于 -vzrtopgDlP(还多了Dl功能)生产环境常用 

-avz 定时任务就不用输出过程了可以-az

需了解的命令

-e 使用的信道协议,指定替代rsh的shell程序,例如:ssh

--exclude=PATTERN 指定排除不需要传输的文件模式(和tar参数一样)

--exclude=file(文件名所在的目录文件)(和tar参数一样)

--delete 让目标目录SRC和源目录数据DST一致。

注意:/tmp/ rsync如果tmp/ 加上斜线的话就表示只选中斜线后的文件,不包含tmp。

如果不加上斜线 tmp 那么就是包含目录本身和目录之下的文件。

做数据同步容易将带宽占满,导致网站无法访问

rsync scp ftp 都有限速功能

解决:man rsync里面有一个限速的参数。

我之前刚做运维的时候,在备份数据时,没考虑业务低谷时间点,将带宽占满了,导致网站无法正常访问。发现问题之后,我立即停止数据备份,然后man 了一下,才发现rsync有限速参数。

尽量不要在业务高并发的时候做备份,要在业务低谷时间段进行,限速备份。

当然,scp,ftp都有限速的功能。

借助ssh通道在不同主机之间传输数据

man rsyncd.conf

使用-e参数,利用ssh隧道进行文件传输
[root@backup ~]# rsync -avz /etc/hosts -e 'ssh -p 22' [email protected]:/mnt

可以使用ssh key 免密钥登录,然后可以做定时任务。

优化ssh (让连接服务器进行rsync更快)

[root@backup ~]# vim /etc/ssh/sshd_config 

GSSAPIAuthentication no   (把这个的注释去掉,也就是打开)
#GSSAPIAuthentication yes(把这个注释掉,也就是关闭)
UseDNS no (改成no)

以守护进程(socket)的方式传输数据

rsync 命令使用用法

rsync -参数 用户名@同步服务器的IP::rsyncd.conf中那个方括号里的内容(配置文件中的模块名) 本地存放路径。

rsync -avzP [email protected]::nemo /backup

服务器端的配置过程

安装rysnc

yum -y install rsync xinetd

vi /etc/xinetd.d/rsync

将yes 修改为no IPV6修改为IPV4

1、查看rsync安装包

[root@backup ~]# rpm -qa rsync

2、添加rsync服务的用户,管理本地目录

[root@backup ~]# useradd rsync -s /sbin/nologin -M
[root@backup ~]# tail -1 /etc/passwd
rsync:x:501:501::/home/rsync:/sbin/nologin

3、生成rsyncd.conf配置文件

查看 rsyncd.conf

[root@backup backup]# cat /etc/rsyncd.conf 
config_______________start
#15:01 2007-6-5
#rsyncd.conf start##
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]
path = /backup
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
#rsync_config____________end

配置rsyncd.conf

[root@backup ~]# vim /etc/rsyncd.conf 
#rsync_config_______________start
#15:01 2018-6-5
#rsyncd.conf start##
uid = rsync #用户远端的命令使用rsync访问共享目录
gid = rsync #用户组
use chroot = no #安全相关
max connections = 200 #最大连接数
timeout = 300 #超时时间(单位/秒)
pid file = /var/run/rsyncd.pid  #(进程号对应的进程号文件)
lock file = /var/run/rsync.lock #锁文件,防止文件不一致
log file = /var/log/rsyncd.log  #日志文件
[backup]  #模块名称
path = /backup  #服务器端提供访问的目录
ignore errors #忽略错误
read only = false #可写
list = false  #不让列表(相当于ls)
hosts allow = 172.16.1.0/24 #允许的网段
hosts deny = 0.0.0.0/32  #拒绝的网段
auth users = rsync_backup #连接的虚拟用户,非系统用户
secrets file = /etc/rsync.password #虚拟用户的账号密码文件
#rsync_config____________end

4、rsyncd.conf的auth users配置账户,远程连接

并根据secrets file参数生成密码文件

法一
echo "rsync_backup:cloudbility" >/etc/rsync.password
cat /etc/rsync.password

法二
[root@backup ~]# vim  /etc/rsync.password
rsync_backup:cloudbility

[root@backup ~]# cat /etc/rsync.password
rsync_backup:cloudbility

5、为密码文件配置权限

chmod 600 /etc/rsync.password
ls -l /etc/rsync.password

6、创建共享的目录并授权rsync服务管理

法一:
[root@backup ~]# mkdir /backup

[root@backup ~]# ls -ld /backup/
drwxr-xr-x 2 root root 4096 1月  16 20:08 /backup/
(如果不修改权限的话,远程访问过来是用rsync但是这个目录的属组和属主都收root 就无法把远方的数据推送过来,没有写权限。)

[root@backup ~]# chown rsync.rsync /backup/
(更改属组和属主)

[root@backup ~]# ls -ld /backup/
drwxr-xr-x 2 rsync rsync 4096 1月  16 20:08 /backup/

法二:
mkdir /backup -p
chown -R rsync.rsync /backup
如果没有/backup目录,就会chdir failed.(失败的)

7、启动rsync服务并检查

[root@backup ~]# rsync --daemon  
开启rsync服务

[root@backup ~]# ps -ef|grep rsync|grep -v grep
root       5116   5100  0 19:14 pts/0    00:00:00 vim /etc/rsyncd.conf
root       5193      1  0 20:05 ?        00:00:00 rsync --daemon
(为什么是root在运行呢?因为远程访问的用户才会使用rsync来使用,上面配置文件有写)

查看运行端口
[root@backup ~]# lsof -i :873
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   5193 root    3u  IPv4  19935      0t0  TCP *:rsync (LISTEN)
rsync   5193 root    5u  IPv6  19936      0t0  TCP *:rsync (LISTEN)

[root@backup ~]# netstat -lntup|grep 873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      5193/rsync          
tcp        0      0 :::873                      :::*                        LISTEN      5193/rsync

8、加入开机自启动

[root@backup ~]# echo "/usr/bin/rsync --damon" >>/etc/rc.local

[root@backup ~]# tail -1 /etc/rc.local
/usr/bin/rsync --damon

排错过程

  1. 输出结果

  2. 日志 tail /var/log/rsyncd.log

  3. 熟练部署过程(原理)

rsync客户端操作方法

1、生成连接服务器的密码文件

法一:
echo "cloudbility" >/etc/rsync.password

法二:
[root@nfs01 ~]# vim /etc/rsync.password

[root@nfs01 ~]# cat /etc/rsync.password
cloudbility

2、为密码文件配置权限

[root@nfs01 ~]# chmod 600 /etc/rsync.password

[root@nfs01 ~]# ll /etc/rsync.password
-rw------- 1 root root 7 1月  16 20:58 /etc/rsync.password

3、同步文件

创建样本

[root@nfs01 ~]# mkdir /backup -p

[root@nfs01 ~]# cd /backup/


[root@nfs01 backup]# touch stu{01..100}

[root@nfs01 backup]# ls
stu001  stu010  stu02   stu029  stu038  stu047  stu056  stu065  stu074  stu083  stu092
stu002  stu011  stu020  stu03   stu039  stu048  stu057  stu066  stu075  stu084  stu093

推送到backup server:

法一:需要密码
[root@nfs01 backup]# rsync -avz /backup/ [email protected]::backup/(模块名)

法二:无需密码
[root@nfs01 backup]# rsync -avz /backup/ [email protected]::backup/(模块名) --password-file=/etc/rsync.password

法三:无需密码
[root@nfs01 backup]# rsync -avz /backup/ rsync://[email protected]/backup/(模块名) --password-file=/etc/rsync.password

从backup server拉取/backup下的文件到本地/backup下。

法一:需要密码
[root@nfs01 backup]# rsync -avz [email protected]::backup(模块名) /backup/ 

法二:免密
[root@nfs01 backup]# rsync -avz [email protected]::backup(模块名) /backup/ --password-file=/etc/rsync.password  

法三:免密
[root@nfs01 backup]# rsync -avz rsync://[email protected]/backup/(模块名) /backup/ --password-file=/etc/rsync.password

提示上述的backup为模块名,不是路径

增加模块

[root@backup backup]# vim /etc/rsyncd.conf
#15:01 2018-6-5
#rsyncd.conf start##
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
path = /backup
[cloudbility]
path = /cloudbility
##rsync_config____________end

排除同步:

排除推送

排除单个文件:

--exclude=a  (排除a)

[root@nfs01 backup]# rsync -avz --exclude=a /backup/ [email protected]::backup/ --password-file=/etc/rsync.password
sending incremental file list
./bcdefg

排除多个文件:

1)
排除a 和b 
[root@nfs01 backup]# rsync -avz --exclude={a,b} /backup/ [email protected]::backup/ --password-file=/etc/rsync.password
sending incremental file list
./cdefg

2)
排除连续的a-f
[root@nfs01 backup]# rsync -avz --exclude={a..f} /backup/ [email protected]::backup/ --password-file=/etc/rsync.password
sending incremental file list
./g

sent 75 bytes  received 30 bytes  210.00 bytes/sec
total size is 0  speedup is 0.00

1)拉取和推送都可以排除。

2)也可以服务端排除,配置文件里参数。

3)exclude=a b c d

完全同步:无差异同步–delete

[root@nfs01 backup]# rsync -avz --delete /backup/ [email protected]::backup/ --password-file=/etc/rsync.password
sending incremental file list

abcdef

rsync三种工作模式

1)local(本地模式):(cd,rm)

2)通道模式:
 rsync -avzP -e 'ssh -p 22' /etc [email protected]:/tmp/
 一般配合ssh key免密钥传输,结合定时任务。

 3)daemon模式
内网不需要加密,加密性能有损失。

如果要外网的话使用vpn(PPTP。openVPN,ipsec)

rsync服务模式故障常见问题解答

1)小 BUG

[root@backup backup]# vim /etc/rsyncd.conf
#hosts deny = 0.0.0.0/32
把 hosts deny(拒绝的ip段)注释掉,因为当

hosts allow = 172.16.1.0/24
#hosts deny = 0.0.0.0/32
这两个在一起的时候,发现10段的ip 也能把数据推送到backup server。所以必须注释掉
hosts deny。

提示:更改配置文件之后要重启服务,因为每次Linux都是把配置文件放到内存。
先杀死进程,然后检查
[root@backup backup]# pkill rsync
[root@backup backup]# lsof -i :873

重启再检查看看
[root@backup backup]# rsync --daemon
[root@backup backup]# lsof -i :873
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   7718 root    4u  IPv4  33811      0t0  TCP *:rsync (LISTEN)
rsync   7718 root    5u  IPv6  33812      0t0  TCP *:rsync (LISTEN)

2)服务端没有这个目录

提示报错信息

[root@nfs01 backup]# rsync -avz /backup/ [email protected]::backup/ --password-file=/etc/rsync.password 
@ERROR: chdir failed


解决方法:
服务端创建目录
[root@backup backup]# mkdir /backup
[root@backup backup]# ll -d /backup/
drwxr-xr-x 2 root root 4096 Jan 17 15:52 /backup/

[root@backup backup]# chown -R rsync.rsync /backup/ 
(/etc/rsyncd.conf配置文件里的uid和gid)

3)权限不够

提示报错信息

[root@nfs01 backup]# rsync -avz /backup/ [email protected]::backup/ --password-file=/etc/rsync.password 
sending incremental file list
./
rsync: failed to set times on "." (in backup): Operation not permitted (1)
stu1
stu10
stu2
stu3
stu4
stu5
stu6
stu7
stu8
stu9
rsync: mkstemp ".stu1.oraZ3Y" (in backup) failed: Permission denied (13)
rsync: mkstemp ".stu10.n1jKm7" (in backup) failed: Permission denied (13)
rsync: mkstemp ".stu2.dLFwFf" (in backup) failed: Permission denied (13)
rsync: mkstemp ".stu3.LKKjYn" (in backup) failed: Permission denied (13)
rsync: mkstemp ".stu4.nSI7gw" (in backup) failed: Permission denied (13)
rsync: mkstemp ".stu5.p4CWzE" (in backup) failed: Permission denied (13)
rsync: mkstemp ".stu6.HE5OSM" (in backup) failed: Permission denied (13)
rsync: mkstemp ".stu7.jGRIbV" (in backup) failed: Permission denied (13)
rsync: mkstemp ".stu8.p4cDu3" (in backup) failed: Permission denied (13)
rsync: mkstemp ".stu9.EZbyNb" (in backup) failed: Permission denied (13)

sent 467 bytes  received 201 bytes  1336.00 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]

解决方法
[root@backup backup]# ll -d /backup/
drwxr-xr-x 2 root root 4096 Jan 17 15:52 /backup/

[root@backup backup]# chown -R rsync.rsync /backup/ 
(/etc/rsyncd.conf配置文件里的uid和gid)

4)没有创建uid

提示错误信息

[root@nfs01 backup]# rsync -avz /backup/ [email protected]::backup/ --password-file=/etc/rsync.password 
@ERROR: invalid uid rsync


解决方法:
[root@backup backup]# useradd rsync -s /sbin/nologin -M

5)客户端/etc/rsync.password 配置文件里的密码错误(注意空格)

[root@nfs01 backup]# rsync -avz /backup/ [email protected]::backup/ --password-file=/etc/rsync.password 
@ERROR: auth failed on module backup

解决:
查看服务端的日志配置文件的报错信息。
[root@backup backup]# cat /var/log/rsyncd.log  
2017/01/17 16:04:36 [7813] auth failed on module backup from unknown (172.16.1.31): password mismatch

提示我们密码错误:
查看服务器端的配置文件和密码,然后,再看客户端的。
[root@backup backup]# vim /etc/rsync.password 
rsync_backup:cloudbility

6)连接被拒绝

提示信息

[root@nfs01 backup]# rsync -avz /backup/ [email protected]::cloudbility/
rsync: failed to connect to 172.16.1.41: Connection refused (111)

解决,
1)服务端防火墙是否关闭iptables

2)873端口是否开放。
重启rsync服务。
[root@backup cloudbility]# rsync --daemon
[root@backup cloudbility]# lsof -i :873

rsync守护进程(daemon)服务传输数据排错思路:

rsync服务端排错思路

1)查看rsync服务配置文件路径是否正确,正确的默认路径为:/etc/rsyncd.conf

2)查看配置文件里的host allow.host deny允许的网段是否允许客户端访问的IP网段。

3)查看配置文件中path参数里的路径是否存在,权限是否正确(正常应为位置文件中的UID参数对应的属主和组)

4)查看rsync服务是否启动,查看命令为:ps -ef|grep rsync. 端口是否存在 netstat -lnt|grep 873.

5)查看iptables防火墙和SELinux是否开启允许rsync服务通过,也可以考虑关闭。

6)查看服务端rsync配置的密码文件权限是否是600;密码文件格式是否正确。 正确格式为 用户名:密码 。文件路径和配置文件里的secrect files参数对应。

7)如果是推送数据,要查看下,配置rsyncd.conf文件中用户是否对模块下的目录有可读写权限

rsync客户端排除思路

1)查看客户端rsync配置的密码文件是否为600权限,密码文件格式是否正确。
注意,仅西药有密码,并且和服务端的密码一致。

2)用telnet连接rsync服务器IP地址873端口,查看服务是否启动
(可测试服务端防火墙是都阻挡)。 telnet 10.0.0.41 873

3)客户端执行命令时rsync -avzP [email protected]::cloudbility/test/test --password-file=/etc/rsync.password
此命令的细节要记清楚,尤其是10.0.0.41::cloudbility/test/处的双冒号及随其后的cloudbility
的模块名称;

rsync优缺点:

rsync优点:

1)增量备份,支持socket(daemon守护进程),集中备份(支持推拉,都是以客户端为参照物);

2)远程SEHLL通道模式还可以加密(SSH)传输,socket(daemon守护进程)需要加密传输,
可以利用VPN服务或ipsec服务;

rsync缺点:

1)大量小文件同步的时候,比对时间较长,有的时候,rsync进程可能会停止。

2)同步大文件,10G这样的大文件有时也会出现问题,中断。未完整同步之前,是隐藏文件.
可以通过续传等参数实现传输,

一次性远程拷贝可以用scp;

(完)

rsync的小坑——请绕过

今天磁盘满了,打算将占磁盘大的文件给移走,再采用软连接的。同步的时候出现了一点小问题。

第一天:

先将要同步的文件给同步了一遍。

执行命令

rsync -auv /data/mysql/game /data1/mysql
rsync -auv /data/mysql/integral /data1/mysql
rsync -auv /data/mysql/interact /data1/mysql
rsync -auv /data/mysql/match /data1/mysql
rsync -auv /data/mysql/sns_admin /data1/mysql
rsync -auv /data/mysql/stock /data1/mysq

第二天:

又同步了一遍

rsync -auv /data/mysql/game /data1/mysql/game
rsync -auv /data/mysql/integral /data1/mysql/integral
rsync -auv /data/mysql/interact /data1/mysql/interact
rsync -auv /data/mysql/match /data1/mysql/match
rsync -auv /data/mysql/sns_admin /data1/mysql/sns_admin 
rsync -auv /data/mysql/stock /data1/mysql/stock

快要同步完的时候,检查文件大小,发现文件大了一倍。

原来第二步命令将/data/mysql/game文件 放在/data1/mysql/game文件下了。

这也不算是rsync的坑吧

应该这样这行

rsync -auv /data/mysql/game/ /data1/mysql/game/
rsync -auv /data/mysql/integral/ /data1/mysql/integral/
rsync -auv /data/mysql/interact/ /data1/mysql/interact/
rsync -auv /data/mysql/match/ /data1/mysql/match/
rsync -auv /data/mysql/sns_admin/ /data1/mysql/sns_admin/ 
rsync -auv /data/mysql/stock/ /data1/mysql/stock/

或者执行和昨天一样的命令

            rsync -auv /data/mysql/game         /data1/mysql
            rsync -auv /data/mysql/integral    /data1/mysql
            rsync -auv /data/mysql/interact     /data1/mysql
            rsync -auv /data/mysql/match        /data1/mysql
            rsync -auv /data/mysql/sns_admin    /data1/mysql
            rsync -auv /data/mysql/stock        /data1/mysq 

就可以了。

小结:

1、使用rsync同步,只要之前同步的内容和现在同步的内容有一点改变,就会重新全量同步,因此,同步的时候尽量找长时间没有变化的大文件
2、rsync不会追加文件

python的BaseHTTPServer模块接收post请求

#!/usr/bin/python
#encoding=utf-8
'''
基于BaseHTTPServer的http server实现,包括get,post方法,get参数接收,post参数接收。
'''
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import io,shutil  
import urllib
import os, sys

class MyRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        mpath,margs=urllib.splitquery(self.path) # ?分割
        self.do_action(mpath, margs)

    def do_POST(self):
        mpath,margs=urllib.splitquery(self.path)
        datas = self.rfile.read(int(self.headers['content-length']))
        self.do_action(mpath, datas)

    def do_action(self, path, args):
            self.outputtxt(path + args )

    def outputtxt(self, content):
        #指定返回编码
        enc = "UTF-8"
        content = content.encode(enc)          
        f = io.BytesIO()
        f.write(content)
        f.seek(0)  
        self.send_response(200)  
        self.send_header("Content-type", "text/html; charset=%s" % enc)  
        self.send_header("Content-Length", str(len(content)))  
        self.end_headers()  
        shutil.copyfileobj(f,self.wfile)

配置一个nginx+php-fpm的web服务器

一、基本信息

  • 系统(L):CentOS 6.9 #下载地址:http://mirrors.sohu.com
  • Web服务器(N):NGINX 1.14.0 #下载地址:http://nginx.org/en/download.html
  • 数据库服务器(M):MySQL 5.6.40 #下载地址:https://dev.mysql.com/downloads/mysql
  • PHP-FPM服务器(P):php-5.6.8.tar.gz #下载地址:http://mirrors.sohu.com/php/
  • OPENSSL:openssl-1.0.2o.tar.gz #下载地址:https://www.openssl.org/source/

指定服务安装的通用位置

mkdir /usr/local/services
SERVICE_PATH=/usr/local/services

创建服务运行的账户

useradd -r -M -s /sbin/nologin www

安装所需依赖包

yum -y install pcre pcre-devel 
gperftools gcc zlib-devel 
libxml2 libxml2-devel 
bzip2 bzip2-devel 
curl curl-devel 
libjpeg-devel libjpeg 
libpng-devel libpng 
freetype freetype-devel 
libmcrypt libmcrypt-devel 
openssl-devel

二、软件安装配置

1、NGINX+OPENSSL安装

下载解压NGINX+OPENSSL

NGINX_URL="http://nginx.org/download/nginx-1.14.0.tar.gz"
OPENSSL_URL="https://www.openssl.org/source/openssl-1.1.0h.tar.gz"

wget -P ${SERVICE_PATH} ${NGINX_URL} && tar -zxvf ${SERVICE_PATH}/nginx*.tar.gz -C ${SERVICE_PATH}
wget -P ${SERVICE_PATH} ${OPENSSL_URL} && tar -zxvf ${SERVICE_PATH}/openssl*.gz -C ${SERVICE_PATH}

编译安装NGINX

cd ${SERVICE_PATH}/nginx-*;./configure 
--prefix=${SERVICE_PATH}/nginx 
--user=www --group=www 
--with-http_stub_status_module 
--with-http_ssl_module 
--with-http_flv_module 
--with-pcre 
--with-http_gzip_static_module 
--with-openssl=${SERVICE_PATH}/openssl* 
--with-http_realip_module 
--with-google_perftools_module 
--without-select_module 
--without-mail_pop3_module 
--without-mail_imap_module 
--without-mail_smtp_module 
--without-poll_module 
--without-http_autoindex_module 
--without-http_geo_module 
--without-http_uwsgi_module 
--without-http_scgi_module 
--without-http_memcached_module 
--with-cc-opt='-O2' && cd ${SERVICE_PATH}/nginx-*;make && make install

NGINX+OPENSSL安装完成后的清理与其他配置

ln -sv ${SERVICE_PATH}/nginx /usr/local/
rm -rf ${SERVICE_PATH}/nginx/conf/*.default
cd ${SERVICE_PATH} ; rm -rf nginx*.tar.gz openssl*.tar.gz

写入主配置文件nginx.conf(配置已优化)

cat << EOF >/usr/local/nginx/conf/nginx.conf
user www;
worker_processes WORKERNUMBER;
worker_cpu_affinity auto;
worker_rlimit_nofile 655350;

error_log /var/log/nginx_error.log;
pid /tmp/nginx.pid;

google_perftools_profiles /tmp/tcmalloc;

events {
use epoll;
worker_connections 655350;
multi_accept on;
}

http {
charset utf-8;
include mime.types;
default_type text/html;

log_format main '"$remote_addr" - [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" '
'"$sent_http_server_name $upstream_response_time" '
'$request_time '
'$args';


sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 120;
client_body_buffer_size 512k;
client_header_buffer_size 64k;
large_client_header_buffers 4 32k;
client_max_body_size 300M;
client_header_timeout 15s;
client_body_timeout 50s;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
server_names_hash_max_size 2048;
server_names_hash_bucket_size 256;
server_tokens off;
gzip on;
gzip_proxied any;
gzip_min_length 1024;
gzip_buffers 4 8k;
gzip_comp_level 9;
gzip_disable "MSIE [1-6].";
gzip_types application/json test/html text/plain text/css application/font-woff application/pdf application/octet-stream application/x-javascript application/javascript application/xml text/javascript;
fastcgi_cache_path /dev/shm/ levels=1:2 keys_zone=fastcgicache:512m inactive=10m max_size=3g;
fastcgi_cache_lock on;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_send_timeout 300;
fastcgi_connect_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 256k;
fastcgi_buffers 4 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;


include vhost/*.conf;
}
EOF

NGINX worker进程数配置,指定为逻辑CPU数量的2倍

THREAD=`expr $(grep process /proc/cpuinfo |wc -l) * 2`
sed -i s"/WORKERNUMBER/$THREAD/" ${SERVICE_PATH}/nginx/conf/nginx.conf

2、PHP-FPM安装

下载并解压PHP-FPM软件

FPM_URL="http://mirrors.sohu.com/php/php-5.6.8.tar.gz"
wget -P ${SERVICE_PATH} ${FPM_URL} && tar -zxvf ${SERVICE_PATH}/php*.tar.gz -C ${SERVICE_PATH}

编译安装PHP-FPM

cd ${SERVICE_PATH}/php-*;./configure 
--prefix=${SERVICE_PATH}/php 
--with-gd 
--with-mcrypt 
--with-mysql=mysqlnd 
--with-mysqli=mysqlnd 
--with-pdo-mysql=mysqlnd 
--enable-maintainer-zts 
--enable-ftp 
--enable-zip 
--with-bz2 
-with-iconv-dir 
--with-freetype-dir 
--with-jpeg-dir 
--with-png-dir 
--with-config-file-path=${SERVICE_PATH}/php 
--enable-mbstring 
--enable-fpm 
--with-fpm-user=www 
--with-fpm-group=www 
--disable-debug 
--enable-opcache 
--enable-soap 
--with-zlib 
--with-libxml-dir=/usr 
--enable-xml 
--disable-rpath 
--enable-bcmath 
--enable-shmop 
--enable-sysvsem 
--enable-inline-optimization 
--with-curl 
--enable-mbregex 
--enable-gd-native-ttf 
--with-openssl 
--with-mhash 
--enable-pcntl 
--enable-sockets 
--with-xmlrpc 
--with-pear 
--with-gettext 
--disable-fileinfo && cd ${SERVICE_PATH}/php-*;make && make install

若FPM程序有插件需求,如mongo或redis连接插件,则可通过pecl安装php相关插件

${SERVICE_PATH}/php/bin/pecl install mongo || exit

${SERVICE_PATH}/php/bin/pecl install redis || exit

安装完成后的配置清理

ln -sv ${SERVICE_PATH}/php /usr/local/

php.ini配置文件写入(配置已优化)

cat << EOF >${SERVICE_PATH}/php/php.ini 
[PHP]
engine = On
short_open_tag = Off
asp_tags = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = 17
disable_functions = shell_exec,phpinfo,exec
disable_classes =
zend.enable_gc = On
expose_php = Off
max_execution_time = 60
max_input_time = 60
memory_limit = 128M
error_reporting = E_WARING & ERROR
display_errors = Off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 2048
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
html_errors = Off
error_log = /var/log/php_errors.log
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
cgi.fix_pathinfo=0
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = Off
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
[filter]
[iconv]
[intl]
[sqlite]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQL]
mysql.allow_local_infile = On
mysql.allow_persistent = On
mysql.cache_size = 2000
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port =
mysql.default_socket =
mysql.default_host =
mysql.default_user =
mysql.default_password =
mysql.connect_timeout = 60
mysql.trace_mode = Off
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = Off
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[Sybase-CT]
sybct.allow_persistent = On
sybct.max_persistent = -1
sybct.max_links = -1
sybct.min_server_severity = 10
sybct.min_client_severity = 10
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.save_path = "/tmp"
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"
[MSSQL]
mssql.allow_persistent = On
mssql.max_persistent = -1
mssql.max_links = -1
mssql.min_error_severity = 10
mssql.min_message_severity = 10
mssql.compatibility_mode = Off
mssql.secure_connection = Off
[Assertion]
[COM]
[mbstring]
[gd]
gd.jpeg_ignore_warning = 0
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[opcache]
opcache.enable=1
opcache.enable_cli=0
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.validate_timestamps=1
opcache.revalidate_freq=30
opcache.fast_shutdown=1
opcache.enable_file_override=1
[curl]
[openssl]
extension_dir='${SERVICE_PATH}/php/lib/php/extensions/'
;extension=mongo.so
;extension=redis.so
EOF

php-fpm.conf配置文件写入(配置已优化)

cat << EOF >${SERVICE_PATH}/php/etc/php-fpm.conf
[global]
error_log = /var/log/php-fpm-error.log
log_level = warning
process_control_timeout = 10
rlimit_files = 655350
events.mechanism = epoll
[www]
user = www
group = www
listen = /dev/shm/php-fpm.sock
listen.backlog = 2048
listen.owner = www
listen.group = www
listen.mode = 0660
pm = dynamic
pm.max_children = 200
pm.start_servers = 105
pm.min_spare_servers = 10
pm.max_spare_servers = 200
pm.process_idle_timeout = 10s;
pm.max_requests = 1000
pm.status_path = /fpmstatus
ping.path = /ping
ping.response = pong
slowlog = /var/log/php-slow-$pool.log
request_slowlog_timeout = 10
request_terminate_timeout = 0
rlimit_files = 655350
security.limit_extensions = .php
EOF

三、基于以上配置PHP网站

mkdir /usr/local/nginx/conf/vhost
cat << EOF > /usr/local/nginx/conf/vhost/erbiao.ex.com.conf
server
{
listen 80 backlog=1024;
server_name erbiao.ex.com;
index index.php index.html ;
root /www/web/;
access_log off;
add_header Server-Name WEBerbiaoEX;

location ~ .php {
fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;


location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*.(js|css)?$
{
expires 12h;
}

}

}
EOF

若在同一服务器运行nginx和php-fpm,并发量不超过1000,选择unix socket,如此可避免一些检查操作(路由等),因此更快,更轻。若是高并发业务,则选择使用更可靠的tcp socket,以负载均衡、内核优化等运维手段维持效率

四、启动服务

启动nginx和php-fpm

/usr/local/nginx/sbin/nginx
/usr/local/php-fpm/sbin/php-fpm

命令其他选项

nginx
├── -s选项,向主进程发送信号
|   ├── reload参数,重新加载配置文件
|   ├── stop参数,快速停止nginx
|   ├── reopen参数,重新打开日志文件
|   ├── quit参数,Nginx在退出前完成已经接受的连接请求
├── -t选项,检查配置文件是否正确
├── -c选项,用于指定特定的配置文件并启动nginx
├── -V选项(大写),显示nginx编译选项与版本信息

php-fpm
├── -t选项,检查配置文件是否正确
├── -m选项,显示所有已安装模块
├── -i选项,显示PHP详细信息
├── -v选项,显示版本信息