zoukankan      html  css  js  c++  java
  • saltstack自动化运维系列⑧SaltStack实践配置管理安装nginx-1.10.3

    saltstack自动化运维系列⑧SaltStack实践配置管理安装nginx-1.10.3

    安装nginx-1.10.3.tar.gz

    # mkdir -p /srv/salt/prod/pkg /srv/salt/prod/nginx /srv/salt/prod/nginx/files
    # cd /srv/salt/prod/pkg

    1.初始化nginx相关配置文件
    ①下载nginx-1.10.3.tar.gz上传到/srv/salt/prod/nginx/files目录
    ②配置文件
    # cat /srv/salt/prod/nginx/files/nginx.conf 

    user www;
    worker_processes 16;
    error_log logs/error.log error;
    worker_rlimit_nofile 30000;
    pid logs/nginx.pid;
    events {
    use epoll;
    worker_connections 65535;
    }
    
    http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    tcp_nopush on;
    underscores_in_headers on;
    keepalive_timeout 10;
    send_timeout 60;
    include /usr/local/nginx/conf/vhost/*.conf;
    server {
    listen 8080;
    server_name 127.0.0.1;
    location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
    }
    }
    }

    ③服务管理脚本
    # cat nginx-init

    #!/bin/sh
    #
    # nginx - this script starts and stops the nginx daemon
    #
    # chkconfig:   - 85 15 
    # description:  Nginx is an HTTP(S) server, HTTP(S) reverse 
    #               proxy and IMAP/POP3 proxy server
    # processname: nginx
    # config:      /etc/nginx/nginx.conf
    # config:      /etc/sysconfig/nginx
    # pidfile:     /var/run/nginx.pid
     
    # Source function library.
    . /etc/rc.d/init.d/functions
     
    # Source networking configuration.
    . /etc/sysconfig/network
     
    # Check that networking is up.
    [ "$NETWORKING" = "no" ] && exit 0
     
    nginx="/usr/local/nginx/sbin/nginx"
    prog=$(basename $nginx)
     
    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
     
    [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
     
    lockfile=/var/lock/subsys/nginx
     
    make_dirs() {
       # make required directories
       user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=([^ ]*).*/1/g' -`
       if [ -z "`grep $user /etc/passwd`" ]; then
           useradd -M -s /bin/nologin $user
       fi
       options=`$nginx -V 2>&1 | grep 'configure arguments:'`
       for opt in $options; do
           if [ `echo $opt | grep '.*-temp-path'` ]; then
               value=`echo $opt | cut -d "=" -f 2`
               if [ ! -d "$value" ]; then
                   # echo "creating" $value
                   mkdir -p $value && chown -R $user $value
               fi
           fi
       done
    }
     
    start() {
        [ -x $nginx ] || exit 5
        [ -f $NGINX_CONF_FILE ] || exit 6
        make_dirs
        echo -n $"Starting $prog: "
        daemon $nginx -c $NGINX_CONF_FILE
        retval=$?
        echo
        [ $retval -eq 0 ] && touch $lockfile
        return $retval
    }
     
    stop() {
        echo -n $"Stopping $prog: "
        killproc $prog -QUIT
        retval=$?
        echo
        [ $retval -eq 0 ] && rm -f $lockfile
        return $retval
    }
     
    restart() {
        configtest || return $?
        stop
        sleep 1
        start
    }
     
    reload() {
        configtest || return $?
        echo -n $"Reloading $prog: "
        $nginx -s reload
        RETVAL=$?
        echo
    }
     
    force_reload() {
        restart
    }
     
    configtest() {
      $nginx -t -c $NGINX_CONF_FILE
    }
     
    rh_status() {
        status $prog
    }
     
    rh_status_q() {
        rh_status >/dev/null 2>&1
    }
     
    case "$1" in
        start)
            rh_status_q && exit 0
            $1
            ;;
        stop)
            rh_status_q || exit 0
            $1
            ;;
        restart|configtest)
            $1
            ;;
        reload)
            rh_status_q || exit 7
            $1
            ;;
        force-reload)
            force_reload
            ;;
        status)
            rh_status
            ;;
        condrestart|try-restart)
            rh_status_q || exit 0
                ;;
        *)
            echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
            exit 2
    esac

    2.编写依赖包安装
    vim /srv/salt/prod/pkg/pkg-init.sls

    pkg-init:
    pkg.installed:
    - names:
    - gcc
    - gcc-c++
    - glibc
    - make
    - autoconf
    - openssl
    - openssl-devel
    - pcre
    - pcre-devel
    - glib
    - glib-devel

    3.用户添加模块
    # mkdir /srv/salt/prod/user
    # vim /srv/salt/prod/user/www.sls
    www-user-group:
    group.present:
    - name: www
    - gid: 1000

    user.present:
    - name: www
    - fullname: www
    - shell: /sbin/nologin
    - uid: 1000
    - gid: 1000

    4.编写nginx状态模块
    # cd /srv/salt/prod/nginx
    vim /srv/salt/prod/nginx/install.sls

    include:
    - pkg.pkg-init
    - user.www
    nginx-source-install:
    file.managed:
    - name: /usr/local/src/nginx-1.10.3.tar.gz
    - source: salt://nginx/files/nginx-1.10.3.tar.gz
    - user: root
    - group: root
    - mode: 755
    cmd.run:
    - name: cd /usr/local/src && tar zxf nginx-1.10.3.tar.gz && cd nginx-1.10.3 && ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-http_dav_module --with-pcre && make && make install && chown -R www:www /usr/local/nginx
    - unless: test -d /usr/local/nginx
    - require:
    - user: www-user-group
    - file: nginx-source-install
    - pkg: pkg-init
    
    服务模块
    # vim /srv/salt/prod/nginx/service.sls 
    include:
    - nginx.install
    
    nginx-init:
    file.managed:
    - name: /etc/init.d/nginx
    - source: salt://nginx/files/nginx-init
    - mode: 755
    - user: root
    - group: root
    - require:
    - cmd: nginx-source-install
    cmd.run:
    - name: chkconfig --add nginx
    - unless: chkconfig --list | grep nginx
    - require:
    - file: nginx-init
    
    /usr/local/nginx/conf/nginx.conf:
    file.managed:
    - source: salt://nginx/files/nginx.conf
    - user: www
    - group: www
    - mode: 644
    
    nginx-service:
    file.directory:
    - name: /usr/local/nginx/conf/vhost
    - require:
    - cmd: nginx-source-install
    service.running:
    - name: nginx
    - enable: True
    - reload: True
    - require:
    - cmd: nginx-init
    - watch:
    - file: /usr/local/nginx/conf/nginx.conf

    执行配置,至此nginx服务已安装完毕:
    salt '*' state.sls nginx.service env=prod

  • 相关阅读:
    MyBatis热部署
    FreeMarker速查手册
    SpringBoot从Eclipse添加的Tomcat容器中启动
    Delphi中的GetEnumName和GetEnumValue的使用方法
    [数据库连接字符串] Access 连接字符串
    [数据库连接字符串] Access 连接字符串
    Ms SQLServer中的Union和Union All的使用方法和区别
    Ms SQLServer中的Union和Union All的使用方法和区别
    RasAPI函数实现PPPOE拨号
    RasAPI函数实现PPPOE拨号
  • 原文地址:https://www.cnblogs.com/reblue520/p/6732942.html
Copyright © 2011-2022 走看看