Basic Davical Setup Script

From Davical
Jump to: navigation, search

This is a basic bash script for a sample setup of DAViCal with apache2 and postgres. You will have to provide a virtual host file named "davical" in the same folder. If you have SSL enabled in the virtual host file it will be enabled in apache and certificates are created it a certain folder with a certain prefix.

The bash script

#! /bin/bash

# This software licenced with the MIT license as follows.
#
###############################################################################
#Copyright (C) <2011> by <Felix Stiehler - <davical a)t felix-stiehler do)t de>>
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.
###############################################################################

if [ ! $(id -u) -eq 0 ]; then
	echo "Must be run as root." >&2
	exit 1
fi

DAVICAL_SOURCE='deb http://debian.mcmillan.net.nz/debian lenny awm'
TMP_FOLDER='/tmp/davical'
VHOST_FILE_NAME='davical'
SSL_DIR='/etc/apache2/ssl'
SSL_PREFIX='server'

echo -e "This will install DAViCal for Debian with all depending software - if needed - in a basic fashion. Please make sure to have the appropriate virtual host file (named \"$VHOST_FILE_NAME\") in the same folder as this install script. \nProceed? [y/n]"

read ok

if [ $ok != "y" -a $ok != "Y" ]; then
	exit 2
fi

if [ -d $TMP_FOLDER ]; then
	echo "Temporary folder already exists."
else
	mkdir $TMP_FOLDER
	echo "Creating temporary folder for the installation at $TMP_FOLDER."
fi

echo "Looking for the DAViCal source in your /etc/apt/sources.list"

grep "$DAVICAL_SOURCE" /etc/apt/sources.list > /dev/null

if [ $? -eq 1 ]; then
	echo "Adding \"$DAVICAL_SOURCE\" to the sources."
	echo -e "\n#Davical source\n$DAVICAL_SOURCE" >> /etc/apt/sources.list
	echo "Updating."
	apt-get update
else
	echo "Davical Source already in the sources.list."
fi

echo "Installing packages for Davical, Apache2, PHP5 and Postgres now. If something is already installed it will be skipped."

apt-get install apache2 libapache2-mod-php5 postgresql davical

if [ ! $? -eq 0 ]; then
	echo "An error occured when installing software from repos. Aborting."
	exit 1
fi

echo "Looking for your pg_hba.conf in /etc/postgresql/."

find /etc/postgresql/ -name "pg_hba.conf" > $TMP_FOLDER/find_result

if [ $(wc -l $TMP_FOLDER/find_result | cut -d " " -f 1) -eq 0 ]; then
	echo "No pg_hba.conf found in /etc/postgresql/. Must be edited manually."
else
	if [ $(wc -l $TMP_FOLDER/find_result | cut -d " " -f 1) -gt 1 ]; then
		echo "More than one pg_hba.conf found in /etc/postgresql/. Must be edited manually."
	else
		grep davical $(cat $TMP_FOLDER/find_result)
		if [ $? -eq 0 ]; then
			echo "The pg_hba.conf seems to contain davical configurations already. Skipping all database actions now. FAILED."
		else
			cp $(cat $TMP_FOLDER/find_result) $TMP_FOLDER/pg_hba.conf.old
			echo -e "# Davical configuration\nlocal\tdavical\tdavical_app\ttrust\nlocal\tdavical\tdavical_dba\ttrust\n" > $TMP_FOLDER/pg_hba.conf.addition
			cat $TMP_FOLDER/pg_hba.conf.addition $TMP_FOLDER/pg_hba.conf.old > $(cat $TMP_FOLDER/find_result)
			echo "The pg_hba.conf at "$(cat $TMP_FOLDER/find_result)" has been updated to accept connection from localhost for the davical databse. Reloading postgresql config now (only works with postgres 8.3)."
			/etc/init.d/postgresql-8.3 reload
			if [ ! $? -eq 0 ]; then
				echo "\nWARNING: Postgresql configuration could not be reloaded automaticly. You will have to run \" su postgres -c /usr/share/davical/dba/create-database.sh\" manually after this script"
			else
				echo "Running the script to create the database."
				su postgres -c /usr/share/davical/dba/create-database.sh
				if [ ! $? -eq 0 ]; then
					echo "An error occured when installing the database. FAILED."
				fi
			fi
		fi
	fi
fi


if [ -f "/etc/apache2/sites-available/$VHOST_FILE_NAME" ]; then
	echo "Virtual host file already existing. Overwrite? [y/n]"
	read ok
else
	read="y"
fi

if [ $ok != "y" -a $ok != "Y" ]; then
	echo "Skipping."
else
	if [ -f "$(dirname $0)/$VHOST_FILE_NAME" ]; then
		cp $(dirname $0)/$VHOST_FILE_NAME /etc/apache2/sites-available/.
		a2ensite $VHOST_FILE_NAME
		echo "Virtual host installed"
		grep "SSLEngine on" "$(dirname $0)/$VHOST_FILE_NAME"
		if [ $? -eq 0 ]; then
			echo "Enabling ssl in apache as it seems to be enabled in your virtual host file."
			if [ -e $SSL_DIR ]; then
    			echo "SSL directory already eyisting. No certificates created."	
			else
				mkdir $SSL_DIR
				cd $SSL_DIR

				openssl req -new > $SSL_PREFIX.cert.csr
				openssl rsa -in privkey.pem -out $SSL_PREFIX.cert.key
				openssl x509 -in $SSL_PREFIX.cert.csr -out $SSL_PREFIX.cert.crt  -req -signkey $SSL_PREFIX.cert.key -days 1460
			fi
			a2enmod ssl
		fi
		/etc/init.d/apache2 reload
	else
		echo "No virtual host file named $VHOST_FILE_NAME found. Skipping the virtual host installation. FAILED."
	fi
fi


if [ -f "/etc/davical/config.php" ]; then
	echo "Php config file already existing. Overwrite? [y/n]"
	read ok
else
	read="y"
fi

if [ $ok != "y" -a $ok != "Y" ]; then
	echo "Skipping."
else
# always use trailing tabs for the here document lines
cat <<-'EOF'> /etc/davical/config.php
	<?php
	//  $c->domain_name = "calendar.example.net";
	//  $c->sysabbr     = 'DAViCal';
	//  $c->admin_email = 'admin@example.net';
	//  $c->system_name = "Example DAViCal Server";
	//  $c->enable_row_linking = true;

	  $c->pg_connect[] = 'dbname=davical port=5432 user=davical_app';
	?>
	EOF
	echo "Php config installed at /etc/davical/config.php."
fi

echo "Done."

exit 0

A basic virtual host file

You will have to edit some information according to your setup.

# Virtual Host def for Debian packaged DAViCal
<VirtualHost 123.4.56.78 >
  DocumentRoot /usr/share/davical/htdocs
  DirectoryIndex index.php index.html
  ServerName davical.example.net
  ServerAlias calendar.example.net
  Alias /images/ /usr/share/davical/htdocs/images/
  <Directory /usr/share/davical/htdocs/>
      AllowOverride None
      Order allow,deny
      Allow from all
  </Directory>
  AcceptPathInfo On
  php_value include_path /usr/share/awl/inc
  php_value magic_quotes_gpc 0
  php_value register_globals 0
  php_value error_reporting 2
  php_value default_charset "utf-8"
  php_admin_flag suhosin.server.strip off
  php_value magic_quotes_runtime 0
</VirtualHost>

Note: When using PHP via CGI/FastCGI, "AcceptPathInfo Default" is enough.