Skip to content

Instantly share code, notes, and snippets.

@sipofwater
Last active November 25, 2021 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sipofwater/aa6e041c1cd13f14a897f45f7b8091fa to your computer and use it in GitHub Desktop.
Save sipofwater/aa6e041c1cd13f14a897f45f7b8091fa to your computer and use it in GitHub Desktop.
createnewsite.sh - creates a home for a new vhost on a LAMP server

The Purpose of createnewsite.sh

is to automate the creation of a new a VHOST (virtual host) for an Apache server based on the assumptions below. This script has been tested as working on Ubuntu 14.04, 16.04, 20.04.

  • takes 1 input parameter: the <projectname>
  • generates and set permissions on /etc/apache2/sites-available/<projectname>.conf
  • creates /web/webroot/<projectname> folder and sets folder permissions
  • adds /etc/apache2/sites-available/<projectname>.conf to a2ensite
  • runs service apache2 restart enabling the website and making it active
  • generates a reference to <projectname> in /etc/hosts for local DNS resolution of http://<projectname>/

assumptions

  • creating a LAMP-based website (linux/apache/mysql/php)
  • website rooted @ /web/webroot/<projectname>
  • script located @ /web/scripts/createnewsite.sh
  • owner www-data
  • group webdev
  • this file exists: /etc/apache2/sites-available/website-tpl.conf (located below)

dependencies required

  • apache2
  • conf template file: /etc/apache2/sites-available/website-tpl.conf

usage:

  • sh /web/scripts/createnewsite.sh <projectname>

The <projectname> will be used to:

  • create a <projectname> directory under /web/webroot/
  • add the DNS HOSTS entry to resolve http://<projectname> locally

notes:

This script currently has the webroot folder, and the projectowner and projectgroup permissions hardcoded.

Future plans involve removing these hardcoded values.

#!/bin/bash
set -x # echo on
# Created: December 4, 2016
# Updated: November 29, 2017
# Purpose: To create a new home for a website on an apache webserver that works in the browser.
# Input: Program takes 1 input parameter <projectname>.
# ################# setting variables ###########################
projectname=$1
projectdir="/web/webroot/$projectname"
projectowner="www-data"
projectgroup="webdev"
conf="/etc/apache2/sites-available/website-tpl.conf"
newconf="/etc/apache2/sites-available/${projectname}.conf"
# ###############################################################
# Create, update and set permissions for apache2 site.conf file
sudo cp ${conf} ${newconf}
sudo sed -Ei "s/PROJECTDIR/$projectname/g" ${newconf}
sudo chmod -R 775 ${newconf}
sudo chown -R ${projectowner}:${projectgroup} ${newconf}
# Create webroot home for website
sudo mkdir ${projectdir}
# Set permissions for website folder
sudo chmod -R 775 ${projectdir}
sudo chown -R ${projectowner}:${projectgroup} ${projectdir}
# Enabling the website and restarting apache
sudo a2ensite ${projectname}
sudo service apache2 restart
# Add website to /etc/hosts file
sudo sed -i "$ a 127.0.0.1\t${projectname}" /etc/hosts
echo "I am being served @ http://${projectname}/"
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /web/webroot/PROJECTDIR
ServerName PROJECTDIR
ServerAlias PROJECTDIR
ErrorLog /web/logfiles/PROJECTDIR-error.log
CustomLog /web/logfiles/PROJECTDIR-access.log combined
</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment