搭建web服务端网络共享及实时备份(只能用堡垒机连接)


目录

1.原理解析

1.1实验思路

1.1.2搭建好两个web服务器,实验是否搭建成功。
1.1.3搭建DNS服务器。(注:这里只需要写正向解析文件)搭建完成之后解析一下域名,看是否搭建成功。
1.1.4搭建NFS服务器,创建目录并在web服务器上挂载使用。
1.1.5搭建rsync服务器并测试是否能备份nfs服务器内容。
1.1.6在NFS服务器上安装sersync实现实时同步。
1.1.7搭建堡垒机,使远程连接只能连接堡垒机。

2.web服务器

2.1解释:

web服务器是搭建网站用的。

2.2安装命令并修改主机名:

# yum -y install gcc gcc-c++ apr apr-devel cyrus-sasl-devel expat-devel libdb-devel openldap-devel apr-util-devel apr-util pcre-devel pcre lrzsz

在官网apache.org下载最新的httpd软件包(这里以httpd-2.4.48.tar.gz软件包为例)

# hostnamectl set-hostname web

2.3把软件包拖入虚拟机并解压到指定位置:

# tar xf httpd-2.4.48.tar.gz -C /usr/src 

把软件包解压到/usr/src/目录下。注:这里的指定路径的参数为大写的c

2.4配置:

#  cd /usr/src/httpd-2.4.28
# ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi 

进入解压后的软件包,配置并指定安装路径。

2.5编译及安装:

# make && make install && echo $?

编译并安装装,且查看编译和安装装是否成功。
结果为0表示成功,结果非0表示不成功,需要从新编译及安装。

2.6创建连接 :

 # ln -s /usr/local/httpd/bin* /usr/local/bin

创建软连接到PATH变量中。

2.7启动并查看版本信息:

 # apachectl
 AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::6de9:6d50:2cc3:a0d9. Set the 'ServerName' directive globally to suppress this message 
 # httpd -v
 Server version: Apache/2.4.48 (Unix)
 Server built:   Aug 20 2021 21:53:51

启动apache,查看版本信息。

2.8 添加httpd系统服务

2.8.1复制配置文件:

# cp /usr/local//httpd/bin/apachectl /etc/init.d/httpd
# vim /etc/init.d/httpd

复制配置文件并改名,且打开配置文件。

2.8.2添加内容:

#chkconfig: 35 81 21
#description:httpd servier

在第一行下面添加这两行内容。

2.8.3使配置文件生效并重启httpd

# chkconfig --add httpd
# systemctl restart httpd

现在就可以使用系统服务了。

注:

之后就可以进行访问测试,且同原理创建web02服务器。

3.DNS服务器

3.1DNS服务器的作用

DNS服务器最只要的作用是可以使物理机访问web服务器的是后不需要输入密码,直接输入域名即可。

3.2搭建DNS服务器

3.2.1安装命令并修改主机名:

# yum -y install bind-chroot bind bind-utils
# hostnamectl set-hostname dns

3.2.2启动并设置开机自启:

# systemctl start named
# systemctl enable named

3.2.3安装命令查看一下端口

# yum -y install net-tools
# netstat -anput | grep named
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN      1217/named          
tcp        0      0 127.0.0.1:953           0.0.0.0:*               LISTEN      1217/named          
tcp6       0      0 ::1:53                  :::*                    LISTEN      1217/named          
tcp6       0      0 ::1:953                 :::*                    LISTEN      1217/named          
udp        0      0 127.0.0.1:53            0.0.0.0:*                           1217/named          
udp6       0      0 ::1:53                  :::*                                1217/named

3.2.3备份配置文件并打开配置文件

# cp /etc/named.conf /var/named.conf.back
# vim /etc/named.conf

3.2.4删除全部内容并写入新内容

options {
        listen-on port 53 { any; };
        listen-on-v6 port 53 { any; };
        directory       "/var/named";
        allow-query     { any; };
        recursion yes;
        forwarders { 8.8.8.8; 114.114.114.114; };
};
zone "web.com" IN {
        type master;
        file "web.com.zone";
};

3.2.5查看一下配置文件的属主属组

# ll /etc/named.conf 
-rw-r----- 1 root named 241 Aug 20 22:32 /etc/named.conf

3.2.6复制模板并改名

# cp /var/named/named.empty /var/named/web.com.zone

这里改的名需要和之前配置文件里写的名字相同。

3.2.7打开文件

# vim /var/named/web.com.zone

3.2.8修改内容

$TTL 1D
@       IN SOA  @ web.com. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
web.com.        IN      NS      dns.web.com.

dns.web.com.    IN      A       192.168.1.4  #这里是的dns服务器的IP
www.web.com.    IN      A       192.168.1.2  #这里是web01服务器的IP
www.web.com.    IN      A       192.168.1.3  #这里是web02服务器的IP

这里是相同域名不同IP

3.2.9查看一下是否有语法错误

# named-checkconf -z /etc/named.conf 
zone web.com/IN: loaded serial 0

结果显示为0或者为ok表示语法没问题。

3.2.10修改属组

# chown :named /var/named/web.com.zone 
# ll /var/named/web.com.zone 
-rw-r----- 1 root named 237 Aug 20 22:36 /var/named/web.com.zone

3.2.11重启服务

# systemctl restart named

备注:

打开另一台虚拟机并打开网卡配置文件修改dns的IP为dns服务器的IP
在重启网卡,之后解析域名即可

# nslookup www.web.com
服务器:  UnKnown
Address:  192.168.1.4

名称:    www.web.com
Addresses:  192.168.1.3
          192.168.1.2

4.NFS服务器

4.1 NFS服务器的作用

nfs服务器的作用是为了两个web服务器共享网络文件的

4.2搭建NFS服务器

4.2.1安装命令并修改主机名

# yum-y install rpcbind nfs-utils

修改主机名

# hostnamectl set-hostname nfs

需要在nfs服务器,web01和web02服务器上都安装命令。

4.2.2启动服务并设置开机自启

# systemctl start rpcbind
# systemctl enable rpcbind
# systemctl start nfs
# systemctl enable nfs
Created symlink from /etc/systemd/system/multiuser.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

分别在nfs服务器,web01服务器和web02服务器上启动服务并设置开机自启。

4.2.3创建用户

# useradd -M -s /sbin/nologin -u 2000 www

分别在nfs,web01,web02三台服务器上创建用户。

4.2.4创建目录

# mkdir /web

创建的目录要在nfs服务器上创建。

4.2.5修改属主属组

# chown -R www:www /web
# ll -d /web
drwxr-xr-x 2 www www 6 Aug 20 23:15 /web

4.2.6编辑/etc/exports配置文件,将本地的/web目录共享发布

# vim /etc/exports
/web 192.168.1.2(rw,sync,all_squash,anonuid=2000,anongid=2000)
/web 192.168.1.3(rw,sync,all_squash,anonuid=2000,anongid=2000)

4.2.7使修改的/etc/exports配置内容生效

# systemctl reload nfs

4.2.8查看NFS服务器共享出来目录

# showmount -e localhost
Export list for localhost:
/web 192.168.1.3,192.168.1.2

4.2.9查看配置

# exportfs -v
/web          	192.168.1.2(rw,sync,wdelay,hide,no_subtree_check,anonuid=2000,anongid=2000,sec=sys,secure,root_squash,all_squash)
/web          	192.168.1.3(rw,sync,wdelay,hide,no_subtree_check,anonuid=2000,anongid=2000,sec=sys,secure,root_squash,all_squash)

4.2.10挂载(需要在web01和web02服务器上操作)

4.2.10.1打开配置文件(这是永久挂载方法)
# vim /etc/fstab
4.2.10.2写入内容
192.168.1.5:/web        /usr/local/httpd/htdocs nfs     defaults        0 0
4.2.10.3使挂载生效
# mount -a
4.2.10.4查看一下
# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        98G  1.3G   97G   2% /
devtmpfs                devtmpfs  476M     0  476M   0% /dev
tmpfs                   tmpfs     488M     0  488M   0% /dev/shm
tmpfs                   tmpfs     488M  7.7M  480M   2% /run
tmpfs                   tmpfs     488M     0  488M   0% /sys/fs/cgroup
/dev/sr0                iso9660   4.2G  4.2G     0 100% /mnt/cdrom
/dev/sda1               xfs       497M  123M  375M  25% /boot
tmpfs                   tmpfs      98M     0   98M   0% /run/user/0
192.168.1.5:/web        nfs4       98G  1.1G   97G   2% /usr/local/httpd/htdocs

注:

相同的原理挂载web02服务器。

4.2.10.5写入内容测试
# echo "123" > /usr/local/httpd/htdocs/index.html 
4.2.10.6查看一下

需要在nfs服务器上查看。

# cat /web/index.html 
123

注:

web01服务器和web02服务器上都要挂载。

5.rsync服务器

5.1rsync服务器的作用

作为nfs服务器实时备份的备份端服务器。

5.2搭建rsync服务器

5.2.2创建用户并设置密码

# useradd rupt && echo "rupt:1" | chpasswd

5.2.3创建ACL权限并修改主机名

# setfacl -R -m u:rupt:rwx /nfs-back/
# setfacl -R -m d:u:rupt:rwx /nfs-back/
# hostnamectl set-hostname rsync

5.2.4创建目录

# mkdir /nfs-back

5.2.5查看一下

[root@rsync ~]# getfacl /nfs-back/
getfacl: Removing leading '/' from absolute path names
# file: nfs-back/
# owner: root
# group: root
user::rwx
user:rupt:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:rupt:rwx
default:group::r-x
default:mask::rwx
default:other::r-x

5.2.6安装命令

# yum -y install rsync

需要在nfs服务器上也安装命令。

5.2.7修改属主属组并查看一下

# chown -R rupt:rupt /nfs-back/
# ll -d /nfs-back/
drwxrwxr-x+ 2 rupt rupt 6 Aug 21 00:42 /nfs-back/

5.2.8测试

在nfs服务器上的/web目录中创建一个文件

[root@nfs ~]# echo "123" > /web/1.txt
[root@nfs ~]# rsync -avz --delete /web/ rupt@192.168.1.6:/nfs-back
The authenticity of host '192.168.1.6 (192.168.1.6)' can't be established.
ECDSA key fingerprint is SHA256:Qb8j3RAgCnkdywVUxprkutcdxESNw64buWzrKWEjGq8.
ECDSA key fingerprint is MD5:0e:04:2f:bd:a9:44:9c:bb:38:c2:0a:0a:c7:d3:83:a8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.6' (ECDSA) to the list of known hosts.
rupt@192.168.1.6's password: 
sending incremental file list
./
1.txt

sent 115 bytes  received 38 bytes  34.00 bytes/sec
total size is 4  speedup is 0.03

显示成功。

5.2.8.1查看一下

在人sync服务器上查看一下

[root@rsync ~]# cat /nfs-back/1.txt 
123

表示成功

5.2.9做免密

在nfs服务器上创建密钥对

[root@nfs ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:0rbsOJ04p2YyH47h1mzGESCzhV0ciZblKvmBhOYMFVI root@nfs
The key's randomart image is:
+---[RSA 2048]----+
|..E+ *+o         |
| ++ B.o          |
|o..* ..          |
|=..o ...         |
| o+ o ..S        |
|   o ..+ .       |
|    o+.+o.       |
|   .++%++        |
|   .oO==.        |
+----[SHA256]-----+

这里不需要给私钥设置密码。

5.2.10发送公钥到rsync服务器

[root@nfs ~]# ssh-copy-id -i rupt@192.168.1.6
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
rupt@192.168.1.6's password: #这里输入用户rupt的密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'rupt@192.168.1.6'"
and check to make sure that only the key(s) you wanted were added.
5.2.10.1测试

在nfs服务器上创建文件并测试

[root@nfs ~]# touch /web/2.txt
[root@nfs ~]# rsync -avz --delete /web/ rupt@192.168.1.6:/nfs-back
sending incremental file list
./
2.txt

sent 129 bytes  received 38 bytes  334.00 bytes/sec
total size is 4  speedup is 0.02
5.2.10.2查看一下
[root@rsync ~]# ls /nfs-back/
1.txt  2.txt

显示成功。

5.2.11修改配置文件

需要在备份端完成。
首先备份配置文件

# cp /etc/rsyncd.conf rsycnd.conf.back
5.2.11.1打开并修改配置文件
# vim /etc/rsyncd.conf
5.2.11.2删除原来的文件并添加新的内容
uid = root
gid = root
address = 192.168.1.6
port = 873
hosts allow =192.168.1.0/24
use chroot = yes
max connections = 5
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/run/rsyncd.log
motd file = /etc/rsyncd.motd
[abc]
path = /nfs-back
comment = XXX
read only = false
list = yes
auth users = backuper
secrets file = /etc/rsyncd.passwd
5.2.11.3添加欢迎词
# echo "Welocm to Backup Server" > /etc/rsyncd.motd
5.2.11.4添加密码
[root@rsync ~]# vim /etc/rsyncd.passwd
[root@rsync ~]# cat /etc/rsyncd.passwd
backuper:1
5.2.11.5修改权限
# chmod 600 /etc/rsyncd.passwd

这里的权限必须是600的权限。

5.2.12启动并写入启动文件设置开机自启

[root@rsync ~]# rsync --daemon --config=/etc/rsyncd.conf 
[root@rsync ~]# echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.d/rc.local 

5.2.13修改权限

[root@rsync ~]# chmod +x /etc/rc.d/rc.local

5.2.14创建文件测试一下

在nfs服务器上创建文件。

[root@nfs ~]# rsync -avz --delete /web/ backuper@192.168.1.6::abc
Welocm to Backup Server

Password: 
sending incremental file list
./
3.txt

sent 156 bytes  received 44 bytes  133.33 bytes/sec
total size is 4  speedup is 0.02
5.2.14.1查看一下
[root@rsync ~]# ls /nfs-back/
1.txt  2.txt  3.txt

显示成功。

5.2.15做免密

这里做的免密是虚拟用户的免密

[root@nfs ~]# vim /etc/rsyncd.passwd
[root@nfs ~]# cat /etc/rsyncd.passwd 
1

修改权限

[root@nfs ~]# chmod 600 /etc/rsyncd.passwd

测试

[root@nfs ~]# rsync -avz --delete /web/ backuper@192.168.1.6::abc --password-file=/etc/rsyncd.passwd
Welocm to Backup Server

sending incremental file list
./
6.txt

sent 170 bytes  received 38 bytes  138.67 bytes/sec
total size is 4  speedup is 0.02

5.3安装sersync

5.3.1解压到当前目录

[root@nfs ~]# rz -E
rz waiting to receive.
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz

5.3.2移动并改名

[root@nfs ~]# mv GNU-Linux-x86/ /opt/sersync
[root@nfs ~]# cd /opt/sersync/
[root@nfs sersync]# ls
confxml.xml  sersync2
[root@nfs sersync]# cp confxml.xml confxml.xml.back

5.3.3打开配置文件并修改。

[root@nfs sersync]# vim confxml.xml
24         <localpath watch="/web">
 25             <remote ip="192.168.1.6" name="abc"/>
 26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
 27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 28         </localpath>
 29         <rsync>
 30             <commonParams params="-artuz"/>
 31             <auth start="true" users="backuper" passwordfile="/etc/rsyncd    .passwd"/>  

修改第24行25行和31行的内容

5.3.4启动

[root@nfs sersync]# vim confxml.xml
[root@nfs sersync]# /opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d 	run as a daemon
option: -r 	rsync all the local files to the remote servers before the sersync work
option: -o 	config xml name:  /opt/sersync/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost	host port: 8008
daemon start,sersync run behind the console 
use rsync password-file :
user is	backuper
passwordfile is 	/etc/rsyncd.passwd
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) 
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /web && rsync -artuz -R --delete ./ backuper@192.168.1.6::abc --password-file=/etc/rsyncd.passwd >/dev/null 2>&1 
run the sersync: 
watch path is: /web

