ロケールで仮想ホストapache2を操作するためのスクリプト

0.イントロ



これは何のためですか? 目標は2

  1. コンソールとファイルで選択するルーチンを取り除きます
  2. シェルスクリプト開発の専門家を取得します。


1.なに?



ローカルマシンのみの 立方メートルの下でApache仮想ホストを操作するためのスクリプトを以下に提示します。 サーバーが行う可能性は低いです。 これらの目的のために書かれたものではありません。



それで何ですか?


このスクリプトにより、マシン上のホストを作成、削除、アクティブ化、非アクティブ化しながら、時間を節約できます。 つまり、何かを規定したり、設定を調べたり、シンボリックリンクや他のサーバーを再起動したりする必要はありません。 コンソールの1つのコマンドだけで時間を短縮できます。 行こう! )



2.短いヘルプ



スクリプトはトレースを取得します。 引数:

vhost(追加| del |有効|無効)vhostname



したがって、コマンド(括弧内のエイリアス)によって:

-追加(作成)-ホストを作成し、基本的なファイル構造を作成してアクティブにします*。

-del(delete、rm)-構成のあるホストのみを削除します。

-delall-ホストと構成を削除し、このサイトのすべてのファイルを削除します**

-有効(オン)-ホストをApacheに追加します。 (サイト対応のリンクは単にスローされます)。

-無効(オフ)-ホストを無効にします(構成は残ります)



例:

sudo vhost add test.lan

sudo vhost rm test.lan



/ etc / hostsに書き込み、Apacheを再起動するには、ルートが必要です。



*フォルダ構造は私が使用しているものです。 スクリプトの上部には、サイトの保存場所を決定する変数があります。



**一般的な構造が維持されている場合のみ。

たとえば、ホストはtest.lanと呼ばれ、ファイルは/home/user/www/test.lanにあります。 この場合、このコンテンツフォルダーは削除されます。



3.世界の源。



#!/bin/bash

projects= "/home/alex/web/www" ;

hosts= "/home/alex/web/hosts" ;

enabled_hosts= '/etc/apache2/sites-enabled' ;

user=$(whoami);

action=$1;

name=$2

db=$3;



add_folders() {

mkdir -p "$projects/$name/www" ;

mkdir -p "$projects/$name/logs"

mkdir -p "$projects/$name/tmp"

chown -R $user\: "$projects/$name/"

chmod -R 777 "$projects/$name" ;

}



add_host() {

echo "

<VirtualHost $name:80>

ServerAdmin webmaster@localhost

ServerName $name

ServerAlias www.$name

DocumentRoot $projects/$name/www

ErrorLog $projects/$name/logs/error.log

CustomLog $projects/$name/logs/access.log combined

DirectoryIndex index.php index.htm index.html

LogLevel debug

</VirtualHost>"
> $hosts/$name



ln -s $hosts/$name $enabled_hosts/$name



echo "127.0.0.1 $name" >> /etc/hosts

}



add_index_file() {

echo "<html>

<body>

<h1>$name works!</h1>

</body>

</html>"
> $projects/$name/www/index.htm

}



create_host() {

if [ -z $name ]; then

echo 'No name for vhost given.' ;

exit 1;

fi



if [ -f "$hosts/$name" ]; then

echo 'Virtual host already exists.' ;

exit 0;

fi



echo 'All is ok, fast forward -->' ;

add_folders;

echo '* Folders created' ;

add_host;

echo '* Host created' ;

add_index_file;



reload_apache;

}



delete_host() {

if [ -z $name ]; then

echo 'No name for vhost given. Exit.' ;

exit 1;

fi



if [ -f "$hosts/$name" ]; then

echo "Remove vhost $name? (y/n)" ;

read confirm;



if [ $confirm = 'y' ]; then

echo "* Removing vhost file from $hosts/$name" ;

rm $hosts/$name



echo "* Removing link from $enabled_hosts/$name"

rm $enabled_hosts/$name



echo "* Cleaning up /etc/hosts"

sed "/$name/d" /etc/hosts > /tmp/tmp-hosts;

mv -f /tmp/tmp-hosts /etc/hosts



echo "* Vhost $name removed" ;



reload_apache;

else

echo "Skipped." ;

fi

else

echo 'No such vhost.' ;

exit;

fi

}



