Skip to content

Instantly share code, notes, and snippets.

@versedi
Last active June 1, 2016 12:09
Show Gist options
  • Save versedi/f79cab707e3ff3d83466b7ade7986999 to your computer and use it in GitHub Desktop.
Save versedi/f79cab707e3ff3d83466b7ade7986999 to your computer and use it in GitHub Desktop.
Shell script to automatically create new dev sites in Apache2, Usage: ./newsite mysite.dev /home/mysites/optional-full-path-to-site
#!/bin/bash
CYAN='\033[1;36m'
RED='\033[0;31m'
NC='\033[0m' # No Color
if [ `id -u` != 0 ]; then
echo -e "${RED}-- ERROR --${NC}"
echo -e "-- This script should be run as root so that files"
echo -e "-- can be created freely."
exit
fi
if [ $# -eq 0 ]
then
echo -e "${RED}-- ERROR --${NC}"
echo -e "No arguments supplied. Usage:"
echo -e "${CYAN}sudo ./newsite mysite.dev [optional:path-to-site]${NC}"
echo -e "If no path to site is specified it assumes /var/www/mysite.dev"
exit
else
if [[ "$1" == "-h" || "$1" == "-help" || "$1" == "help" || "$1" == "--h" ]]; then
echo -e "Usage:"
echo -e "${CYAN}sudo ./newsite mysite.dev [optional:path-to-site]${NC}"
echo -e "If no path to site is specified it assumes /var/www/mysite.dev"
exit
fi
fi
NAME=$1
APACHE="/etc/apache2/sites-available/"
LOGS="/var/log/apache2/"
LOGSDIR="$LOGS$NAME"
HOSTS="/etc/hosts"
SITEFILE="$APACHE$NAME.conf"
SITEPATH=$2
if [ -z "$2" ]
then
SITEPATH="/var/www/$NAME"
fi
if [ ! -d "$SITEPATH" ]
then
mkdir $SITEPATH
fi
DEFAULTSITE="<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.
ServerAdmin webmaster@$NAME
ServerName $NAME
ServerAlias www.$NAME
DocumentRoot $SITEPATH
<Directory "$SITEPATH">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</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}/$NAME/error.log
CustomLog \${APACHE_LOG_DIR}/$NAME/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>"
if [[ -v $NAME ]]; then
echo -e "${RED}-- ERROR --${NC}"
echo -e "-- You need to supply a site name,"
echo -e "-- example:"
echo -e " ${CYAN}newsite mysite.dev${NC}"
exit
else
if [[ -f "$SITEFILE" ]]; then
echo -e "${RED}-- ERROR --${NC}"
echo -e "-- Site with this name already exists!,"
exit
fi
if [ ! -d "$LOGSDIR" ]; then
echo -e "Creating Apache logs directory."
echo -e $LOGSDIR
mkdir $LOGSDIR
fi
if [[ ! -f "$SITEFILE" ]]; then
echo -e "Creating Apache site configuration."
echo -e $SITEFILE
touch $SITEFILE
echo "$DEFAULTSITE" > $SITEFILE
a2ensite $NAME
service apache2 restart
line=" $NAME www.$NAME"
if ! grep -q -x -F -e "$line" <"$HOSTS"; then
ed -s "$HOSTS" < <(printf '%s\n' 1 a "$line" . 1,2j w q)
fi
echo -e "${CYAN}-- Success! --${NC}"
echo -e "${CYAN}-- Created and enabled new website $NAME --${NC}"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment