Switching to a virtual private server (VPS) from shared webhosting is a great way to improve performance, stability and security. For the most part, setting up the webserver and database is fairly routine and straightforward. However, setting up an email server is non-trivial.
It’s a lot of work to run a full mailserver (and which is why some webhosting companies recommend that you use a dedicated email service) but there are ways to setup a trivial mailserver that requires minimal maintenance. It simply accepts mail to various domains and then forwards them on to an actual mailserver.
For this example, we’ll have two domains running on our VPS: xxx.com and yyy.com
We’ll also have an “info” address at each domain that will be forwarded to xxx@gmail.com and yyy@yahoo.com
First, install a mailserver. I use the CentOS distribution which has the yum package manager. It’s as easy as:
$ yum install exim
We need to create a file for each domain that we’ll be servicing.
Create a directory to hold our virtual hosts:
$ mkdir /etc/exim/vhosts
Create a configuration file for each domain. Use the same format as the /etc/aliases format.
# file:/etc/exim/vhosts/xxx.com
info: xxx@gmail.com
# file:/etc/exim/vhosts/yyy.com
info: yyy@yahoo.com
We need to tell exim to use these files when delivering mail. Near the top of /etc/exim.conf there will be a domainlist line.
# change...
domainlist local_domains = @ : localhost : localhost.localdomain
# to...
domainlist local_domains = @ : localhost : localhost.localdomain : dsearch;/etc/exim/vhosts
Now exim will accept mail for our domains. To have it forward the mail correctly, add the following “router”. Look for begin routers. The first router is probably “dnslookup”. After this, add the following:
virtual:
driver = redirect
domains = dsearch;/etc/exim/vhosts
data = ${lookup{$local_part}lsearch{/etc/exim/vhosts/$domain}}
no_more
Restart (or start) exim to have these changes take effect.
$ /etc/init.d/exim restart
You have have a trivial mailserver. However, you need to specify that your server handles the mail for your domains. This is defined in the MX (Mail eXchange) record. To view the current setting:
$ dig -t mx xxx.com
If the wrong address is returned (or no address) then you need to update your domain name servers. (Whoever provides you with domain name servers - typically your VPS provider or domain registrar - they should be able to set up MX records for you, or provide you with a tool so you can do this yourself.)
If you view the exim log file, you can watch mail being processed.
$ tail -f /var/log/exim/main.log
If mail is not being routed directly, or you want to see more info, the debugging feature of exim is helpful. You can activate this by passing in ‘-v’ flag when starting exim.
$ exim -v -bd
The process will not disconnect from the terminal, so you can see how exim processes mail as it arrives.
To make sure that exim is started as part of the boot process, you can mess with the symbolic links under /etc/rc.d or use this helper tool:
$ chkconfig exim on
If you’re looking for a good VPS, I highly recommend Slicehost. Their 256MB slice is $20USD/month is amazing. I love it.
Update: In mid-2010 I switched over to Linode. I love it even more. Much more bang for the buck.
Permanent link to this post: http://xinsight.ca/blog/how-to-setup-a-trivial-forwarding-mail-server-on-a-vps/
Older: Tips for configuring Apache's mod_rewrite
Newer: BikeFixTO: iPhone app source code for sale