设为首页 收藏本站
查看: 940|回复: 0

[经验分享] saltstack 安装nginx

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-10-30 08:54:38 | 显示全部楼层 |阅读模式
                      1. 目录结构
[iyunv@qing salt]# tree /srv/salt/nginx/
/srv/salt/nginx/
+-- config.sls
+-- files
|   +-- nginx
|   +-- nginx-1.0.15.tar.gz
|   +-- nginx.conf
|   +-- nginx_log_cut.sh
|   +-- nginx-upstream-jvm-route-0.1.tar.gz
+-- init.sls
+-- install.sls

1 directory, 8 files

2.
[iyunv@qing nginx]# cat init.sls
include:
  - nginx.install
  - nginx.conf
  - nginx.vhost

[iyunv@qing nginx]# cat config.sls
nginx_conf:
  file.managed:
    - name: /usr/local/webserver/nginx/conf/nginx.conf
    - source: salt://nginx/files/nginx.conf
    - template: jinja
nginx_script:
  file.managed:
    - name: /etc/init.d/nginx
    - source: salt://nginx/files/nginx
    - mode: 755
    - user: root
  cmd.run:
    - names:
      - /sbin/chkconfig --add nginx
      - /sbin/chkconfig  nginx on
    - unless: /sbin/chkconfig --list nginx
  service.running:
    - name: nginx
    - enable: True
    - reload: True
    - watch:
      - file: nginx_conf
nginx_log_cut:  
  file.managed:
    - name: /usr/local/webserver/nginx/sbin/nginx_log_cut.sh
    - source: salt://nginx/files/nginx_log_cut.sh
  cron.present:
    - name: /usr/local/webserver/nginx/sbin/nginx_log_cut.sh
    - user: root
    - minute: 59
    - hour: 23
    - require:

[iyunv@qing nginx]# cat install.sls
nginx_trans:
  file.managed:
    - source: salt://nginx/files/nginx-1.0.15.tar.gz
    - name: /tmp/nginx-1.0.15.tar.gz
    - unless: test -e /tmp/nginx-1.0.15.tar.gz

nginx_extract:
  cmd.run:
    - cwd: /tmp
    - names:
      - tar zxpf nginx-1.0.15.tar.gz
    - require:
      - file: nginx_trans
nginx_pkgs:
  pkg.installed:
    - pkgs:
       - gcc
       - openssl-devel
       - pcre-devel
       - zlib-devel
       - libxml2
       - libxml2-devel
       - libxslt-devel
nginx_user:
  user.present:
    - name: nginx
    - uid: 1502
    - createhome: False
    - shell: /sbin/nologin
nginx_compile:
  cmd.run:
    - cwd: /tmp/nginx-1.0.15
    - names:
      - mkdir -p /usr/local/webserver/
      - ./configure --user=nginx --group=nginx --prefix=/usr/local/webserver/nginx --with-pcre --with-http_stub_status_module --with-http_flv_module --with-http_ssl_module --with-http_dav_module --with-http_gzip_static_module --with-mail --with-mail_ssl_module --with-http_realip_module --with-http_xslt_module --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl
      - make
      - make install
    - unless: test -d /usr/local/webserver/nginx

[iyunv@qing nginx]# ll files/
total 700
-rw-r--r-- 1 root root   2588 Sep  7 00:46 nginx
-rw-r--r-- 1 root root 693025 Apr 12  2012 nginx-1.0.15.tar.gz
-rw-r--r-- 1 root root   1529 Sep  7 01:28 nginx.conf
-rw-r--r-- 1 root root    568 Sep  7 01:40 nginx_log_cut.sh
-rw-r--r-- 1 root root   5807 Jul  1  2010 nginx-upstream-jvm-route-0.1.tar.gz
[iyunv@qing nginx]#

[iyunv@qing nginx]# cat files/nginx
#!/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:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/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/webserver/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/webserver/nginx/conf/nginx.conf"


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: "
    killproc $nginx -HUP
    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


[iyunv@qing nginx]# cat files/nginx_log_cut.sh
#!/bin/bash
logs_path="/usr/local/webserver/nginx/logs/"
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/webserver/nginx/logs/nginx.pid`
cd ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
tar zcpf access_$(date -d "yesterday" +"%Y%m%d").log.tar.gz access_$(date -d "yesterday" +"%Y%m%d").log
rm -f access_$(date -d "yesterday" +"%Y%m%d").log
exit

                   


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-132528-1-1.html 上篇帖子: saltstack 安装mysql 下篇帖子: SaltStack研究心得
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表