sh 以编程方式创建Apache VHost
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 以编程方式创建Apache VHost相关的知识,希望对你有一定的参考价值。
#!/bin/bash
# /usr/local/bin/createSite.sh -d domain -t com -u user -p password -r git@github.com:user/repo.git
# TODO: SCP could be done in reverse
domain=''
tld=''
username=''
password=''
repo=''
function make_vhost(){
cat <<- _EOF_
# $domain.$1
<VirtualHost *:80>
ServerAdmin digital@bitecp.com
ServerName $domain.$1
# Directory Root.
DocumentRoot /var/www/vhosts/$domain.$tld/httpdocs
# Logfiles
ErrorLog /var/www/vhosts/$domain.$tld/logs/error.log
CustomLog /var/www/vhosts/$domain.$tld/logs/access.log combined
Include "/var/www/vhosts/$domain.$tld/conf/vhost.conf"
</VirtualHost>
# template.$domain.$1
<VirtualHost *:80>
ServerAdmin digital@bitecp.com
ServerName $domain.$1
ServerAlias template.$domain.$1
# Directory Root.
DocumentRoot /var/www/vhosts/$domain.$tld/template
# Logfiles
ErrorLog /var/www/vhosts/$domain.$tld/logs/error.log
CustomLog /var/www/vhosts/$domain.$tld/logs/access.log combined
</VirtualHost>
_EOF_
}
while getopts "d:t:u:p:r:" opt; do
case $opt in
d)
domain=$OPTARG
;;
t)
tld=$OPTARG
;;
u)
username=$OPTARG
;;
p)
password=$OPTARG
;;
r)
repo=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [ -n "$domain" -a -n "$tld" -a -n "$username" -a -n "$password" -a -n "$repo" ]; then
echo -e "\n======== Initialising Mercury ========"
ssh root@mercury.bitecp.com <<- _EOF_
# Create subscription
/usr/local/psa/bin/subscription --create $domain.$tld -owner admin -service-plan 'Default Domain' -ip 184.106.119.47 -login $username -passwd '$password'
# Create vhost.conf (not included in VC but worth it for consistency)
touch /var/www/vhosts/$domain.$tld/conf/vhost.conf
# Add template folder and holding file
mkdir /var/www/vhosts/$domain.$tld/template/
touch /var/www/vhosts/$domain.$tld/template/index.html
_EOF_
echo "Created website files"
# Site is now initialised on Live, send over gitignore
scp -r /usr/resources/templates/git/.gitignore_base root@mercury.bitecp.com:/var/www/vhosts/$domain.$tld/.gitignore
echo "Base gitignore transferred"
# SSH back in and initialise git
ssh -t root@mercury.bitecp.com <<- _EOF_
git init /var/www/vhosts/$domain.$tld/
cd /var/www/vhosts/$domain.$tld/
git add .
git commit -am "Initial import"
git remote add -t master -m master origin $repo
git push origin master
_EOF_
echo "Git initialised.. all done!"
echo -e "\n======== Initialising Mercury-Stage ========"
make_vhost bitestage.com > /tmp/$domain.$tld
scp /tmp/$domain.$tld root@mercury-stage.bitecp.com:/etc/apache2/sites-available/
rm -f /tmp/$domain.$tld
ssh root@mercury-stage.bitecp.com <<- _EOF_
# Clone github repository
cd /var/www/vhosts/
su bite -c "git clone $repo $domain.$tld"
cd $domain.$tld
su bite -c "git checkout -b staging; git branch -D master; git push origin staging; git branch --set-upstream staging origin/staging;"
echo "Cloned GitHub repository"
# Make logs
mkdir -p /var/www/vhosts/$domain.$tld/logs/
touch /var/www/vhosts/$domain.$tld/logs/access.log
touch /var/www/vhosts/$domain.$tld/logs/error.log
echo "Created logs"
# Make vhost.conf
if [ ! -d "/var/www/vhosts/$domain.$tld/conf/" ]; then
mkdir /var/www/vhosts/$domain.$tld/conf/
touch /var/www/vhosts/$domain.$tld/conf/vhost.conf
echo "Created vhost.conf"
fi
# Make template directory
if [ ! -d "/var/www/vhosts/$domain.$tld/template/" ]; then
mkdir /var/www/vhosts/$domain.$tld/template/
echo "Created template directory"
fi
chown -R bite:bite /var/www/vhosts/$domain.$tld/
echo "Set ownership to bite"
a2ensite $domain.$tld
echo "Enabled Vhost"
service apache2 reload
echo "Reloaded apache config.. all done!!"
_EOF_
echo -e "\n======== Initialising Terra ========"
make_vhost dev > /etc/apache2/sites-available/$domain.$tld
# Clone repository
cd /var/www/vhosts/
su bite -c "git clone $repo $domain.$tld"
cd $domain.$tld
su bite -c "git checkout -b development; git branch -D master; git push origin development; git branch --set-upstream development origin/development;"
echo "Cloned GitHub repository"
# Make logs
mkdir -p /var/www/vhosts/$domain.$tld/logs/
touch /var/www/vhosts/$domain.$tld/logs/access.log
touch /var/www/vhosts/$domain.$tld/logs/error.log
echo "Created logs"
# Make vhost.conf
if [ ! -d "/var/www/vhosts/$domain.$tld/conf/" ]; then
mkdir /var/www/vhosts/$domain.$tld/conf/
touch /var/www/vhosts/$domain.$tld/conf/vhost.conf
echo "Created vhost.conf"
fi
# Make template directory
if [ ! -d "/var/www/vhosts/$domain.$tld/template/" ]; then
mkdir /var/www/vhosts/$domain.$tld/template/
echo "Created template directory"
fi
chown -R bite:bite /var/www/vhosts/$domain.$tld/
echo "Set ownership to bite"
a2ensite $domain.$tld
echo "Enabled Vhost.. all done!!"
service apache2 reload
echo "Reloaded apache config.. all done!!"
echo -e "\n!======== Website created! ========!"
else
echo "Missing required options (-d -t -u -p -r)"
fi
以上是关于sh 以编程方式创建Apache VHost的主要内容,如果未能解决你的问题,请参考以下文章
如何配置 apache-vhost.conf 文件以从 Java 获取会话值
sh 创建vHost Ubuntu Lamp-Server(bash和python)
用vhost.map 实现APACHE虚拟主机方式(win7环境下)