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
- A working CentOS server. In this guide I am using CentOS 6.3 x32 (see my playground server)
- Windows users should download Putty while Mac and Linux users can simply use Terminal
- You’ll also need a basic skill to use Putty and to navigate through SSH. Read: Most common Unix commands used to SSH.
- Your server should already has necessary software to host a website. In this case are: Apache, PHP5, and MySQL.
- 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.
You may also need to type this command to go to the root directorty:
cd ~
Step 2 – Basic 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:
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 1 – Edit 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):
Step 2 – Edit 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 3 – Setup 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:
Again, hit Control+O to save then Control+X to exit.
Step 4 – Turn 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:
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
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.
after postmap /etc/postfix/virtual
get
postmap: fatal: bad numerical configuration: debug_peer_level = 2? PATH=/bin:/usr/bin:/usr/local /bin:/usr/X11R6/bin? ddd /usr/libexec/postfix/ & sleep 5
Is it possible to get advice how to add multiple domains? To be able receive mails to multiple domains?
I have an error when I try and run:
postmap /etc/postfix/virtual
I deleted the contents of the virtual file like you said and entered:
user@domain.com
/* */
user\@domain.com
Do you have any ideal on where the dupe is? I am new to Linux…
i want to send mass mails, is there option to break those bulk mails into single mails and queue 1000 recipients to one after the other ?
Hi ..!! i want to send mass mails, is there option to break those bulk mails into single mails and queue 1000 recipients to one after the other ?
There’s should be a way to do that, but I don’t remember how.
I need your help in setting up an email server with ip rotation in centos.
can you help me out.
I’m setting up using postfix 2.6.6 and see many different code inside. Can you help me in tutoring for setting the postfix so it would work as you mentioned here?
hello mom, i’ve got the warning message after enter the postmap /etc/postfix/virtual, the warning message something like this
”
postmap:warning: /etc/postfix/virtual.db: duplicate entry: “/*”
”
can you help me 🙂
Hi andy,
delete all content of /etc/postfix/virtual and save it
To me, it has worked. 😉
Hi,
I am running a meteor.js app on node.js web server, which is working fine but I’d like to be able to add email capability. Your pre-requites say the server has to have Apache, PHP5, and MySQL. Is it possible to use POSTFIX (and this tutorial( on the VPS running node.js web server? The server is a production machine now otherwise, I’d test it myself.
Thanks in advance for the answer as well as this awesome blog. 🙂
Kamal
Sorry but I never use meteor.js so I can’t answer your question. You tell me 🙂
OK, thanks. I did some more digging around the web after I posted my question and looks like it should be possible.
hi, I have centos 5.11 ….for this command :
postmap /etc/postfix/virtual
postman command not found
how could I handle it?
Thank you for the great articles.
Thanks for easy language and step by step description very help full since i am then to :)…..
I have configured my centos based server with cwp – pane.Everything is working perfectly, but the problem is that my server can send mail.but not able to accept incoming messages.What would be the solution for that problem?????
Hi Mom,
I have postfix 2.6.6 and this procedure is not working for me. Please help.
I can’t send emails to my google account
I have problem when add new email in my centos, please help :
Warning: [obfuscated](): Access denied for user ‘postfix’@’localhost’ (using password: YES) in /usr/local/cwpsrv/htdocs/resources/admin/modules/mail_add-new.php on line 0
Warning: [obfuscated]() expects parameter 2 to be resource, boolean given in /usr/local/cwpsrv/htdocs/resources/admin/modules/mail_add-new.php on line 0
Could not connect to database postfix check configuration file: /usr/local/cwpsrv/htdocs/admin/include/postfix.php