Hướng dẫn cài đặt mail server với Postfix, Dovecot, ViMbAdmin, RoundCube trên Centos 7

Lý do bạn áp dụng cài đặt mail server:

  1. Hỗ trợ nhiều domain như lcdung.top, rtcamp.com, google.com, …
  2. Hỗ trợ quản lý nhiều tài khoản mail ảo như admin@lcdung.top, rahul@apple.com, steve@rtcamp.com, … Tài khoản ảo này chỉ login mail server để gửi mail chứ không dùng như tài khoản ftp nhé.
  3. Hỗ trợ quản lý alias  như mail gửi cho you@example.com cũng sẽ forward tới admin@lcdung.top)
  4. Hỗ trợ giao diện web để quản lý tài khoản mail, domain mail, alias và nhiều thứ nửa bới Vimbadmin.
  5. Hỗ trợ giao diện webmail để người dùng có thể đăng nhập gửi mail, nhận mail, đổi password một cách dễ dàng bời Round Cube.

1. Cài đặt các packed postfix, dovecot, mysql

yum -y install postfix postfix-mysql dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd dovecot-mysql mysql-server dovecot-sieve dovecot-managesieved

Nếu mail server của bạn đã cài đặt packed nào thì bạn có thể bỏ qua những packed đó. Lưu ý là server của mình đã cài đặt PHP-FPMNginx

2. Cấu hình Postfix

Bạn có thể kiểm tra version của mình bằng command:

postconf mail_version

Version mình đang cài là mail_version = 2.10.1

Cấu hình Postfix tại folder “/etc/postfix/” qua 2 file: main.cfmaster.cf. Và một số file cấu hình virtual

2.1 Cấu hình Postfix master.cf

File: /etc/postfix/master.cf

Nếu bạn sử dụng với SSL thì dùng smtp port 465, với TLS thì dùng smtp port 587. Bạn cần bỏ comment và cấu hình theo các dòng sau:

# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (yes)   (never) (100)
# ==========================================================================
smtp      inet  n       -       n       -       -       smtpd
#smtp      inet  n       -       n       -       1       postscreen
#smtpd     pass  -       -       n       -       -       smtpd
#dnsblog   unix  -       -       n       -       0       dnsblog
#tlsproxy  unix  -       -       n       -       0       tlsproxy
submission inet n       -       -       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING
#smtps     inet  n       -       -       -       -       smtpd
#  -o syslog_name=postfix/smtps
#  -o smtpd_tls_wrappermode=yes
#  -o smtpd_sasl_auth_enable=yes
#  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING

Lưu ý: Mail server của mình không mã hóa SSL và TLS nên đã comment smtps. Nếu dùng thì bạn bỏ comment nhé

 

2.2 Cấu hình Postfix main.cf

File: /etc/postfix/main.cf

Bạn có thể xóa hết và cấu hình theo chuẩn của mình:

# Change postfix TLS parameter to use dovecot 
#smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
#smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
#smtpd_tls_cert_file=/etc/ssl/certs/dovecot.pem
#smtpd_tls_key_file=/etc/ssl/private/dovecot.pem
#smtpd_use_tls=yes
#smtpd_tls_auth_only = yes

#Handle SMTP authentication using Dovecot
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

# other destination domains should be handled using virtual domains 
mydestination = localhost

# using Dovecot's LMTP for mail delivery and giving it path to store mail
virtual_transport = lmtp:unix:private/dovecot-lmtp

# virtual mailbox setups
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
virtual_alias_maps = mysql:/etc/postfix/mysql/virtual_alias_maps.cf
virtual_mailbox_domains = mysql:/etc/postfix/mysql/virtual_domains_maps.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql/virtual_mailbox_maps.cf

# DKIM lcdung.top
smtpd_milters = inet:127.0.0.1:8891
non_smtpd_milters = $smtpd_milters
milter_default_action = accept

milter_protocol = 2

Lưu ý:

1. Mail server của mình không mã hóa SSL và TLS nên đã comment

#smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
#smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
#smtpd_tls_cert_file=/etc/ssl/certs/dovecot.pem
#smtpd_tls_key_file=/etc/ssl/private/dovecot.pem
#smtpd_use_tls=yes
#smtpd_tls_auth_only = yes

Nếu mail server bạn có hỗ trợ thì bỏ comment ra và sửa đường dẫn theo đúng folder của bạn.

2. Bạn sẽ thấy dòng

virtual_uid_maps = static:5000
virtual_gid_maps = static:5000

5000 là tên group và user dùng trong dovecot. Khi đến bước cài đặt dovecot bạn sẽ thấy

3. Virtual mailbox để lưu trữ và quản lý nhiều domain, alias, tài khoản mail trong database mysql

virtual_alias_maps = mysql:/etc/postfix/mysql/virtual_alias_maps.cf
virtual_mailbox_domains = mysql:/etc/postfix/mysql/virtual_domains_maps.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql/virtual_mailbox_maps.cf

 

2.3 Cấu hình Postfix Virtual Mailbox

Trước tiên tạo folder để lưu file cấu hình postfix-mysql:

mkdir /etc/postfix/mysql

Virtual Alias Mapping

File: /etc/postfix/mysql/virtual_alias_maps.cf

user = vimbadmin
password = password
hosts = 127.0.0.1
dbname = vimbadmin
query = SELECT goto FROM alias WHERE address = '%s' AND active = '1'

Virtual Domain Mapping

File: /etc/postfix/mysql/virtual_domains_maps.cf

user = vimbadmin
password = password
hosts = 127.0.0.1
dbname = vimbadmin
query = SELECT domain FROM domain WHERE domain = '%s' AND backupmx = '0' AND active = '1'

Virtual Mailbox (user) Mapping

File: /etc/postfix/mysql/virtual_mailbox_maps.cf

user = vimbadmin
password = password
hosts = 127.0.0.1
dbname = vimbadmin
query = SELECT maildir FROM mailbox WHERE username = '%s' AND active = '1'

Lưu ý: user và password là tài khoản để quản lý database của Vimbadmin trong Mysql. Bạn nên tạo 1 tài khoản và 1 dabatase rỗng nhé.

Cuối cùng để start Postfix trên Centos 7 dùng command sau:

Systemctl start postfix

Tuy nhiên nếu dùng nhân Linux khác hoặc phiên bản Centos khác thì có lệnh khác tuy nhiên cũng là start postfix

3. Cấu hình Dovecot

Bạn có thể kiểm tra version của mình bằng command:

dovecot --version

Version mình sử dụng là 2.2.10

Tạo user trong linux – vmail

Như ghi chú trong cấu hình Postfix cần tạo tài khoản người sở hữu vùng lưu trữ email

groupadd -g 5000 vmail
useradd -g vmail -u 5000 vmail -d /var/vmail -m

Cấu hình Dovecot

Cấu hình Dovecot bạn cần cấu hình một số file sau:

Bật protocols

File: /etc/dovecot/dovecot.conf

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

Cấu hình nơi lưu trữ khi nhận mail

File: /etc/dovecot/conf.d/10-mail.conf

mail_location = maildir:/var/vmail/%d/%n

Cấu hình chứng thực

File: /etc/dovecot/conf.d/10-auth.conf

#!include auth-deny.conf.ext
#!include auth-master.conf.ext
passdb {
    driver = sql
    args = /etc/dovecot/dovecot-sql.conf.ext
}
userdb {
    driver = static
    args = uid=5000 gid=5000 home=/var/vmail/%d/%n allow_all_users=yes
}
#!include auth-system.conf.ext
#!include dovecot-sql.conf.ext
#!include auth-ldap.conf.ext
#!include auth-passwdfile.conf.ext
#!include auth-checkpassword.conf.ext
#!include auth-vpopmail.conf.ext
#!include auth-static.conf.ext

Cấu hình mysql parameters trong dovecot

File: /etc/dovecot/dovecot-sql.conf.ext

driver = mysql
connect = host=127.0.0.1 dbname=vimbadmin user=yyyyy password=xxxxxx
password_query = SELECT username AS user, password, homedir AS userdb_home, uid AS userdb_uid, gid AS userdb_gid FROM mailbox WHERE username = '%u'
iterate_query = SELECT username AS user FROM mailbox

Cấu hinh hình  master

File: /etc/dovecot/conf.d/10-master.conf

Tùy nhu cầu mà bạn có thể thay đổi cho hợp lý hoặc theo toàn bộ cấu hình này.


service imap-login {
  inet_listener imap {
    port = 143
  }
  inet_listener imaps {
    #port = 993
    #ssl = yes
  }

}

service pop3-login {
  inet_listener pop3 {
    port = 110
  }
  inet_listener pop3s {
    #port = 995
    #ssl = yes
  }
}

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 = vmail
  }

  user = dovecot
}

service auth-worker {
  user = vmail
}

Cấu hình Logging

File: /etc/dovecot/conf.d/10-logging.conf

log_path = /var/log/dovecot.log

Cấu hình Debug logging

Nếu bạn muốn ghi nhận debug khi chứng thực thì bật lên theo đoạn code sau:

#debuggign authentication requests
auth_debug = yes

#debugging other mail related stuff
mail_debug = yes

Doveconf

Để track xem những cấu hình đã thay đổi và kiểm tra có bug phát sinh không

doveconf -n

Tương tự xem tất cả cấu hình của các file

doveconf -a

Cuối cùng để start Dovecot trên Centos 7 dùng command sau:

# Start Dovecot
systemctl start dovecot
# Restart dovecot
systemctl restart dovecot

Tuy nhiên nếu dùng nhân Linux khác hoặc phiên bản Centos khác thì có lệnh khác tuy nhiên cũng là start dovecot

4. ViMbAdmin – Virtual Mail Server Administration

ViMbAdmin là giải pháp tối ưu nếu server bạn đang sử dụng PHP để phát triển ứng dụng hay website. Còn nếu  đang sử dụng sẳng ruby/rails thì bạn nên sử dụng posty để quản trị.

Cài đặt ViMbAdmin v3

Để cài Vimbadmin cần cài đặt composer trước.

curl -sS https://getcomposer.org/installer | php

Sau đó

cd /usr/local
# Dùng lệnh cài đặt git
yum install subversion git-core
# Hoặc lệnh
yum install git

# clone mã nguồn về
git clone git://github.com/opensolutions/ViMbAdmin.git vimbadmin

cd /usr/local/vimbadmin
# Dùng lệnh cài đặt composer
php composer.phar install
# Hoặc lệnh
composer install

Tạo database hoặc user cho vimbadmin

Bạn có thể chạy lệnh sau hoặc tự custom theo ý mình

CREATE DATABASE `vimbadmin`;
GRANT ALL ON `vimbadmin`.* TO `vimbadmin`@`127.0.0.1` IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

Cấu hình vimbadmin

Backup file cấu hình

cp application/configs/application.ini.dist application/configs/application.ini

Sau đó chỉnh file cấu hình: application/configs/application.ini

securitysalt = "superadmin-password"
defaults.mailbox.uid = 5000
defaults.mailbox.gid = 5000
defaults.mailbox.homedir = "/var/vmail/"
resources.doctrine2.connection.options.driver   = 'pdo_mysql'
resources.doctrine2.connection.options.dbname   = 'vimbadmin'
resources.doctrine2.connection.options.user     = 'vimbadmin'
resources.doctrine2.connection.options.password = 'password'
resources.doctrine2.connection.options.host     = 'localhost'

Lưu ý: đảm bảo đúng cấu hình MySQL

Memcache glitch

Nếu bạn dùng memcache thì comment dòng session  này lại

;resources.session.save_path = APPLICATION_PATH "/../var/session"

Tạo bảng trong MySQL tự động

Như đã cấu hình trên Postfix và Dovecot dùng database tên Vimbadmin  nên cần chạy lệnh sau:

./bin/doctrine2-cli.php orm:schema-tool:create

Chnage Ownership

chown -R www-data:www-data /usr/local/vimbadmin

set timezone in php

Vào file: /etc/php.ini

Thêm timezone

date.timezone = "UTC"

Restart PHP-FPM

systemctl restart php-fpm

Cấu hình Nginx chạy web mail server

