Tuesday, September 30, 2014

How to configure mail server using postfix, dovecot and mysql supporting virtual domain

1- install necessary packages
root@localhost # apt-get install postfix postfix-mysql dovecot-core dovecot-imapd dovecot-lmtpd dovecot-mysql dovecot-pop3d

If postfix configuration prompted choose "No configuration"

2- mysql configuration
mysqladmin -p create servermail 
mysql -u root -p 
mysql > GRANT SELECT ON servermail.* TO 'usermail'@'127.0.0.1' IDENTIFIED BY 'mailpassword'; 
mysql > FLUSH PRIVILEGES;
mysql> USE servermail;
mysql> CREATE TABLE `virtual_domains` ( `id` INT NOT NULL AUTO_INCREMENT, `name` VARCHAR(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

mysql> CREATE TABLE `virtual_users` ( `id` INT NOT NULL AUTO_INCREMENT, `domain_id` INT NOT NULL, `password` VARCHAR(106) NOT NULL, `email` VARCHAR(120) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`), FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

mysql> CREATE TABLE `virtual_aliases` ( `id` INT NOT NULL AUTO_INCREMENT, `domain_id` INT NOT NULL, `source` varchar(100) NOT NULL, `destination` varchar(100) NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

mysql> INSERT INTO `servermail`.`virtual_domains` (`id` ,`name`) VALUES ('1', 'example.com'), ('2', 'hostname.example.com');
mysql> INSERT INTO `servermail`.`virtual_domains` (`id` ,`name`) VALUES ('3', 'test.qq'), ('4', 'mail.test.qq'); 

mysql> INSERT INTO `servermail`.`virtual_users` (`id`, `domain_id`, `password` , `email`) VALUES ('1', '1', ENCRYPT('firstpassword', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'email1@example.com'), ('2', '1', ENCRYPT('secondpassword', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'email2@example.com');

mysql> INSERT INTO `servermail`.`virtual_users` (`id`, `domain_id`, `password` , `email`) VALUES ('3', '3', ENCRYPT('123', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'email1@test.qq'), ('4', '3', ENCRYPT('123', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'email2@test.qq');

mysql> INSERT INTO `servermail`.`virtual_aliases` (`id`, `domain_id`, `source`, `destination`) VALUES ('1', '1', 'alias@example.com', 'email1@example.com');

3- postfix configuration
  a- change /etc/postfix/main.cf content to the following

append_dot_mydomain = no
readme_directory = no
myhostname = hostname.example.com
smtpd_use_tls=no
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination
#mydestination = example.com, hostname.example.com, localhost.example.com, localhost mydestination = localhost
myhostname = hostname.example.com
virtual_transport = lmtp:unix:private/dovecot-lmtp
virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps.cf
myorigin = /etc/mailname
mydestination = localhost
relayhost =
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all


   b- change /etc/postfix/master.cf content to the following

smtp      inet  n       -       -       -       -       smtpd
submission inet n       -       -       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_sasl_auth_enable=yes
pickup    fifo  n       -       -       60      1       pickup
cleanup   unix  n       -       -       -       0       cleanup
qmgr      fifo  n       -       n       300     1       qmgr
tlsmgr    unix  -       -       -       1000?   1       tlsmgr
rewrite   unix  -       -       -       -       -       trivial-rewrite
bounce    unix  -       -       -       -       0       bounce
defer     unix  -       -       -       -       0       bounce
trace     unix  -       -       -       -       0       bounce
verify    unix  -       -       -       -       1       verify
flush     unix  n       -       -       1000?   0       flush
proxymap  unix  -       -       n       -       -       proxymap
proxywrite unix -       -       n       -       1       proxymap
smtp      unix  -       -       -       -       -       smtp
relay     unix  -       -       -       -       -       smtp
showq     unix  n       -       -       -       -       showq
error     unix  -       -       -       -       -       error
retry     unix  -       -       -       -       -       error
discard   unix  -       -       -       -       -       discard
local     unix  -       n       n       -       -       local
virtual   unix  -       n       n       -       -       virtual
lmtp      unix  -       -       -       -       -       lmtp
anvil     unix  -       -       -       -       1       anvil
scache    unix  -       -       -       -       1       scache
maildrop  unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}
uucp      unix  -       n       n       -       -       pipe
  flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
ifmail    unix  -       n       n       -       -       pipe
  flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r $nexthop ($recipient)
bsmtp     unix  -       n       n       -       -       pipe
  flags=Fq. user=bsmtp argv=/usr/lib/bsmtp/bsmtp -t$nexthop -f$sender $recipient
scalemail-backend unix  -       n       n       -       2       pipe
  flags=R user=scalemail argv=/usr/lib/scalemail/bin/scalemail-store ${nexthop} ${user} ${extension}
mailman   unix  -       n       n       -       -       pipe
  flags=FR user=list argv=/usr/lib/mailman/bin/postfix-to-mailman.py
  ${nexthop} ${user}


  c- create mysql-virtual-alias-maps.cf in /etc/postfix and add the following content
user = usermail
password = mailpassword
hosts = 127.0.0.1
dbname = servermail
query = SELECT destination FROM virtual_aliases WHERE source='%s'


  d- create mysql-virtual-mailbox-domains.cf in /etc/postfix and add the following content

user = usermail
password = mailpassword
hosts = 127.0.0.1
dbname = servermail
query = SELECT 1 FROM virtual_domains WHERE name='%s'





  e- create mysql-virtual-mailbox-maps.cf in /etc/postfix and add the following content
user = usermail
password = mailpassword
hosts = 127.0.0.1
dbname = servermail
query = SELECT 1 FROM virtual_users WHERE email='%s'




4- dovecot configuration
  a- edit /etc/dovecot/dovecot.conf to the following content

dict {
  #quota = mysql:/etc/dovecot/dovecot-dict-sql.conf.ext
  #expire = sqlite:/etc/dovecot/dovecot-dict-sql.conf.ext
}

!include conf.d/*.conf

!include_try /usr/share/dovecot/protocols.d/*.protocol
protocols = imap lmtp pop3

postmaster_address=admin@example.com


  b- edit /etc/dovecot/dovecot-sql.conf.ext to the following content

driver = mysql
connect = host=127.0.0.1 dbname=servermail user=usermail password=mailpassword
default_pass_scheme = SHA512-CRYPT
password_query = SELECT email as user, password FROM virtual_users WHERE email='%u';


  c- edit /etc/dovecot/conf.d/10-auth.conf to the following content

disable_plaintext_auth = no
auth_mechanisms = plain login
!include auth-sql.conf.ext


  d- edit /etc/dovecot/conf.d/10-mail.conf to the following content
mail_location = maildir:/var/mail/vhosts/%d/%n
namespace inbox {
  inbox = yes
}
mail_privileged_group = mail


  e- edit  /etc/dovecot/conf.d/10-master.conf to the following content

service imap-login {
  inet_listener imap {
    port = 0
  }
  inet_listener imaps {
  }
}
service pop3-login {
  inet_listener pop3 {
    port = 110
  }
  inet_listener pop3s {
  }
}
service lmtp {
  unix_listener /var/spool/postfix/private/dovecot-lmtp {
     mode = 0600
     user = postfix
     group = postfix
  }
}
service imap {
}
service pop3 {
}
service auth {
  unix_listener /var/spool/postfix/private/auth {
      mode = 0666
      user = postfix
      group = postfix
  }
  unix_listener auth-userdb {
      mode = 0600
  user = dovecot
}
service auth-worker {
  user = vmail
}
service dict {
  unix_listener dict {
  }
}


  f- edit /etc/dovecot/conf.d/auth-sql.conf.ext to the following content

passdb {
  driver = sql

  args = /etc/dovecot/dovecot-sql.conf.ext
}
userdb {
  driver = static
  args = uid=vmail gid=vmail home=/var/vmail/vhosts/%d/%n
}





5- create user and assign permission
root@localhost# ls -ld /var/mail:
drwxrwsr-x 3 root vmail 4096 Jan 24 21:23 /var/mail

root@localhost# mkdir -p /var/mail/vhosts/example.com
root@localhost# mkdir -p /var/mail/vhosts/test.qq
root@localhost# groupadd -g 5000 vmail
root@localhost# useradd -g vmail -u 5000 vmail -d /var/mail
root@localhost# chown -R vmail:vmail /var/mail
root@localhost# chown -R vmail:dovecot /etc/dovecot
root@localhost# chmod -R o-rwx /etc/dovecot

root@localhost# service dovecot restart
root@localhost# service postfix restart

6- testing and what we get
test email with mysql configuration -- return 1 mean successful
root@localhost# postmap -q email1@example.com mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf

test alias map to the real emal -- return email1@example.com mean successful
root@localhost#  postmap -q alias@example.com mysql:/etc/postfix/mysql-virtual-alias-maps.cf 


so finally we get
- 2 virtual domain: example.com and test.qq
- 1 smtp service listen on port 25
- 1 imap service listen on port 143
- 1 pop3 service listen on port 110


1 comment:

  1. Thanks for sharing, nice post!

    Máy đưa võng hay võng điện cho bé hay vong dua em be giúp bé ngủ ngon mà vong dua tu dong cho em be không tốn sức ru võng của bố mẹ. Võng tự động hay vong ru tu dong cho be chắc chắn, gọn gàng, dễ tháo xếp, dễ di chuyển và may dua vong dễ dàng bảo quản. Lợi ích mà máy đưa võng tự động vô cùng thiết thực.
    Chia sẻ các mẹ không nên cho trẻ dưới 6 tháng tuổi uống nước hay mẹo giúp tăng cường trí nhớ hiệu quả, kinh nghiệm trị tiêu chảy cho bé bằng cà rốt hiệu quả, những thực phẩm giúp cải thiện trí nhớ hiệu quả, bí quyết trị sẹo thâm bằng rau má, chia sẻ kinh nghiệm thực phẩm giúp trẻ thông minh hơn, mẹo hay giúp trẻ thích ăn rau hay cách giúp trẻ hạ sốt nhanh hiệu quả, bệnh viêm khớp không nên ăn gì, một số mẹo giúp giảm độ cận thị cho bạn, bí quyết chống nắng với cà chua cực hiệu quả hay những thực phẩm giúp tóc mọc nhanh hiệu quả, cách giúp bé ngủ ngon giấcthực phẩm giúp bé ngủ ngon mẹ nên biết, chia sẻ cách làm trắng da toàn thân bằng thực phẩm, những món ăn chữa bệnh mất ngủ giúp ngủ ngon, mách mẹ mẹo giúp bé không sốt khi mọc răng hiệu quả
    Những thực phẩm tốt cho da tại http://thucphamtotchoda.blogspot.com/
    Chăm sóc da mặt ở http://chamsocdamato.blogspot.com/
    Cách chăm sóc da mặt bằng http://cachchamsocdamatbang.blogspot.com/
    Dạy trẻ sơ sinh tại http://daytresosinh.blogspot.com/
    Bí quyết giảm cân hay tại http://biquyetgiamcanhay.blogspot.com/

    ReplyDelete