云运维-任务5.3-Ansible(3)playbook实战自动化安装nginx-20201224

Ansible playbook 实战

 
 

4.1 ansible 自动化安装nginx

(1)编译安装 nginx

1)使用 wget 下载 nginx 包,下载地址:

#wget http://nginx.org/download/nginx-1.9.6.tar.gz

2)解压下载的 nginx 包

#./configure --prefix=/usr/local/nginx
#echo $?
#make && make install

3)编写/etc/init.d/nginx 文件

下面yml的yml形式:https://download.csdn.net/download/weixin_45447770/13773185

 

内容如下:

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usx/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}

stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}

reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}

restart()
{
stop
start
}

configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}

case "$1" in
start)
       start
       ;;
stop)
       stop
       ;;
reload)
       reload
       ;;

restart)
       restart
       ;;

configtest)
       configtest
       ;;
*)

       echo $"Usage: $0 {start|stop|reload|restart|configtest}"
       RETVAL=1
esac


exit $RETVAL

 

4)清空配置文件并重新编写

#> /usr/local/nginx/conf/nginx.conf

内容如下:
user nobody nobody;             //定义 nginx 运行的用户和用户组
worker_processes 2;             //定义 nginx 运行的用户和用户组
error_log /usr/local/nginx/logs/nginx_error.log crit;    //全局错误日志定义类型
pid /usr/local/nginx/logs/nginx.pid;       //进程文件
worker_rlimit_nofile 51200;

events           
{
use epoll;
worker_connections 6000;
}

http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for 

[$time_local]'
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
sendfile on; 
tcp_nopush on; 
keepalive_timeout 30; 
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;

server
{
listen 81;                    //这里博主改为81端口
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/1ocal/nginx/html$fastcgi_script_name;
}
}
}

 

5)编写完成后可以检查一下

# /usr/local/nginx/sbin/nginx -t

 

6)启动 nginx

#service nginx start
编译安装完成!

 

(2)环境准备

1)将nginx.tar.gz复制到/etc/ansible/nginx_install/roles/install/files下启动脚本和配置文件都放到/etc/ansible/nginx_install/roles/install/template下。

#cd    /usr/local/ 
#tar zcvf nginx.tar.gz --exclude=/usr/local/nginx/conf/nginx.conf  nginx
#mv nginx.tar.gz /etc/ansible/nginx_install/roles/install/files/
# cp nginx/conf/nginx.conf /etc/ansible/nginx_install/roles/install/templates/
# cp /etc/init.d/nginx /etc/ansible/nginx_install/roles/install/templates/

2)编写需要的yml文件

#cd /etc/ansible/nginx_install/
#cat install.yml 
---
- hosts: 192.168.2.20               //入口文件
  remote_user: root
  gather_facts: True
  roles:
    - common
    - install

#cat roles/common/tasks/main.yml
- name: install initialization require software
  yum: name={{ item }} state=installed
  with_items:
    - zlib-devel
    - pcre-devel
    - gcc

#cat cat roles/install/vars/main.yml
nginx_user: www      //定义所需变量
nginx_port: 81
nginx_basedir: /usr/local/nginx


#cat roles/install/tasks/copy.yml
- name: Copy Nginx Software 
  copy: src=nginx.tar.gz dest=/tmp/nginx.tar.gz owner=root group=root
- name: Uncompression Nginx Software
  shell: tar zxf /tmp/nginx.tar.gz -C /usr/local/
- name: Copy Nginx Start Script
  template: src=nginx dest=/etc/init.d/nginx owner=root group=root mode=0755
- name: Copy Nginx Config
  template: src=nginx.conf dest={{ nginx_basedir }}/conf/ owner=root group=root mode=0644


# cat roles/install/tasks/install.yml
- name: create nginx user
  user: name={{ nginx_user }} state=present createhome=no shell=/sbin/nologin
- name: start nginx service 
  shell: /etc/init.d/nginx start
- name: add boot start nginx service 
  shell: chkconfig --level 345 nginx on
- name: delete nginx compression files
  shell: rm -rf /tmp/nginx.tar.gz

# cat roles/install/tasks/main.yml
- include: copy.yml       //调用 copy.yml 和 install.yml
- - include: install.yml
(3)执行文件

运行 install.yml 文件:

# ansible-playbook /etc/ansible/nginx_install/install.yml
注:要检查远程机器存在端口占用,及时卸载。

#结果如下:

在这里插入图片描述

4.2 管理配置文件

生产环境中大多时候是需要管理配置文件的,安装软件包只是在初始化环境的时候用一下。下面我们来写个管理 nginx 配置文件的 playbook。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值