text 创建apache2虚拟主机的脚本#bash #linux #apache #wsl #host #vhost

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 创建apache2虚拟主机的脚本#bash #linux #apache #wsl #host #vhost相关的知识,希望对你有一定的参考价值。

#!/bin/bash
#

# Display usage info
vhost-usage() {
cat <<"USAGE"

Usage: vhost [OPTIONS] <name>
	-h|--help	this screen
	-pub		to create the webhost root in ~/www/name/public/
	-url 		to specify a local address, default is http://name.local
	-rm 		to remove a previously created vhost, see examples
	-d 		to specify the webroot directory location, default is in ~/www (NO TRAILING SLASH)
	-email 		to specify the email of the administrator in the virtual host file
	-l 		to list the current virtual hosts

IMPORTANT Notes:
1- You need git setup to grab the template file if you do not have it already.
2- You need to make sure you do not have a vhost called 'template' or that it is the one from this gist: https://gist.github.com/gistwebdev/5640113
3- Check your version of sed, you need to make sure -i works. GNU sed is suggested.

Examples:
vhost mysite                                                    this will create a new apache2 vhost named mysite with a webroot of ~/www/mysite/ reachable at http://mysite.local
vhost -pub mysite                                               this will create a new apache2 vhost named mysite with a webroot of ~/www/mysite/public/ reachable at http://mysite.local
vhost -d ~/sites/mysite/myroot -url dev.mysite.dev mysite       this will create a new apache2 vhost named mysite with a webroot of ~/sites/mysite/myroot reachable at http://dev.mysite.dev
vhost -rm mysite.local mysite                                   this will remove the apache2 vhost named mysite and remove the mysite.local entry from the /etc/hosts file. Be sure to specify boths

USAGE
exit 0
}

# Delete a virtual host file and it's entry in /etc/hosts
vhost-remove() {
	sudo -v
	echo "Removing $url from /etc/hosts."
	sudo sed -i '/'$url'/d' /etc/hosts

	echo "Disabling and deleting the $name virtual host."
	sudo a2dissite $name
	sudo rm /etc/apache2/sites-available/$name.conf
	sudo service apache2 reload
	exit 0
}

vhost-list() {
	echo "Available virtual hosts:"
	ls -l /etc/apache2/sites-available/
	echo "Enabled virtual hosts:"
	ls -l /etc/apache2/sites-enabled/
	exit 0
}

# Define and create default values
name="${!#}"
email="webmaster@localhost"
url="$name.local"
webroot="$HOME/www/$name"


# Check if git is installed
if ! hash git 2>/dev/null; then
    echo -e "Git is not installed! You will need it at some point anyways..."
    echo -e "Exiting, install git first."
    exit 0
fi

# Loop to read options and arguments
while [ $1 ]; do
    case "$1" in
    	'-l') vhost-list;;
		'-h'|'--help') vhost-usage;;
        '-rm') url="$2"
			   vhost-remove;;
        '-pub') webroot="$HOME/www/$name/public";;
        '-d') webroot="$2";;
        '-url') url="$2";;
		'-email') email="$2";;
    esac
    shift
done

sudo -v

# Check if the webroot exists
if [ ! -d "$webroot" ]; then
	echo "Creating $webroot directory"
	mkdir -p $webroot
fi

echo "Checking for the virtual host template file..."

if [ ! -f /etc/apache2/sites-available/template ]; then
    echo "Downloading template file with Git..."

	sudo git clone https://gist.github.com/5640113.git /etc/apache2/sites-available/temp

	sudo mv /etc/apache2/sites-available/temp/template /etc/apache2/sites-available/template

	sudo rm -rf /etc/apache2/sites-available/temp
fi

echo "Creating the new $name virtual host file that has a webroot of: $webroot"

sudo cp /etc/apache2/sites-available/template /etc/apache2/sites-available/$name.conf
sudo sed -i 's/template.email/'$email'/g' /etc/apache2/sites-available/$name.conf
sudo sed -i 's/template.url/'$url'/g' /etc/apache2/sites-available/$name.conf
sudo sed -i 's#template.webroot#'$webroot'#g' /etc/apache2/sites-available/$name.conf

# TODO: CHECK IF IS A WINDOWS SUBSYSTEM ( WSL )
#       AND FIND A WAY TO RUN WINDOWS COMMAND 'net start dnscache'
echo "Adding $url to the /etc/hosts file..."
sudo sed -i '$a127.0.0.1       '$url'' /etc/hosts

sudo a2ensite $name

sudo service apache2 reload

echo "Virtual host $name created with a webroot at $webroot reachable from http://$url"

exit 0

以上是关于text 创建apache2虚拟主机的脚本#bash #linux #apache #wsl #host #vhost的主要内容,如果未能解决你的问题,请参考以下文章

text ForEachPT_ChangeFieldSummaryFunctions.bas

text 通过Excel VBA.bas调用PowerPoint

在 ubuntu mint apache 上创建虚拟主机

启用虚拟主机后出现错误 apache2

apache2.2 和 2.4 虚拟主机配置文件

debian apache2 多端口对应多文件 虚拟端口配置