Skip to main content

apache2 configuration file parser and query tool

Project description

a2conf is utility and python module to work with apache2 config files.

For all examples we will use file example.conf which is available examples/example.conf. Use export PYTHONPATH=. to use module if it's not installed. utility


Just smart grep

$ bin/a2conf examples/example.conf --cmd ServerName ServerAlias

$ bin/a2conf examples/example.conf --cmd SSLCertificateFile
SSLCertificateFile /etc/letsencrypt/live/

Only arguments (one line, space-separated, non-unique):

$ bin/a2conf examples/example.conf --cmd ServerName ServerAlias --args

Unique arguments:

$ bin/a2conf examples/example.conf --cmd ServerName ServerAlias --uargs

Filtering sections

# Only SSL hosts. Note: listed
$ bin/a2conf examples/example.conf --cmd ServerName ServerAlias --uargs --filter sslengine on

# Inverted filtering, hosts without SSLEngine on. Note: not listed
$ bin/a2conf examples/example.conf --cmd ServerName ServerAlias --uargs --filter sslengine on --neg

Per-vhost info

$ bin/a2conf examples/example.conf  --cmd servername serveralias --uargs --vhost '{vhostargs} {args}'

List ServerName and DocumentRoot for each virtualhost with SSL

$ bin/a2conf examples/example.conf --vhost '{servername} {documentroot}' --filter SSLEngine on /var/www/example

You can get list of all available tokens for --vhost option in verbose mode (-v option).

Node class

Properties and methods

raw - text line as-is, with all spaces, tabs and with comments

cmd - cmd ('ServerName') without args or None (if section)

section - section (e.g. 'VirtualHost')

args - one text line args to cmd or section. for vhost args could be '*:80', for ServerAlias: ''

name - name of node. cmd if node has cmd, or section name (in brackets) if this is section. e.g. 'ServerName' or '<VirtualHost>'


For container sections (VirtualHost) attr content is not None. For usual lines (ServerName) content is None

content - list of child nodes or None

children(name=None, recursive=None) - return generator for all children nodes (e.g. for VirtualHost node). Generator is empty if no children. If name specified, generator will return only nodes with this name (e.g. 'servername' or '<VirtualHost>'). If recursive is On, generator will return nested nodes too (e.g. what is inside <IfModule> or <Directory> settings)


Just dump apache config

examples/ just loads config and dumps its structure:

#!/usr/bin/env python3
import sys
import a2conf
root = a2conf.Node(name='#root')
def recdump(node, prefix=""):
    if node.section:
        print(prefix, "SECTION", node.section, "ARGS", node.args, "CONTENT", len(node.content))
    elif node.cmd:
            print(prefix, repr(node.cmd), repr(node.args))
    for ch in node.children():
        recdump(ch, prefix+"  ")


$ examples/ examples/example.conf
   SECTION VirtualHost ARGS *:80 CONTENT 6
     'DocumentRoot' '/var/www/example'
     'ServerName' ''
     'ServerAlias' ''
     'DirectoryIndex' 'index.html index.htm default.htm index.php'
     'Options' '-Indexes +FollowSymLinks'
   SECTION VirtualHost ARGS *:443 CONTENT 9
     'DocumentRoot' '/var/www/example'
     'ServerName' ''
     'ServerAlias' ''
     'DirectoryIndex' 'index.html index.htm default.htm index.php'
     'Options' '-Indexes +FollowSymLinks'
     'SSLEngine' 'On'
     'SSLCertificateFile' '/etc/letsencrypt/live/'
     'SSLCertificateKeyFile' '/etc/letsencrypt/live/'
     'SSLCertificateChainFile' '/etc/letsencrypt/live/'


examples/ print all SSL sites from config:

#!/usr/bin/env python3
import sys
import a2conf
root = a2conf.Node(name='#root')
for vhost in root.children('<VirtualHost>'):
    servername = next(vhost.children('servername')).args
        ssl_option = next(vhost.children('sslengine')).args
        if ssl_option.lower() == 'on':
            print("{} has SSL enabled".format(servername))
    except StopIteration:
        # No SSL Engine directive in this vhost


$ examples/ examples/example.conf has SSL enabled

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for a2conf, version 0.1.9
Filename, size File type Python version Upload date Hashes
Filename, size a2conf-0.1.9.tar.gz (9.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page