自动安装nginx 不同版本

自动化部署Nginx教程
本文提供了一种自动化部署Nginx的方法,包括下载指定版本的Nginx、安装依赖包、克隆必要的第三方模块并进行编译安装等步骤。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python
# conding:utf8
 
from optparse import OptionParser
import re
import pycurl
import StringIO
import sys
import urllib2
import subprocess
import threading
import os
from optparse import OptionParser
 
def parse_url(url):
    = pycurl.Curl()
    = StringIO.StringIO()
    c.setopt(c.URL, url)
    c.setopt(pycurl.FOLLOWLOCATION, 1)
    c.setopt(c.WRITEFUNCTION, b.write)
    c.perform()
    c.setopt(c.CONNECTTIMEOUT, 5)
    c.setopt(c.TIMEOUT, 5)
    status = c.getinfo(pycurl.HTTP_CODE)
    if status == 200:
        content = b.getvalue()
        return content
    c.close()
    b.close()
 
def nginx_download(version):
    nginx_package = "nginx-%s.tar.gz" % (version)
    url = "http://nginx.org/download/%s" % (nginx_package)
    if not os.path.exists("/root/%s" % (nginx_package)):
        print "download %s ..." % (nginx_package)
        = urllib2.urlopen(url)
        data = f.read()
        with open("/root/%s" % (nginx_package), "wb") as nginx:
            nginx.write(data)
        print "download success"
    return 0
 
def list_nginx():
    url = "http://nginx.org/download/"
    content = parse_url(url)
    version = []
    = re.compile(r'>(nginx-(.*?).tar.gz)<')
    for in p.finditer(content):
        version.append(m.group(2)) 
    return version[:-21:-1]
 
def install_nginx(version):
    nginx_package = "nginx-%s.tar.gz" % (version)
    lua_module = "https://github.com/openresty/lua-nginx-module.git"
    echo_module = "https://github.com/openresty/echo-nginx-module.git"
    check_module = "https://github.com/yaoweibin/nginx_upstream_check_module"
     
    packages = ['openssl-devel''zlib-devel''pcre-devel''lua-devel''gcc']
     
    for package in packages:
        = subprocess.call("rpm -qa | grep '%s'" % (package), shell=True)
       print p
        if p != 0:
            p1 = subprocess.call("yum install %s -y" % (package), shell=True)
        if p1 == 0:
            print "yum install %s success" % (package)
 
    if os.path.exists("/root"):
            subprocess.call("cd /root", shell=True)
 
        if not os.path.exists("/root/lua-nginx-module"): 
            print "git lua-nginx-module ..."
            = subprocess.call("git clone %s" % (lua_module), shell=True)
            if == 0:
                print "git lua-nginx-module success"
 
        if not os.path.exists("/root/echo-nginx-module"):
            print "git echo-nginx-module ..."
            = subprocess.call("git clone %s" % (echo_module), shell=True)
            if == 0:
                print "git echo-nginx-module success"
 
        if not os.path.exists("/root/nginx_upstream_check_module"):  
            print "git nginx_upstream_check_module ..."
            = subprocess.call("git clone %s" % (check_module), shell=True)
            if == 0:
                print "git nginx_upstream_check_module success"
         
        if not os.path.exists("/root/nginx-%s" % (version)):
            print "tar %s ..." % (nginx_package)
            subprocess.call("tar -xzvf %s" % (nginx_package), shell=True)
            print "tar success"
 
        print "install %s ..." % (nginx_package)
        = subprocess.call("cd /root/nginx-%s;./configure --prefix=/usr/local/nginx-%s --with-http_ssl_module --with-http_gzip_static_module --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --add-module=/root/lua-nginx-module --add-module=/root/echo-nginx-module" % (version, version), shell=True)
        if == 0:
            = subprocess.call("cd /root/nginx-%s;make" % (version), shell=True)
            if == 0:
                subprocess.call("cd /root/nginx-%s;make install" % (version), shell=True)
                print "install %s success" % (nginx_package)
 
def main():
    parser = OptionParser()
    parser.add_option("-l""--list", action="store_true", dest="list"help="list nginx version")
    parser.add_option("-i""--version", action="store", dest="version"help="install nginx version")
    (options, args) = parser.parse_args()
    if options.list:
        for version in list_nginx():
            print version
    elif options.version:
        if options.version in list_nginx():
            if nginx_download(options.version) == 0:
                install_nginx(options.version)
     
if __name__ == '__main__':
    main() 









本文转自 Art_Hero 51CTO博客,原文链接:http://blog.51cto.com/curran/1624960,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值