Debian Setting up a Postfix and Dovecot mail server

By | December 8, 2008

Ok over the weekend I spent some time to finally set up my personal mail server on the VPS system I have. I had been planning this for some time, and made several unsuccessful attempts. But this weekend after spending some hard time on it I finally got it working.

My setup is as follows:

  • Postfix as a SMTP service
  • Dovecot as the IMAPS service and authentication service
  • MySQL as a storage location indicating the virtual email boxes and domains
  • PostfixAdmin as a package to manage the virtual domains and boxes

Installing the whole thing

The first thing you might wanna do is install the needed services and applications on your Debian machine. You can install everything using:

apt-get install dovecot-common dovecot-imaps postfix postfix-mysql

At this point I am asuming you already have MySQL installed and configured. If not then you should do so, but I won’t include that in this tutorial. The PostfixAdmin needs to be downloaded from http://sourceforge.net/projects/postfixadmin/. You will need to setup this somewhere on the webserver site, either in an already configured apache directory or by configuring a new one.

Setting up everything in MySQL

After everything is installed you will need to prepare everything in MySQL. Connect to your machine and run the following code to generate a database and tables in that database:

create database postfix;
grant all on postfix.* to postfix identified by 'postfix';
grant all on postfix.* to postfixadmin identified by 'postfixadmin';
set password for postfixadmin = old_password('postfixadmin');

This will setup the basic stuff for postfix and postfix admin to work. Next step is setting up the data for postfix. First configure the postfixAdmin plugin by setting some of the settings in ‘config.inc.php’.

Setup Postfix configuration

The next step is configuring Postfix to enable it to sent mails, use the MySQL database and login using the Dovecot application. The latter you will setup in a later stage. First open the ‘/etc/postfix/main.cf’ file and change the entire content to something like:

queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/lib/postfix
mail_owner = postfix
## Host name given by ISP
myhostname = <isp hostname>
mydomain   = <domainname>
unknown_local_recipient_reject_code = 550
debug_peer_level = 1

# Virtual domain administration MySQL
virtual_alias_maps      = mysql:/etc/postfix/mysql_virtual_alias_maps.cf
virtual_gid_maps        = static:104
virtual_mailbox_base    = /usr/postfix/
virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf
virtual_mailbox_limit   = 51200000
virtual_mailbox_maps    = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
virtual_minimum_uid     = 104
virtual_transport       = virtual
virtual_uid_maps        = static:104

# The settings for the SASQL authentication using the autdaemon.
smtpd_recipient_restrictions =
   permit_mynetworks,
   permit_sasl_authenticated,
   reject_unauth_destination,
   reject_unauth_pipelining,
   reject_invalid_hostname,
   reject_rbl_client list.dsbl.org,
   reject_rbl_client sbl-xbl.spamhaus.org
smtpd_sasl_auth_enable           = yes
broken_sasl_auth_clients         = yes

# Setup authentication using Dovecot
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth

In the above configuration you need to manually change the domain name to your primary domain and the ISP domain to the name given by your ISP. Another thing you must change is the number 104 this should be the UID of the postfix user on your system. You can find out what the UID is by running: 

 id postfix

Setting up the Postfix SQL files

The last step in the postfix configuration is setting up the SQL files that instruct postfix on how to load the data from the MySQL database. Run the commands as instructed below.

:> vi /etc/postfix/mysql_virtual_mailbox_maps.cf

user     = postfix
password = <password>
hosts    = <ipaddress>
dbname   = postfix
query    = SELECT maildir FROM mailbox WHERE username='%s' AND active = 1

:> vi /etc/postfix/mysql_virtual_domains_maps.cf

user     = postfix
password = <password>
hosts    = <ipaddress>
dbname   = postfix
query    = SELECT domain FROM domain WHERE domain='%s'

:> vi /etc/postfix/mysql_virtual_alias_maps.cf

user     = postfix
password = <password>
hosts  = <ipaddress>
dbname = postfix
query  = SELECT goto FROM alias WHERE address='%s' AND active = 1
 

Change the password and the host name to the ones as configured in your MySQL server. If everything went correct your postfix should be up and running by now. Please note that the logon when sending mail does not work yet as dovecot has not yet been configured.

Configuring Dovecot

Your last step should be easy. Edit the dovecot.conf file into the following.

protocols = imaps
log_path  = /var/log/dovecot_log
log_timestamp = "%Y-%m-%d %H:%M:%S "

default_mail_env      = maildir:/usr/postfix/%d/%n
mail_privileged_group = mail
first_valid_uid       = 100

protocol imap {
  imap_client_workarounds = outlook-idle
}

##
## Authentication processes
##
auth_username_chars = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890.-_@
auth default {
  mechanisms = plain login
  userdb sql {
    args=/etc/dovecot/dovecot-mysql.conf
  }
  passdb sql {
    args=/etc/dovecot/dovecot-mysql.conf
  }
  socket listen {
    client {
      path = /var/spool/postfix/private/auth
      mode = 0660
      user = postfix
      group = postfix
    }
  }
}

This simple configuration will enable dovecot to use IMAPS (you could also add POP or IMAP). The auth default group defines how to authenticate the user. In our case this is using the MySQL database to locate passwords and the directory of the user. We also define a socket in this group that indicates a location where Dovecot should offer the authentication service for other application, being Postfix in our case.

The last step for the dovecot setup is creating the query to find the data. Run the following command and then paste the code below:

:> vi /etc/dovecot/dovecot-mysql.conf

driver = mysql
connect = host=<host> dbname=postfix user=postfix password=<password>
default_pass_scheme = CRYPT
password_query = SELECT password FROM mailbox WHERE username = '%u'
user_query = SELECT maildir, <uid> AS uid, <uid> AS gid FROM mailbox WHERE username = '%u'

You should change the host, password and UID. The UID is the one you previously obtained for postfix.

If everything is done correctly you can start adding domains, mailboxes and aliasses using PostfixAdmin. As well as starting the Postfix and dovecot service. You can do this by running:

:> /etc/init.d/dovecot start
:> postfix start

And you’re all done. Congratulations you now have a mail service running on you Debian machine. If you have any difficulties please let me know and please post a snippet of the ‘/var/log/mail.info’ to help me understand what’s going on. Trust me that I’ve probably seen most of the errors that you can encounter come by.

2 thoughts on “Debian Setting up a Postfix and Dovecot mail server

  1. Pingback: Narnio » » Setting up a mailserver, continued

  2. Jongerius Post author

    Update: Added the postfix-mysql in the apt-get statement as this is needed for postfix to support MySql interaction.

    Reply

Leave a Reply