ansible安装nginx (tengine)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible安装nginx (tengine)相关的知识,希望对你有一定的参考价值。

# 接上篇

    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)的主要内容,如果未能解决你的问题,请参考以下文章

安装nginx后,怎么安装/升级到tengine

编译安装Tengine

基于nginx和tengine的tcp反向代理,负载均衡 安装和配置

Nginx/Tengine 服务器安装 SSL 证书

Nginx和Tengine的详细安装图文教程(Linux下)

在Nginx/Tengine服务器上安装证书