Sunday, July 19, 2009

Web Mail with Postfix, Dovecot, and Hastymail

I wanted to be able to check my email from anywhere, so I looked into what webmail options there were. A Google search quickly pointed to Hastymail as a good possible solution, so I decided to give it a try. I've been running Postfix and Dovecot together for a long time and I didn't know if I would run into any issues that might be specific to the servers I was using. As it turns out, there were no settings specific to either of those and it couldn't have been easier.

Firstly, Hastymail is written in PHP so I had to install that first. On Ubuntu Server, I just did

# apt-get install php5-cgi
# apt-get install php5-cli

I downloaded Hastymail from http://www.hastymail.org, and untarred it into the document root, which in my case is /www/vhosts/mail.example.com. I also linked the resulting directory to hastymail2 so that I wouldn't have to modify the web server settings if I decided to upgrade hastymail later. I could just point the symlink to the new version.

$ cd /www/vhosts/mail.example.com
$ gunzip -c hastymail2_rc_6.tar.gz | tar xvf -
$ ln -s hastymail2_rc_6 hastymail2

Then I created some directories Hastymail needs.

# mkdir /etc/hastymail2
# mkdir /var/lib/hastymail2
# mkdir /var/lib/hastymail2/attachments
# mkdir /var/lib/hastymail2/user_settings

I changed file ownership to the user that the web server will run as.

# chown -R www-data:www-data /var/lib/hastymail2

There is a config file in the document root called hastymail2.conf.example that comes with it. Copy this to /etc/hastymail2/hastymail2.conf. There were only a few changes I needed to make to get this up and running. First was the url_base variable. Since I am putting the mail application at the root of mail.example.com, I set this to "/".

url_base = /

Secondly, I changed it to use https. I don't know why this isn't set by default.

http_prefix = https

I also changed attachments_path and settings_path to the directories I created earlier.

attachments_path = /var/lib/hastymail2/attachments
settings_path = /var/lib/hastymail2/user_settings

Now all I had to do was run the install script.

# php /www/vhosts/mail.example.com/hastymail2_rc_6/install_scripts/install_config.php /etc/hastymail2/hastymail2.conf /etc/hastymail2/hastymail2.rc

The web server needs to be set up next. I decided to use lighttpd with FastCGI. On Ubuntu (or Debian), you can put your custom configs into /etc/lighttpd/conf-available, then link the ones you want to activate into /etc/lighttpd/conf-enabled. Lighttpd comes with some of these custom configs, and you will need to activate the fastcgi one.

# cd /etc/lighttpd/conf-enabled
# ln -s ../conf-available/10-fastcgi.conf

Also, if you're like me you'll want to activate SSL. The SSL config that comes with lighttpd looks for a certificate called server.pem in /etc/lighttpd, so you'll need to have that. I created a self signed certificate, using instructions at http://www.cyberciti.biz.

# ln -s ../conf-available/10-ssl.conf
# cd /etc/lighttpd
# openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes

My virtual host config was very simple. I put this into a file called /etc/lighttpd/conf-available/90-mail.example.com.conf:

$HTTP["host"] == "mail.example.com" {
# Redirect login page to SSL
$HTTP["scheme"] == "http" {
url.redirect = ( "^/$" => "https://mail.example.com/" )
}
accesslog.filename = "/var/log/lighttpd/mail.example.com-access.log"
server.document-root = "/www/vhosts/mail.example.com/hastymail2"
}

Then I linked to it from the conf-enabled directory.

# cd /etc/lighttpd/conf-enabled
# ln -s ../conf-available/90-mail.example.com

After this when I started lighttpd, I was greeted by the mail login.

No comments:

Post a Comment