1、编写nginx安装模块
[root@server1 salt]# mkdir pkgs ##创建一个模块,用来存放nginx依赖包安装方法
[root@server1 salt]# cd pkgs
[root@server1 pkgs]# vim make.sls
nginx-make:
pkg.installed:
- pkgs:
- gcc
- pcre-devel
- openssl-devel
[root@server1 pkgs]# cd ..
[root@server1 salt]# mkdir nginx
[root@server1 salt]# cd nginx/
[root@server1 nginx]# mkdir files
[root@server1 nginx]# cd files
[root@server1 files]# ls
nginx-1.12.0.tar.gz
[root@server1 files]# cd ..
[root@server1 nginx]# vim install.sls
include:
- pkgs.make
nginx-install:
file.managed:
- name: /mnt/nginx-1.12.0.tar.gz
- source: salt://nginx/files/nginx-1.12.0.tar.gz
cmd.run:
- name: cd /mnt && tar zxf nginx-1.12.0.tar.gz && cd nginx-1.12.0 && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && sed -i.bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/core/nginx.h && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make > /dev/null && make install > /dev/null
- creates: /usr/local/nginx
2、推送
[root@server1 nginx]# salt server3 state.sls nginx.install
server3:
----------
ID: nginx-make
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 13:56:47.123749
Duration: 391.928 ms
Changes:
----------
ID: nginx-install
Function: file.managed
Name: /mnt/nginx-1.12.0.tar.gz
Result: True
Comment: File /mnt/nginx-1.12.0.tar.gz is in the correct state
Started: 13:56:47.517375
Duration: 153.751 ms
Changes:
----------
ID: nginx-install
Function: cmd.run
Name: cd /mnt && tar zxf nginx-1.12.0.tar.gz && cd nginx-1.12.0 && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && sed -i.bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/core/nginx.h && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make > /dev/null && make install > /dev/null
Result: True
Comment: /usr/local/nginx exists
Started: 13:56:47.671844
Duration: 0.387 ms
Changes:
Summary for server3
------------
Succeeded: 3
Failed: 0
------------
Total states run: 3
Total run time: 546.066 ms
[root@server1 nginx]# salt server3 state.sls nginx.install
server3:
----------
ID: nginx-make
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 13:57:22.091714
Duration: 391.989 ms
Changes:
----------
ID: nginx-install
Function: file.managed
Name: /mnt/nginx-1.12.0.tar.gz
Result: True
Comment: File /mnt/nginx-1.12.0.tar.gz updated
Started: 13:57:22.485368
Duration: 64.674 ms
Changes:
----------
diff:
New file
mode:
0644
----------
ID: nginx-install
Function: cmd.run
Name: cd /mnt && tar zxf nginx-1.12.0.tar.gz && cd nginx-1.12.0 && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && sed -i.bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/core/nginx.h && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make > /dev/null && make install > /dev/null
Result: True
Comment: Command "cd /mnt && tar zxf nginx-1.12.0.tar.gz && cd nginx-1.12.0 && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && sed -i.bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/core/nginx.h && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make > /dev/null && make install > /dev/null" run
Started: 13:57:22.550752
Duration: 24813.337 ms
Changes:
----------
pid:
2230
retcode:
0
stderr:
stdout:
Summary for server3
------------
Succeeded: 3 (changed=2)
Failed: 0
------------
Total states run: 3
Total run time: 25.270 s
3、nginx服务模块编写
[root@server1 nginx]# vim service.sls
[root@server1 nginx]# pwd
/srv/salt/nginx
include:
- nginx.install
- users.add
/usr/local/nginx/conf/nginx.conf:
file.managed:
- source: salt://nginx/files/nginx.conf
- mode: 644
nginx-service:
file.managed:
- name: /etc/init.d/nginx
- source: salt://nginx/files/nginx
- mode: 755
service.running:
- name: nginx
- reload: tree
- watch:
- file: /usr/local/nginx/conf/nginx.conf
nginx运行如果需要用nginx运行的话,需要创建nginx模块,编写创建用户nginx模块
[root@server1 users]# pwd
/srv/salt/users
[root@server1 users]# vim add.sls
nginx-group:
group.present:
- name: nginx
- gid: 888
nginx-user:
user.present:
- name: nginx
- uid: 888
- gid: 888
- shell: /sbin/nologin
- home: /usr/local/nginx
- createhome: false
因为需要nginx的脚本,所以还需要编辑脚本文件
[root@server1 files]# pwd
/srv/salt/nginx/files
[root@server1 files]# ls
nginx nginx-1.12.0.tar.gz nginx.conf
[root@server1 files]# cat nginx
#!/bin/bash
# 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
nginx=${NGINX-/usr/local/nginx/sbin/nginx}
prog=nginx
pidfile=${PIDFILE-/usr/local/nginx/logs/nginx.pid}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
RETVAL=0
start() {
echo -n $"Starting $prog: "
$nginx
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} $nginx
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
$nginx -s reload
RETVAL=$?
if [ $RETVAL -eq 7 ]; then
failure $"nginx shutdown"
fi
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $nginx
RETVAL=$?
;;
restart)
stop
start
;;
reload)
reload
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|}"
RETVAL=2
esac
exit $RETVAL
四、推送nginx模块的service方法
[root@server1 files]# salt server3 state.sls nginx.service
server3:
----------
ID: nginx-make
Function: pkg.installed
Result: True
Comment: All specified packages are already installed
Started: 14:23:43.427037
Duration: 456.271 ms
Changes:
----------
ID: nginx-install
Function: file.managed
Name: /mnt/nginx-1.12.0.tar.gz
Result: True
Comment: File /mnt/nginx-1.12.0.tar.gz is in the correct state
Started: 14:23:43.885001
Duration: 70.8 ms
Changes:
----------
ID: nginx-install
Function: cmd.run
Name: cd /mnt && tar zxf nginx-1.12.0.tar.gz && cd nginx-1.12.0 && sed -i.bak 's/CFLAGS="$CFLAGS -g"/#CFLAGS="$CFLAGS -g"/g' auto/cc/gcc && sed -i.bak 's/#define NGINX_VER "nginx\/" NGINX_VERSION/#define NGINX_VER "nginx"/g' src/core/nginx.h && ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio &> /dev/null && make > /dev/null && make install > /dev/null
Result: True
Comment: /usr/local/nginx exists
Started: 14:23:43.956525
Duration: 0.369 ms
Changes:
----------
ID: nginx-group
Function: group.present
Name: nginx
Result: True
Comment: Group nginx is present and up to date
Started: 14:23:43.991358
Duration: 0.424 ms
Changes:
----------
ID: nginx-user
Function: user.present
Name: nginx
Result: True
Comment: User nginx is present and up to date
Started: 14:23:44.002306
Duration: 1.033 ms
Changes:
----------
ID: /usr/local/nginx/conf/nginx.conf
Function: file.managed
Result: True
Comment: File /usr/local/nginx/conf/nginx.conf is in the correct state
Started: 14:23:44.003446
Duration: 63.864 ms
Changes:
----------
ID: nginx-service
Function: file.managed
Name: /etc/init.d/nginx
Result: True
Comment: File /etc/init.d/nginx is in the correct state
Started: 14:23:44.067439
Duration: 50.732 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: Started Service nginx
Started: 14:23:44.120387
Duration: 86.153 ms
Changes:
----------
nginx:
True
Summary for server3
------------
Succeeded: 8 (changed=1)
Failed: 0
------------
Total states run: 8
Total run time: 729.646 ms
server3查看
[root@server3 ~]# netstat -antlp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4854/nginx