Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick shell script which takes a top-level FDQN as an argument and creates a folder in /var/www and creates the relevant sites-available config. Outputs list to be run by second script for enabling.
#!/bin/bash
# N V1.0
# Bash script automagically creates a configuration file for each virtual host. The script takes one input on the command line, the FQDN only.
# USAGE: ./CreateVHConfig.sh testing.com
# MAY REQUIRE SUDO (If users/permissions not set correctly)
# No fancy variable naming here. There's one variable. It's called var. This is the command line input.
var=$1
# Well, 2 variables. This is the ultimate destination of the file.
config_file=/etc/apache2/sites-available/$var.conf
#Create a separate directory for this domain.
mkdir -p /var/www/$var
if [ ! -e "$config_file" ]; then
touch $config_file
cat > "$config_file" <<EOL
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@$var
DocumentRoot /var/www/$var
ServerName $var
ServerAlias www.$var
<Directory /var/www/$var>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog \${APACHE_LOG_DIR}/error.log
CustomLog \${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
EOL
fi
# Finally, output what the correct syntax is to a separate shell script, so that this can be run after importing all domains.
echo "a2ensite $var.conf" >> enable.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.