案例需求
代码分发服务器 172.18.20.78
PHP后端分布式服务器所在网段172.18.20.0/24
需要:
1、将代码自动分发到PHP后端服务器
2、其中qcms系统为老系统,需要将其中的上传的图片文件实时同步到负载均衡服务器[nginx](这里和代码分发服务器是同一台)
3、在负载均衡服务器上使用workerman将实时同步过来的老系统图片文件再同步到七牛云,然后删除
4、使用nginx重写将不存在的图片301跳转到七牛云
一、完成代码分发(代码在/data/www/laravel 和 /data/www/qcms)
代码分发服务器rsync配置
/etc/rsyncd.conf
uid = www
gid = www
port = 873
use chroot = no
max connections = 200
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[www]
path = /data/www
comment = from source mirror server to backend instance server
ignore errors = yes
read only = no
list = no
timeout = 600
auth users = php
secrets file = /etc/rsyncd.passwd
hosts allow = 172.18.20.0/24
hosts deny = 0.0.0.0/32
systemctl enable rsyncd
systemctl start rsyncd
客户端rsync排除文件配置
/data/www/rsync.exclude
laravel/storage/
laravel/bootstrap/cache/
qcms/Public/uploads/
qcms/Data/Cache/
客户端rsync crontab配置
* * * * * rsync -rltuvz php@172.18.20.78::www/rsync.exclude /data/www/rsync.exclude --password-file=/etc/rsync.passwd
* * * * * rsync -rltuvz --exclude-from='/data/www/rsync.exclude' php@172.18.20.78::www /data/www/ --password-file=/etc/rsync.passwd
二、客户端系统上传文件实时同步到网关
yum install lsyncd -y
lsyncd 配置,vi /etc/lsyncd.conf
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 4,
}
sync {
default.rsync,
source = "/data/www",
target = "php@172.18.20.78::www",
delete= false,
excludeFrom = "/data/www/lsyncd.exclude",
delay = 0,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
password_file = "/etc/rsync.passwd",
_extra = {"--bwlimit=200"}
}
}
vi /data/lsyncd.exclude 配置
storage/
bootstrap/cache/
Data/
启动服务
systemctl enable lsyncd
systemctl start lsyncd
如果启动lsyncd,报错 rsync error: error in socket IO (code 10),可尝试重启代码分发服务器rsyncd服务