nginx+NFS+rsync
█ 1、nginx 虚拟主机的搭建(A 服务器) 通过在 windows 客户端设置 host 方式(代替 DNS)实现域名解析完成如下工作需求: a.当在浏览器输入 http://www.etiantian.org 或 http://etiantian.org 浏览器页面返回内容为 http://www.etiantian.org 字符串,下同。 b.当在浏览器输入 http://bbs.etiantian.org 浏览器页面返回内容 http://bbs.etiantian.org c.当在浏览器输入 http://blog.etiantian.org 浏览器页面返回内容 http://blog.etiantian.org 硬性要求(老男孩给大家的定的要求) : 1)虚拟主机目录目录说明 a.www.etiantian.org 的站点目录为/data/www/www b.bbs.etiantian.org 的站点目录为/data/www/bbs c.blog.etiantian.org 的站点目录为/data/www/blog 2)日志配置 访问日志放到/app/logs 目录,请按日期对 nginx 访问日志(access log)实施按天日志轮询 3)备份配置 将 nginx 的站点目录打包备份,每日 00 点 10 分推送到备份服务器(必须要有)上。 由于本地磁盘空间有限,要求仅在本地保留 3 份备份,但在备份服务器 B 上要保留 180 天的备份数据,备份的数据同时包括系统配置文件、程序目录及访问日志文件 (/etc/rc.local、/var/spool、/server/scripts、/etc/sysconfig/iptables、/data/www、/app/logs 等) 解答: █ 2、备份服务器搭建(B 服务器) a.备份目录为/backup,要求所有服务器数据备份到此目录下,并且通过程序检查备份目录备 份数据的完整性,把检查结果于每日早晨 7 点发给管理员及老大信箱。 所有备份数据,保留 180 天,但是每周五的备份数据永远保留。 b.作为 C 服务器 NFS 数据的实时热备服务器,当 NFS 宕机后可以接管 NFS 服务器提供服务。 █ 3、NFS 存储服务器搭建(C 服务器) a.在 web A 服务器上每个站点目录下创建一个 upload 目录,将每个站点的 upload 目录挂载 到 NFS 对应数据目录,注意:实现开机挂载。 即 NFS 为每个站点目录下的 upload 目录提供数据存储。 b.NFS 上所有存储的数据,需要实时同步到备份服务器搭建(B 服务器)的/backup 目录。 █ 4、批量分发管理服务器搭建(D 服务器) a.通过 D 服务器可以批量分发指定文件(如 host)到所有服务器(A\B\C)的指定目录下。 b.通过 D 服务器可以查看所有服务器的相关信息(IP,内存,负载,IO 等) 提示:本考试题可能会涉及 nginx、定时任务 crond、rsync 服务、NFS、inotify、SSH 及简 单 shell 脚本编写能力、备份解决方案、批量管理方案、集群共享方案等方面的技术协调配 合。 |
1.系统优化
(1) ssh 优化
[[email protected] ~]# mkdir /data/root/backup/ -p 创建备份文件夹 [[email protected] ~]# cp /etc/ssh/sshd_config /data/root/backup/ 操作前备份 [[email protected] ~]# sed -i "s/#UseDNS yes/UseDNS no/g" /etc/ssh/sshd_config |
(2)修改或查看字符集(en_UTF-8就OK)
[[email protected] ~]# echo $LANG en_US.UTF-8 |
(3)配置服务器和互联网时间同步
[[email protected] ~]# date 查看时间 Mon Jun 22 19:25:48 CST 2015 [[email protected] ~]# mkdir /server/scripts/ -p [[email protected] ~]# ntpdate time.nist.gov 更新时间(暂时) [[email protected] ~]# echo "ntpdate time.nist.gov" >/server/scripts/ntpdate.sh 写入脚本 ntpdate time.nist.gov [[email protected] ~]# /bin/sh /server/scripts/ntpdate.sh 执行脚本 [[email protected] ~]# crontab -e 编辑定时任务 [[email protected] ~]# crontab -l 查看定时任务 |
(4)基础环境
配置IP、修改主机名、配置免密码登录、分发host、关闭防火墙
① 配置IP(一般不要用DHCP)
② 修改主机名
① 配置IP(一般不要用DHCP) ② 修改主机名 方法一: [[email protected] ~]# hostname Distribution-Center [[email protected] ~]# vim /etc/sysconfig/network [[email protected] ~]# vim /etc/hosts [[email protected] .ssh]# reboot 方法二: [[email protected] .ssh]# hostname NFS-server [[email protected] .ssh]# echo "192.168.10.106 NFS-server" >>/etc/hosts [[email protected] .ssh]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 [[email protected] .ssh]# reboot |
③ 配置免密码登录:
③ 配置免密码登录: [[email protected] ~]# mkdir -p ~/.ssh/ 创建ssh目录 [[email protected] ~]# ssh-keygen -t dsa 创建密码文件 [[email protected] ~]# cd ~/.ssh/ [[email protected] .ssh]# ssh-copy-id -i id_dsa.pub [email protected] 分发公钥文件 [[email protected] .ssh]# ssh p22 [email protected] /sbin/ifconfig 没有要密码哦 |
④ 关闭防火墙:
关闭防火墙: [[email protected] ~]# /etc/init.d/iptables stop |
2.搭建nginx虚拟主机
[[email protected] ~]# cat /etc/redhat-release 查看系统版本号 CentOS release 6.6 (Final) [[email protected] ~]# uname -r //print the kernel release 2.6.32-504.el6.x86_64 [[email protected] ~]# uname -m // print the machine hardware name x86_64 [[email protected] ~]# rpm -qa gcc gcc-c++ gcc-4.4.7-11.el6.x86_64 gcc-c++-4.4.7-11.el6.x86_64 [[email protected] ~]# rpm -qa pcre pcre-devel [[email protected] ~]# rpm -qa openssl openssl-devel [[email protected] ~]# rpm -qa pcre pcre-devel openssl openssl-devel [[email protected] nginx-1.6.3]# wget http://nginx.org/download/nginx-1.6.3.tar.gz 下载安装包 [[email protected] nginx-1.6.3]# ls 确认安装包下载成功 [[email protected] nginx-1.6.3]# tar -zxvf nginx-1.6.3.tar.gz 解压 [[email protected] nginx-1.6.3]# ls 查看解压结果 [[email protected] nginx-1.6.3]# cd nginx-1.6.3 [[email protected] nginx-1.6.3]# ./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module 自定义安装配置 [[email protected] nginx-1.6.3]# make && make install 编译安装 [[email protected] nginx-1.6.3]# echo $? 查看执行结果 0 成功 [[email protected] nginx-1.6.3]# useradd nginx -s /sbin/nologin -M 创建用户 uid=500(nginx) gid=500(nginx) groups=500(nginx) [[email protected] nginx-1.6.3]# ll /application/nginx-1.6.3/ [[email protected] nginx-1.6.3]# ln -s /application/nginx-1.6.3/ /application/nginx 为了方便使用,做软链接 [[email protected] nginx-1.6.3]# /application/nginx/sbin/nginx 启动服务 [[email protected] nginx-1.6.3]# ps -ef |grep nginx |grep -v grep 查看服务进程 [[email protected] nginx-1.6.3]# netstat -lntup |grep nginx 端口是80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN [[email protected] nginx-1.6.3]# curl 127.0.0.1 测试本地 浏览器登录测试成功。 |
Nginx虚拟主机:
虚拟主机: [[email protected] ~]# cd /application/nginx/conf [[email protected] conf]# cp nginx.conf /data/root/backup/ 备份nginx.conf配置文件 将nginx.conf.default中没有注释、不是空白行的内容导出到nginx.conf [[email protected] conf]# mkdir ../html/www [[email protected] conf]# cat ../html/{www,bbs,blog}/index.html [[email protected] conf]# vim nginx.conf [[email protected] conf]# vim /etc/hosts 192.168.10.104 www.etiantian.org etiantian.org [[email protected] conf]# /application/nginx/sbin/nginx -t [[email protected] conf]# ll /app/logs/bbs_logs/ [[email protected] conf]# ll /app/logs/www_logs/ 日志轮询: [[email protected] www_logs]# vim /server/scripts/logs_poll.sh 配置好rsync daemon服务器后,将此web服务器配置为rsync daemon客户端。 [[email protected] ~]# echo "oldboy" >/etc/rsync.password [[email protected] ~]# rsync -r /data [email protected]::backup --password-file=/etc/rsync.password 测试推送 [[email protected] ~]# mkdir -p /data/www/www [[email protected] ~]# mkdir -p /data/www/html [[email protected] ~]# mkdir -p /data/app/logs [[email protected] etc]# mkdir -p /data/etc_rc.local [[email protected] html]# vim /server/scripts/tar.sh #!/bin/bash #print for tar nginx logs to /data/app/logs by yvonne 2015-7-5 22:33:48. cd /app &&\ tar -zcf /data/app/logs/`/sbin/ifconfig eth4 |awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_nginx_a pp_logs_`date +%F` logs/ #print for tar system files to /data/ by yvonne 2015-7-5 22:33:48. cd /etc/ &&\ tar -zcf /data/etc_rc.local/`/sbin/ifconfig eth4 |awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_etc _rc.local_`date +%F` rc.local cd /etc/sysconfig/ &&\ tar -zcf /data/etc_sysconfig_iptables/`/sbin/ifconfig eth4 |awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_etc_sysconfig_iptables_`date +%F` iptables cd /var/spool/ &&\ tar -zcf /data/var_spool_cron/`/sbin/ifconfig eth4 |awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_v ar_spool_cron_`date +%F` cron/ cd /server/ &&\ tar -zcf /data/server_scripts/`/sbin/ifconfig eth4 |awk -F ‘[: ]+‘ ‘NR==2 {print $4}‘`_s erver_scripts_`date +%F` scripts/ [[email protected] html]# /bin/sh /server/scripts/tar.sh [[email protected] html]# crontab -e [[email protected] html]# crontab -l 备份文件(推送到rsync daemon) [[email protected] ~]# vim /server/scripts/rsync.sh [[email protected] ~]# /bin/sh /server/scripts/rsync.sh #print for backup /data/ to rsync daemon /backup by yvonne 2015-7-5 23:16. 10 00 * * * /bin/sh /server/scripts/rsync.sh >/dev/null 2&>1 [[email protected] ~]# crontab -l 只保留本地备份文件3天 [[email protected] server]# vim /server/scripts/reserv.sh [[email protected] server]# /bin/sh /server/scripts/reserv.sh #print for reserv 3 days local backup files by 2015-7-5 23:30:11. 00 2 * * * /bin/sh /server/scripts/reserv.sh >/dev/null 2&>1 [[email protected] server]# crontab -l 搭好NFS-server后,将此web配置为NFS-client : [[email protected] ~]# rpm -qa rpcbind nfs-utils [[email protected] ~]# /etc/init.d/rpcbind start [[email protected] ~]# ps -ef |grep rpcbind |grep -v grep [[email protected] ~]# mkdir /data/www/www/upload [[email protected] ~]# mount -t nfs 192.168.10.103:/data/www/www/upload/ /data/www/www/upload/ [[email protected] ~]# mount [[email protected] ~]# ls /data/www/www/upload/ [[email protected] ~]# vim /etc/rc.local 开机自动挂载 |
3.搭建rsync daemon 备份服务器
[[email protected] ~]# rpm -qa rsync rsync-3.0.6-12.el6.x86_64 [[email protected] ~]# touch /etc/rsyncd.conf [[email protected] ~]# vim /etc/rsyncd.conf #created by yvonne 2015-7-5 21:59:55 ##rsyncd.conf start## uid = rsync gid = rsync use chroot = no max connections = 2000 timeout = 600 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 = 192.168.10.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password ##################################### [backup] comment = backup server by yvonne 2015-7-5 22:00:06 path = /backup "/etc/rsyncd.conf" 22L, 696C written [[email protected] ~]# mkdir /backup [[email protected] ~]# useradd rsync [[email protected] ~]# id rsync [[email protected] ~]# echo "rsync_backup:oldboy" >>/etc/rsync.password [[email protected] ~]# rsync --daemon [[email protected] ~]# vim /etc/rc.local 备份服务器上保留180天的数据,但周五数据永久保留: [[email protected] ~]# vim /server/scripts/reserve.sh [[email protected] ~]# /bin/sh /server/scripts/reserve.sh #print for reserve backup files 180 days by yvonne 2015-7-5 23:38:34. 00 05 * * * /bin/sh /server/scripts/reserve.sh >/dev/null 2&>1 [[email protected] ~]# crontab -l |
检查备份目录的备份数据完整性
程序检查备份目录的备份数据完整性,并把检查结果于每日早晨7点发给管理员及老大信箱;
此处用inotify 检测完整性没有成功;做检测完整性(有问题还没做) [[email protected] ~]# rpm -qa inotify [[email protected] ~]# ls [[email protected] ~]# ls -l /proc/sys/fs/inotify/ [[email protected] ~]# tar zxvf inotify-tools-3.14.tar.gz [[email protected] ~]# ls [[email protected] ~]# ln -s /usr/local/inotify-tools-3.14/ /usr/local/inotify-tools [[email protected] ~]# ll /usr/local/inotify-tools [[email protected] ~]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt ‘%d/%m/%y%H:%M‘ --format ‘%T%w%f‘ -e create,delete /backup/ [[email protected] ~]# vim /server/scripts/inotify.sh
MD5算法验证网络文件传输的完整性 [[email protected] ~]# find /backup/ -type f -name "*.tar.gz" [[email protected] ~]# md5sum $(find /backup/ -type f -name "*.tar.gz")>md5sumtar.md5 [[email protected] ~]# vim /server/scripts/md5sum.sh |
4.NFS存储
[[email protected] ~]# rpm -qa rpcbind nfs-utils [[email protected] ~]# yum -y install rpcbind nfs-utils [[email protected] ~]# rpm -qa rpcbind nfs-utils [[email protected] ~]# mkdir /data/www/www/upload -p [[email protected] ~]# vim /etc/exports ~ [[email protected] ~]# /etc/init.d/nfs reload [[email protected] ~]# showmount -e 192.168.10.103 [[email protected] ~]# chown -R nfsnobody /data/www/www/upload/ [[email protected] ~]# touch /data/www/www/upload/iam_nfs_server [[email protected] ~]# vim /etc/rc.local 开机启动服务 |
Rsync+Inotify实时备份
保存NFS上的所有数据到rsync daemon 服务器,实时备份: [[email protected] ~]# rpm -qa inotify [[email protected] ~]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz inotify-tools-3.14.tar.gz [[email protected] ~]# tar -zxvf inotify-tools-3.14.tar.gz [[email protected] ~]# ls [[email protected] inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools-3.14 [[email protected] inotify-tools-3.14]# echo $? [[email protected] inotify-tools-3.14]# cd [[email protected] ~]# echo "oldboy" >/etc/rsync.password [[email protected] ~]# /usr/local/inotify-tools/bin/inotifywait -mrq --timefmt ‘%d/%m/%y%H:%M‘ --format ‘%T%w%f‘ -e create,delete /backup/ [[email protected] ~]# vim /server/scripts/inotify.sh |