Skip to content

Instantly share code, notes, and snippets.

@idimopoulos
Last active January 25, 2022 17:17
Show Gist options
  • Save idimopoulos/faa6623533184ddb619d516d674b59c2 to your computer and use it in GitHub Desktop.
Save idimopoulos/faa6623533184ddb619d516d674b59c2 to your computer and use it in GitHub Desktop.
a2createsite
#!/bin/bash
# ======= Variables ========
# Set the path to your localhost
CDPATH=/var/www/html/
apache_config=/etc/apache2/sites-available
host_name_suffix=test
# ======= /Variables ========
# This script creates virtual hosts.
# you should put it under /usr/local/bin/
# and run it with '$ sudo a2createsite'
# Based on https://gist.github.com/tsi/3088993#file-newvhost
# This script runs for apache2 on Ubuntu 14+. Possibly on other systems as well
# but I have not tried them.
# Move to webroot for autocomplete.
cd "${webroot}"
echo "Moving to webroot."
if [ "$(whoami)" != "root" ]; then
echo "This command needs to run as root. Please run 'sudo a2createsite' instead."
exit 1
fi
while [[ -z "$site_dir_true" ]]
do
read -e -p "Enter directory name under $CDPATH: " site_dir
# If passed data is empty, display an error.
if [[ -z "$site_dir" ]]; then
echo "Directory cannot be empty."
# If directory is not found, prompt to create it.
elif [[ ! -d "${CDPATH}${site_dir}" ]]; then
while [[ "${create_dir}" != 'y' && "${create_dir}" != 'n' && "${create_dir}" != 'i' ]]
do
read -p "Directory does not exist. Create it? (I/y/n for 'ignore', 'yes' and 'no'(abort))): " create_dir
if [[ -z "${create_dir}" ]]; then
create_dir='i'
fi
create_dir="${create_dir,,}"
if [[ "${create_dir}" != 'y' && "${create_dir}" != 'n' && "${create_dir}" != 'i' ]]; then
echo "Only 'y', 'n' and 'i' are accepted."
fi
done
if [[ "${create_dir}" = 'y' ]]; then
mkdir -p "${CDPATH}${site_dir}"
site_dir_true=true
elif [[ "${create_dir}" = 'i' ]]; then
site_dir_true=true
elif [[ "${create_dir}" = 'n' ]]; then
echo "Nothing to do here. Aborting."
exit 1
fi
# If directory exists, everything is ok.
elif [[ -d "${CDPATH}${site_dir}" ]]; then
site_dir_true=true
fi
done
# Remove trailins slash.
site_dir=${site_dir%/}
echo "The site directory to be used is under ${CDPATH}${site_dir}"
# Get a name for the configuration file.
while [[ -z "${config_unique}" ]]; do
read -p "Enter the name of the configuration file (without the trailing '.conf'): " config_name
if [[ -z "${config_name}" ]]; then
echo "You must give a name (without the trailing '.conf')."
elif [[ -f "${apache_config}/${config_name}.conf" ]]; then
echo "File already exists. Enter another name."
else
config_unique=true
fi
done
# Get a name for the configuration file.
read -p "Enter the hostname for the site (suffix .${host_name_suffix} will be added)[${config_name}]: " host_name
if [[ -z "${host_name}" ]]; then
host_name=${config_name}
fi
# Create the file with VirtualHost configuration in /etc/apache2/site-available/
echo "<VirtualHost *:80>
DocumentRoot ${CDPATH}${site_dir}/
ServerName ${host_name}.${host_name_suffix}
<Directory ${CDPATH}${site_dir}/>
Options +Indexes +FollowSymLinks +MultiViews +Includes
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:443>
DocumentRoot ${CDPATH}${site_dir}/
ServerName ${host_name}.${host_name_suffix}
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/apache.pem
<Directory ${CDPATH}${site_dir}/>
Options +Indexes +FollowSymLinks +MultiViews +Includes
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>" >$apache_config/$config_name.conf
# Add the host to the hosts file
echo 127.0.0.1 ${host_name}.${host_name_suffix} >> /etc/hosts
# Enable the site
a2ensite $config_name
# Reload Apache2
#/etc/init.d/apache2 restart
service apache2 reload
echo "Your new site is available at http://${host_name}.${host_name_suffix}"
@sandervd
Copy link

sandervd commented Sep 1, 2016

<VirtualHost *:8000>
ServerAlias localhost *.local *.loc #wildcard catch all
VirtualDocumentRoot /var/www/%1
UseCanonicalName Off
<Directory "/var/www">
Options FollowSymLinks
AllowOverride None

    #
    # Apache/PHP/Drupal settings:
    #

    # Protect files and directories from prying eyes.
    <FilesMatch "\.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$">
      Order allow,deny
    </FilesMatch>

    # Don't show directory listings for URLs which map to a directory.
    Options -Indexes

    # Follow symbolic links in this directory.
    Options +FollowSymLinks

    # Make Drupal handle any 404 errors.
    ErrorDocument 404 /index.php

    # Set the default handler.
    DirectoryIndex index.php index.html index.htm

    # Override PHP settings that cannot be changed at runtime. See
    # sites/default/default.settings.php and drupal_initialize_variables() in
    # includes/bootstrap.inc for settings that can be changed at runtime.

    # PHP 5, Apache 1 and 2.
    <IfModule mod_php5.c>
      php_flag magic_quotes_gpc                 off
      php_flag magic_quotes_sybase              off
      php_flag register_globals                 off
      php_flag session.auto_start               off
      php_value mbstring.http_input             pass
      php_value mbstring.http_output            pass
      php_flag mbstring.encoding_translation    off
    </IfModule>

    # Requires mod_expires to be enabled.
    <IfModule mod_expires.c>.
      ExpiresActive On
      ExpiresDefault A1209600
      <FilesMatch \.php$>
        ExpiresActive Off
      </FilesMatch>
    </IfModule>

    # Various rewrite rules.
    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteRule "(^|/)\." - [F]
      RewriteBase /
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} !=/favicon.ico
      RewriteRule ^ index.php [L]
      RewriteRule ^(.+)(\.php|\.html)/$  /$1$2 [R=301,L]
    </IfModule>
</Directory>

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