Python -- deploying LNMP business service environment

Please indicate the source of Reprint:

Before we go online, we need to deploy the corresponding environment, often a business involves multiple environments, such as Web, DB, Proxy, Cache, etc. in this example, we define different roles through env.roledefs, and then use the @ roles("webservers") modifier to bind to the corresponding task function to realize the host deployment differences of different roles. The specific code is as follows:

# -*- coding:UTF-8 -*-
//Deploy LNMP business service environment
Created on 2018 January 12, 2010

@author: liuyazhuang

from fabric.colors import *
from fabric.api import *

env.user = 'root'
#Define business role groups
env.roledefs = {
    'webservers':['', ''],

env.passwords = {
    'root@' : "Password 1",
    'root@' : "Password 2",
    'root@' : "Password 3",

@roles('webservers') #webtask function references' webservers' role modifier
def webtask():  #Deploying nginx PHP FPM environment
    print yellow("Install nginx php php-fpm...")
    with settings(warn_only = True):
        run("yum -y install nginx")
        run("yum -y install php-fpm php-mysql php-mbstring php-xml php-mcrypt php-gd")
        run("chkconfig --levels 235 php-fpm on")
        run("chkconfig --levels 235 nginx on")
@roles('dbservers') #dbatsk task function references dbservers role modifier
def dbtask():   #Deploy mysql environment
    print yellow("Install MySQL...")
    with settings(warn_only = True):
        run("yum -y install mysql mysql-server")
        run("chkconfig --levels 235 mysqld on")
@roles('webservers', 'dbservers')   #The publictask task function references two role modifiers at the same time
def publictask():   #Deploy public class environment, such as epel, ntp, etc
    print yellow("Install epel ntp...")
    with settings(warn_only = True):
        run("rpm -Uvh")
        run("yum -y install ntp")
def deploy():
This example uses roles to distinguish different business service environments and deploy different packages. We only need a Python script to complete the customization of different business environments.

Tags: PHP MySQL Nginx yum

Posted on Mon, 04 May 2020 08:48:51 -0700 by shibobo12