1./srv/salt/nginx目录树

.
conf.sls
file
|--- nginx
|--- nginx-1.5.1.tar.gz
|--- nginx.conf
|--- nginx_log_cut.sh
|--- vhost.conf
init.sls
install.sls
vhost.sls

/srv/salt/top.sls

base:
'test82.salt.cn':
- nginx.init

2.init.sls 初始化所有sls文件

/srv/salt/nginx/init.sls

include:
- nginx.install
- nginx.conf
- nginx.vhost

3.install.sls nginx的安装sls

/srv/salt/nginx/install.sls

nginx_source:
file.managed:
- name: /tmp/nginx-1.5.1.tar.gz
- unless: test -e /tmp/nginx-1.5.1.tar.gz
- user: root
- group: root
- makedirs: True
- source: salt://nginx/file/nginx-1.5.1.tar.gz
nginx_extract:
cmd.run:
- cwd: /tmp
- names:
- tar zxf nginx-1.5.1.tar.gz
- unless: test -d /tmp/nginx-1.5.1
- require:
- file: nginx_source
nginx_user:
user.present:
- name: www
- createhome: False
- gid_from_name: True
- shell: /sbin/nologin
nginx_pkg:
pkg.installed:
- pkgs:
- gcc
- gcc-c++
- openssl-devel
- pcre-devel
- zlib-devel
nginx_compile:
cmd.run:
- cwd: /tmp/nginx-1.5.1
- names:
- ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-http_realip_module
- make
- make install
- require:
- cmd: nginx_extract
- pkg: nginx_pkg
- unless: test -d /usr/local/nginx
create_dir:
cmd.run:
- names:
- chown -R www.www /var/html/www
- mkdir -p /usr/local/nginx/conf/vhost
- unless: test -d /usr/local/nginx/conf/vhost
- require:
- cmd: nginx_compile

4.conf.sls 管理nginx主配置文件

/srv/salt/nginx/conf.sls

include:
- nginx.install {% set nginx_user = 'www' %} nginx_conf:
file.managed:
- name: /usr/local/nginx/conf/nginx.conf
- source: salt://nginx/file/nginx.conf
- template: jinja
- defaults:
nginx_user: {{ nginx_user }}
num_cpus: {{ grains['num_cpus'] }}
nginx_service :
file.managed:
- name: /etc/init.d/nginx
- user: root
- mode: 755
- source: salt://nginx/file/nginx
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: /usr/local/nginx/conf/vhost/*.conf
nginx_log_cut:
file.managed:
- name: /usr/local/nginx/sbin/nginx_log_cut.sh
- source: salt://nginx/file/nginx_log_cut.sh
cron.present:
- name: sh /usr/local/nginx/sbin/nginx_log_cut.sh
- user: root
- minute: 10
- hour: 0
- require:
- file: nginx_log_cut

5.使用pillar适合针对不同的主机动态生成配置

/srv/pillar目录树

.
top.sls
vhost.sls

/srv/pillar/top.sls

base:
'test82.salt.cn':
- vhost

/srv/pillar/vhost.sls

vhost:
{% if 'test8' in grains['id'] %}
- name: www
target: /usr/local/nginx/conf/vhost/vhost_www.conf
{% else %}
- name: bbs
target: /usr/local/nginx/conf/vhost/vhost_bbs.conf
{% endif %}

6.vhost.sls 生成虚拟机配置文件

/srv/salt/nginx/vhost.sls

include:
- nginx.install {% for vhostname in pillar['vhost'] %} {{ vhostname['name'] }}:
file.managed:
- name: {{ vhostname['target'] }}
- source: salt://nginx/file/vhost.conf
- target: {{ vhostname['target'] }}
- template: jinja
- defaults:
server_name: {{ grains['fqdn_ip4'][0] }}
log_name: {{ vhostname['name'] }}
- watch_in:
service: nginx {% endfor %}

6.nginx.conf 主配置文件模板

/srv/salt/nginx/file/nginx.conf

#
user {{ nginx_user }};
worker_processes {{grains['num_cpus']}};
error_log logs/nginx_error.log notice;
pid /usr/local/nginx/sbin/nginx.pid; worker_rlimit_nofile 65535; events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 128m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
server_tokens off;
client_body_buffer_size 512k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$host"' ; include vhost/*.conf;
}

7.nginx nginx服务管理脚本

/srv/salt/nginx/file/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/nginx/sbin/nginx"
prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/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

8.nginx_log_cut.sh nginx日志切割脚本

/srv/salt/nginx/file/nginx_log_cut.sh

#!/bin/bash

logs_path=/usr/local/nginx/logs
yesterday=`date -d "yesterday" +%F` mkdir -p $logs_path/$yesterday cd $logs_path for nginx_logs in `ls *log` ;
do
mv $nginx_logs ${yesterday}/${yesterday}-${nginx_logs} kill -USR1 `cat /usr/local/nginx/sbin/nginx.pid`
done

9.vhost.sls 虚拟机配置文件

/srv/salt/nginx/file/vhost.conf

server
{
listen 80;
server_name {{ server_name }};
index index.html index.htm ;
root html;
#location ~ .*\.(php|php5)?$
# {
# try_files $uri =404;
# fastcgi_pass unix:/tmp/php-cgi.sock;
# fastcgi_index index.php;
# include fcgi.conf;
# }
location /status {
stub_status on;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1d;
}
access_log logs/{{ log_name }}-access.log main;
}

10.安装配置nginx

命令行执行如下

salt 'test82.salt.cn' state.highstate

saltstack之nginx部署的更多相关文章

  1. CentOS Mono Nginx 部署 MVC4+WebApi

    CentOS Mono Nginx 部署 MVC4+WebApi 经过几天的折磨,终于在CentOS上成功部署了MVC4+WebApi.Mono上的服务器推荐两种:Jexus(国产高人写的一款很牛的服 ...

  2. Ubuntu上通过nginx部署Django笔记

    Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式.今天在Ubuntu上使用Nginx部署Django服务,虽然不是第一次搞这个了,但是发现还是跳进了好多坑,g ...

  3. Ubuntu 14.04 上使用 Nginx 部署 Laravel

    本教程将会涉及以下工具: Ubuntu 14.04 LTS PHP 5.5 MySQL Laravel 5.0 Nginx 参考文章:Ubuntu 14.04 上使用 Nginx 部署 Laravel ...

  4. 使用uWSGI+nginx部署Django项目

    最近使用django写了一些项目,不过部署到服务器上碰到一些问题,还有静态文件什么的一堆问题,这里总结一下碰到的问题和解决方案,总体思路是按照官方文档走的. 原文地址:http://uwsgi-doc ...

  5. FastDFS+Nginx部署详细教程

    本例使用到的所有tar和zip包地址:http://download.csdn.net/detail/corey_jk/9758664 本例中使用CentOS1.CentOS2两台机器实现. 1 GC ...

  6. Centos6.5中Nginx部署基于IP的虚拟…

    Centos6.5 中Nginx 部署基于IP 的虚拟主机 王尚2014.11.18 一.介绍虚拟主机 虚拟主机是使用特殊的软硬件技术,把一台真实的物理电脑主机 分割成多个逻辑存储单元,每个单元都没有 ...

  7. Ubuntu 下使用 Nginx 部署 .NET Core 2.0 网站

    前言 本文介绍如何在 Ubuntu 16.04 服务器上安装 .NET Core 2.0 SDK.创建项目与发布,并使用 Nginx 部署 .NET Core 2.0 Web 项目. 安装 .NET ...

  8. CentOS7上Docker简单安装及nginx部署

    安装 如果原来安装过docker,先把原来的删掉,再安装(如果是首次安装docker忽略第一步,直接在第二步看起) 1.1先查看下已经安装了那些docker yum list installed | ...

  9. Nginx部署静态页

    简答说一下如何用Nginx部署静态网页,并绑定域名访问 1.通过FTP上传静态页到服务器指定目录 2.编写nginx的.conf文件 3.重启nginx 如图,这是centos上传文件路径 nginx ...

随机推荐

  1. 如何隐藏 video 元素的下载按钮

    1. 使用 video 元素的 ControlList API <video controls controlsList="nodownload"></video ...

  2. Linux常用网络带宽监控工具(转)

    本文介绍了一些可以用来监控网络使用情况的Linux命令行工具.这些工具可以监控通过网络接口传输的数据,并测量目前哪些数据所传输的速度.入站流量和出站流量分开来显示. 一些命令可以显示单个进程所使用的带 ...

  3. JavaScript indexOf() 方法,获取元素的位置;Object.keys()获取对象的所有key的数组

    定义和用法 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. 语法 stringObject.indexOf(searchvalue,fromindex) 参数 描述 sea ...

  4. Android服务Service具体解释(作用,生命周期,AIDL)系列文章-为什么须要服务呢?

    Android服务Service具体解释(作用,生命周期,AIDL) 近期沉迷于上班,没有时间写博客了.解衣入睡,未眠.随起床写一篇博客压压惊! ##我们android系统为什么须要服务Service ...

  5. WIN7 安装其他的系统boot

    1,安装win7系统不赘述. 2,在安装完win7系统后,准备安装CentOS7.0 3,准备ISO文件和所需软件 1.CentOS官网下载DVD ISO文件​ 一般选择DVD ISO ​     2 ...

  6. 模拟服务器MockServer之Moco详细介绍

    转载:http://blog.csdn.net/vite_s/article/details/54583243 前面一篇介绍了如何用mockito来测试我们的一些异步任务,例如网络请求时候的异步回调. ...

  7. 在vs2010中编译log4cxx-0.10.0具体方法(从下载、编译、解决错误具体介绍)

    一. 简单介绍 log4cxx是Java社区著名的log4j的c++移植版.用于为C++程序提供日志功能,以便开发人员对目标程序进行调试和审计,log4cxx是apache软件基金会的开源项目,基于A ...

  8. DeleteDC、ReleaseDC 、DeleteObject的使用

    DeleteDC 该函数删除指定的设备上下文环境(DC). 原型: BOOL DeleteDC(HDC hdc): 参数: hdc:设备上下文环境的句柄. 返回值: 成功,返回非零值:失败,返回零.调 ...

  9. Linux下实现文件双向同步

    修改的地方时:私钥和公约处. 其次 vi /root/.unison/default.prf 这个文件在这个位置 root = /tmp/test root = ssh://root@10.10.3. ...

  10. mysql 语句要求

    mysql 语句不可以有单引号,要把单引号替换为双引号!