Skip to content

Instantly share code, notes, and snippets.

@edavis25
Created December 28, 2017 23:27
Show Gist options
  • Save edavis25/9a1a187f2896f24aaee4c8bae2604fe3 to your computer and use it in GitHub Desktop.
Save edavis25/9a1a187f2896f24aaee4c8bae2604fe3 to your computer and use it in GitHub Desktop.
Shell script to enable a site in Apache.
#!/bin/bash
# This shell script automates the process of creating virtual host config files and enabling sites in Apache
# for a localhost. The script requires super user permissions as it creates a config file in the Apache
# directory and edits the /etc/hosts file.
#
# Function: The script will first prompt user to enter a name that will be used for the site and then prompt
# for a filepath pointing to the location of the website files (aka DocumentRoot in Apache config). It will
# then use these variables to create a config file, enable the site using a2ensite, and then restart Apache.
# The script will also append a line to hosts file (ex: 127.0.0.1 site-name.local) to allow access through localhost.
#
# Prompted inputs:
# @name: The name of the site without extensions. This name is used for config file and localhost access.
# Example: entering "website" at this prompt will enable a config file named "website.conf" and
# "website.local" will be added to hosts file to allow localhost access at "website.local"
#
# @path: The directory path containing the website index files (used as DocumentRoot in Apache config file)
# Define global variables
name="";
path="";
# Display prompt for site/config name
echo "Enter the name without extensions that will be used for config files and localhost access: ";
# Read user input for site
read name;
# Validate name input
while [[ ! $name =~ ^[A-Za-z0-9._-]+$ ]]
do
echo "Error: name must only contain letters, numbers, periods(.), dashes(-), and underscores(_). Please try again.";
echo "Enter the name without extensions that will be used for config files and localhost access: ";
read name;
done
# Display prompt for setting document root
echo "Enter the full path of the directory containing the site's files (Apache's Document Root)";
# Read user input for path
read -e path;
# Validate filepath input
while [[ ! -d $path ]]
do
echo "Error: \"$path\" is not a directory or does not exist. Please try again.";
echo "Enter the full path of the directory containing the site's files (Apache's Document Root)";
read -e path;
done
# Create the config file
cat << EOF > /etc/apache2/sites-available/$name.conf
<VirtualHost *:80>
ServerName $name.local
DocumentRoot "$path"
</VirtualHost>
EOF
# Add new entry to hosts file
echo "127.0.0.1 $name.local" >>/etc/hosts;
# Enable config with a2ensite
`a2ensite $name.conf` 2>/dev/null;
# Restart Apache
echo "Restarting Apache...";
`service apache2 restart`;
echo "Done!";
# Print success message
echo "Your new site has been created!";
echo "################################";
echo "The config file for your site can found at: /etc/apache2/sites-enabled/$name.conf";
echo "You can now visit your site at: $name.local";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment