Recommended VPS Complete list of best VPS hosting providers.

Install and Configure Postfix on CentOS

How to setup Postfix mail server on CentOS. Installing a web server and MySQL database server itself is not enough (for most websites) because your VPS may not be able to send email. That could be painful as nowadays there is no website with no email function at all. Lets say WordPress, the number one free blogging platform. It will be painful if: your blog doesn’t send you email each time new user registered, new visitor leaves comment, or if you’ve setup a “Contact us” page but it doesn’t even work. Hence, it is important to also install a mail server.

So what is Postfix anyway? It is a free open source software acts as Mail Transfer Agent (MTA) with main job to route and deliver emails from your server. Without such MTA, your site will not be able to send email because your server simply does not have ability to do that.

Prerequisite

  1. A working CentOS server. In this guide I am using CentOS 6.3 x32 (see my playground server)
  2. Windows users should download Putty while Mac and Linux users can simply use Terminal
  3. You’ll also need a basic skill to use Putty and to navigate through SSH. Read: Most common Unix commands used to SSH.
  4. Your server should already has necessary software to host a website. In this case are: Apache, PHP5, and MySQL.
  5. About 15 minutes of your time and a cup of tea if you like.

How To Install Postfix

Step 1 – Login to your server and follow my previous guide about Basic setup for CentOS before you build a live web server. You may and may not follow that tutorial but if you followed, it will give you some basic security tweak to your server.

Before you proceed to the next steps, it is better to explain that all commands in this tutorial are written without the “sudo” prefix. However if you disabled root login and you logged in using another username with root privilege, you can add the “sudo” prefix all by your self. Alternatively you can simply type su, hit Enter and type in your password twice to switch login as root.

switch-root-login

You may also need to type this command to go to the root directorty:

cd ~

Step 2Basic command syntax to install Postfix is:

yum install postfix

It may ask your for confirmation but you can simply type Y (which means Yes) then hit Enter. But sometimes you may also find it already installed as what I found in my VPS:

install postfix

How To Configure Postfix

So you now installed Postfix already but you still have to setup some configurations which are located at /etc/postfix/main.cf file. Use Nano or vi editor to edit that file:

Step 1Edit Postfix main.cf file. In this example I use Nano which is easier for me:

nano /etc/postfix/main.cf

The main first screen looks like this (click it to enlarge):

postfix config file

Step 2Edit Postfix configuration as needed. The config file you are about to edit has all you need to configure Postfix to be up and running as expected. It is a very long file provides all detailed information to make Postfix works as you wish. If you use Nano to edit the file, you can hit Control+V to quickly jump to next page or Control+Y to go back.

Now find following lines. Below are all lines you need to edit and make it similar to what provided below for quick setup. However you can use different value but at least make sure you know what you are doing:

soft_bounce             = no
queue_directory         = /var/spool/postfix
command_directory       = /usr/sbin
daemon_directory        = /usr/libexec/postfix
mail_owner              = postfix

# The default_privs parameter specifies the default rights used by
# the local delivery agent for delivery to external file or command.
# These rights are used in the absence of a recipient user context.
# DO NOT SPECIFY A PRIVILEGED USER OR THE POSTFIX OWNER.
#
#default_privs = nobody

myhostname              = mail.domain.com 
mydomain                = domain.com

mydestination           = $myhostname, localhost
unknown_local_recipient_reject_code = 550

mynetworks_style        = host
mailbox_transport       = lmtp:unix:/var/lib/imap/socket/lmtp
local_destination_recipient_limit       = 300
local_destination_concurrency_limit     = 5
recipient_delimiter=+

virtual_alias_maps      = hash:/etc/postfix/virtual

header_checks           = regexp:/etc/postfix/header_checks
mime_header_checks      = pcre:/etc/postfix/body_checks
smtpd_banner            = $myhostname

debug_peer_level        = 2
debugger_command =
         PATH=/bin:/usr/bin:/usr/bin:/usr/X11R6/bin
         xxgdb $daemon_directory/$process_name $process_id & sleep 5

sendmail_path           = /usr/sbin/sendmail.postfix
newaliases_path         = /usr/bin/newaliases.postfix
mailq_path              = /usr/bin/mailq.postfix
setgid_group            = postdrop
html_directory          = no
manpage_directory       = /usr/share/man
sample_directory        = /usr/share/doc/postfix-2.3.3/samples
readme_directory        = /usr/share/doc/postfix-2.3.3/README_FILES

smtpd_sasl_auth_enable          = yes
smtpd_sasl_application_name     = smtpd
smtpd_recipient_restrictions    = permit_sasl_authenticated,
                                  permit_mynetworks,
                                  reject_unauth_destination,
                                  reject_invalid_hostname,
                                  reject_non_fqdn_hostname,
                                  reject_non_fqdn_sender,
                                  reject_non_fqdn_recipient,
                                  reject_unknown_sender_domain,
                                  reject_unknown_recipient_domain,
                                  reject_unauth_pipelining,
                                  reject_rbl_client zen.spamhaus.org,
                                  reject_rbl_client bl.spamcop.net,
                                  reject_rbl_client dnsbl.njabl.org,
                                  reject_rbl_client dnsbl.sorbs.net,
                                  permit

smtpd_sasl_security_options     = noanonymous
smtpd_sasl_local_domain         = 
broken_sasl_auth_clients        = yes

smtpd_helo_required             = yes

As always, you have to replace / change “domain.com” with your actual domain name and its TLD. In this example I use fikitips.com (my spare domain for testing purpose only). You have to also remove the # symbol in each config line found above.

p.s: In postfix v2.6.6 the configurations may different a bit.

Once done, hit Control+O to save then Control+X to exit.

Step 3Setup postfix virtual aliases database. Issue following command:

nano /etc/postfix/virtual

There should be more lines and text you see. You can then simply delete all text within that file or you may also simply add following line in the very top of it:

user@domain.com   user\@domain.com

of course you have to replace “user” and “domain.com” with your own. Example:

setup postfix

Again, hit Control+O to save then Control+X to exit.

Step 4Turn the Postfix virtual file into a lookup table. Issue this command to create the database required for postfix to work:

postmap /etc/postfix/virtual

It should return with nothing:

postmap postfix

Step 5 – Final command you have to also issue to create create the new file that postfix expects before sending anything out. Type this and hit enter:

touch /etc/postfix/body_checks

touch postfix

That’s it. You are now basically having postfix installed. What’s next? You may also need to install Cyrus, a free software helps your server to manage and organize all mails. I’ll talk about it later.

Keep up!

Next: Install Cyrus and MailX.

19 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

Get more stuff like this
in your inbox

Subscribe and get interesting stuff plus faster updates to your email.