Skip to content

Instantly share code, notes, and snippets.

@jae-jae
Last active June 16, 2017 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jae-jae/52b04cdc5bba3748b4442e6241765f3a to your computer and use it in GitHub Desktop.
Save jae-jae/52b04cdc5bba3748b4442e6241765f3a to your computer and use it in GitHub Desktop.
apache的host管理
#!/bin/bash
#
# apache虚拟host管理
#
# @Author: Jaeger <JaegerCode@gmail.com>
# @Version: 0.3
function Option()
{
echo "选择操作:"
select selected in '列出Host' '添加Host' '删除Host' '退出';do
case $selected in
'列出Host')
ListHost
break
;;
'添加Host')
AddHost
break
;;
'删除Host')
DelHost
break
;;
'退出')exit;;
esac
done;
Option;
}
function ListHost()
{
path='/etc/apache2/sites-available/'
cd $path
echo -e "\n"
for filename in `ls`
do
if [[ $filename != *default* ]]; then
echo $(basename $filename .conf)
fi
done
echo -e "\n"
}
function AddHost()
{
echo '输入:域名 文件夹路径'
read host dir
hostTpl="
<VirtualHost *:80> \n
ServerName $host \n
ServerAdmin webmaster@localhost \n
DocumentRoot $dir \n
ErrorLog \${APACHE_LOG_DIR}/$host-error.log \n
CustomLog \${APACHE_LOG_DIR}/$host-access.log combined \n
</VirtualHost>
"
echo -e $hostTpl>"/etc/apache2/sites-available/$host.conf"
echo "127.0.0.1 $host">>/etc/hosts
a2ensite $host > /dev/null
service apache2 reload
echo -e "\n $host 添加完成!\n"
}
function DelHost()
{
echo '输入:域名'
read host
a2dissite $host > /dev/null
rm "/etc/apache2/sites-available/$host.conf"
sed -i "s/127.0.0.1 $host//" /etc/hosts
service apache2 reload
echo -e "\n $host 已删除!\n"
}
Option
#!/bin/bash
#
# apache虚拟host管理,windows版本
#
# @Author: Jaeger <JaegerCode@gmail.com>
# @Version: 0.3
function Option()
{
echo "选择操作:"
select selected in '列出Host' '添加Host' '删除Host' '退出';do
case $selected in
'列出Host')
ListHost
break
;;
'添加Host')
AddHost
break
;;
'删除Host')
DelHost
break
;;
'退出')exit;;
esac
done;
Option;
}
function ListHost()
{
path='/etc/apache2/sites-available/'
cd $path
echo -e "\n"
for filename in `ls`
do
if [[ $filename != *default* ]]; then
echo $(basename $filename .conf)
fi
done
echo -e "\n"
}
function AddHost()
{
echo '输入:域名 文件夹路径'
read host dir
hostTpl="
<VirtualHost *:80> \n
ServerName $host \n
ServerAdmin webmaster@localhost \n
DocumentRoot $dir \n
ErrorLog \${APACHE_LOG_DIR}/$host-error.log \n
CustomLog \${APACHE_LOG_DIR}/$host-access.log combined \n
</VirtualHost>
"
echo -e $hostTpl>"/etc/apache2/sites-available/$host.conf"
echo "127.0.0.1 $host">>/mnt/c/Windows/System32/drivers/etc/hosts
a2ensite $host > /dev/null
service apache2 reload
echo -e "\n $host 添加完成!\n"
}
function DelHost()
{
echo '输入:域名'
read host
a2dissite $host > /dev/null
rm "/etc/apache2/sites-available/$host.conf"
sed -i "s/127.0.0.1 $host//" /mnt/c/Windows/System32/drivers/etc/hosts
service apache2 reload
echo -e "\n $host 已删除!\n"
}
Option
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment