Bash script to create virtual hosting in Fedora

August 15th, 2014

I am often needing to add a script to create virtual hosting, below is one I have written for fedora (that can be altered for any distro) that creates the necessary folder structure, vhost host entries and restarts Apache.

#!/bin/bash

#################################
# SETTINGS LOCATIONS
WWW_ROOT='/var/www' # no trailing slash
DOC_ROOT='httd_docs' # no trailing slash

# SETTINGS TEMPLATE FILE
INDEX_MSG='HOSTING READY FOR <DN>' # <DN> WILL BE REPLACED WITH THE DOMAIN
INDEX_FILE='index.html'

# SETTINGS DIRECTORY PERMISSIONS
DIR_OWNER='apache'
DIR_GROUP='apache'
DIR_MOD='770'

# APACHE (FEDORA)
APACHE_RELOAD='service httpd restart' # cmd to reload/restart apache
APACHE_CONF_LOCATION='/etc/httpd/conf/httpd.conf' # no trailing slash
APACHE_CONF_TEMPLATE="
# <FQDN>
<VirtualHost *:80>
    ServerAdmin webmaster@<DN>
    ServerName <FQDN>
    ServerAlias <DN>

    DocumentRoot <DN_ROOT>
    ErrorLog /var/log/httpd/<DN_SLUG>-apache-error_log
    CustomLog /var/log/httpd/<DN_SLUG>-access_log common
</VirtualHost>"; # <DN> WILL BE REPLACED WITH THE DOMAIN, FQDN WITH THE FQDN, DN_SLUG WITH THE DOMAIN SLUG AND DN_ROOT WITH DOCUMENT ROOT

# HOSTS FILE
HOSTS_FILE='/etc/hosts'
#################################

#################################
# FUNCTIONS
function notice() {
        echo "============================================================================================================"
        echo $1
        echo "============================================================================================================"
}

function slugify(){
	echo $* | sed 's/^dl-*//ig' | tr '[:punct:]' '-' | tr '[:upper:]' '[:lower:]' | tr -s '[:blank:]' '[\-*]'
}
#################################

#################################
# START
if [[ $(/usr/bin/id -u) -ne 0 ]]; then
	notice "You must be root to run this script"
else
	# GET DN AND FQDN
	notice "Please enter the domain name"
	read DN
	DN="${DN/#\www./}"
	FQDN="www.$DN"

	# BEGIN
	host $DN 2>&1 > /dev/null
	if [ ! $? -eq 0 ]; then
		echo "$DN is not valid domain"
	else
		DN_SLUG=$(slugify "$DN");
		if [ -d "$WWW_ROOT/$DN_SLUG" ]; then
			notice "$DN already exists in $WWW_ROOT/$DN_SLUG"
		else
			# MAIN STRUCTURE
			mkdir "$WWW_ROOT/$DN_SLUG"
			mkdir "$WWW_ROOT/$DN_SLUG/$DOC_ROOT"
			echo "${INDEX_MSG//<DN>/$FQDN}" > "$WWW_ROOT/$DN_SLUG/$DOC_ROOT/$INDEX_FILE"

			# OWNERSHIP
			chown -R "$DIR_OWNER:$DIR_GROUP" "$WWW_ROOT/$DN_SLUG"
			chmod -R "$DIR_MOD" "$WWW_ROOT/$DN_SLUG"

			# QUICK LOOK UP LOCALLY
			HOSTS_FILE_BK="$(date +$HOSTS_FILE-%d%m%Y_%R.bak)";
			cp "$HOSTS_FILE" "$HOSTS_FILE_BK";
			if [ ! -f "$HOSTS_FILE_BK" ]; then
				notice "Failed to create host file backup at $HOSTS_FILE_BK"
			else 

				echo "# ${FQDN}" >> "$HOSTS_FILE";
				echo 127.0.0.1   ${DN} >> "$HOSTS_FILE";
				echo 127.0.0.1   ${FQDN} >> ""$HOSTS_FILE"";

				# APACHE ENTRIES
				APACHE_TEMPLATE="${APACHE_CONF_TEMPLATE//<DN>/$DN}";
				APACHE_TEMPLATE="${APACHE_TEMPLATE//<FQDN>/$FQDN}";
				APACHE_TEMPLATE="${APACHE_TEMPLATE//<DN_ROOT>/$WWW_ROOT/$DN_SLUG/$DOC_ROOT/}";
				APACHE_TEMPLATE="${APACHE_TEMPLATE//<DN_SLUG>/$DN_SLUG}";
				APACHE_CONF_LOCATION_BK="$(date +$APACHE_CONF_LOCATION-%d%m%Y_%R.bak)";
				cp "$APACHE_CONF_LOCATION" "$APACHE_CONF_LOCATION_BK";
				if [ -f "$APACHE_CONF_LOCATION_BK" ]; then
					echo "$APACHE_TEMPLATE" >> $APACHE_CONF_LOCATION;
					$APACHE_RELOAD;
					notice "$DN created at $WWW_ROOT/$DN_SLUG (apache bkup file found at $APACHE_CONF_LOCATION_BK)"
				else
					notice "Failed to create Apache config backup at $APACHE_CONF_LOCATION_BK"
				fi
			fi
		fi
	fi

fi
read STOP
exit;
################################
VN:F [1.9.9_1125]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.9_1125]
Rating: 0 (from 0 votes)

ZF2 different layouts in modules

June 4th, 2013

To enable a different layout in a module, to your module.php for the module define a new layout like so

public function init(ModuleManager $mm)
    {
        $mm->getEventManager()->getSharedManager()->attach(__NAMESPACE__, 'dispatch', function($e) {
            $e->getTarget()->layout('admin/layout');
        });
    }

then within your module.config.php view_manager settings define the template

'view_manager' => array(
        'template_map' => array(
            'admin/layout'           => __DIR__ . '/../view/layout/cms.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),

or you can define a direct path in your module.php without any template mapping i.e

public function init(ModuleManager $mm)
    {
        $mm->getEventManager()->getSharedManager()->attach(__NAMESPACE__, 'dispatch', function($e) {
            $e->getTarget()->layout('layout/admin');
        });
    }

This will load module/view/layout/admin.phtml when this module is run

VN:F [1.9.9_1125]
Rating: 4.3/10 (4 votes cast)
VN:F [1.9.9_1125]
Rating: -1 (from 1 vote)

Galaxy S3 no internet on virgin media super hub

March 17th, 2013

Samsung galaxy S3 will not connect to virgin media super hub can be a problem, I found mine would connect but had no internet – and everything else on the network did.

After several hours of looking through forums, rebooting and changing settings, I found the issue to be with the hub itself, for some reason in Advanced-settings>DHCP>DHCP Reservation I found the mobile was being addressed on a strange IP like 10.125.x.x.x and not the one it had been allocated by DHCP.

I took note of the mac and device name in the IP Lease table, switched off the WIFI on the mobile (disconnecting it from the router), hit refresh to make sure it was no longer listed in the IP lease table.

Next I added a reservation entry to the the lease table for the device, using its mac and device name, and bound it to an IP that was not being used.

Voila.. Internet on the S3

VN:F [1.9.9_1125]
Rating: 6.0/10 (1 vote cast)
VN:F [1.9.9_1125]
Rating: 0 (from 2 votes)