Skip to main content

apache2 config file utilities

Project description

Table of Contents


Package consist of few CLI utilities (based on a2conf library)

  • a2conf - query apache2 config (e.g. get DocumentRoot or get all hostnames for specific VirtualHost)
  • a2certbot - diagnose problems with Apache2 VirtualHost and LetsEncrypt certificates and make SSL sites easily
  • a2vhost - manipulate apache2 VirtualHosts
  • a2okerr - generate indicators for SSL VirtualHosts in okerr monitoring system.

Why a2utils is so great?

You can create VirtualHost right from shell:

a2vhost --basic -d --auto

or even better (create pair of http and https hosts with default auto-guessed settings and LetsEncrypt certificate):

a2vhost --both -d --auto

See all SSLCertificateFile directives for vhosts which has SSLEngine On

a2conf --cmd sslcertificatefile  --filter sslengine on

See all sites and documentroot for them (better then apache2ctl -S)

a2conf --vhfmt '{vhostargs} {servername} {documentroot}'

Which way is easier and error-prone to request certificate?

a2certbot --create -d --aliases


certbot certonly --webroot -w /var/www/website_1234 -d -d -d -d

a2certbot reads all needed data right from apache config.


Usual simple way:

pip3 install a2utils

or get sources from git repo:

git clone

If using git sources (without installing), work from root dir of repo and do export PYTONPATH=.

CLI utilities


a2vhost is utility to create new http/https websites from CLI. Easy to use from your scripts.

Example uses hosts, but you should test with your hostname(s).

Mighty one-liner: create HTTP/HTTPS websites (http will redirect to https), obtain certificate for https. (as root)

a2vhost --both -d --auto

--both instructs to make both https website (main) and small plain http website to handle letsencrypt verification and redirect to https.

--auto auto-detects virtualhost config file name (you may override with -c) and guesses and creates webroot directory if it's missing (override with -w)

Following commands will make similar job step-by-step and without --auto:

Create basic HTTP website

# Create files for new site
$ mkdir /var/www/virtual/
$ echo hello > /var/www/virtual/

# Create HTTP VirtualHost and test
$ a2vhost --basic -d -w /var/www/virtual/ -c /etc/apache2/sites-available/
$ a2ensite
$ systemctl reload apache2
$ curl

Now, lets make this site HTTPS and make new plain HTTP site which will redirect to secure HTTPS

# Generate LetsEncrypt certificate. Yes, thats very simple. We do not need --alises for this vhost, but we may need it if VirtualHost has ServerAlias'es and we want certificates for them.
$ a2certbot --create -d --aliases

# Convert to HTTPS
$ a2vhost --convert -d

# Make HTTP-to-HTTPS redirection
$ a2vhost --redirect -d

# Reload
$ systemctl reload apache2

# List all websites
$ a2vhost --list

In the end we got this config file

  <VirtualHost *:443> 
    DocumentRoot /var/www/virtual/ 

    SSLEngine On 
    SSLCertificateFile /etc/letsencrypt/live/ 
    SSLCertificateKeyFile /etc/letsencrypt/live/ 
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" 

  # auto-generated plain HTTP site for redirect
  <VirtualHost *:80> 
    DocumentRoot /var/www/virtual/ 
    RewriteEngine On 
    RewriteCond %{HTTPS} !=on 
    RewriteCond %{REQUEST_URI} !^/\.well\-known 
    RewriteRule (.*) https://%{SERVER_NAME}$1 [R=301,L] 

Add any directive to any VirtualHost. We will add comment:

# add directive
sudo bin/a2vhost --add '# This site is main https site'  -d --vhost '*:443'

Delete vhost:

a2vhost --delete --vhost '*:80' -d

(you may add -a /etc/apache2/apache2.conf and/or -c /etc/apache2/sites-available/ if your configuration is non-standard)



For all examples we will use file examples/example.conf. You can omit this parameter to use default /etc/apache2/apache2.conf.

Use export PYTHONPATH=. to use module if it's not installed.

Most useful examples:

$ bin/a2conf examples/example.conf --dump --vhost 
# examples/example.conf:15
<VirtualHost *:443> 
    # SSL site
    DocumentRoot /var/www/example 
    ServerName # .... OUR TEST SITE ....
    DirectoryIndex index.html index.htm default.htm index.php 
    Options -Indexes +FollowSymLinks 
    SSLEngine On # SSL Enabled for this virtual host
    SSLCertificateFile /etc/letsencrypt/live/ 
    SSLCertificateKeyFile /etc/letsencrypt/live/ 
    SSLCertificateChainFile /etc/letsencrypt/live/ 

# Only specific commands with --vhost filter
$ bin/a2conf examples/example.conf --vhost --cmd documentroot sslcertificatefile 
DocumentRoot /var/www/example
SSLCertificateFile /etc/letsencrypt/live/

# Same output achieved with other way of filtering (based on SSLEngine directive)
$ bin/a2conf examples/example.conf --filter sslengine on --cmd documentroot sslcertificatefile
DocumentRoot /var/www/example
SSLCertificateFile /etc/letsencrypt/live/

# All hostnames configured in this config file
$ bin/a2conf examples/example.conf --cmd servername serveralias --uargs

# per-vhost summary with filtering
$ bin/a2conf examples/example.conf --cmd servername serveralias --vhfmt 'Host: {servername} Root: {documentroot} Cert: {sslcertificatefile}' --filter sslcertificatefile
Host: Root: /var/www/example Cert: /etc/letsencrypt/live/

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


a2certbot utility used to quickly detect common LetsEncrypt configuration errors such as:

  • DocumentRoot mismatch between VirtualHost and LetsEncrypt renew config file (e.g. if someone moved site content)
  • RewriteRule or Redirect apache directives preventing verification
  • DNS record points to other host or not exists at all
  • And ANY OTHER problem (such as using wrong certificate path in apache or whatever). a2certbot simulates HTTP verification (If LetsEncrypt verification fails, a2certbot will fail too, and vice versa).

a2certbot does not calls LetsEncrypt servers for verification, so if you will use a2certbot to verify your configuration, you will not hit failed validation limit (5 failures per account, per hostname, per hour at moment) and will not be blacklisted on LetsEncrypt site.

Requesting new certificate and troubleshooting

Before requesting new certificates:

# Verify configuration for website for which you want to request certificate for first time.
bin/a2certbot --prepare -w /var/www/virtual/ -d
=== manual ===
    ( is local
    ( Vhost: /etc/apache2/sites-enabled/
    ( DocumentRoot: /var/www/virtual/
    ( DocumentRoot /var/www/virtual/ matches LetsEncrypt and Apache
    ( Simulated check match root: /var/www/virtual/

# You can verify all hostnames for site
bin/a2certbot --prepare -w /var/www/virtual/ -d -d

# ... and finally simple main all-in-one command, it guesses aliases and root (command below does same as command above):
bin/a2certbot --prepare -d --aliases

a2certbot can generate letsencrypt certificates in simple way (automatically detecting all aliases and DocumentRoot, but you can use -d instead of --aliases):

root@bravo:/home/xenon# a2certbot --create -d --aliases
Create cert for
RUNNING: certbot certonly --webroot -w /var/www/virtual/ -d -d
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for
Using the webroot path /var/www/virtual/ for all unmatched domains.
Waiting for verification...
Cleaning up challenges

 - Congratulations! Your certificate and chain have been saved at:

Troubleshooting renew certificates

If certbot renew fails:

# Check (verify) ALL existing LetsEncrypt certificates (to check why 'certbot renew' may fail ):
root@bravo:/home/xenon# a2certbot 
=== /etc/letsencrypt/renewal/ PROBLEM ===
    ( Vhost: /etc/apache2/sites-enabled/okerr.conf:17
    LetsEncrypt conf file: /etc/letsencrypt/renewal/ is local
    No DocumentRoot in vhost at /etc/apache2/sites-enabled/okerr.conf:17

# Verify only one certificate 
root@bravo:/home/xenon# a2certbot --host
=== /etc/letsencrypt/renewal/ PROBLEM ===
    ( Vhost: /etc/apache2/sites-enabled/okerr.conf:17
    LetsEncrypt conf file: /etc/letsencrypt/renewal/ is local
    No DocumentRoot in vhost at /etc/apache2/sites-enabled/okerr.conf:17

a2certbot warnings (false positives)

a2certbot expects that requests to .well-known directory of HTTP (port 80) virtualhost must not be redirected. If you have redirection like this: Redirect 301 / it will report problem:

    Requests will be redirected: Redirect 301 /

Actually, this could be OK (false positive) and real verification from certbot renew may pass (if https site has same DocumentRoot). To see if this is real problem or not see result for 'Simulated check'. If simulated check matches - website will pass certbot verification.

To avoid such false positive, do not use such 'blind' redirection, better use this:

      RewriteCond %{REQUEST_URI} !^/\.well\-known        
      RewriteRule (.*) https://%{SERVER_NAME}$1 [R=301,L]

This code in <VirtuaHost *:80> context will redirect all requests to HTTPS site EXCEPT LetsEncrypt verification requests.


a2certbotssh is wrapper to get certificates using remote machines for verification. Remote machine must have a2utils installed. Example: (any machine):

bin/a2certbotssh --aliases --ssh -d

With this command it will run certbot certonly --manual ... using itself as hooks, to place (and cleanup) validation hooks on remote machine. If --aliases given, it will request all aliases for this virtualhost. For example, also and If --test-cert is given, staging server is used (staging server has much higher rate limits, useful for testing)


a2okerr is useful only if you are using okerr: free and open source hybrid (host/network) monitoring system.

Okerr is like nagios or zabbix, but can perform network checks from remote locations, has tiny and optional local client which can run from cron, has powerful logical indicators (notify me only if more then 2 servers are dead, notify me if any problem is not fixed for more then 30 minutes, ...), public status pages (like but free), fault-tolerant sites (okerr will redirect dynamic DNS record to backup server if main server is dead, and point it back to main server when it's OK), supports Telegram and has many other nice features.

You can use it as free service (like wordpress or gmail) or you can install okerr server on your own linux machine from okerr git repository.

You will need to install small okerrupdate package to use a2okerr: pip3 install okerrupdate.

a2okerr discovers all https sites from apache config and creates SSL-indicator in your okerr project for each website. You will get alert message to email and/or telegram if any of your https sites has any problem (certificate is not updated in time for any reason and will expire soon or already expired. Website unavailable for any reason). If you have linux server or website - you need okerr.

# Create indicator for all local https websites. If indicator already exists, HTTP error 400 will be received - this is OK.

# alter prefix, policy and description
a2okerr --prefix my:prefix: --policy Hourly --desc "I love okerr and a2okerr"

# do not really create indicators, just dry run
a2okerr --dry

Project details

Download files

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

Built Distribution

a2utils-0.0.30-py3-none-any.whl (21.1 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page