Skip to content

Instantly share code, notes, and snippets.

@kanreisa
Created September 12, 2010 20:31
Show Gist options
  • Save kanreisa/576420 to your computer and use it in GitHub Desktop.
Save kanreisa/576420 to your computer and use it in GitHub Desktop.
#!/usr/bin/php -q
<?php
/* a2addsite - てwきwとwうw自分用apache2サイト追加スクリプト */
/* 最初は10行ぐらいだったんだけどねwwwwwwww */
ob_end_clean();
//放り込んでいくディレクトリ(最後にスラッシュ付ける)
$a2confdir = '/etc/apache2/sites-available/';
//DocumentRootが存在しない場合は作成を試みるか
$automkdir = true;
//automkdirのパーミッション
$automkdir_mode = 0701;
/* お好みにあわせて編集 */
$a2conf = <<<_EOF_
<VirtualHost *:80>
ServerName [[DOMAIN]]
[[UID]]
DocumentRoot "[[DIR]]"
<Directory [[DIR]]>
Options All
AllowOverride All
</Directory>
</VirtualHost>
_EOF_;
$domains = '(af|al|dz|as|ad|ao|ai|aq|ag|ar|am|aw|ac|au|at|az|bh|bd|bb|by|bj|bm|bt|bo|ba|bw|br|io|bn|bg|bf|bi|kh|cm|ca|cv|cf|td|gg|je|cl|cn|cx|cc|co|km|cg|cd|ck|cr|ci|hr|cu|cy|cz|dk|dj|dm|do|tp|ec|eg|sv|gq|er|ee|et|fk|fo|fj|fi|fr|gf|pf|tf|fx|ga|gm|ge|de|gh|gi|gd|gp|gu|gt|gn|gw|gy|ht|hm|hn|hk|hu|is|in|id|ir|iq|ie|im|il|it|jm|jo|kz|ke|ki|kp|kr|kw|kg|la|lv|lb|ls|lr|ly|li|lt|lu|mo|mk|mg|mw|my|mv|ml|mt|mh|mq|mr|mu|yt|mx|fm|md|mc|mn|ms|ma|mz|mm|na|nr|np|nl|an|nc|nz|ni|ne|ng|nu|nf|mp|no|om|pk|pw|pa|pg|py|pe|ph|pn|pl|pt|pr|qa|re|ro|ru|rw|kn|lc|vc|ws|sm|st|sa|sn|sc|sl|sg|sk|si|sb|so|za|gs|es|lk|sh|pm|sd|sr|sj|sz|se|ch|sy|tw|tj|tz|th|bs|ky|tg|tk|to|tt|tn|tr|tm|tc|tv|ug|ua|ae|uk|us|um|uy|uz|vu|va|ve|vn|vg|vi|wf|eh|ye|yu|zm|zw|com|net|org|gov|edu|int|mil|biz|info|name|pro|jp|asia|local)';
if (
($argv[1]=='--help')||
(!preg_match('/^[0-9A-Za-z][0-9a-zA-Z_\.-]+\.'.$domains.'$/i', $argv[1]))||
(!$argv[2])
){
echo 'Usage:'."\n";
echo ' a2addsite (str)Domain (str)DocumentRoot'."\n";
echo ' a2addsite (str)Domain (str)DocumentRoot (str)AssignUserID[for MPM-ITK]'."\n";
echo 'Example:'."\n";
echo ' a2addsite example.com /home/user/www/'."\n";
echo ' a2addsite ex.example.com /home/user/www/ex/ user'."\n";
} else {
//replace
$a2conf = str_replace('[[DOMAIN]]', $argv[1] , $a2conf);
$a2conf = str_replace('[[DIR]]', $argv[2] , $a2conf);
if($argv[3])
$a2conf = str_replace('[[UID]]', "AssignUserID $argv[3] $argv[3]", $a2conf);
else
$a2conf = str_replace('[[UID]]', "", $a2conf);
//put a2conf
if(file_exists($a2confdir)){
if( (!file_exists($argv[2]))&&($automkdir==true) ){
//DocumentRootがない場合は作成
echo 'making DocumentRoot('.$argv[2].')'."\n";
mkdir ($argv[2], $automkdir_mode, true);
//オーナー,グループ変更
if($argv[3]){
chown ($argv[2], $argv[3]);
chgrp ($argv[2], $argv[3]);
}
}else if(!file_exists($argv[2])){
exit('Error: DocumentRoot('.$argv[2].') is not exsists'."\n");
}
//put
file_put_contents($a2confdir.$argv[1], $a2conf);
echo 'done.'."\n";
}else{
exit('Error: a2confdir('.$a2confdir.') is not exsists'."\n");
}
}
@kanreisa
Copy link
Author

//Debian/Ubuntuでの使い方。
//PHP5とか必須

sudo -i
vim /usr/sbin/a2addsite
chmod 700 a2addsite
a2addsite --help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment