Skip to content

Instantly share code, notes, and snippets.

@k4ml
Created March 12, 2011 22:29
Show Gist options
  • Save k4ml/867635 to your computer and use it in GitHub Desktop.
Save k4ml/867635 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from optparse import OptionParser
from os.path import join, dirname, abspath
from string import Template
import os, sys
parser = OptionParser()
parser.add_option("-a", "--address", default="127.0.0.1", help="Server address to listen")
parser.add_option("-p", "--port", default="8000", help="Server port to listen")
parser.add_option("-k", "--command", help="Apache2 command to run")
parser.add_option("-b", "--httpd", default="apache2", help="Apache2 httpd binary to run")
(option, args) = parser.parse_args()
HOME = os.environ['HOME']
APP_ROOT = '%s/webapps' % HOME
DOC_ROOT = '%s/var/www' % HOME
SERVER_ROOT = join(APP_ROOT, 'apache2')
CONFIG_FILE = join(SERVER_ROOT, 'etc/apache2.conf')
if option.command in ('stop', 'restart'):
ret = os.system("%s -f %s -k %s" % (option.httpd, CONFIG_FILE, option.command))
if ret == 0:
print "%s apache2 ..." % option.command
sys.exit()
CONFIG = """
LoadModule authz_host_module /usr/lib/${httpd}/modules/mod_authz_host.so
LoadModule dir_module /usr/lib/${httpd}/modules/mod_dir.so
LoadModule status_module /usr/lib/${httpd}/modules/mod_status.so
LoadModule wsgi_module /usr/lib/${httpd}/modules/mod_wsgi.so
KeepAlive Off
Listen $address:$port
LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\" \\"%{Host}i\\"" combined
CustomLog $server_root/var/log/access_log combined
ErrorLog ${server_root}/var/log/error_log
ServerName localhost
PidFile ${server_root}/var/run/apache2.pid
DocumentRoot $doc_root
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory ${doc_root}>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
"""
if 0:
print "Option are: ", option
print "Args are: ", args
print APP_ROOT
print CONFIG_FILE
template = Template(CONFIG)
template_vars = {
'doc_root': DOC_ROOT,
'server_root': SERVER_ROOT,
'address': option.address,
'port': option.port,
'httpd': option.httpd,
}
f = open(CONFIG_FILE, "w")
try:
f.write(template.substitute(template_vars))
finally:
f.flush()
f.close()
ret = os.system("%s -f %s -k start" % (option.httpd, CONFIG_FILE))
if ret == 0:
print "Running apache at %s:%s" % (option.address, option.port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment