Генерим конфиги для апача автоматом.
Добавлено: 16 фев 2012, 16:58
Что получилось: самомодифицирующийся скрипт, который генерит конфиг виртуалхоста для апача по шаблону.
Код: Выделить всё
<VirtualHost *:81>
ServerAdmin webmaster@${DOMAIN}
ServerName $PROJECT.${DOMAIN}
DocumentRoot /var/www/vhosts/$PROJECT
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/vhosts/$PROJECT >
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/$PROJECT.${DOMAIN}-error.log
LogLevel warn
CustomLog /var/log/apache2/$PROJECT.${DOMAIN}-access.log combined
</VirtualHost>
Код: Выделить всё
#!/usr/bin/env python
# Author: Gen1us2k
# Adding apache default config to new virtual host
# This script self-modifying and storing all your projects located at vdir
#
from socket import gethostname
import re, os, inspect, sys, io
def main():
domain = "kg"
conf = "/path/to/vhost/template/vhost.conf"
apache_path = "/etc/apache2/sites-available/"
vdir = "/var/www/vhosts/"
projects = '[www1, www2]'
projects = projects[1:-1].split(', ')
projects.sort()
cprojects = []
nprojects = []
for project in os.listdir(vdir):
path = vdir + "/" + project
if os.path.isdir(path):
cprojects.append(project)
cprojects.sort()
srcfile = inspect.getsourcefile(sys.modules[__name__])
src = open(srcfile, 'r').read()
match = re.search ("projects = '(.*)", src)
for cproject in cprojects:
if not cproject in projects:
print "Adding apache configuration for " + cproject + " virtual host"
config = open(apache_path + cproject + domain, 'w')
for line in io.open(conf, 'r'):
line = line.replace('$PROJECT', cproject)
line = line.replace('${DOMAIN}', cproject + "." + domain)
config.write(line)
config.close()
nprojects.append(cproject)
for project in projects:
if not project in cprojects:
print "Removing apache configuration for " + project + " virtual host"
os.popen("rm /etc/apache2/sites-enabled/" + project + "." + domain)
os.popen("rm /etc/apache2/sites-available/" + project + "." + domain)
os.popen("/etc/init.d/apache2 reload").readline()
projects.remove(project)
projects.sort()
if match:
src = src[:match.start(1)] + "[" + ", ".join(projects) + ", " + ", ".join(nprojects) + "]'" + src[match.end(1):]
f = open(srcfile, 'w')
f.write(src)
f.close()
print os.popen('/etc/init.d/apache2 stop').readline()
os.popen("sudo chown -R www-data:www-data /var/www/vhosts")
if __name__=='__main__':
main()
conf = "/path/to/vhost/template/vhost.conf"
apache_path = "/etc/apache2/sites-available/"
vdir = "/var/www/
Эти переменные редактируем на свой лад, запускаем, радуемся.
ЗЫ подогнано для Debian
На гитхабе: https://github.com/gen1us2k/bash-script ... nerator.py