Created
April 20, 2012 11:16
-
-
Save JulienBreux/2427844 to your computer and use it in GitHub Desktop.
Add website (sudo ./addSite.php <user> <host> <alias>)
This file contains 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
#!/usr/bin/php | |
<?php | |
define('SUCCESS', "\033[32m".'OK'."\033[37m - "); | |
define('WARN', "\033[31m".'INFO'."\033[37m - "); | |
define('EOL', "\n"); | |
## FUNCTIONS | |
function pecho($string) {fwrite(STDOUT, $string);} | |
## LOAD | |
if ($argc < 3) | |
die(pecho(WARN.'Le nom du site et de l\'utilisateur ne sont pas définis. ('.$argc.')'.EOL)); | |
$user = $argv[1]; | |
$site = $argv[2]; | |
if (!is_dir('/home/'.$user)) | |
die(pecho(WARN.'L\'utilisateur '.$user.' n\'existe pas.'.EOL)); | |
if (!preg_match('#^[^\.]+[a-z0-9\.]+[^\.]$#', $site)) | |
die(pecho(WARN.'Le nom du site '.$site.' n\'est pas conforme.'.EOL)); | |
## CONSTS | |
define('USER', $user); | |
define('SITE', $site); | |
define('NUM_ALIAS', (isset($argv[3]) && is_numeric($argv[3]) ? (int) $argv[3] : 6)); | |
define('TLD', '.local'); | |
define('ACTION_DIRS', true); | |
define('ACTION_FILES', true); | |
define('ACTION_HOST', true); | |
define('ACTION_VHOST', true); | |
define('TAB', "\t"); | |
define('DS', DIRECTORY_SEPARATOR); | |
define('DIR_ETC', DS.'etc'); | |
define('DIR_HOME', DS.'home'); | |
define('DIR_APACHE', DIR_ETC.DS.'apache2'); | |
define('DIR_SITES', DIR_HOME.DS.USER.DS.'Sites'); | |
define('DIR_SITE', DIR_SITES.DS.SITE.TLD); | |
define('DIR_SITE_WWW', DIR_SITE.DS.'www'); | |
define('DIR_SITE_LOG', DIR_SITE.DS.'log'); | |
define('DIR_SITE_CONFIG', DIR_SITE.DS.'config'); | |
define('DIR_SITES_AVAILABLE', DIR_SITE_CONFIG); // Use /home/<USER>/config/<SITE><TLD> | |
//define('DIR_SITES_AVAILABLE', DIR_APACHE.DS.'sites-available'); // Use /<DIR_APACHE>/site-available | |
define('DIR_SITES_ENABLED', DIR_APACHE.DS.'sites-enabled'); | |
define('FILE_HOSTS', 'hosts'); | |
define('FILE_VHOST', SITE.TLD); | |
define('FILE_PHP_INI', 'php.ini'); | |
define('FILE_PHP_ERROR', 'error.php.log'); | |
## ACTION DIRS | |
if (ACTION_DIRS) | |
{ | |
if (!is_dir(DIR_SITE)) | |
{ | |
mkdir(DIR_SITE); pecho(SUCCESS."Création du dossier : ".DIR_SITE.EOL); | |
mkdir(DIR_SITE_WWW); pecho(SUCCESS."Création du dossier : ".DIR_SITE_WWW.EOL); | |
mkdir(DIR_SITE_LOG); pecho(SUCCESS."Création du dossier : ".DIR_SITE_LOG.EOL); | |
mkdir(DIR_SITE_CONFIG); pecho(SUCCESS."Création du dossier : ".DIR_SITE_CONFIG.EOL); | |
exec('chown -R '.USER.':'.USER.' '.DIR_SITE); | |
} | |
else | |
die(pecho(WARN."Le dossier ".DIR_SITE." existe déjà !".EOL)); | |
} | |
## ACTION FILES | |
if (ACTION_FILES) | |
{ | |
//@todo Tout doux ! | |
} | |
## ACTION HOST | |
if (ACTION_HOST) | |
{ | |
$lines = file(DIR_ETC.DS.FILE_HOSTS); | |
foreach ($lines as $line) | |
if (preg_match('`'.SITE.TLD.'$`', $line)) | |
die(pecho(WARN."Le domaine ".SITE.TLD." existe déjà dans le fichier ".DIR_ETC.DS.FILE_HOSTS." !".EOL)); | |
$content = '127.0.0.1'.TAB.TAB.SITE.TLD.EOL; | |
if (NUM_ALIAS > 0) | |
{ | |
for($i = 1, $iMax = NUM_ALIAS; $i <= $iMax; $i++) | |
{ | |
$content .= '127.0.0.1'.TAB.TAB.'alt'.$i.'.'.SITE.TLD.EOL; | |
} | |
} | |
file_put_contents(DIR_ETC.DS.FILE_HOSTS, $content, FILE_APPEND); | |
pecho(SUCCESS."Ajout du domaine ".SITE.TLD." au fichier ".DIR_ETC.DS.FILE_HOSTS.EOL); | |
} | |
## ACTION VHOST | |
if (ACTION_VHOST) | |
{ | |
if (!file_exists(DIR_SITES_AVAILABLE.DS.SITE.TLD)) | |
{ | |
$content = '<VirtualHost *:80>'.EOL; | |
$content .= TAB.'DocumentRoot '.DIR_SITE_WWW.EOL; | |
$content .= TAB.'ServerName '.SITE.TLD.EOL.EOL; | |
if (NUM_ALIAS > 0) | |
{ | |
for($i = 1, $iMax = NUM_ALIAS; $i <= $iMax; $i++) | |
{ | |
$content .= TAB.'ServerAlias alt'.$i.'.'.SITE.TLD.EOL; | |
} | |
$content .= EOL; | |
} | |
$content .= TAB.'<Directory '.DIR_SITE_WWW.'>'.EOL; | |
$content .= TAB.TAB.'Options -Indexes FollowSymLinks MultiViews'.EOL; | |
$content .= TAB.TAB.'AllowOverride All'.EOL; | |
$content .= TAB.'</Directory>'.EOL.EOL; | |
$content .= TAB.'ErrorLog '.DIR_SITE_LOG.DS.'error.apache.log'.EOL; | |
$content .= TAB.'LogLevel warn'.EOL; | |
$content .= TAB.'CustomLog '.DIR_SITE_LOG.DS.'access.apache.log combined'.EOL.EOL; | |
// $content .= TAB.'PHPINIDir '.DIR_SITE_CONFIG.DS.'php.ini'.EOL.EOL; | |
$content .= TAB.'php_flag log_errors on'.EOL; | |
$content .= TAB.'php_flag display_errors on'.EOL; | |
$content .= TAB.'php_value error_reporting 30719'.EOL; | |
$content .= TAB.'php_value error_log '.DIR_SITE_LOG.DS.'error.php.log'.EOL; | |
$content .= '</VirtualHost>'; | |
file_put_contents(DIR_SITES_AVAILABLE.DS.SITE.TLD, $content); | |
symlink(DIR_SITES_AVAILABLE.DS.SITE.TLD, DIR_SITES_ENABLED.DS.SITE.TLD); | |
exec('/etc/init.d/apache2 restart'); | |
pecho(SUCCESS.'L\'hôte virtuel '.SITE.TLD.' a été créé dans le dossier '.DIR_SITES_AVAILABLE.'.'.EOL); | |
} | |
else | |
die(pecho(WARN.'L\'hôte virtuel '.SITE.TLD.' existe déjà dans le dossier '.DIR_SITES_AVAILABLE.' !'.EOL)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment