Setup & Configure Postfix Email: VPS BIBLE




new password?
login
X

Prepare Linux Server for Email with Postfix

configuring Postfix for email functionality - image

In this tutorial we’ll configure an email function for the VPS. That’s not to turn this Linux system into a mail server .. but to allow our web apps to be able to send and automate outgoing email.

If you recall the introductory post to this series – Set Up an Unmanaged VPS (4 Newbies) .. The V-P-S Bible – I said the aim was to set up the fastest possible web server, while retaining options to strap on other functionality later on. For most of us, the most likely addition would be a mail server. But.

Setup Unmanaged VPS: The Ubuntu-Nginx Guide

Take your virtual private server from zero to hero

  .. from blank box to cute-as server ..  

with this easy-to-follow copy/paste guide.

22+ parts with video, here’s the index.

But. A mail server takes up valuable resources. SMTP, for example, eats RAM. Which slows down your sites. OK, we could set up POP3 and retain greater efficiency. Then again, we could just use Google Apps, and focus this box 100% towards our web server solution.

…and that’s my plan, for now; retaining the option to add a mail server later on, yet allowing our web apps a mail handling capacity. So we need Postfix for that, and some configuration.

Video: Enable Email Functions

Watch the, er, guvideo for a better idea of how to do this.


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Change the Hostname

To see your hostname, at the CLI, type:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

…and hit return. The name displayed is probably not what you’re after. Let’s change it.

I’ve got a domain hanging about, gathering dust, so I’ll use that as an ongoing example. It’s waywiderweb.com. For the mail function, it would be mail.waywiderweb.com, and so that is what I will call my hostname. You do likewise, substituting waywiderweb for your domain name.

We’ll change the hostname in 2 files, and then change the Reverse DNS to match. For the first file we’ll use the echo command:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

.. swapping your hostname for mine.

The Echo Command

.. figured I’d introduce this handy command, used to parse a string in various ways.

Take our example command above:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

In this case we’re inserting the string “mail.waywiderweb.com” into a file.

One important note about using the > symbol ..

  • > The ‘greater than’ symbol deletes file content, replaced with the new string
  • >> Using 2 of these appends file content, adding the new string to the end

.. So if in doubt, use a text editor like Nano instead.

For more on “echo” type man echo.

(Er, “man” is short for manual and works for most commands.)

For the second hostname change we’ll use Nano:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

.. replacing the single instance of the existing hostname with mail.waywiderweb.com.

If your Linux distro doesn’t have a line with the hostname, no worries, just add the line and ..

.. it should look something like this…

edit hosts file image

By the way, if your hosts file doesn’t contain those last 7 lines, don’t worry about it.

Set the Reverse DNS (RDNS)

Now for the reverse DNS. For this, using your web browser, head to your VPS manager.

With Linode, for example, go to https://www.linode.com/members/, log in, then click on your Linode, then on Network, to find a link to Reverse DNS.

For Slicehost, after logging into the Slicemanager, you click the DNS tab, then the Reverse DNS link. Other unmanaged VPS providers will have similar control panels.

Clicking on the Reverse DNS link, fill the field with the desired hostname. So I type in mail.waywiderweb.com, click OK and confirm, and can see the RDNS is now set to mail.waywiderweb.com.

after changing the RDNS with Linode image

Perform a quick reboot:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Logged back in, now type:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

That’s better, huh?

To check the RDNS, do this:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Then, replacing mine with your IP address:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Provided the output contains the following, but with your IP, we’re fine:-

checking RDNS is properly set image

…if it doesn’t, make some tea and try again. The RDNS may take a little time to propogate.

Install Postfix

Type:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Prompted, choose Internet Site, as shown in the image at the top of this post.

Then enter your hostname:-

entering your hostname in Postfix configuration - image

Now we’ll redirect mail from root to our username, in my case guvnr:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

…and where you see this line…


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

…change “some_value” to your username, like this in my case…


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

configuring Postfix to redirect email - image

Reload the aliases database:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Edit the Postfix configuration:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

…and replace 3 lines, substituting waywiderweb for your domain:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Refresh Postfix:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Test it by sending yourself an email, with your email address:-


Please sign up for automatic premium content access.

Just $15/year* will save you hours, maybe days!

Alternatively, the vpsBible forum is free and you are welcome.

Go to your email client, or Gmail or whatever, and check for that.

OK, that’s outgoing email sorted for your server, so you’ll be able to send email from your web applications, for each and every domain using the name of each domain *. For incoming email, we’ll be setting up Google Apps later in this series, which is free, easy, saves time and your system resources, and can be popped or smtp’ed to your local email client, using your own domain name. Very splendid.

* ie, catch-all@somedomain.com as well as another-catch-all@anotherdomain.tld, as many domains as you want, independent of each other regardless of you RDNS setting. Nice, huh!

Next up…

In the next section Install PHP5 & MySQL – The One-Stop FastCGI Index Page, as well as setting up MySQL for your databases we’ll look at the options for setting up PHP to work with Nginx, plus how to extend that solution with added security and caching. If that sounds complex, don’t sweat it, it’s pretty clear and I’ll steer you in the right direction.

Meantime, here’s the index…

Setup Unmanaged VPS: The Ubuntu-Nginx Guide

Take your virtual private server from zero to hero

with this easy-to-follow copy/paste guide.

“My local PC runs Windows” Show me for Linux

22+ parts with video, here’s the index ..

Manage Unmanaged VPS: Ubuntu-Nginx Administration

Already set it up? We’d best maintain it then.

Toggle to the ..  Ubuntu-Nginx Admin Index

Manage Unmanaged VPS: Ubuntu-Nginx Administration

Maintain your virtual private server with ..

.. cheatsheets, tutorials, tips & guides.

Head back to the ..  Ubuntu-Nginx Installation Index

Nginx Admin: In the Works ..

This lot’s marked for addition already:-

  • Setup or Edit DNS using Bind
  • Network Tools Troubleshooting Guide
  • The Comprehensive Permissions Guide
  • Configuring Nginx Rewrites
  • Custom Website Error Page
  • Setting up Cron Jobs
  • Rsync for Incremental Remote-to-Local Backup
  • Cron & Rsync for Automatic Backup
  • Cron & mysqldump for Auto DB Backup
  • Safeguard Bandwidth with Hotlink Protection
  • Block Access with Nginx’ IP Deny



Got a Question? Want to Comment?

This site has no comments system. We've got a better system!

Please use the link at the top of the post and goto this topic's forum thread:-

  • to help build community
  • for a superior knowledge base
  • to keep info in one place, not diluted between forum & comments.

For those posts ported from Guvnr.com (pre-Feb/2010), archive comments remain.

Handy info from these comments is in the process of being ported to the forum and attributed to the original author.

If you think this idea sucks, let me know.

Thank you.

  • Josh Sanders May 26th, 2009 at 8:38 am

    Hello

    Do you know how I can turn VPS into a mail server wit Postfix and webmin?

    I am a Newbie

  • the_guv May 26th, 2009 at 11:24 am

    @Josh,

    I do know how, but boy it’s a lot of work to set out.

    I may write a tutorial on turning a VPS into a mail server, but the current series concentrates on the fastest, most secure web server, and a mail server spoils that, and is generally unnecessary with Google Apps…have you considered GA?

    …maybe in a couple of months, that guide. A few more enquiries like yours and it’s more likely.

    Meantime, the almighty Google is my suggestion.

  • dan May 29th, 2009 at 1:00 am

    Awesome information you have here. Thanks for sharing

  • Dale Lukas Peterson July 6th, 2009 at 3:58 am

    Guv, your tutorials are awesome, I can follow them almost to the letter and everything works, except for one step in this tutorial, which didn’t work on my VPS host (fivebeans.com). The step that didn’t work was the mail command from within the interactive php, I got this error:

    postdrop: warning: unable to look up public/pickup: No such file or directory

    Here is what I did to fix it:

    sudo mkfifo /var/spool/postfix/public/pickup
    sudo /etc/init.d/postfix restart

    Additionally, by default it seems that Ubuntu 8.04 has sendmail installed and up and running, so I had to find that process and kill it, and for good measure I also did an aptitude purge on all the sendmail packages.

    This thread details what is needed to get this all straightened out:
    http://ubuntuforums.org/showthread.php?t=666018

  • the_guv July 7th, 2009 at 12:03 am

    @Dan .. tx to you.
    @Dale. Appreciate that.
    Sure, Linux distributions may vary a bit between VPS providers.

  • Ryan in Austin July 30th, 2009 at 1:15 am

    Excellent tutorials!

    Would I be correct in saying that changing my hostname to mail.xxx.com and reverse DNS literally changes my machine to that name?

    I’m confused because many resources say to use the fqdn, but then if you have several virtual servers running, then your hostname should be localhost or something random … I don’t know, maybe the guvnr could clarify for us noobs?

    Thanks for the great information!

  • the_guv August 2nd, 2009 at 2:26 pm

    @Ryan .. thank you. Here’s some basics ..

    A hostname is a machine’s name, so it can be identified on a network like, say, the web. It can be changed.

    For the computer to talk to itself (to loopback) it uses another, standard hostname called localhost, with the IP address 127.0.0.1

    Reverse DNS translates an IP address into a hostname. In this case, it will help outgoing email being considered legit and not spam.

    A FQDN is a complete DNS name. Say, you’ve got the domain somesite.com, and strapped on the sub-domain blog, then the FQDN to your blog would be blog.somesite.com. (including that last dot, tho it’s often left out).

    .. Equally, mail.somesite.com is a FQDN, but may just happen to be your machine’s hostname as well.

    What are you actually wanting to achieve? Lemme know and I’ll try to really confuse the issue :)

  • trav 19 August 3rd, 2009 at 4:54 am

    hello guv! my vps hosts file has the hostname pointed to localhost like so:
    127.0.0.1 ubuntu localhost localhost.localdomain

    all in one line. should i point “ubuntu” to a different ip like the one in your example (which was 127.0.1.1)?

  • the_guv August 3rd, 2009 at 12:53 pm

    @Trav 19 .. aye, change to that for your hostname.

    *Unless you’ve got a permanent IP address*, in which case use that instead of 127.0.1.1.

  • angel gomez August 26th, 2009 at 1:16 am

    Hello guv!
    Thanks for your time doind these tutorials. I am just starting with all this stuff. I am pretty newbie.

    So far, I have followed all your instructions. I have one question:
    When I enter the commands:

    php -a
    mail (‘your@email.com’, “Test from my VPS”, “Hey, don’t forget to fix an RSS or email feed from guvnr.com!”);
    exit ();

    and then I checked in may mail box, I received the email from:
    myuser@mail.mysite.com

    I was expecting receiving from:
    myuser@mysite.com

    Is something wrong with my configuration?

    Sorry, if this question is very newbie.

    Thanks a lot again!!!

  • the_guv August 26th, 2009 at 12:23 pm

    hey Angel, tx for your comments.

    That’s fine. This is just a generic test to show that the mail function works. As long as you got the mail, you know that, say, when someone sends a mail through one of your sites, you’ll get those too.

    You could build a mail server proper on the VPS too but, for most sites, Google Apps is better as it won’t drain your resources. So what we have here is the bare bones so you never miss email sent thru your sites. (When you reply to those, you’ll be doing so via GA, if you follow the Bible’s method.)

  • angel gomez August 26th, 2009 at 5:23 pm

    Thanks guv for your answer.

    I am planning to have two or more web pages on my IP using virtual hosts: mysite.com and mysite2.com (or mysite.org).

    Can I generate separate: mail.mysite.com, mail.mysite2.com (or mail.mysite.org) following these instructions or do I need to do something especial?

    I really appreciate your time writing these tutorials and answering our questions.

  • Patrick August 26th, 2009 at 6:02 pm

    After
    sudo nano /etc/hosts
    I got this

    127.0.0.1 localhost localhost
    194.145…….. vps104.vps.nl vps104

    Can i change the IP to mail.blognetwerk.net ?

    Thanks i am very happy with your site.

  • angel gomez August 26th, 2009 at 6:39 pm

    Thanks guv.

    Also, I would like to have two sites:
    mysite.com and mysite2.com (or mysite.org)

    Can I have two separate: mail.mysite.com and mail.mysite2.com (or mail.mysite.org) following these instructions or do I need to do in a different way?

    I realle appreciate your help and time writing the tutorials and answering our questions.

  • the_guv August 27th, 2009 at 11:48 am

    @Patrick .. dude, that’s all covered in the guide, word-for-word! follow it and you will have a rock solid solution .. at least by the time i finish writing the VPS Admin section :P but basically, yes.

  • Ashesh August 27th, 2009 at 2:15 pm

    Guv, thanks again for such exhaustive tutorial.

    I have got a problem. After clicking Reverse DNS link on the next page it askes for “hostname Lookup” search box and following text is shown:

    “The hostname you enter must have a valid A/CNAME record pointing to the IP you want to set the reverse DNS for. The A/CNAME record must be propagated and working before we will be able to find it. You may also enter the IP address if you don’t want an RDNS entry for that IP.”

    When enter the domain name I have entered so far, it says No match was found for “xxxxxxxxxx”. Reverse DNS must have a matching forward entry for one of your IPs.

    I have followed your tutorial word by word.

  • Ashesh August 27th, 2009 at 4:38 pm

    Hi Guv,

    It is possible that because the mail.domainname.com I am using is already in use and pointing to another server?

    Anyways I am skipping this part for now and will work on it again once I have change the DNS of domain to linode NS

    thanks

  • Ashesh August 27th, 2009 at 8:44 pm

    Hi Guv,

    It worked. I changed the DNS at registrar to point to linode NS

    Thanks

  • the_guv August 28th, 2009 at 1:39 pm

    @Ashesh .. 3 possibilities there. Either you’ve not properly set up your hostname, as explained here, else your Domain Zone, but also it can take a little time (some hours sometimes, maybe a day or so with some VPS providers, I guess) for those records to propagate before you can input a reverse DNS record.

    Allow a little time for the RDNS propagation, folks.

  • felipe August 29th, 2009 at 3:07 am

    Hi guv,

    Thanks a lot for your tutorials.

    I am creating two websites:

    mywebsite.com and mywebsite2.com (or mywebsite.org).

    How can I have two separate:

    mail.mywebsite.com and mail.mywebsite2.com (or mail.mywebsite.org)?

    Can I do that following the instructions here or do I need to do something different?

  • the_guv August 29th, 2009 at 2:13 pm

    @Felipe and @Angel,

    Tx for cmts .. Very good question. (Apologies for delay answering.)

    You need (and only can, per server) set only the one RDNS/hostname. Essentially, along with the instructions in this post, this will give your VPS the capability of email. Then, when you set up however many websites, whether using a mail server or Google Apps, your various domains’ email will resolve via their individual address, such as whatever@thissite.com or somethingelse@thatsite.net.

    This is standard stuff. So, the RDNS/hostname is simply a reference point, but is not necessarily the name used for individual domain emails. Indeed, using exactly this Bible, Google Apps and this Postfix solution, I’ve got a dozen websites/blogs running, all with catch-all email accounts using their specific domain names. Nice and tidy, totally professional.

    Probably I should make this clear at the top of the tut. Bet there’s been a lot of folks with a similar concern. Think I had that worry myself with my first web server setup.

  • felipe August 29th, 2009 at 2:49 pm

    Thanks a lot !!!! I really apreciate your effort and time helping us.

  • Patrick August 31st, 2009 at 8:45 pm

    The Guv

    Yessss it works after I try it again and again :D

  • the_guv September 1st, 2009 at 5:54 pm

    @Patrick .. jolly fine. Fiddly init! Good to hear.

  • agus September 2nd, 2009 at 7:51 pm

    Hello Guvs,

    I just changed the host to mail.mydomain.com but im stuck on the reverse ip part. I am having the same problem as comment #16.

    My domain name dns is currently hosted at zoneedit. Maybe you can help directing how I can use Linode along with Zoneedit.

    I am running a pretty busy site (at least thats how I see it, around 7000 to 8000 visitors a day), and I really cant afford the down time.

    Thank you, and you are a God sent as someone have previously mentioned somewhere on your site… :)

  • agus September 4th, 2009 at 6:19 pm

    Hi Guv,

    It’s me again, I have successfully created some virtual hosts per your tutorial… they worked flawlessly… expect for my main site that is hosted with zoneedit….

    Anyway my main site is a drupal site, and I was wondering if you could teach me backup and migrate my drupal site.

    I have checked everywhere about this and found it to be quite daunting, and they dont have your kind of tutorial, which if followed, guaranteed to work like a charm.

    Thanks!

  • Agus September 9th, 2009 at 7:56 am

    Hi Guv,

    Everything is good now… thanks! btw when we installed xcache, that goes for all the other site too right?

    I found even Xcache is not enough if Mysql is not optimized. I optimized mysql setting and everything seems to fly now… I am so happy… :D

    What about writing tutorial on how to optimize Mysql? Everyone will surely appreciate this… :D

    Thanks Guv! Gbu…

    Agus

  • Dave September 12th, 2009 at 3:28 am

    I cannot get Linode to recognize the Reverse DNS (I have tried 3 times from the beginning! Deleting everything and starting square 1) and I still cannot manage to get it :( .

    It says:

    “Lookup Results

    No match was found for “mail.[myweburl].com”. Reverse DNS must have a matching forward entry for one of your IPs.”

    Of course [myweburl] is the actual url. This is after I search for the reverse DNS in Linode. I skipped the step on my first time around and I was able to send emails no problem and everything else worked with no red flags, but I am sure there is a reason for this, so any help would be greatly appreciated!

    Thanks,

    Dave

  • OZKAN September 13th, 2009 at 1:19 am

    is ther any french speeking and experimented VPS people in this communuty able to help me to solve mail server setting ?

  • the_guv September 16th, 2009 at 8:25 pm

    @Agus .. you can find full backup instructions, both for files and db, whether to Windoze or Linux, in the VPS Admin section.

    Optimizin’ MySQL, that is a good idea. Ha, on the list, along with the rest!

    Sorry for delay .. holiday’ed out.

    @Dave .. sounds like a classic case of the RDNS taking a while to propagate. Time it takes varies from host to host.

    Lemme know if it’s not sorted now.

    @Ozkan .. good luck to you. Anyone?

  • Yendis September 20th, 2009 at 1:59 pm

    Hi Guv, when I do dig -x XX.XXX.XXX.XX, I get the right IP in the answer section but where in your example it displays your domain name, I get something else. Is this significant?

    ;; ANSWER SECTION:
    XX.XXX.XXX.XX.in-addr.arpa. 86400 IN PTR li97-190.members.linode.com.

  • the_guv September 23rd, 2009 at 10:16 am

    @Yendis .. LOL! Yes, that’s significant. Basically, it’s wrong .. so retrace your steps, you’ve missed something out, very likely in the hostname steps.

  • Pep Talk - GUVNR November 30th, 2009 at 9:04 pm

    [...] a recap, take a peek:- Set Up Unmanaged VPS (4 Newbies) – Part 10: Prepare Linux Server for Email with PostfixPart 10: How to install an email function for the Linux server, installing Postfix, changing the [...]

  • Alborz December 1st, 2009 at 7:14 pm

    After I modified both hostname and hosts files to my own domain names, by ‘hostname -f’ I got: ‘Unknown’ error. I had a chat with olly and it turned out after modifying the host name, you gotta reboot for it to take effect.

  • the_guv December 1st, 2009 at 7:28 pm

    cheers Alborz ..

    yes, and bear in mind it can take a little while for the hostname change to propagate. how long varies from webhost to webhost

  • kevinayP December 16th, 2009 at 2:12 am

    Hi Guv,

    First of all, thank you so much for the extensive tutorial, really appreciated everything you did. :D

    Before reading this tutorial from scratch, I already did set up a Google Apps account and connected it with my domain, verified it, changed the MX records, and get everything (including mail) working. That’s actually the first thing I would do once I get a domain name. :)

    Now that all of my mail is managed from Google Apps, should I still install Postfix and if I do, can I change the hostname from “mail.xxxx.com” into just “xxxx.com”? Will that affect something? :o

    Thanks,
    Kevin

  • Philip Churchill January 1st, 2010 at 12:50 am

    I have the same question as Kevin above as Google apps is already setup.