server {
	server_name vma.example.com;

	access_log   /var/log/nginx/vma.example.com.access.log;
	error_log    /var/log/nginx/vma.example.com.error.log;

	root /usr/local/vimbadmin/public;
	index index.php;

	location / {
		try_files $uri $uri/ /index.php?$args;
	}

	location ~ \.php$ {
		try_files $uri =404;
		include fastcgi_params;
		fastcgi_pass 127.0.0.1:9000;
	}

}

Bạn có thể thay đổi vma.example.com thành sub domain của bạn.

Sau khi cài đặt lần chạy đầu tiên sẽ yêu cầu bạn cài đặt mã salt, user và password đề vào hệ thống quản lý domain, virtual mail-users,… bằng tài khoản ViMbAdmin.

5. Cài đặt Round Cube – Giao diện quản lý mail box

Sau khi chắc chắn việc cài đặt mail server thành công và kiểm thử gửi mail cũng thành công trên IMAP hoặc POP3 thì tiến hành cài Round Cube để sử dụng web mail.

yum -y install roundcubemail

Sau khi cài đặt Round Cube được lưu tại đây: /usr/share/roundcubemail

Cấu hình Round Cube để chạy cài đặt tại đây: /etc/roundcubemail/defaults.inc.php

$config['enable_installer'] = true;

Chỉnh file cấu hình kết nối với database roundcube đã tạo tại đây: /etc/roundcubemail/config.inc.php

<?php

$config = array();

$config['db_dsnw'] = 'mysql://root:password@localhost/roundcube';

$config['default_host'] = 'localhost';

$config['smtp_server'] = 'localhost';

$config['smtp_port'] = 25;

$config['smtp_user'] = '%u';

$config['smtp_pass'] = '%p';

$config['support_url'] = '';

// Name your service. This is displayed on the login screen and in the window title
$config['product_name'] = 'Roundcube Webmail';

$config['des_key'] = 'rcmail-!24ByteDESkey*Str';

$config['plugins'] = array(
    'archive',
    'zipdownload',
);

$config['skin'] = 'larry';

Cấu hình Nginx để chạy Webmail Round Cube

server {
	server_name mail.example.com;

	access_log   /var/log/nginx/mail.example.com.access.log;
	error_log    /var/log/nginx/mail.example.com.error.log;

	root /usr/share/roundcube;
	index index.php;

	location / {
		try_files $uri $uri/ /index.php?$args;
	}

	location ~ \.php$ {
		try_files $uri =404;
		include fastcgi_params;
		fastcgi_pass 127.0.0.1:9000;
	}

}

Sau khi chạy mail.example.com trên trình duyệt và sử dụng tài khoản mail được tạo ra từ Vimbadmin để login vào và sử dụng.

7 comments On Hướng dẫn cài đặt mail server với Postfix, Dovecot, ViMbAdmin, RoundCube trên Centos 7

  • bác ơi, mình cài đặt tới dovecot xong rồi làm sao check postfix với dovecot chạy được chưa vậy?

  • Bạn xem lại link Hướng dan nhé.
    Cái này thì dùng lệnh cmd gõ lệnh kiểm tra: “telnet ip/domain PORT“
    VD:
    telnet 192.168.16.181 25
    telnet mailserver.lcdung.top 110
    Chú ý: POP3 port 25 là gửi mail, port 110 là nhận mail. Ngoài ra IMAP thì sẽ có port khác lúc cài đặt sẽ thấy

  • mình dùng win 10 không gõ lệnh telnet được!

  • Dễ thôi lên google tìm “telnet windows 10” xem có hướng dẫn bật đó bạn

  • Cảm ơn bác mình làm check được rồi!

  • Good job! dùng telnet bạn có thể check ip hoặc domain dùng cho mail server lun nhé

  • OK, you try my theme for your blog. Realistic theme by Themient http://themient.com/

Comments are closed.

Sliding Sidebar

About Me

About Me

Hello, my name is Dũng (Johnny). Welcome to my blog.

As I’m a developer, I write about topics related to the field of programming, mainly from a technical point of view. On this blog you’ll find posts which encourage discussion, information about development trends, case studies, reviews, tutorials, tips on how to improve your effectiveness, and anything else that might be fascinating to people from the IT industry.
I love PHP, NodeJS, Java,... and Fullstack.