Linux笔记:自动化运维之服务器软件的自动化安装与配置

概述

在部署一台新服务器的时候,总是要安装各种各样的软件:mysql,nginx,python等,如果要避免重复劳动批量化部署多台服务器,那么我们就要编写自动化运维脚本, 让我们的安装和配置工作都能自动的完成。

编写自动化安装程序

  • 安装软件一般有两种方式:二进制包(简单需要下载后手动安装)、源码编译(麻烦)
  • 我们以源码编译的方式来实现脚本编写

源码编译的方式安装步骤

以编译安装Nginx为例

  • wget https://nginx.org/download/nginx-1.13.11.tar.gz 安装
  • tar -zxf nginx-1.13.11.tar.gz 解压
  • cd nginx-1.13.11 进入目录
  • apt update 更新软件源
  • apt install -y gcc make libpcre3-dev zlib1g-dev openssl libssl-dev 安装其他依赖
  • ./configure --prefix=/usr/local/nginx --with-http_ssl_module 进行相关配置 此处仅作举例,配置参考官方文档
  • make 开始编译
  • make install 最后编译安装
  • 最后补充:官方编译文档:http://nginx.org/en/docs/configure.html

实现自动化安装

还是以Nginx为例

  • 创建install.py文件
    import os, sys
    
    # 定义一个异常退出及提示的方法
    def sys_exit(what, msg):
        if what != 0:
            print(msg)
            sys.exit(1)
    
    # 检查是否是root用户, 此处需要权限, 使用root来执行
    sys_exit(os.getuid(), 'please use root to run!')
    
    # 版本号的选择
    def_ver = '1.17.8'
    ver = input('please input version: (default: {})'.format(def_ver)) # 用于记录版本号
    ver = ver or def_ver
    
    # print(ver)
    
    # 安装目录的配置
    def_path = '/usr/local/nginx'
    path = input('please input install path: (default: {})'.format(def_path)) # 用于记录版本号
    path = path or def_path
    
    # 下载源码包 在nginx官方网站找到下载相关链接:https://nginx.org/en/download.html
    # https://nginx.org/download/nginx-1.17.8.tar.gz
    nginx_tar = 'nginx-{}.tar.gz'.format(ver)
    if os.path.exists(nginx_tar):
        os.remove(nginx_tar)
    
    url = 'https://nginx.org/download/nginx-{}.tar.gz'
    cmd = 'wget ' + url.format(ver) # 下载命令
    # print(cmd)
    sys_exit(os.system(cmd), 'download error!')
    
    # 解压命令
    cmd = 'tar -zxf nginx-{}.tar.gz'.format(ver)
    sys_exit(os.system(cmd), 'tar error!')
    
    # 安装依赖
    cmd = 'apt install -y gcc make libpcre3-dev zlib1g-dev openssl libssl-dev'
    sys_exit(os.system(cmd), 'dependencies install error!')
    
    # 配置 具体配置参考官方文档
    cmd = 'cd nginx-{} && ./configure --prefix=/usr/local/nginx --with-http_ssl_module'.format(ver)
    sys_exit(os.system(cmd), 'configure error!')
    
    # 编译
    cmd = 'cd nginx-{} && make && make install'.format(ver)
    sys_exit(os.system(cmd), 'make error!')
    
    print('install suc!')
    

自动化配置

还是以Nginx为例

1 ) nginx的配置文件举例

worker_processes    1;
pid                 logs/nginx.pid;
events {
    worker_connctions   1000;
}
http {
    gzip            on;
    sendfile        on;
    access_log      /var/log/nginx/access.log;
    error_log       /var/log/nginx/error.log;
    include         /etc/nginx/conf.d/*.conf;
    include         mime.types;
    server {
        listen 80;
        server_name localhost;
        location / {
            root html;
            index index.html index.htm;
        }
    }
}
  • 以前我们有很多配置的文件, 使用apt安装,配置在/etc/nginx目录下,但是源码编译的,它会在/usr/local/nginx/conf下
  • 我们有很多需要修改的地方,由此我们需要灵活配置,那么我们就需要制作nginx的模板文件
  • 我们可以通过nginx.conf文件来作为我们的模板配置文件

2 ) 制作nginx模板

  • 新建nginx.config.tpl文件

    worker_processes    $worker_processes;
    pid                 logs/nginx.pid;
    events {
        worker_connctions   1000;
    }
    http {
        gzip            on;
        access_log      /var/log/nginx/access.log;
        error_log       /var/log/nginx/error.log;
        include         /etc/nginx/conf.d/*.conf;
        server {
            listen $listen;
            server_name $server_name;
            location / {
                root $root;
                index index.html index.htm;
            }
        }
    }
    
  • 注意到变量都是以$来配置,用于后期的替换

3 ) 编写配置文件

  • 新建config.py文件

    from string import Template
    import psutil, os, sys, datatime
    
    # 定义一个异常退出及提示的方法
    def sys_exit(what, msg):
        if what != 0:
            print(msg)
            sys.exit(1)
    
    # 读取配置模板文件
    with open('./nginx.config.tpl') as f:
        lines = f.readlines()
        lines = "".join(lines)
    
    cpu = psutil.cpu_count() # 取cpu核心数量
    
    params = {
        'worker_processes': cpu,
        'listen': 8001,
        'server_name': 'localhost',
        'root': '/data/webroot/'
    }
    
    tpl = Template(lines)
    res = tpl.safe_substitute(**params)
    
    # 现将要替换的nginx生成到一个临时目录中,先测试该nginx的可用性,再进行真实替换
    temp = '/usr/local/nginx/conf/nginx.conf.temp'
    with open(temp, 'w') as f:
        f.write(res)
    
    # 检查nginx配置文件的可用性
    cmd = '/usr/local/nginx/sbin/nginx -c {} -t'.format(temp)
    sys_exit(os.system(cmd), 'config content error!')
    
    # 备份原配置文件
    config_file = '/usr/local/nginx/conf/nginx.conf'
    if os.path.exists(config_file):
        os.rename(config_file, config_file + datatime.datatime.now().strftime('%Y%m%d%H%M%S'))
    
    # 将新的文件替代原来的配置文件
    os.rename(temp, config_file)
    
    # 重启nginx
    if os.system('/usr/local/nginx/sbin/nginx -s reload') != 0:
        print('reboot error!')
        sys.exit(1)
    
  • 将上述文件传入服务器, 注意重新启动nginx服务, $/usr/local/nginx/sbin/nginx

    扫描二维码关注公众号,回复: 9009672 查看本文章
  • 执行config.py文件, $python3 config.py

  • 创建/data/webroot/目录,默认是没有的$mkdir -p /data/webroot/

  • 在里面创建一个index.html随便写些内容

  • 访问nginx服务器服务器ip:8001 如:192.168.55.66:8001, 内容可见即成功!

发布了403 篇原创文章 · 获赞 198 · 访问量 69万+

猜你喜欢

转载自blog.csdn.net/Tyro_java/article/details/104190153