def __init__(self, nginx_dir, sid, ipaddr, offset_char=' '): NginxConfig.__init__(self, offset_char=' ') self.nginx_dir = nginx_dir self.sid = sid self.ipaddr = ipaddr nginx_conf = os.path.join(nginx_dir, 'nginx.conf') self.format_data(nginx_conf)
def process_include(self, globpath): if not path.isabs(globpath): globpath = path.join(self.config_dir, globpath) conf = [] for fname in sorted(glob(globpath)): nc = NginxConfig() nc.load(open(fname).read()) conf += list(nc) return conf
def loadNginxConfig(configPath='/etc/nginx/nginx.conf'): """ Load the nginx configuration, then parse with pynginxconfig. Default to loading from /etc/nginx/nginx.conf """ with open (configPath, 'r') as configFile: raw=configFile.readlines() confData = ''.join(raw) nc = NginxConfig() nc.load(confData) return nc
def __init__(self, conf_files, default_addr="127.0.0.1", excluded_hosts=None, config_dir="/etc/nginx", use_listen=False ): self.default_addr = default_addr self.excluded_hosts = excluded_hosts if isinstance(excluded_hosts, list) else [] self.config_dir = config_dir self.use_listen = use_listen self.data = [] nc = NginxConfig() for file in conf_files: nc.load(file.read()) self.process_main(nc)
def info_nginx(self): """Display Nginx information""" version = os.popen("nginx -v 2>&1 | cut -d':' -f2 | cut -d' ' -f2 | " "cut -d'/' -f2 | tr -d '\n'").read() allow = os.popen("grep ^allow /etc/nginx/common/acl.conf | " "cut -d' ' -f2 | cut -d';' -f1 | tr '\n' ' '").read() nc = NginxConfig() nc.loadf('/etc/nginx/nginx.conf') user = nc.get('user')[1] worker_processes = nc.get('worker_processes')[1] worker_connections = nc.get([('events',), 'worker_connections'])[1] keepalive_timeout = nc.get([('http',), 'keepalive_timeout'])[1] fastcgi_read_timeout = nc.get([('http',), 'fastcgi_read_timeout'])[1] client_max_body_size = nc.get([('http',), 'client_max_body_size'])[1] data = dict(version=version, allow=allow, user=user, worker_processes=worker_processes, keepalive_timeout=keepalive_timeout, worker_connections=worker_connections, fastcgi_read_timeout=fastcgi_read_timeout, client_max_body_size=client_max_body_size) self.app.render((data), 'info_nginx.mustache')
def debug_php73(self): """Start/Stop PHP debug""" # PHP global debug start if (self.app.pargs.php73 == 'on' and not self.app.pargs.site_name): if not (WOShellExec.cmd_exec( self, "sed -n \"/upstream php73" "{/,/}/p \" /etc/nginx/" "conf.d/upstream.conf " "| grep 9173")): Log.info(self, "Enabling PHP 7.3 debug") # Change upstream.conf nc = NginxConfig() nc.loadf('/etc/nginx/conf.d/upstream.conf') nc.set([( 'upstream', 'php73', ), 'server'], '127.0.0.1:9173') nc.savef('/etc/nginx/conf.d/upstream.conf') # Enable xdebug WOFileUtils.searchreplace( self, "/etc/php/7.3/mods-available/" "xdebug.ini", ";zend_extension", "zend_extension") # Fix slow log is not enabled default in PHP5.6 config = configparser.ConfigParser() config.read('/etc/php/7.3/fpm/pool.d/debug.conf') config['debug']['slowlog'] = '/var/log/php/7.3/slow.log' config['debug']['request_slowlog_timeout'] = '10s' with open('/etc/php/7.3/fpm/pool.d/debug.conf', encoding='utf-8', mode='w') as confifile: Log.debug( self, "Writting debug.conf configuration into " "/etc/php/7.3/fpm/pool.d/debug.conf") config.write(confifile) self.trigger_php = True self.trigger_nginx = True else: Log.info(self, "PHP debug is already enabled") self.msg = self.msg + ['/var/log/php/7.3/slow.log'] # PHP global debug stop elif (self.app.pargs.php73 == 'off' and not self.app.pargs.site_name): if WOShellExec.cmd_exec( self, " sed -n \"/upstream " "php72 {/,/}/p\" " "/etc/nginx/conf.d/upstream.conf " "| grep 9172"): Log.info(self, "Disabling PHP 7.2 debug") # Change upstream.conf nc = NginxConfig() nc.loadf('/etc/nginx/conf.d/upstream.conf') nc.set([( 'upstream', 'php72', ), 'server'], 'unix:/var/run/php/php72-fpm.sock') nc.savef('/etc/nginx/conf.d/upstream.conf') # Disable xdebug WOFileUtils.searchreplace( self, "/etc/php/7.2/mods-available/" "xdebug.ini", "zend_extension", ";zend_extension") self.trigger_php = True self.trigger_nginx = True else: Log.info(self, "PHP 7.2 debug is already disabled")
def debug_php7(self): """Start/Stop PHP debug""" # PHP global debug start if (self.app.pargs.php7 == 'on' and not self.app.pargs.site_name): if (EEVariables.ee_platform_codename == 'wheezy' or EEVariables.ee_platform_codename == 'precise'): Log.error(self,"PHP 7.0 not supported.") if not (EEShellExec.cmd_exec(self, "sed -n \"/upstream php7" "{/,/}/p \" /etc/nginx/" "conf.d/upstream.conf " "| grep 9170")): Log.info(self, "Enabling PHP 7.0 debug") # Change upstream.conf nc = NginxConfig() nc.loadf('/etc/nginx/conf.d/upstream.conf') nc.set([('upstream','php',), 'server'], '127.0.0.1:9170') if os.path.isfile("/etc/nginx/common/wpfc-hhvm.conf"): nc.set([('upstream','hhvm',), 'server'], '127.0.0.1:9170') nc.savef('/etc/nginx/conf.d/upstream.conf') # Enable xdebug if (EEVariables.ee_platform_codename != 'jessie'): EEFileUtils.searchreplace(self, "/etc/php/7.0/mods-available/" "xdebug.ini", ";zend_extension", "zend_extension") else: EEFileUtils.searchreplace(self, "/etc/php/mods-available/" "xdebug.ini", ";zend_extension", "zend_extension") # Fix slow log is not enabled default in PHP5.6 config = configparser.ConfigParser() config.read('/etc/php/7.0/fpm/pool.d/debug.conf') config['debug']['slowlog'] = '/var/log/php/7.0/slow.log' config['debug']['request_slowlog_timeout'] = '10s' with open('/etc/php/7.0/fpm/pool.d/debug.conf', encoding='utf-8', mode='w') as confifile: Log.debug(self, "Writting debug.conf configuration into " "/etc/php/7.0/fpm/pool.d/debug.conf") config.write(confifile) self.trigger_php = True self.trigger_nginx = True else: Log.info(self, "PHP debug is already enabled") self.msg = self.msg + ['/var/log/php/7.0/slow.log'] # PHP global debug stop elif (self.app.pargs.php7 == 'off' and not self.app.pargs.site_name): if EEShellExec.cmd_exec(self, " sed -n \"/upstream php {/,/}/p\" " "/etc/nginx/conf.d/upstream.conf " "| grep 9170"): Log.info(self, "Disabling PHP 7.0 debug") # Change upstream.conf nc = NginxConfig() nc.loadf('/etc/nginx/conf.d/upstream.conf') nc.set([('upstream','php',), 'server'], '127.0.0.1:9070') if os.path.isfile("/etc/nginx/common/wpfc-hhvm.conf"): nc.set([('upstream','hhvm',), 'server'], '127.0.0.1:8000') nc.savef('/etc/nginx/conf.d/upstream.conf') # Disable xdebug if (EEVariables.ee_platform_codename != 'jessie'): EEFileUtils.searchreplace(self, "/etc/php/7.0/mods-available/" "xdebug.ini", "zend_extension", ";zend_extension") else: EEFileUtils.searchreplace(self, "/etc/php/mods-available/" "xdebug.ini", "zend_extension", ";zend_extension") self.trigger_php = True self.trigger_nginx = True else: Log.info(self, "PHP 7.0 debug is already disabled")
def debug_php(self): """Start/Stop PHP debug""" # PHP global debug start if (self.app.pargs.php == 'on' and not self.app.pargs.site_name): if not (WOShellExec.cmd_exec( self, "sed -n \"/upstream php" "{/,/}/p \" /etc/nginx/" "conf.d/upstream.conf " "| grep 9001")): Log.info(self, "Enabling PHP debug") # Change upstream.conf nc = NginxConfig() nc.loadf('/etc/nginx/conf.d/upstream.conf') nc.set([( 'upstream', 'php', ), 'server'], '127.0.0.1:9001') nc.savef('/etc/nginx/conf.d/upstream.conf') # Enable xdebug WOFileUtils.searchreplace( self, "/etc/{0}/mods-available/".format("php/7.2" if ( WOVariables.wo_platform_distro == 'ubuntu' ) else "php/7.2") + "xdebug.ini", ";zend_extension", "zend_extension") # Fix slow log is not enabled default in PHP5.6 config = configparser.ConfigParser() config.read( '/etc/{0}/fpm/pool.d/debug.conf'.format("php/7.2" if ( WOVariables.wo_platform_distro == 'ubuntu' ) else "php5")) config['debug']['slowlog'] = '/var/log/{0}/slow.log'.format( "php/7.2" if (WOVariables.wo_platform_distro == 'ubuntu' ) else "php5") config['debug']['request_slowlog_timeout'] = '10s' with open( '/etc/{0}/fpm/pool.d/debug.conf'.format("php/7.2" if ( WOVariables.wo_platform_distro == 'ubuntu' ) else "php5"), encoding='utf-8', mode='w') as confifile: Log.debug( self, "Writting debug.conf configuration into " "/etc/{0}/fpm/pool.d/debug.conf".format("php/7.2" if ( WOVariables.wo_platform_distro == 'ubuntu' ) else "php5")) config.write(confifile) self.trigger_php = True self.trigger_nginx = True else: Log.info(self, "PHP debug is already enabled") self.msg = self.msg + [ '/var/log/{0}/slow.log'.format("php/7.2" if ( WOVariables.wo_platform_distro == 'ubuntu') else "php5") ] # PHP global debug stop elif (self.app.pargs.php == 'off' and not self.app.pargs.site_name): if WOShellExec.cmd_exec( self, " sed -n \"/upstream php {/,/}/p\" " "/etc/nginx/conf.d/upstream.conf " "| grep 9001"): Log.info(self, "Disabling PHP debug") # Change upstream.conf nc = NginxConfig() nc.loadf('/etc/nginx/conf.d/upstream.conf') nc.set([( 'upstream', 'php', ), 'server'], '127.0.0.1:9000') nc.savef('/etc/nginx/conf.d/upstream.conf') # Disable xdebug WOFileUtils.searchreplace( self, "/etc/{0}/mods-available/".format("php/7.2" if ( WOVariables.wo_platform_distro == 'ubuntu' ) else "php5") + "xdebug.ini", "zend_extension", ";zend_extension") self.trigger_php = True self.trigger_nginx = True else: Log.info(self, "PHP debug is already disabled")
def prepare_for_nginx(config, _, compose_conf_template): compose_conf = deepcopy(compose_conf_template) compose_conf['ports'] = [ '{0}:{0}'.format(params['APPLICATION_PORT']) for params in config.values() if params.get('APPLICATION_PORT') ] nc = NginxConfig() nc.append(('user', 'nginx')) nc.append(('pid', '/var/run/nginx.pid')) nc.append(('worker_processes', '1')) nc.append(('error_log', 'stderr info')) nc.append({ 'name': 'events', 'param': '', 'value': [('worker_connections', '1024')] }) http_conf = { 'name': 'http', 'param': '', 'value': [ ('include', '/etc/nginx/mime.types'), ('default_type', 'application/octet-stream'), ('log_format main', '$remote_addr [$time_local] "$request" $status $bytes_sent "$http_referer"' ), ('sendfile', 'on'), ('keepalive_timeout', '65'), ('include', '/etc/nginx/conf.d/*.conf'), ] } for service_name, params in config.items(): if 'nginx' in params['DEPENDS_ON']: project_name = params['PROJECT_NAME'] server_conf = { 'name': 'server', 'param': '', 'value': [ ('server_name', params['SERVER_NAME']), ('charset', 'utf-8'), ('client_max_body_size', '75M'), { 'name': 'location', 'param': '/', 'value': [('uwsgi_pass', 'unix:///opt/sockets/{}.sock'.format(service_name)), ('include', '/opt/uwsgi_params')] }, ] } if params['USE_SSL']: server_conf['value'].extend([ ('listen', '{} ssl'.format(params['APPLICATION_PORT'])), ('ssl_certificate', '/opt/certs/{}'.format(params['SSL_CERT'])), ('ssl_certificate_key', '/opt/certs/{}'.format(params['SSL_KEY'])), ]) else: server_conf['value'].append( ('listen', '{}'.format(params['APPLICATION_PORT']))) if params['USE_STATIC']: server_conf['value'].append({ 'name': 'location', 'param': '/static', 'value': [('alias', '/opt/static/{}'.format(project_name))] }) if params['USE_MEDIA']: server_conf['value'].append({ 'name': 'location', 'param': '/media', 'value': [('alias', '/opt/media/{}'.format(project_name))] }) http_conf['value'].append(server_conf) nc.append(http_conf) with open('nginx.conf', 'w') as f: f.write(nc.gen_config()) return compose_conf
def debug_php7(self): """Start/Stop PHP debug""" # PHP global debug start if (self.app.pargs.php7 == 'on' and not self.app.pargs.site_name): if (EEVariables.ee_platform_codename == 'wheezy' or EEVariables.ee_platform_codename == 'precise'): Log.error(self, "PHP 7.2 not supported.") if not (EEShellExec.cmd_exec( self, "sed -n \"/upstream php7" "{/,/}/p \" /etc/nginx/" "conf.d/upstream.conf " "| grep 9170")): Log.info(self, "Enabling PHP 7.2 debug") # Change upstream.conf nc = NginxConfig() nc.loadf('/etc/nginx/conf.d/upstream.conf') nc.set([( 'upstream', 'php7', ), 'server'], '127.0.0.1:9170') if os.path.isfile("/etc/nginx/common/wpfc-hhvm.conf"): nc.set([( 'upstream', 'hhvm', ), 'server'], '127.0.0.1:9170') nc.savef('/etc/nginx/conf.d/upstream.conf') # Enable xdebug EEFileUtils.searchreplace( self, "/etc/php/7.2/mods-available/" "xdebug.ini", ";zend_extension", "zend_extension") # Fix slow log is not enabled default in PHP5.6 config = configparser.ConfigParser() config.read('/etc/php/7.2/fpm/pool.d/debug.conf') config['debug']['slowlog'] = '/var/log/php/7.2/slow.log' config['debug']['request_slowlog_timeout'] = '10s' with open('/etc/php/7.2/fpm/pool.d/debug.conf', encoding='utf-8', mode='w') as confifile: Log.debug( self, "Writting debug.conf configuration into " "/etc/php/7.2/fpm/pool.d/debug.conf") config.write(confifile) self.trigger_php = True self.trigger_nginx = True else: Log.info(self, "PHP debug is already enabled") self.msg = self.msg + ['/var/log/php/7.2/slow.log'] # PHP global debug stop elif (self.app.pargs.php7 == 'off' and not self.app.pargs.site_name): if EEShellExec.cmd_exec( self, " sed -n \"/upstream php7 {/,/}/p\" " "/etc/nginx/conf.d/upstream.conf " "| grep 9170"): Log.info(self, "Disabling PHP 7.2 debug") # Change upstream.conf nc = NginxConfig() nc.loadf('/etc/nginx/conf.d/upstream.conf') nc.set([( 'upstream', 'php7', ), 'server'], '127.0.0.1:9070') if os.path.isfile("/etc/nginx/common/wpfc-hhvm.conf"): nc.set([( 'upstream', 'hhvm', ), 'server'], '127.0.0.1:8000') nc.savef('/etc/nginx/conf.d/upstream.conf') # Disable xdebug EEFileUtils.searchreplace( self, "/etc/php/7.2/mods-available/" "xdebug.ini", "zend_extension", ";zend_extension") self.trigger_php = True self.trigger_nginx = True else: Log.info(self, "PHP 7.2 debug is already disabled")
def generate(inputf, outputf, PROJECT_PATH='/webapp/', DJANGO_SERVERS=["localhost:80", "localhost:81"], TWISTED_SERVERS=["localhost:8000", "localhost:8001"], SHINY_SERVERS=["localhost:80", "localhost:81"]): UWSGI_PARAM = PROJECT_PATH + 'uwsgi_params' DJANGO_ROOT = PROJECT_PATH + "energyinsight2.0/" nc = NginxConfig() nc.loadf(inputf) # Set twisted server upstream twisted_servers = [('server', x) for x in TWISTED_SERVERS] nc.set([('http', ), ('upstream', 'twisted')], twisted_servers) # Set django server upstream django_servers = [('server', x) for x in DJANGO_SERVERS] nc.set([('http', ), ('upstream', 'django')], django_servers) # Set shiny server upstream shiny_servers = [('server', x) for x in SHINY_SERVERS] nc.set([('http', ), ('upstream', 'shiny')], shiny_servers) # Set the uwsgi_parameters nc.set([('http', ), ('server', ), ('location', '~ ^\\/api\\/(put|get)series'), 'include'], UWSGI_PARAM) nc.set([('http', ), ('server', ), ('location', '/'), 'include'], UWSGI_PARAM) nc.set([('http', ), ('server', ), ('location', '/static')], [('root', DJANGO_ROOT + "/")]) # set lua path nc.set([('http', ), 'lua_package_path'], "\"" + PROJECT_PATH + "jwt/?.lua;;\"") # http_block=nc.get(('http',))['value']; # for b in http_block: # if b['name']=='server': # for i,t in enumerate(b['value']): # if isinstance(t,tuple) and t[0]=='lua_package_path': # b['value'][i]=('lua_package_path',"\""+PROJECT_PATH+";;\""); nc.savef(outputf) # setup twisted server configuration #nc.append({'name':'upstrea','param':'twisted','value':[('server','localhost'),('server','app.equotaenergy.com')]},http_block,position=1); # setup django server configuration #nc.append({'name':'upstrea','param':'django','value':[('server','localhost'),('server','app.equotaenergy.com')]},http_block,position=1); #print(nc.gen_config())
def install_nginx(): """ Install NGINX and make it use certs. """ require.arch.package("nginx") contents = PROXIED_SITE_TEMPLATE % {"server_name": "cozy", "port": 443, "proxy_url": "http://127.0.0.1:9104"} if files.exists("/etc/nginx/conf.d"): require.files.file("/etc/nginx/conf.d/cozy.conf", contents=contents, use_sudo=True) else: config = NginxConfig() config.load(sudo("cat /etc/nginx/nginx.conf")) server = NginxConfig() server.load(contents) config.append(server[0], root=config.get_value(config.get(("http",)))) put(StringIO(config.gen_config()), "/etc/nginx/nginx.conf", use_sudo=True) if files.exists("/etc/nginx/conf.d/default.conf"): su_delete("/etc/nginx/conf.d/default.conf") if files.exists("/etc/nginx/conf.d/example_ssl.conf"): su_delete("/etc/nginx/conf.d/example_ssl.conf") service.restart("nginx") print(green("Nginx successfully installed."))
#!/usr/bin/env python __author__ = 'babnik' from pynginxconfig import NginxConfig import argparse import subprocess parser = argparse.ArgumentParser(description='Test editor for nginx host') parser.add_argument('-b', '--backend', metavar=' ip:port', type=str, help='Add backend servers to pool', nargs='+') args=parser.parse_args() val=[] for host in args.backend: val.append(('server', host)) nc = NginxConfig() nc.load('') nc.append({'name': 'upstream', 'param': 'backend', 'value': val}, position=1) f1 = open("/etc/nginx/sites-available/default", 'a') f1.write(str(nc.gen_config())) f1.close()
# coding=utf-8 import os from pynginxconfig import NginxConfig """ 分析nginx配置目录下的.conf结尾的配置文件 """ nc = NginxConfig() #nginxdir = "/root/python_nginx_conf/conf/" #nginxconf = "/root/python_nginx_conf/conf/nginx.conf" nginxdir = "/opt/config_file/2017-03-23_1/10.201.96.7/2017-03-23/nginx/conf" nginxconf = "/opt/config_file/2017-03-23_1/10.201.96.7/2017-03-23/nginx/conf/nginx.conf" # nginxdir = "/root/python_nginx_conf/new_conf" # nginxconf = "/root/python_nginx_conf/new_conf/nginx.conf" read_conf = " " def get_read(nginxconf): global read_conf if os.path.exists(nginxconf): with open(nginxconf) as fs: f = fs.readlines() for line in f: if "#" in line or "mime.types" in line or \ "proxy_set_header_group" in line: continue elif "include" in line and "conf" in line: fp = line.split()[1].strip(';') if fp.endswith("*.conf"): if fp.startswith("/"):