Friday 28 June 2013

Setup an Email Server on a CentOS VPS



I recently had some issues fun setting up an Email server on a CentOS VPS (Virtual Private Server). I run a CentOS 5.6 + Nginx + Phusion Passenger + Rails setup in ChicagoVPS 1GB Xen plan, and my Rails App needs to send Verification emails.

Here’s how to set one up on a CentOS VPS:

1) Install Postfix
Postfix is the CentOS email server that’s relatively easy to setup and configure. Follow the instructions in the CentOS wiki to setup Postfix:

http://wiki.centos.org/HowTos/postfix

2) Make Adjustments to Run on a VPS – Configure the Virtual Alias Table
Edit the virtual alias table:
$ vi /etc/postfix/virtual

Then add the first 2 lines at the top of the file:

/etc/postfix/virtual
  
1. johnd@yourdomain.com          johnd
2. postmaster@yourdomain.com      johnd
3. 
4. # VIRTUAL(5)                                                          VIRTUAL(5)
5. #
6. # NAME
7. #        virtual - Postfix virtual alias table format

In the above configuration, any email sent to johnd@yourdomain.com and postmaster@yourdomain.com will be forwarded to the linux user johnd. Replace yourdomain.com with the domain name that you registered.

Edit the postfix configuration file:
$ vi /etc/postfix/main.cf

Ensure $mydomain is listed in mydestination:
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

Add the following line if it does not exist:
virtual_alias_maps = hash:/etc/postfix/virtual

In my setup, I found that adding this line caused issues so make sure it’s commented out:
# This line caused issues with email sending/receiving. Comment out.
# virtual_alias_domains = soqlcommander.com

Rebuild the virtual alias db and reload postfix:
$ postfix reload
$ postmap /etc/postfix/virtual

3) Check DNS MX Record
You can skip this section if you only want to send emails.

If you need to receive emails, the sending email server needs to know how to route emails to your domain (yourdomain.com). It does this checking the MX record in yourdomain.com.

    In the control panel for the DNS that you registered, look for the MX record setting, and make sure it points to your server’s IP, or mail.yourdomain.com. On Netfirms, this is in Domain Central > DNS > MX Record
    Check the A records for yourdomain.com. Ensure that mail.yourdomain.com, mx.yourdomain.com all point to your server’s IP address

4) Test

Tail the logs to see what happens when you try to send an email:
tail -f /var/log/maillog

Send an email (perform this test on the CentOS VPS server):
telnet localhost smtp

ehlo localhost
mail from:root
rcpt to:youremail@yahoo.com
data
This is a test message
.

quit

Check the logs and see if there are any errors in sending.

Troubleshooting

If you have trouble sending or receiving emails, check the firewall configuration. For an example firewall configuration, refer to Setup a Firewall (iptables) for CentOS, Rails, Nginx, Phusion Passenger

Source: http://railsforsure.wordpress.com/2012/02/09/setup-an-email-server-on-a-centos-vps/