delete_all() {

if [ -z $name ]; then

echo 'No name for vhost given. Exit.' ;

exit 1;

fi



if [ -f "$hosts/$name" ]; then

echo "Remove vhost $name?" ;

echo "This action will remove all files and directories from this project (y/n)" ;

read confirm;



if [ $confirm = 'y' ]; then

echo "* Removing files and directories from $projects/$name" ;

rm -rf $projects/$name



echo "* Removing vhost file from $hosts/$name" ;

rm $hosts/$name



echo "* Removing link from $enabled_hosts/$name"

rm $enabled_hosts/$name



echo "* Cleaning up /etc/hosts"

sed "/$name/d" /etc/hosts > /tmp/tmp-hosts;

mv -f /tmp/tmp-hosts /etc/hosts



echo "* Done. The matrix has project $name" ;



reload_apache;

else

echo "Skipped." ;

fi

else

echo 'No such vhost.' ;

exit;

fi

}



enable_host() {

if [ -f "$hosts/$name" ]; then

ln -s $hosts/$name $enabled_hosts/$name

echo "127.0.0.1 $name" >> /etc/hosts



reload_apache;

else

echo 'No such vhost.' ;

fi

}



disable_host() {

if [ -f "$enabled_hosts/$name" ]; then

echo '* Removing symlink for current host...' ;

rm /etc/apache2/sites-enabled/$name

echo '* Updating /etc/hosts' ;

sed "/$name/d" /etc/hosts > /tmp/tmp-hosts;

mv -f /tmp/tmp-hosts /etc/hosts



reload_apache;

echo '* Done'

else

echo 'No such vhost.' ;

fi

}



show_help() {

echo 'Available actions:

create - creates a new project. Includes folder structure and database (optional).

delete - delete all project files and drops database (optional). Asks for confirmation.

enable - enables selected virtual host.

disable - disables selected virtual host

help - shows this message.'
;

}



reload_apache() {

service apache2 reload

}



if [ $(whoami) != "root" ]; then

echo 'You must be root.'

exit 1;

fi



case $action in

create)

create_host;

;;

add)

create_host;

;;

rm)

delete_host;

;;

del)

delete_host;

;;

delete)

delete_host;

;;

delall)

delete_all;

;;

on)

enable_host;

;;

enable)

enable_host;

;;

off)

disable_host;

;;

disable)

disable_host;

;;

help)

show_help;

;;

*)

echo "Usage: `basename $0` (create|delete|enable|disable) vhostname" ;

exit 0;

;;

esac





* This source code was highlighted with Source Code Highlighter .









#!/bin/bash

projects= "/home/alex/web/www" ;

hosts= "/home/alex/web/hosts" ;

enabled_hosts= '/etc/apache2/sites-enabled' ;

user=$(whoami);

action=$1;

name=$2

db=$3;



add_folders() {

mkdir -p "$projects/$name/www" ;

mkdir -p "$projects/$name/logs"

mkdir -p "$projects/$name/tmp"

chown -R $user\: "$projects/$name/"

chmod -R 777 "$projects/$name" ;

}



add_host() {

echo "

<VirtualHost $name:80>

ServerAdmin webmaster@localhost

ServerName $name

ServerAlias www.$name

DocumentRoot $projects/$name/www

ErrorLog $projects/$name/logs/error.log

CustomLog $projects/$name/logs/access.log combined

DirectoryIndex index.php index.htm index.html

LogLevel debug

</VirtualHost>"
> $hosts/$name



ln -s $hosts/$name $enabled_hosts/$name



echo "127.0.0.1 $name" >> /etc/hosts

}



add_index_file() {

echo "<html>

<body>

<h1>$name works!</h1>

</body>

</html>"
> $projects/$name/www/index.htm

}



create_host() {

if [ -z $name ]; then

echo 'No name for vhost given.' ;

exit 1;

fi



if [ -f "$hosts/$name" ]; then

echo 'Virtual host already exists.' ;

exit 0;

fi



echo 'All is ok, fast forward -->' ;

add_folders;

echo '* Folders created' ;

add_host;

echo '* Host created' ;

add_index_file;



reload_apache;

}