5.3.5设置开机自启并修改权限

[root@nfs sersync]# echo "/opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml" > /etc/rc.d/rc.local 
[root@nfs sersync]# chmod +x /etc/rc.d/rc.local

5.3.6创建目录并测试

[root@nfs sersync]# touch /web/qqq.txt

5.3.7查看一下

[root@rsync ~]# ls /nfs-back/
1.txt  2.txt  3.txt  6.txt  qqq.txt
此时web服务器网络共享及实时备份已做完

6堡垒机

6.1堡垒机的作用

使用远程连接的时候只能连接到堡垒机才能连接连个web服务器,dns服务器,nfs服务器和rsync服务器进行维护。

6.1.1搭建堡垒机

分别在两个web服务器,dns服务器,nfs服务器和rsync服务器上创建admin用户并设置密码。

# useradd admin && echo "admin:1" | chpasswd

6.1.2修改配置文件

6.1.2.1分别在两个web服务器,dns服务器,nfs服务器和rsync服务器上修改配置文件。
# vim /etc/sudoers
6.1.2.2在最下面写入这行内容。
admin ALL=(ALL) NOPASSWD:/bin/bash
6.1.2.3打开白名单配置文件。
# vim /etc/ssh/sshd_config
6.1.2.4把第一行这里原先的no换成yes并添加下面的一行。
UseDNS yes
AllowUsers admin@192.168.1.7
6.1.2.5重启服务。
# systemctl restart sshd
注:以上操作需要分别在两个web服务器,dns服务器,nfs服务器和rsync服务器上完成。

6.1.2打开堡垒机并修改主机名。

# hostnamectl set-hostname jumpserver
6.1.2.1创建密钥对
# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:QH++hNC38kWzv7PwZCdm/DACWnayaMoczj7f3pd44E0 root@dns
The key's randomart image is:
+---[RSA 2048]----+
|      .          |
|     . o         |
|      o o o o    |
|       o = o o   |
|        S O +    |
|         O B.oE  |
|      . + +.o=@..|
|     =.+ . .oO*B |
|     .Bo..o .o+o.|
+----[SHA256]-----+
6.1.2.2依次发送公钥

分别发送公钥到两个web服务器,dns服务器,nfs服务器,rsync服务器和堡垒机上。(修改后面的ip即可)

# ssh-copy-id -i admin@192.168.1.2

6.1.3安装命令

在堡垒机上安装。

# yum -y install lrzsz
6.1.3.1发送私钥

发送私钥到windows中。

# sz .ssh/id_rsa

此时所有的服务器及堡垒机搭建完成。

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值