PHP7

by zonmob
6 deployments · 4 still active · last rev. 7 months ago

Installs PHP7 on a Linode. Can also be used as a library for other StackScripts.

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

"""
PHP7 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
PHP7. The functions in this StackScript are designed to be run across the 
Linode Core Distributions:
    - Ubuntu
    - CentOS
    - Debian
    - Fedora

StackScript User-Defined Variables (UDF):

    <UDF name="fpm_enable" label="Install FPM(yes/no)" default="yes" />
"""

import os
import os.path
import subprocess
import sys

try: # we'll need to rename included StackScripts before we can import them
    os.rename("/root/ssinclude-3", "/root/pythonlib.py")
except:
    pass

import pythonlib

def php7_install():

    package = {
        'debian': 'php7.0',
        'redhat': 'php70w'
    }

    if pythonlib.distro is None:
        pythonlib.system_detect_distro()
    
    if pythonlib.distro['family'] == "debian":
        pythonlib.system_package_install("python-software-properties", False)
        subprocess.call(['add-apt-repository','ppa:ondrej/php'])
        pythonlib.system_update()
        
    
    elif pythonlib.distro['family']=="redhat":
        pythonlib.system_package_install("epel-release", False)
        subprocess.call(['rpm','-Uvh','https://mirror.webtatic.com/yum/el7/webtatic-release.rpm'])
    
    pythonlib.system_package_install(package[pythonlib.distro['family']], False)

def php7_fpm_install():
    php7_install_module("fpm")


def php7_install_module(module, update_index=True):
    """Install a PHP7 module."""

    prefix = {
        'debian': 'php7.0-',
        'redhat': 'php70w-'
    }

    pythonlib.system_package_install(prefix[pythonlib.distro['family']] + module, update_index)


def php7_install_module_common():
    """Install most common PHP modules.

    Install Xml, Soap, XmlRpc, mbstring, json, gd, 
    mcrypt, pear, mysql, and the cli."""


    php7_install_module("xml", False)
    php7_install_module("soap", False)
    php7_install_module("xmlrpc", False)
    php7_install_module("mbstring", False)
    php7_install_module("json", False)
    php7_install_module("gd", False)
    php7_install_module("mcrypt", False)
    php7_install_module("mysql", False)
    php7_install_module("cli", False)


def apache_restart():
    if pythonlib.distro['family'] == "debian":
        if os.path.isfile('/usr/sbin/apache2'):
            subprocess.call(['service', 'apache2', 'restart'])
    elif pythonlib.distro['family'] == "redhat":
        if os.path.isfile('/usr/sbin/httpd'):
            subprocess.call(['systemctl', 'restart', 'httpd'])

def nginx_restart():
    if os.path.isfile('/usr/sbin/nginx'):
        if pythonlib.distro['family'] == "debian":
            subprocess.call(['service', 'nginx', 'restart'])
        elif pythonlib.distro['family'] == "redhat":
            subprocess.call(['systemctl', 'restart', 'nginx'])

def main():
    """Install PHP7."""
    # add logging support
    
    pythonlib.init()
    pythonlib.system_update()
    php7_install()
    php7_install_module_common()
    
    if os.environ['FPM_ENABLE'] == "yes":
        php7_fpm_install()
        nginx_restart()
    elif os.environ['FPM_ENABLE'] == "no":
        apache_restart()
    
    pythonlib.end()

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