#!/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 .
Source: https://habr.com/ru/post/84815/
All Articles