1. 程式人生 > >ansible安裝nginx (tengine)

ansible安裝nginx (tengine)

nginx haproxy tengine ansible

# 接上篇

http://xiong51.blog.51cto.com/5239058/1941193

ansible管理機:192.168.8.35 名稱:kick

tomcat主機: 192.168.8.244,192.168.8.245

haproxy : 192.168.8.35

系統版本: Centos7.2



[[email protected] ~]# ansible nginx -m copy -a "src=tengine-2.2.0.tar.gz dest=/tmp"

[[email protected] ~]# ansible nginx -m copy -a "src=teng.sh dest=/tmp"

[[email protected] ~]# ansible nginx -m shell -a "/bin/bash /tmp/teng.sh"


##################### 安裝腳本 #####################

#!/bin/bash

#


yum -y install pcre pcre-devel openssl openssl-devel

tar xf /tmp/tengine-2.2.0.tar.gz -C /tmp

cd /tmp/tengine-2.2.0

./configure --prefix=/usr/local/nginx/ --with-http_realip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_auth_request_module --with-http_upstream_check_module

make -j 4 && make -j 4 install

useradd nginx -s /sbin/nologin

mkdir /usr/local/nginx/run

chown nginx.nginx /usr/local/nginx -R

chmod 777 /usr/local/nginx/run

####################################################



################## 啟動腳本 ##################

cat >> /usr/lib/systemd/system/nginx.server << EOF

[Unit]

Description=The nginx HTTP and reverse proxy server

After=network.target remote-fs.target nss-lookup.target


[Service]

Type=forking

PIDFile=/usr/local/nginx/logs/nginx.pid

# Nginx will fail to start if /usr/local/nginx/logs/nginx.pid already exists but has the wrong

# SELinux context. This might happen when running `nginx -t` from the cmdline.

# https://bugzilla.redhat.com/show_bug.cgi?id=1268621

ExecStartPre=/usr/bin/rm -f /usr/local/nginx/logs/nginx.pid

ExecStartPre=/usr/local/nginx/sbin/nginx -t

ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

ExecReload=/bin/kill -s HUP $MAINPID

KillSignal=SIGQUIT

TimeoutStopSec=5

KillMode=process

PrivateTmp=true


[Install]

WantedBy=multi-user.target

EOF

#####################################################


[[email protected] ~]# ansible nginx -m copy -a "src=nginx.service dest=/usr/lib/systemd/system/"

[[email protected] ~]# ansible nginx -m shell -a "systemctl daemon-reload"

[[email protected] ~]# ansible nginx -m shell -a "systemctl restart nginx"


########## 如一直出錯檢查一下端口被占情況 #########




####################### nginx 配置文件 ##########################

user nginx;

worker_processes 2;

error_log /usr/local/nginx/logs/error.log;

pid /usr/local/nginx/logs/nginx.pid;


# Load dynamic modules. See /usr/share/nginx/README.dynamic.

include /usr/local/nginx/modules/*.conf;


events {

worker_connections 1024;

}


http {

log_format main ‘$http_x_forwarded_for $request_time - $remote_user [$time_local] "$request" ‘

‘$status $body_bytes_sent "$http_referer" ‘

‘"$http_user_agent"‘;


access_log /usr/local/nginx/logs/access.log main;


sendfile on;

tcp_nopush on;

tcp_nodelay on;

keepalive_timeout 65;

types_hash_max_size 2048;


include /usr/local/nginx/conf/mime.types;

default_type application/octet-stream;


upstream xiong.com {

server 192.168.8.247:8080;

server 192.168.8.246:8080;

check interval=3000 rise=2 fall=5 timeout=1000 type=http;

}


server {

listen 80 default_server;

server_name localhost;

charset utf-8;

server_tokens off;


# Load configuration files for the default server block.

include /etc/nginx/default.d/*.conf;


location / {

proxy_pass http://xiong.com;

proxy_set_header Host $proxy_host;

proxy_set_header Connection Close;

proxy_set_header X-Forwarded-For $remote_addr;

}

location /status {

check_status;

access_log off;

}

}

}

###########################################################################


# 復制文件並生啟Nginx

[[email protected] ~]# ansible nginx -m copy -a "src=nginx.conf dest=/usr/local/nginx/conf/"

[[email protected] ~]# ansible nginx -m shell -a "systemctl restart nginx"




# nginx_check_module 訪問 在Haproxy中訪問效果,也可以直接使用nginx地址訪問

http://192.168.8.35:81/status






###################### haproxy配置 ######################

# kickstact中安裝

yum -y install haproxy


##################### haproxy配置文件 #####################

global

log 127.0.0.1 local2

chroot /var/lib/haproxy

pidfile /var/run/haproxy.pid

maxconn 4000

user haproxy

group haproxy

daemon


stats socket /var/lib/haproxy/stats


defaults

mode http

log global

option httplog

option dontlognull

option http-server-close

option forwardfor except 127.0.0.0/8

option redispatch

retries 3

timeout http-request 10s

timeout queue 1m

timeout connect 10s

timeout client 1m

timeout server 1m

timeout http-keep-alive 10s

timeout check 10s

maxconn 3000


listen stats

bind 0.0.0.0:88 #配置狀態信息 端口號為88

mode http #模式為http

stats enable #開啟查詢狀態

stats hide-version #隱藏版本號

stats uri /xiong?status #訪問狀態的uri

stats auth xiong:xiong #認證帳號密碼信息


frontend http *:81

option forwardfor header X-Forwarded-For #配置後端顯示的IP信息

default_backend tes #默認後端地址


backend tes

balance roundrobin #使用rr方式

server tes1 192.168.8.244:80 check

server tes2 192.168.8.245:80 check


本文出自 “xiong” 博客,請務必保留此出處http://xiong51.blog.51cto.com/5239058/1941194

ansible安裝nginx (tengine)