Mail server - Configuring Postfix to use MySQL - part 2

Now we have created the external files Postfix will use when connecting to the 'mail' database, we can carry on and edit the main Postfix configuration file.


main.cf

The main Postfix configuration file is know as 'main.cf'.

Let's go ahead and edit that:

sudo nano /etc/postfix/main.cf

The default entries include the hostname that was set during the Postfix installation.

As such, the bottom part of the file looks like this:

myhostname = mail.demoslice.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination =
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all

You will find the 'myhostname' and 'mydestination' already have the hostname set - you will need to remove any entries for the 'mydestination' field.

Virtual Files

Now we need to add the details of the four files we created in the previous article so Postfix knows to refer to those in any mail execution.

At the bottom of the file add these lines:

virtual_alias_domains =
virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-forwards.cf, mysql:/etc/postfix/mysql-email.cf
virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-domains.cf
virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-mailboxes.cf
virtual_mailbox_base = /home/vmail
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000

proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps $transport_maps $mynetworks $virtual_mailbox_limit_maps

You can see the references to the files we created and also the references to the 'vmail' user we created (where the mail is physically located).

The last line lets Postfix know what to search for in a mail domain to get the relevant details for the database query.

Done.

Summary

Now we have Postfix configured to query the database whenever needed.

Next we need to configure Postfix for our SSL certificate so we can use secure connections when connecting to the mail server.

PickledOnion

Article Comments:

Jesper Christiansen commented Sun Sep 07 19:19:25 UTC 2008 ago:

After I have added the lines under "Virtual Files", then I can't use "mail" to send mail to external adresses anymore - if i remove those lines "mail" works again..

Is that how it should be?

PickledOnion commented Tue Sep 09 16:54:24 UTC 2008 ago:

Hi,

That is not as it should be - we actually use mail later on to test the setup.

You should be able to send mail to external addresses via the 'mail' command.

PickledOnion

Jesper Christiansen commented Tue Sep 09 23:29:41 UTC 2008 ago:

Ok! Great.. Then the error is somewhere there - either in the .cf files or somewhere else.. cause when I add the above lines to my main.cf, my mail stops working..

Thanks :)

Andrew Powell commented Sun Sep 28 11:37:55 UTC 2008 ago:

Like Jasper, since adding the virtual mailbox stuff everything screws up for me. I can revert back to the original way, because I just commented out the original stuff. SMTP works when I revert, but IMAP never worked.

KFCSpike commented Wed Oct 08 20:25:35 UTC 2008 ago:

I have the same problem.

I have tried twice to set this up,checking every entry, but same problem both times.

Checking /var/log/mail.log seems to show an error connecting to mysql.

I can connect to mysql using the mailadmin user in phpmyadmin, so can confirm that user is set up properly.

All I can think of is that I installed MySQL using an earlier article and maybe I have blocked a port needed by this article?

PickledOnion commented Thu Oct 09 10:11:15 UTC 2008 ago:

Hi,

I am not sure what the issue may be. It is true that the article series is based on a fresh Slice but I have gone through it many times and have no issues.

I am not sure what to suggest to be honest.

PickledOnion

KFCSpike commented Thu Oct 09 18:36:52 UTC 2008 ago:

I discovered (after a lot of googling) that the problem was mysql in my set-up was not listening on 127.0.0.1

My error was in /etc/mysql/my.cnf bind-address was set to my slice ip, it should be set to 127.0.0.1

I probably set this to my slice ip when setting up phpmyadmin, but it looks like I can still access phpmyadmin remotely after changing to it to 127.0.0.1 :)

I would suggest others having similar problems try:

sudo nano /etc/mysql/my.cnf

[...] bind-address = 127.0.0.1 [...]

/etc/init.d/mysql restart

Ben Wilson commented Sun Oct 12 16:50:46 UTC 2008 ago:

Okay, I had the loop problem. But I added the following lines to main.cf, and things worked.

virtual_alias_domains =
virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-forwards.cf, mysql:/etc/postfix/mysql-email.cf
virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-domains.cf
virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-mailboxes.cf
virtual_mailbox_base = /home/vmail
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000

proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps $transport_maps $mynetworks $virtual_mailbox_limit_maps

Jeremiah commented Wed Oct 29 18:41:31 UTC 2008 ago:

For anyone else having the same problem connecting to mysql after the previous steps, where removing the last lines fixes it, issuing: sudo /etc/init.d/postfix restart fixed it for me. I'm not sure why it would, but it did.

Jeremiah commented Wed Oct 29 18:57:59 UTC 2008 ago:

Sorry, my last comment was not as clear as it should have been (feel free to delete it).

I was receiving messages in /var/log/mail.log like the following:

Oct 29 18:35:38 disgruntledpeasants postfix/cleanup[5351]: warning: 6E52CA81A4:  virtual_alias_maps map lookup problem for ...

along with mysql authentication errors.

I could access mysql with the user in question via command-line, and could verify that the user had the correct privileges granted to it.

Issuing sudo /etc/init.d/postfix restart solved the issue, although I'm not sure why (guessing that config items conflicted or something).

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)