rpmbuild软件包管理、编写编译配置文件、systemd服务管理、GREVPN、L2TP+IPsec、systemdUnit文件

目录

1 配置nginx+tomcat集群

2 案例1:制作nginx的RPM包

2.1 问题

2.2 方案

2.3 步骤

3 案例2:配置GRE VPN

3.1 问题

3.2 方案

3.3 步骤

4 案例4:创建L2TP+IPSec VPN

4.1 问题

4.2 方案

4.3 步骤

5 案例5:编写systemd Unit文件

5.1 问题

5.2 方案

5.3 步骤


1 配置nginx+tomcat集群

1) 在192.168.99.5主机上配置Nginx调度器

    [root@proxy ~]# vim  /usr/local/nginx/conf/nginx.conf
    http{
        upstream toms {
            server 192.168.99.100:8080;
            server 192.168.99.200:8080;
        }
        server  {
            listen 80;
            server_name localhost;
            location / {
                proxy_pass  http://toms;
            }
        }
    }  

2) 在192.168.99.100和192.168.99.200主机上配置Tomcat,方法参考之前案例

3)启动服务

    [root@web1 ~]# /usr/local/tomcat/bin/startup.sh
    [root@web2 ~]# /usr/local/tomcat/bin/startup.sh

4) 客户端验证访问proxy

2 案例1:制作nginx的RPM包

2.1 问题

本案例使用nginx-1.22.1版本的源码软件,生成对应的RPM包软件,具体要求如下:

软件名称为nginx

软件版本为1.22.1

RPM软件包可以查询描述信息

RPM软件包可以安装及卸载

2.2 方案

安装rpm-build软件包,编写SPEC配置文件,创建新的RPM软件包。

配置文件中的描述信息如表-1:

表-1 SPEC描述信息

2.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:安装rpm-build软件

1)安装rpm-build软件包

    [root@web1 ~]# yum -y install  rpm-build

2)生成rpmbuild目录结构

    [root@web1 ~]# rpmbuild -ba nginx.spec                #会报错,没有文件或目录
    [root@web1 ~]# ls /root/rpmbuild                    #自动生成的目录结构
    BUILD  BUILDROOT  RPMS  SOURCES  SPECS  SRPMS

3)准备工作,将源码软件复制到SOURCES目录

    [root@web1 ~]# cp nginx-1.22.1.tar.gz /root/rpmbuild/SOURCES/

4)创建并修改SPEC配置文件

    [root@web1 ~]# vim /root/rpmbuild/SPECS/nginx.spec 
    Name:nginx                                        #源码包软件名称
    Version:1.22.1                                    #源码包软件的版本号
    Release:    10                                        #制作的RPM包版本号
    Summary: nginx is a web server software.            #RPM软件的概述    
    License:GPL                                        #软件的协议
    URL:    www.test.com                                    #网址
    Source0:nginx-1.22.1.tar.gz                        #源码包文件的全称
    #BuildRequires:                                    #制作RPM时的依赖关系
    Requires:    pcre-devel  openssl-devel                    #安装RPM时的依赖关系
    %description
    nginx is an HTTP and reverse proxy server.    #软件的详细描述
    %post
    useradd nginx                               #非必需操作:安装后脚本(创建账户)
    %prep
    %setup -q                                #自动解压源码包,并cd进入目录
    %build
    ./configure
    make %{?_smp_mflags}
    %install
    make install DESTDIR=%{buildroot}
    %files
    %doc
    /usr/local/nginx/*                    #对哪些文件与目录打包
    %changelog

步骤二:使用配置文件创建RPM包

1)安装依赖软件包

    [root@web1 ~]# yum -y install  gcc  pcre-devel openssl-devel

2)rpmbuild创建RPM软件包

    [root@web1 ~]# rpmbuild -ba /root/rpmbuild/SPECS/nginx.spec
    [root@web1 ~]# ls /root/rpmbuild/RPMS/x86_64/nginx-1.22.1-10.x86_64.rpm

步骤三:安装软件

    [root@web1 ~]# yum install /root/rpmbuild/RPMS/x86_64/nginx-1.22.1-10.x86_64.rpm 
    [root@web1 ~]# rpm -qa |grep nginx
    [root@web1 ~]# ls /usr/local/nginx/

3 案例2:配置GRE VPN

3.1 问题

本案例要求搭建一个GRE VPN环境,并测试该VPN网络是否能够正常通讯,要求如下:

  • 启用内核模块ip_gre
  • 创建一个VPN隧道
  • 实现两台主机点到点的隧道通讯

3.2 方案

使用lsmod查看当前计算机已经加载的模块,使用modprobe加载Linux内核模块,使用modinfo可以查看内核模块的信息。

3.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:启用GRE模块

1)查看计算机当前加载的模块

    [root@web1 ~]# lsmod                        #显示模块列表
    [root@web1 ~]# lsmod  | grep ip_gre        #确定是否加载了gre模块

2)加载模块ip_gre

    [root@web1 ~]# modprobe  ip_gre
    [root@web1 ~]# lsmod  | grep ip_gre 

步骤二:web1主机创建VPN隧道

1)创建隧道

    [root@web1 ~]# ip tunnel add tun0  mode gre \ 
    >  remote 192.168.99.200 local 192.168.99.100
    #ip tunnel add创建隧道(隧道名称为tun0),ip tunnel help可以查看帮助
    #mode设置隧道使用gre模式
    #local后面跟本机的IP地址,remote后面是与其他主机建立隧道的对方IP地址

2)启用该隧道(类似与设置网卡up)

    [root@web1 ~]# ip add show tun0
    [root@web1 ~]# ip link set tun0 up         #设置UP

3)为VPN配置隧道IP地址

    [root@web1 ~]# ip addr add 10.10.10.100/8 peer 10.10.10.200/8 dev tun0
    #为隧道tun0设置本地IP地址10.10.10.100/24
    #隧道对面的主机IP为10.10.10.200/24
    [root@web1 ~]# ip add show tun0                      #查看IP地址

步骤三:web2主机创建VPN隧道

1)加载模块ip_gre

    [root@web2 ~]# modprobe  ip_gre
    [root@web2 ~]# lsmod  | grep ip_gre 

2)创建隧道

    [root@web2 ~]# ip tunnel add tun0  mode gre \ 
    >  remote 192.168.99.100 local 192.168.99.200

3)启用该隧道

    [root@web2 ~]# ip add show tun0
    [root@web2 ~]# ip link set tun0 up         #设置UP

4)为VPN配置隧道IP地址

    [root@web2 ~]# ip addr add 10.10.10.200/8 peer 10.10.10.100/8 dev tun0
    [root@web2 ~]# ip add show tun0                      #查看IP地址

5)测试连通性

    [root@web1 ~]#  ping 10.10.10.200
    [root@web2 ~]#  ping 10.10.10.100

4 案例4:创建L2TP+IPSec VPN

4.1 问题

本案例要求搭建一个L2TP+IPSec VPN环境,并测试该VPN网络是否能够正常通讯,具体要求如下:

  • 使用L2TP协议创建一个支持身份验证与加密的隧道连接
  • 使用IPSec对数据进行加密
  • 为客户端分配10.10.10.10的地址池
  • 客户端连接的用户名为:tom,密码为:123456
  • 预共享密钥为:randpass

4.2 方案

使用web1作为服务端,windows10虚拟机作为客户端,首先要修改window10的ip为99网段,然后加入99网段的虚拟网络

4.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:部署IPSec服务

1)安装

    [root@web1 vpn]# yum  -y  install  libreswan   #安装加密工具
    [root@web1 vpn]# cp  myipsec.conf  /etc/ipsec.d/   #复制配置IPSec密钥验证配置文件
    到ipsec.d目录

2)配置

    [root@web1 vpn]# vim  /etc/ipsec.d/myipsec.conf    #修改配置第16行
    left=192.168.99.100    #设置为本机ip(此处在真实环境为公网ip)
    [root@web1 vpn]# vim  /etc/ipsec.secrets   #修改配置,添加加密信息
    192.168.99.100  %any:  PSK  "randpass"   #另起一行,添加99.100是本机ip,%any:是允许任何客户机连接本服务器,PSK(pre share key)是预共享密钥,randpass是密码,等windows客户连接vpn服务器时需要该密码

步骤二:安装vpn工具

1)安装

    [root@web1 vpn]#yum -y install  ./xl2tpd-1.3.8-2.el7.x86_64.rpm
    [root@web1 vpn]#vim  /etc/xl2tpd/xl2tpd.conf    #打开配置文件,32、33行
    ip range = 10.10.10.10-10.10.10.18  #给客户分配的ip
    local ip = 192.168.99.100    #本机ip

2)配置

[root@web1 vpn]#vim  /etc/ppp/options.xl2tpd   #修改配置文件,将第10、16行注释掉,删除掉21行的#以及空格,就可以启用加密
#crtscts                                                #注释该行
#lock 

3)创建账户

    [root@web1 vpn]#vim  /etc/ppp/chap-secrets   #定义windows客户机的用户名和密码
    tom  *  123456  *     #另起一行创建用户tom,配置密码123456

4)开启服务

    systemctl  start  ipsec   #开启加密服务
    ss  -ntulp |grep  :500    #检查加密服务
    /usr/sbin/xl2tpd            #开启vpn服务
    ss  -ntulp |grep  xl2tpd   #检查加vpn服务

5)翻墙设置(非必需操作)

    [root@client ~]# echo "1" > /proc/sys/net/ipv4/ip_forward    #开启路由转发
    [root@client ~]# iptables -t nat -A POSTROUTING -s 192.168.3.0/24 \
    >  -j SNAT --to-source 201.1.2.10

步骤三:客户端设置

1,启动一台Windows虚拟机,将虚拟机网卡桥接到99网段的虚拟网络,ip配置为99网段的即可

2. 设置Windows注册表,具体操作如下:

  • 单击"开始",单击"运行",键入"regedit",然后单击"确定"
  • 找到下面的注册表子项,然后单击它:
  • HKEY_LOCAL_MACHINE\ System\CurrentControlSet\Services\Rasman\Parameters
  • 在"编辑"菜单上,单击"新建"->"DWORD值"
  • 在"名称"框中,键入"ProhibitIpSec"
  • 在"数值数据"框中,键入"1",然后单击"确定"
  • 退出注册表编辑器,然后重新启动计算机

连接VPN并测试网络连通性,效果如图-1所示。

最下面输入用户名tom 密码 123456。

5 案例5:编写systemd Unit文件

5.1 问题

本案例要求熟练掌握systemd进程如何管理其他服务器,具体要求如下:

  • 熟悉systemctl常用命令
  • 通过systemd管理Nginx服务

5.2 方案

  • Unit文件语法格式参考表

5.3 步骤

实现此案例需要按照如下步骤进行。

步骤二:使用systemd管理shell脚本

1)编写shell脚本

    [root@web1 ~]# vim /root/test.sh 
    #!/bin/bash
    while : 
    do
        echo NB
        echo DACHUI
        sleep 1
    done
    [root@web1 ~]# chmod +x /root/test.sh

2)编写Unit文件

    [root@web1 ~]# cp /usr/lib/systemd/system/{crond.service,test.service}
    [root@web1 ~]# vim /usr/lib/systemd/system/test.service
    [Unit]
    Description=my test script            
    After=time-sync.target
    [Service]
    ExecStart=/root/test.sh
    ExecReload=/bin/kill -HUP $MAINPID
    KillMode=process
    [Install]
    WantedBy=multi-user.target

步骤二:使用systemd管理Nginx服务

1)编写Unit文件

    [root@web1 ~]# vim /usr/lib/systemd/system/nginx.service
    [Unit]
    Description=The Nginx HTTP Server        #描述信息
    After=network.target remote-fs.target nss-lookup.target    
    [Service]
    Type=forking
    #仅启动一个主进程的服务为simple,需要启动若干子进程的服务为forking
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/bin/kill -s QUIT $MAINPID
    [Install]
    WantedBy=multi-user.target
    [root@web1 ~]#systemctl start nginx    #可以控制nginx开启了,这里如果无效可以尝试重启服务器

  • 40
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值