Installing and configuring an imap/pop3 service based on Dovecot

This is the second article of the series about a mail server based on CentOS Linux. It is about how to give people access to their incoming mail over imap/pop3 with Dovecot.

First article: Installing and configuring Postfix

I strongly recommend starting the server setup with it.

First install the parts of Dovecot you need:

yum install dovecot dovecot-mysql
  1. Open the /etc/dovecot/dovecot.conf file in your favourite editor and start editing it without mercy.

1.1. First specify the authorization mechanism (available: login, cram-md5, DIGEST-MD5):

auth_mechanisms = plain

1.2. Specify the dovecot working directory:

base_dir = /var/run/dovecot/

1.3. Enable ssl support (if your dovecot dies at start - try using pem certificates, step 1.3.2.):

ssl = yes
ssl_cipher_list = ALL:!LOW:!SSLv2
ssl_cert=</etc/ssl/certs/server.crt
ssl_key=</etc/ssl/certs/server.key

1.3.1. If you have no security certificates - generate them yourself:

mkdir /etc/ssl/certs/  
cd /etc/ssl/certs/  
openssl genrsa -out server.key 2048  
openssl req -new -key server.key -out server.csr -nodes  
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
openssl pkcs12 -export -out server.pfx -inkey server.key -in server.crt  
openssl pkcs12 -in server.pfx -out server.pem -nodes

1.4. Specify the id of the user Dovecot will knock on the files as:

first_valid_uid = 1150
last_valid_uid = 1150
  • I assume the user was already created.

1.5. Specify where to look for the mail files:

mail_location = maildir:/var/spool/mail/%d/%u

%d - domain
%u - user name

1.6. Password database:

passdb {
	args = /etc/dovecot/dovecot-sql.conf
	driver = sql
}

1.7. User database:

userdb {
	args = /etc/dovecot/dovecot-sql.conf
	driver = sql
}

1.8. Authorization sockets:

service auth {
	unix_listener /var/spool/postfix/private/dovecot-auth {
		user = postfix
		group = postfix
		mode = 0660
	}
	unix_listener auth-master {
		user = vmail
		group = mail
		mode = 0660
	}
	unix_listener auth-userdb {
		user = vmail
		group = mail
		mode = 0660
	}
}

1.9. Socket for the imap login:

service imap-login {
	executable = /usr/libexec/dovecot/imap-login
	inet_listener imap {
		address = *
		port = 143
	}
}

1.10. Location of the imap service:

service imap {
	executable = /usr/libexec/dovecot/imap
}

1.11. Socket for the pop3 login:

service pop3-login {
	executable = /usr/libexec/dovecot/pop3-login
	inet_listener pop3 {
		address = *
		port = 110
	}
}

1.12. Location of the pop3 service:

service pop3 {
	executable = /usr/libexec/dovecot/pop3
}

1.13. Enable logging:

log_path = /var/log/dovecot.log
info_log_path = /var/log/dovecot-info.log

1.14. Configure log compression with logrotate. Create the /etc/logrotate.d/dovecot file and put the following lines into it:

/var/log/dovecot*.log {
	missingok
	notifempty
	delaycompress
	create 666 root mail
	sharedscripts
	postrotate
		/bin/kill -USR1 `cat /var/run/dovecot/master.pid 2>/dev/null` 2> /dev/null || true
	endscript
}
  1. Create the /etc/dovecot/dovecot-sql.conf file. Put the following lines into it:
driver = mysql
connect = host=localhost dbname=mail user=postfix password=password
default_pass_scheme = MD5
user_query = SELECT '/var/spool/mail/%d/%n' as home, 'maildir:/var/spool/mail/%d/%n'as mail, 1150 AS uid, 12 AS gid, concat('dirsize:storage=', quota) AS quota FROM mailbox WHERE username = '%u' AND active = '1'
password_query = SELECT username as user, password, '/var/spool/mail/%d/%n' as userdb_home, 'maildir:/var/spool/mail/%d/%n' as userdb_mail, 1150 as userdb_uid, 12 asuserdb_gid FROM mailbox WHERE username = '%u' AND active = '1'

Keep in mind that postfix and dovecot have to work with the same database.

If the database does not exist - what do you need Dovecot for?

Restart Dovecot so the changes take effect

service dovecot restart

Read on: Installing PostfixAdmin and RoundCube