delete_host() {

if [ -z $name ]; then

echo 'No name for vhost given. Exit.' ;

exit 1;

fi



if [ -f "$hosts/$name" ]; then

echo "Remove vhost $name? (y/n)" ;

read confirm;



if [ $confirm = 'y' ]; then

echo "* Removing vhost file from $hosts/$name" ;

rm $hosts/$name



echo "* Removing link from $enabled_hosts/$name"

rm $enabled_hosts/$name



echo "* Cleaning up /etc/hosts"

sed "/$name/d" /etc/hosts > /tmp/tmp-hosts;

mv -f /tmp/tmp-hosts /etc/hosts



echo "* Vhost $name removed" ;



reload_apache;

else

echo "Skipped." ;

fi

else

echo 'No such vhost.' ;

exit;

fi

}



delete_all() {

if [ -z $name ]; then

echo 'No name for vhost given. Exit.' ;

exit 1;

fi



if [ -f "$hosts/$name" ]; then

echo "Remove vhost $name?" ;

echo "This action will remove all files and directories from this project (y/n)" ;

read confirm;



if [ $confirm = 'y' ]; then

echo "* Removing files and directories from $projects/$name" ;

rm -rf $projects/$name



echo "* Removing vhost file from $hosts/$name" ;

rm $hosts/$name



echo "* Removing link from $enabled_hosts/$name"

rm $enabled_hosts/$name



echo "* Cleaning up /etc/hosts"

sed "/$name/d" /etc/hosts > /tmp/tmp-hosts;

mv -f /tmp/tmp-hosts /etc/hosts



echo "* Done. The matrix has project $name" ;



reload_apache;

else

echo "Skipped." ;

fi

else

echo 'No such vhost.' ;

exit;

fi

}



enable_host() {

if [ -f "$hosts/$name" ]; then

ln -s $hosts/$name $enabled_hosts/$name

echo "127.0.0.1 $name" >> /etc/hosts



reload_apache;

else

echo 'No such vhost.' ;

fi

}



disable_host() {

if [ -f "$enabled_hosts/$name" ]; then

echo '* Removing symlink for current host...' ;

rm /etc/apache2/sites-enabled/$name

echo '* Updating /etc/hosts' ;

sed "/$name/d" /etc/hosts > /tmp/tmp-hosts;

mv -f /tmp/tmp-hosts /etc/hosts



reload_apache;

echo '* Done'

else

echo 'No such vhost.' ;

fi

}



show_help() {

echo 'Available actions:

create - creates a new project. Includes folder structure and database (optional).

delete - delete all project files and drops database (optional). Asks for confirmation.

enable - enables selected virtual host.

disable - disables selected virtual host

help - shows this message.'
;

}



reload_apache() {

service apache2 reload

}



if [ $(whoami) != "root" ]; then

echo 'You must be root.'

exit 1;

fi



case $action in

create)

create_host;

;;

add)

create_host;

;;

rm)

delete_host;

;;

del)

delete_host;

;;

delete)

delete_host;

;;

delall)

delete_all;

;;

on)

enable_host;

;;

enable)

enable_host;

;;

off)

disable_host;

;;

disable)

disable_host;

;;

help)

show_help;

;;

*)

echo "Usage: `basename $0` (create|delete|enable|disable) vhostname" ;

exit 0;

;;

esac





* This source code was highlighted with Source Code Highlighter .









#!/bin/bash

projects= "/home/alex/web/www" ;

hosts= "/home/alex/web/hosts" ;

enabled_hosts= '/etc/apache2/sites-enabled' ;

user=$(whoami);

action=$1;

name=$2

db=$3;



add_folders() {

mkdir -p "$projects/$name/www" ;

mkdir -p "$projects/$name/logs"

mkdir -p "$projects/$name/tmp"

chown -R $user\: "$projects/$name/"

chmod -R 777 "$projects/$name" ;

}



add_host() {

echo "

<VirtualHost $name:80>

ServerAdmin webmaster@localhost

ServerName $name

ServerAlias www.$name

DocumentRoot $projects/$name/www

ErrorLog $projects/$name/logs/error.log

CustomLog $projects/$name/logs/access.log combined

DirectoryIndex index.php index.htm index.html

LogLevel debug

</VirtualHost>"
> $hosts/$name



ln -s $hosts/$name $enabled_hosts/$name



echo "127.0.0.1 $name" >> /etc/hosts

}



add_index_file() {

echo "<html>

<body>

<h1>$name works!</h1>

</body>

</html>"
> $projects/$name/www/index.htm

}



create_host() {

if [ -z $name ]; then

echo 'No name for vhost given.' ;

exit 1;

fi



if [ -f "$hosts/$name" ]; then

echo 'Virtual host already exists.' ;

exit 0;

fi



echo 'All is ok, fast forward -->' ;

add_folders;

echo '* Folders created' ;

add_host;

echo '* Host created' ;

add_index_file;



reload_apache;

}



delete_host() {

if [ -z $name ]; then

echo 'No name for vhost given. Exit.' ;

exit 1;

fi



if [ -f "$hosts/$name" ]; then

echo "Remove vhost $name? (y/n)" ;

read confirm;



if [ $confirm = 'y' ]; then

echo "* Removing vhost file from $hosts/$name" ;

rm $hosts/$name



echo "* Removing link from $enabled_hosts/$name"

rm $enabled_hosts/$name



echo "* Cleaning up /etc/hosts"

sed "/$name/d" /etc/hosts > /tmp/tmp-hosts;

mv -f /tmp/tmp-hosts /etc/hosts



echo "* Vhost $name removed" ;



reload_apache;

else

echo "Skipped." ;

fi

else

echo 'No such vhost.' ;

exit;

fi

}



delete_all() {

if [ -z $name ]; then

echo 'No name for vhost given. Exit.' ;

exit 1;

fi



if [ -f "$hosts/$name" ]; then

echo "Remove vhost $name?" ;

echo "This action will remove all files and directories from this project (y/n)" ;

read confirm;



if [ $confirm = 'y' ]; then

echo "* Removing files and directories from $projects/$name" ;

rm -rf $projects/$name



echo "* Removing vhost file from $hosts/$name" ;

rm $hosts/$name



echo "* Removing link from $enabled_hosts/$name"

rm $enabled_hosts/$name



echo "* Cleaning up /etc/hosts"

sed "/$name/d" /etc/hosts > /tmp/tmp-hosts;

mv -f /tmp/tmp-hosts /etc/hosts



echo "* Done. The matrix has project $name" ;



reload_apache;

else

echo "Skipped." ;

fi

else

echo 'No such vhost.' ;

exit;

fi

}



enable_host() {

if [ -f "$hosts/$name" ]; then

ln -s $hosts/$name $enabled_hosts/$name

echo "127.0.0.1 $name" >> /etc/hosts



reload_apache;

else

echo 'No such vhost.' ;

fi

}



disable_host() {

if [ -f "$enabled_hosts/$name" ]; then

echo '* Removing symlink for current host...' ;

rm /etc/apache2/sites-enabled/$name

echo '* Updating /etc/hosts' ;

sed "/$name/d" /etc/hosts > /tmp/tmp-hosts;

mv -f /tmp/tmp-hosts /etc/hosts



reload_apache;

echo '* Done'

else

echo 'No such vhost.' ;

fi

}



show_help() {

echo 'Available actions:

create - creates a new project. Includes folder structure and database (optional).

delete - delete all project files and drops database (optional). Asks for confirmation.

enable - enables selected virtual host.

disable - disables selected virtual host

help - shows this message.'
;

}



reload_apache() {

service apache2 reload

}



if [ $(whoami) != "root" ]; then

echo 'You must be root.'

exit 1;

fi



case $action in

create)

create_host;

;;

add)

create_host;

;;

rm)

delete_host;

;;

del)

delete_host;

;;

delete)

delete_host;

;;

delall)

delete_all;

;;

on)

enable_host;

;;

enable)

enable_host;

;;

off)

disable_host;

;;

disable)

disable_host;

;;

help)

show_help;

;;

*)

echo "Usage: `basename $0` (create|delete|enable|disable) vhostname" ;

exit 0;

;;

esac





* This source code was highlighted with Source Code Highlighter .












4.アウトロ



このようなシェルスクリプトでは、私はまだ初心者なので、アドバイスに注意します。



p \ s:シェルがないため、強調表示にC#の色を使用していました。



ダウンロードする



All Articles