Created
March 24, 2011 13:25
-
-
Save assertchris/885042 to your computer and use it in GitHub Desktop.
Apache2 dynamic config creator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Change username, password and database to match your db details | |
# | |
# Set up the database table: | |
# CREATE TABLE IF NOT EXISTS `vhosts` ( | |
# `id` int(11) NOT NULL AUTO_INCREMENT, | |
# `name` varchar(255) DEFAULT NULL, | |
# `aliases` text, | |
# `path` text, | |
# `custom` text, | |
# `status` varchar(32) DEFAULT NULL, | |
# `created` datetime DEFAULT NULL, | |
# `modified` datetime DEFAULT NULL, | |
# PRIMARY KEY (`id`) | |
# ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ; | |
# | |
# Change all occurrences of /mnt/share/log/ to match your log directory | |
# | |
# Change /etc/apache2/sites-enabled/000-default to match your Apache2 config path | |
$db = new mysqli('localhost', 'username', 'password', 'database'); | |
$results = $db->query('SELECT * FROM `vhosts` WHERE `status` = "live"'); | |
echo "\nBuilding virtual host information..."; | |
$vhosts = ''; | |
while ($row = $results->fetch_assoc()) | |
{ | |
$vhosts .= "\n" . '<VirtualHost *:80>'; | |
$vhosts .= "\n" . 'ServerName ' . $row['name']; | |
if (!empty($row['aliases'])) { | |
$vhosts .= "\n" . 'ServerAlias ' . preg_replace('/(\n|\s)+/', ' ', $row['aliases']); | |
} | |
$vhosts .= "\n" . 'DocumentRoot ' . $row['path']; | |
$vhosts .= "\n" . 'ErrorLog /mnt/share/log/' . $row['name'] . '.error.log'; | |
$vhosts .= "\n" . 'CustomLog /mnt/share/log/' . $row['name'] . '.access.log combined'; | |
if (!empty($row['custom'])) { | |
$vhosts .= "\n" . preg_replace('/\r\n/', PHP_EOL, $row['custom']); | |
} | |
$vhosts .= "\n" . '</VirtualHost>'; | |
} | |
echo "\nWriting virtual host information to Apache2 configuration files..."; | |
file_put_contents('/etc/apache2/sites-enabled/000-default', $vhosts); | |
echo "\nRestarting Apache2..."; | |
@exec('/etc/init.d/apache2 restart', $output); | |
echo "\nDone!\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment