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



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

[root@kick ~]# ansible nginx -m copy -a "src=teng.sh dest=/tmp"

[root@kick ~]# 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

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


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

[root@kick ~]# ansible nginx -m shell -a "systemctl daemon-reload"

[root@kick ~]# 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

[root@kick ~]# ansible nginx -m copy -a "src=nginx.conf dest=/usr/local/nginx/conf/"

[root@kick ~]# 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






     本文转自812374156 51CTO博客,原文链接:http://blog.51cto.com/xiong51/1941194,如需转载请自行联系原作者


相关文章
|
1月前
|
应用服务中间件 nginx
Nginx安装nginx-rtmp-module模块
【2月更文挑战第4天】 nginx中的模块虽然就是类似插件的概念,但是它无法像VsCode那样轻松的安装扩展。 nginx要安装其它模块必须同时拿到nginx源代码和模块源代码,然后手动编译,将模块打到nginx中,最终生成一个名为nginx的可执行文件。
72 6
|
1天前
|
应用服务中间件 nginx
yum 安装报错 No package nginx available Error:Nothing to do
yum 安装报错 No package nginx available Error:Nothing to do
6 1
|
22天前
|
应用服务中间件 Linux PHP
Linux下安装php环境并且配置Nginx支持php-fpm模块
Linux下安装php环境并且配置Nginx支持php-fpm模块
18 0
|
22天前
|
前端开发 应用服务中间件 网络安全
http转为https,ssl证书安装及nginx配置
http转为https,ssl证书安装及nginx配置
36 1
|
23天前
|
网络协议 应用服务中间件 网络安全
linxu安装nginx
linxu安装nginx
54 0
|
1月前
|
NoSQL 关系型数据库 MySQL
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
Docker安装详细步骤及相关环境安装配置(mysql、jdk、redis、自己的私有仓库Gitlab 、C和C++环境以及Nginx服务代理)
195 0
|
1月前
|
负载均衡 应用服务中间件 nginx
|
1月前
|
应用服务中间件 nginx Windows
windows下快速安装nginx 并配置开机自启动
windows下快速安装nginx 并配置开机自启动
windows下快速安装nginx 并配置开机自启动
|
1月前
|
Kubernetes 负载均衡 应用服务中间件
Ingress Nginx 安装【亲测可用】
Ingress Nginx 安装【亲测可用】
105 2
|
1月前
|
安全 应用服务中间件 nginx