Nginx

by zonmob
12 deployments · 1 still active · last rev. 7 months ago

Install Nginx to a Linode. Can also be used as a library for other StackScripts.

Compatible with: CentOS 7, Debian 8, Ubuntu 14.04 LTS
						#!/usr/bin/env python

"""
Nginx Web Server StackScript
    
    Author: Hieu Bui Duc <hieubd@zonmob.com>
    Version: 1.0.0.0
    Requirements:
        - ss://linode/python-library <ssinclude StackScriptID="3">

This StackScript both deploys as well as provides a library of functions for
the Nginx Web Server. The functions in this StackScript are designed
to be run across the Linode Core Distributions:
    - Ubuntu
    - CentOS
    - Debian
    - Fedora
"""

import os
import subprocess
import sys

try: # we need to rename the included StackScript before we can import it
    os.rename("/root/ssinclude-3", "/root/pythonlib.py")
except:
    pass

import pythonlib

def nginx_install(start=True):

    """Install repo package."""

    if pythonlib.distro is None:
        pythonlib.system_detect_distro()
    
    if pythonlib.distro['family'] == "debian":
        # nginx key
        subprocess.call(['wget', 'http://nginx.org/keys/nginx_signing.key'])
        subprocess.call(['apt-key','add', 'nginx_signing.key'])
        subprocess.call(['rm','-rf', 'nginx_signing.key'])

        # add nginx to source list
        if pythonlib.distro['distname'] == "debian":
            with open("/etc/apt/sources.list", "a") as f:
                f.write("\n#Nginx repo\n")
                f.write("deb http://nginx.org/packages/debian/  jessie nginx\n")
                f.write("deb-src http://nginx.org/packages/debian/ jessie nginx\n")
        elif pythonlib.distro['distname'] == "ubuntu":
            with open("/etc/apt/sources.list", "a") as f:
                f.write("\n#Nginx repo\n")
                f.write("deb http://nginx.org/packages/ubuntu/ %s nginx\n" % pythonlib.distro['codename'])
                f.write("deb-src http://nginx.org/packages/ubuntu/ %s nginx\n" % pythonlib.distro['codename'])

    elif pythonlib.distro['distname'] == "centos":
        with open("/etc/yum.repos.d/nginx.repo", "w") as f:
            f.write("""[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1""")
        
    elif pythonlib.distro['distname'] == "fedora":
        with open("/etc/yum.repos.d/nginx.repo", "w") as f:
            f.write("""[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/7/$basearch/
gpgcheck=0
enabled=1""")

    """Install Nginx Web Server"""

    pythonlib.system_package_install("nginx", False)

    if(pythonlib.distro['family']=="redhat" and start):
        subprocess.call(["systemctl", "start", "nginx"])
	

def main():
    """Install Nginx."""
    
    pythonlib.init()
    pythonlib.system_update()
    nginx_install()

    pythonlib.end()


if __name__ == "__main__":
    sys.exit(main())