📜 ⬆️ ⬇️

Script for working with virtual hosts apache2 on locale

0. Intro


What is it all for? Goals were 2:
  1. get rid of the routine of picking in the console and files
  2. get exp in shell script development.

1. What is it?


Below will be presented the script for working with virtual hosts of Apache under the cubunta for the local machine only . For the server hardly fit. Not for these purposes was written.

So what is it?

The script allows you to create, delete, activate and deactivate hosts on the machine, while saving our time. That is, you do not need to prescribe something, poking around in configs, throwing symlinks and other restarting of the server just for this purpose. Only one team in the console can reduce the time at times. Go! )

2. Short help


The script takes the trail. arguments:
vhost (add | del | enable | disable) vhostname

Accordingly, by command (aliases in brackets):
- add (create) - create a host, create a basic file structure and activate it *.
- del (delete, rm) - delete only the host with configs.
- delall - delete host and configs, and delete all files for this site **
- enable (on) —add a host to apache. (just throw links to sites-enabled).
- disable (off) - disable host (configs remain)
')
Example:
sudo vhost add test.lan
sudo vhost rm test.lan

Root is needed in order to be recorded in / etc / hosts and restarting Apache.

* The folder structure is what I use. There are variables at the top of the script that determine where your sites are stored.

** Only if the overall structure is maintained.
For example: the host is called test.lan, and the files are in /home/user/www/test.lan. In this case this folder with contents will be deleted.

3. The source of the world.


#!/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. Outro


I will take note of any advice, as in such shell scripts I am still a novice.

p \ s: used colors for C # to highlight, for there was no shell.

Download

Source: https://habr.com/ru/post/84815/


All Articles