.

Set Up Unmanaged VPS (4 Newbies) – Part 10: Prepare Linux Server for Email with Postfix

English   English (change)

Set Up Unmanaged VPS (4 Newbies) – Part 10: 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.

[sniplet guvSellBox]
[sniplet vpsIndexSell]

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.

[sniplet video]

Change the Hostname

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

[text]hostname -f[/text]

…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. Let’s open:-

[text]sudo nano /etc/hostname[/text]

…replacing the hostname (for me it’s ubuntu) with the new one mail.waywiderweb.com.

And do the same with:-

[text]sudo nano /etc/hosts[/text]

edit hosts file image

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

It’ll look something like this…

…If it 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, goto the site and click on the Members tab, 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:-

[text]sudo reboot[/text]

Logged back in, now type:-

[text]hostname -f[/text]

That’s better, huh?

To check the RDNS, do this:-

[text]sudo aptitude -y install dnsutils[/text]

Then, replacing mine with your IP address:-

[text]dig -x 97.107.129.135[/text]

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:-

[text]sudo aptitude -y install postfix telnet mailx[/text]

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:-

[text]sudo nano /etc/aliases[/text]

…and where you see this line…

[text]root: some_value[/text]

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

[text]root: guvnr[/text]

configuring Postfix to redirect email - image

Reload the aliases database:-

[text]sudo newaliases[/text]

Edit the Postfix configuration:-

[text]sudo nano /etc/postfix/main.cf[/text]

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

[text]
- Old: myorigin = /etc/mailname
- New: myorigin = $mydomain

- Old: mydestination = mail.waywiderweb.com, localhost.waywiderweb.com, , localhost
- New: mydestination = $waywiderweb, localhost.$waywiderweb, localhost

- Old: mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
- New: mynetworks = 127.0.0.0/8
[/text]

Refresh Postfix:-

[text]sudo /etc/init.d/postfix restart[/text]

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

[text]
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 ();
[/text]

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 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.

Next up…

In the next tutorial Part 11: Nginx (better than Apache) Web Server on Your VPS, we’re getting pretty hot under the collar as we install one of the core modules in our super-sexy virtual private server, Nginx. And from there, we’re pretty much walking downhill all the rest of the way.

Meantime, here’s the index…

[sniplet vpsIndex]

.