Skip to content

Instantly share code, notes, and snippets.

@CodeBrauer
Last active March 3, 2016 07:17
Show Gist options
  • Save CodeBrauer/4782afaf41bdfe8a7b5d to your computer and use it in GitHub Desktop.
Save CodeBrauer/4782afaf41bdfe8a7b5d to your computer and use it in GitHub Desktop.
super simple vhost generator.
#!/bin/bash
# super simple vhost generator.
# @CodeBrauer <https://github.com/CodeBrauer>
# v0.2
if [ -z "$1" ]
then
echo "Error! No domain name given"
exit 1;
fi
echo -ne "\nGenerating vhost '$1'"
VHOST_FILE="/etc/apache2/sites-available/$1.conf"
echo "<VirtualHost *:80>
ServerName $1
ServerAdmin [email protected]
DocumentRoot /var/www/$1/public_html
LogLevel info
ErrorLog /var/www/$1/logs/error.log
LogFormat \"%h %l %u %t \\\"%r\\\" %>s %b\" common
CustomLog /var/www/$1/logs/access.log combined
php_value open_basedir "/var/www/$1"
</VirtualHost>
" > $VHOST_FILE
echo "Generating dirs..."
mkdir -p /var/www/$1/{logs,public_html}
CONF_CHECK=$(apache2ctl -t 2>&1)
if [[ $CONF_CHECK =~ .*Syntax\ OK.* ]]
then
echo "Configuration is valid. Enabling site...";
a2ensite $1.conf
echo "Reloading Apache conf..."
service apache2 reload
echo -ne "\nDone!\n"
else
echo "Configuration is invalid! Check the output of 'apache2ctl -t'"
fi
@CodeBrauer
Copy link
Author

Usage: ./vhost.sh example.org

  • Generates a vhost file in /etc/apache2/sites-available/
  • creates a public_html and a logsdir in /var/www/example.org/
  • creates access and error logs for that vhost in domain-specific dir
  • enables site & reloads apache2 if config-syntax is ok

Tested on Ubuntu 14.04 with Apache 2.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment