Setting up your own cross-platform Calendar server (with iPhone support) 14

google_calendar_goes_caldav.8c3rtxjg91oos4ow0o88cwows.17ldmg3f9ou8088wk04c40sgo.th

Update on the 26th of December 2012: I would like to turn your attention towards the ownCloud project, which I recently started using myself as a replacement for the solution as described in this article. Not only does ownCloud provide you with easy access to user calendars, but also to contacts and file synchronization! (just like DropBox). Using plugins, it is even possible to authenticate users through protocols such as LDAP and more. I would strongly advice you to investigate it further, as I personally find it to be an easier and more robust solution.


In this howto, I will take you through a step-by-step guide, on how to set up your own calendar sharing/synchronizing service on a computer running either Debian or Ubuntu.

This is especially useful, if you are using more than just a single computer or mobile device in your everyday life, and want to have all your calendars in perfect sync.

This article might seem a little long-winded at a glance, but the set up is actually rather simple. All you need is a computer which is able to run Debian or Ubuntu, and, of course, a network connection (and/or an Internet connection) to be able to access the calendar sharing service from your client computers or devices.

First, a little bit about why I made this guide in the first place…

Why I prefer my calendars digital

I’ve never really been any good at maintaining a handwritten calendar.

Sure, I’ve tried having one, but it’s simply too big and unhandy to carry around with you all the time, and that’s exactly where you need it the most. The stupid thing always ends up lying under a bunch of papers and CD’s in a corner somewhere, only to be found when I don’t really need it. Usually while cleaning.

Chandler calendar server and iPhone 3.0 issues

I’ve been running my own Ubuntu server for quite a while now, and a couple of years ago, I decided to try and install a calendar synchronizing service on it.

I went ahead and found a project called “Chandler”. It’s an open source calendar server, written in Java. It uses a well known and standardized protocol, often used for calendar sharing, called CalDAV. It has a neat web interface and it’s very stable in my experience.

In this way, I was now able to access several different calendars from a single account on my calendar server. CalDAV client programs are available for Windows, OS X, Linux and on a load of different mobile devices, so regardless of which operating system I was using, I could always access my all calendars (in perfect sync, I might add).

This sure made things allot easier for me. Yet, I had never tried setting up my calendars on a mobile device.

As you might have guessed from the title, I recently bought an iPhone, and last year, Apple released their much anticipated 3.0 firmware upgrade. And while everyone else was going on about MMS, improved chat og real GPS featues, the one thing that I was extremely happy about, was that the iPhone now also supported the CalDAV protocol.

After installing the new 3.0 firmware, I rushed to the settings menu and tried to set up my Chandler calendar account. The account was installed without any hiccups, and I was now able to see all the entries that I had previously created from my computer, with either iCal or the web client.

My excitement didn’t last very long though, as I soon discovered that any entries I tried to create from my iPhone, only displayed on the iPhone itself. Synchronization back to the server seemed broken on the iPhone.

Why I switched to the DAViCal calendar server

Then I found DAViCal. Another CalDAV server, written in PHP.

DAViCal has no client web interface to add and manage entries, but it still supports all my CalDAV compatible clients. It’s also allot faster than Chandler, since it’s not running on Java.

After installing DAViCal and migrating all my calendars from Chandler, I added my newly created DAViCal account to my Mac Pro’s iCal application, and in my iPhone’s settings menu…

And voilà! Instant syncing in both directions!

How to set up DAViCal on a Debian server

I made this howto, simply because I spent too much time trying to figure out DAViCal’s own documentation on how to install it. I had to visit allot of forums and google around some, to be able to get everything working. My hope is that others will have an easier time setting it up, by following this guide.

We are going to install a number of different things on our system, in order to get DAViCal working.

Using this howto to configure DAViCal, the following software is going to be installed on your system.

  • Apache (version 1.3.x or 2.x.x)
  • PHP5 (version 5.0 or greater)
  • PHP5 extension for Apache
  • Andrew’s Web Libraries – PHP Utility Libraries (libawl-php)
  • PostgreSQL (version 8.1 or greater)

DAViCal is written in PHP, and needs to run within a web service with PHP support and have access to a PostgreSQL database, to store all the CalDAV accounts and entries. There are many possible configurations on how to achieve this, even with other distributions than Ubuntu or Debian and with other web services than Apache version 2. However, I am not going to cover those in this howto.

Prerequisites for this howto

I am currently using Debian 5.03 to install DAViCal, but I’m pretty sure that any recent versions of Debian and Ubuntu, should install in the exact same way.

If you run into trouble with any of the configuration steps in this howto, please leave a comment, and I will try to help out as much as I can.

I am going to assume that you are using either a Debian or an Ubuntu distribution, and that it is already installed and running on your computer. I am also going to assume that you have the “sudo” package installed, and that you have super user privileges on your system. (The “sudo” package needs to be installed and configured manually in some versions of Debian.)

In the making of this howto, I was using the following Debian packages.

  • Apache (version 2.2.9)
  • PHP5 (version 5.2.6-1+lenny4)
  • PHP5 extension for Apache (version 5.2.6.dfsg.1-1+lenny4)
  • Andrew’s Web Libraries (version 0.38-0)
  • PostgreSQL (8.3.8-0lenny1)

Using the examples in the following chapters, might install different versions of these packages.

Installing Apache with PHP5 support and Andrew’s Web Libraries

First, you need to install and configure the Apache service with PHP5 support. Install the required packages, like this:

sudo apt-get install apache2 php5 libapache2-mod-php5

Once the packages are installed, PHP5 should have configured itself with Apache automatically.

Next, you need to install Andrew’s Web Libraries.

sudo apt-get install libawl-php

It might be a good idea to restart the Apache service manually, to ensure that the PHP5 extension and Andrew’s Web Libraries are loaded properly.

/etc/init.d/apache2 restart

You can test your PHP5 configuration, by creating a PHP file with the following contents, and placing it in Apache’s “document root”. (As “/var/www/index.php”)

<?
phpinfo();
?>

Now, visit your website, by going to “http://ip.address.of.server/index.php”.

If you see a lot of information about your PHP configuration, your Apache service should be configured correctly with the PHP5 extension.

Installing DAViCal

To install DAViCal, you have to add the DAViCal repository.

You can easily add the repository to the bottom of your “sources.list” file, by executing this command.

sudo echo “deb http://debian.mcmillan.net.nz/debian lenny awm” >> /etc/apt/sources.list

The repository is signed with Andrew McMillan’s public key, which means that you have to add it to your system, in order to be able to fetch software from it.

Execute the following command, to add the public key to your system.

sudo apt-key advanced –keyserver subkeys.pgp.net –recv-keys F6E0FA5CF0307507BB23A512EAFCFEBF8FEB8EBF

You might run into problems, trying to install the public key, if it should have changed since I last revised this howto. In this case, you should go to the official DAViCAL Installation Page at davical.org. Here you should be able to find the latest version of the public key, and install that instead.

If the key installs without any errors, we should now be able to update the repository and install DAViCal.

sudo apt-get update && sudo apt-get install davical

You should allow any extra packages to be installed, if prompted.

Installing the PostgreSQL service and setting up the database

You should now install the PostgreSQl service.

sudo apt-get install postgresql

On my system, the above command automatically fetched and installed the postgresql-8.3 package.

After installing the PostgreSQL service, you have to set up the database. First, you need to add a user account with minimum privileges, to the main PostgreSQL database. DAViCal needs this user, to be able to connect to the database from the Apache service. You should do this, by first logging in as the user “postgres”, and then creating the user “davical_app”, like this.

sudo su postgres
createuser –no-createdb –no-createrole davical_app
Shall the new role be a superuser? (y/n) n

Now, staying logged in as the “postgres” user, you need to setup the database that DAViCal is going to use. Do this, by executing a script that got installed along with DAViCal.

It is located here: “/usr/share/davical/dba/create-database.sh”, and executes like this. (Keep in mind that you have to be logged in as the “postgres” user.)

/usr/share/davical/dba/create-database.sh
Supported locales updated.
CalDAV functions updated.
RRULE functions updated.
Database permissions updated.
NOTE ====
* You will need to edit the PostgreSQL pg_hba.conf to allow the ‘davical_dba’ database user access to the ‘davical’ database. *
The password for the ‘admin’ user has been set to ‘1A84K1aB‘”
Thanks for trying DAViCal!
Check in /usr/share/doc/davical/examples/ for some configuration examples.
For help, visit #davical on irc.oftc.net.

It’s very important that you jolt down the password for the “admin” account, from your own output of the last command (not from my example above). You are going to need it in the end of this howto.

Now you need to edit the file “pg_hba.conf”, in order to give DAViCal access to the database, as the “davical_app” user, which we created earlier. I found the file in “/etc/postgresql/8.3/main/”, but your file might be placed in a different directory, depending on which version of the PostgreSQL package you installed earlier.

Add the following line in the beginning/top of your “pg_hba.conf” file.

local davical davical_app trust

This means that anyone on the local computer, will have rights to connect to the DAViCal database as the ‘davical_app’ user. If you you have untrusted local users on your system, you might want to reconsider the above configuration, and possibly find a more secure solution.

Finally, you need to restart the PostgreSQL service, to make the changes come into effect.

sudo /etc/init.d/postgresql-8.3 restart

Again, the above command might be a little different for you, if you installed another version of the PostgreSQL package earlier.

Configuring Apache and DAViCal

In this step, we are going to configure Apache to handle a “virtual host” for displaying DAViCal’s web interface. This configuration can also be achieved in many different ways, but I’m going to keep it as simple as possible, to avoid too much confusion for those unfamiliar with “virtual hosts”.

You should keep in mind, however, that if you are already hosting other websites with Apache, you might want to configure this virtual host in another manner. This configuration could possibly make all or some of your other hosted websites unavailable.

First, you need to add a link to the DAViCal PHP files in Apache’s “document root” directory (“/var/www”). This is done like this.

sudo ln -s /usr/share/davical/htdocs /var/www/davical

Next, you need to configure Apache to display the DAViCal PHP files as the default website, by adding a “virtual host”.

If your Apache service is currently not hosting any other websites, you should be able to configure your “/etc/apache2/sites-available/default” file, like this:

<VirtualHost *:80>
DocumentRoot /var/www/davical
DirectoryIndex index.php
Alias /images/ /var/www/davical/images/
<Directory /var/www/davical/>
AllowOverride None
Order allow,deny
Allow from all
</Directory>
php_value include_path /usr/share/awl/inc
php_value magic_quotes_gpc 0
php_value register_globals 0
php_value open_basedir 1
php_value error_reporting “E_ALL & ~E_NOTICE”
php_value default_charset “utf-8″
</VirtualHost>

Now restart the Apache service, to load the new website.

sudo /etc/init.d/apache2 restart

Now, if everything is set up and running as it should be, you should now be able to access the DAViCal web interface at “http://ip.address.of.server“.

There is one last step to be taken, before everything is up and running, though.

When accessing your DAViCal’s webinterface, you should see a page displaying something similar to this.

This means that DAViCal is expecting to find a specific configuration file, based on which IP address or domain name you are using to access DAViCal’s web interface.

In the example above, I am trying to access the web interface with the local IP-address of my server which, in my case, is “192.168.1.161“.

The DAViCal web interface is thus telling me to make a DAViCAL configuration file, which is to be created as “/etc/davical/192.168.1.161-conf.php”, with the following content.

<?php
// $c->domain_name = ‘davical.example.com’;
// $c->sysabbr = ‘davical’;
// $c->system_name = ‘DAViCal CalDAV Server’;

$c->admin_email = ‘admin@example.com’;
$c->pg_connect[] = ‘dbname=davical user=davical_app’;
?>

The important thing to understand here, is that this file needs to be individually created, based on the IP-address and/or domain name of your server.

DAViCal’s web interface will always notify you, if a configuration file is missing, and it will even tell you how to create it, so you should have no problems configuring this part on your own.

Once you have created this configuration file correctly, you should be able to see the login screen.

Now all you need to do, is login with you admin account. (Which you wrote down earlier, setting up the database.)

Once you have logged in, the first thing you should do, would be to change you admin password to something else.

You should then be able to create a new user account, which also automatically creates a standard “home” calendar for your new user.

In the next chapter, I will show you how to add your new DAViCal account with a number of different CalDAV clients. (Including the iPhone.)

Adding your DAViCal accounts to different CalDAV clients

In this chapter, I will show you how to set up your DAViCal accounts on a number of different CalDAV clients.

The iPhone

Setting up your new DAViCal account on the iPhone is fairly simple. You just have to know how to add the server address, which took some time for me to figure out. If you add it incorrectly, you might end up having only a single calendar on your account displayed.

Go to the “Settings” application and choose “Mail, Contacts, Calendars”. You should then select “Add Account…” and choose “Other”.

Select “Add CalDAV Account” and fill in the information for your DAViCal server, like this:

Keep in mind that “http://ip:80/caldav.php/user” should be entered correctly to reflect your set up, of course.

Mac OS X (iCal)

Open up iCal and go the to Preferences menu.

Select the “Accounts” tab and click the “+” button in the bottom of the window and fill in your account information like this:

Keep in mind that “http://ip:80/caldav.php/user” should be entered correctly to reflect your set up, of course.

Linux (Evolution)

Evolition doesn’t seem to be able to add an entire account all at once. You will have to add each of your calendars in your account, individually.

First you need to go to the DAViCal web interface and log in with your account information.

Go to the “User” tab and scroll all the way down to the bottom of the page. There you will find the “collection ID’s” of your individual calendars. The long string of code in the end of each collection, is the “collection ID”. You should copy that code, to use it in Evolution in the next step.

Fire up Evolution and go to the calendar view. Right click on an empty space in the calendar selection window on the left. Choose “New Calendar” and fill out the fields like this:

Keep in mind that “http://ip:80/caldav.php/user/collectionid” should be entered correctly to reflect your set up, of course.

14 thoughts on “Setting up your own cross-platform Calendar server (with iPhone support)

  1. Reply Marcel Mar 27, 2010 08:31

    Thank you! Synchronisation works both ways with the iPhone, but I cannot accept or create an invitation (I can see an invitation, but cannot take any action on it). Do you have an idea on how to resolve this?

    • Reply Brian René Jensen Apr 4, 2010 10:45

      Hi Marcel.

      I currently do not use invitations in my calendar, so I’m not sure really.

      I didn’t even know that you could have invitations inside a caldav calendar.

      Are these invitations something you would receive through your e-mail application that then goes into your calendars?

  2. Reply Grasiani Sep 14, 2011 15:50

    Hi, a few notes about debian squeeze:
    when adding to surces.list, do this:

    sudo echo “deb http://debian.mcmillan.net.nz/debian squeeze awm” >> /etc/apt/sources.list

    postgresql comes with apache, but you have to install this packages as well (before running the script /usr/share/davical/dba/create-database.sh):
    libconfig-yaml-perl
    libdbd-pg-perl
    libdbd-pgsql

  3. Reply Mike Oct 7, 2011 21:31

    Brian,

    Great walkthrough of the process – very useful to those of us trying to do the same.

    I recently installed PHP Web Calendar on a server, which I’m integrating into a WordPress blog. One question. What are the pros (and cons) of installing DaviCal versus Web Calendar? Thanks and Regards.

    Mike

  4. Reply Andrew McMillan Jan 17, 2012 11:22

    Thanks for this blog post. Comprehensive and well-written, and most of all it is clear – much clearer that what I have written about these things :-)

    I would just like to note a couple of minor points:

    * Get rid of the open_basedir setting.
    * Newer versions of DAViCal don’t need you to set the PHP include path in the virtual host.
    * Call the config file /etc/davical/config.php – you only need to use domain-name based config files for hosting multiple DAViCal sites on one server.

    Regards,
    Andrew McMillan.

  5. Reply Steve Feb 8, 2012 18:19

    I’m trying to go though your steps but can’t seem to install “davical”. It’s telling me that it can’t find the package.

    E: Couldn’t find package davical

  6. Reply Steve Feb 9, 2012 10:01

    Nevermind…:-)

    It’s strange to me, but it looks like you need to run both commands at the same time – just as you wrote it.

    apt-get update && apt-get davical

    My bad!

    • Reply Brian René Jensen Feb 9, 2012 11:08

      Hey Steve.

      No worries. And yes, you first have to update your aptitude repository (apt-get update) after you edited your “sources.list”-file, and before you are able to to install davical.

  7. Reply Steve Feb 10, 2012 20:09

    OK! I’ve gotten to the point where I can login to the admin account, changed the password and now ready to setup some calendars – I think.

    How do you do that? Forgive me for the question, but what do you recommend for someone that needs to create user calendars that are visible to all parties. Kind of like google calendar. I’m looking at the following site, but don’t know where to turn.

    http://www.davical.org/clients.php

    • Reply Brian René Jensen Feb 11, 2012 20:24

      Hi Steve.

      Once you are able to login with the admin account, you should be able to create new users. Once you create these users, some standard calendars should automatically be created.

      Now – when you set up a client (authenticating with the new user created), you will be able to see the already created calendars AND create new ones.

      As for how to make these calendars publicly visible, I’m not so sure. I would suspect that this option would be available to you, once you create a new calendar for a particular user.

  8. Reply Steve Feb 11, 2012 20:46

    Hey Brian!
    First, thanks very much for your replies. My current progress is as follows

    1. created an account √
    2. installed chandler √
    3. created second account √
    4. importing calendars from google √
    5. Seeing how I can share/sync/etc…
    4. trying to understand the terminology of davical

    Again really appreciate the help! Liking what I see so far…

  9. Reply Steve Feb 14, 2012 19:59

    OK! I’ve finished everything and ended up settling with Thunderbird + Lightening – Chandler kept crashing on my xp64 system. One last note would be that I had to modify “pg_hba.conf” before setting up the database. I addeded the following lines to the .conf file:

    local davical davical_dba trust
    local davical davical_app trust

    After which, everything was good.

  10. Reply NT Jul 19, 2012 09:56

    This is very good. I just have one question, how to add a “contacts” or “address book” server like this?
    I have Ubuntu 11.10 desktop and need to have it be a server that will share and sync a calendar and contacts to my iPhone. If I add a new contact or calendar event on the Ubuntu computer, I need it to add that to the iPhone. And, the same thing with adding them on the iPhone, I need it to sync back to the Ubuntu computer. Can you suggest a way to add the “contacts’ server part to what you already wrote up? Thank You!

    • Reply Brian René Jensen Jul 19, 2012 17:23

      Hey NT.

      I’m sorry to inform you that this system ONLY supports calendars.

      If you need a contacts-server application you will have to look for another solution for this purpose.