CentOS 7.x Postfix + Dovecot + RoundCube 설치 방법


하나의 서버에 여러개의 도메인을 운영할 때 시스템 계정이 아닌 DB에 가상 메일계정을 이용하는 방법입니다.

검색해서 설정했더니 잘 안되어서 여러곳의 정보를 취합해서 정리하였습니다.

참조 사이트 :

 https://www.linode.com/docs/email/postfix/email-with-postfix-dovecot-and-mariadb-on-centos-7/

 https://www.digitalocean.com/community/tutorials/how-to-configure-a-mail-server-using-postfix-dovecot-mysql-and-spamassassin

mariadb 및 아파치, php 는 설치되어 있다고 가정하고 진행합니다.


# yum install postfix dovecot dovecot-mysql roundcubemail

# systemctl enable postfix

# systemctl start postfix

# systemctl enable dovecot

# systemctl start dovecot

필요한 패키지를 설치합니다.


# useradd -u 5000 -d /home/vmail -s /sbin/nologin -m vmail

가상메일에서 사용될 계정을 만듭니다.


CREATE DATABASE webmail;

GRANT ALL PRIVILEGES ON webmail.* TO webmail@localhost IDENTIFIED BY '비밀번호';

가상메일에서 사용될 DB 계정을 만듭니다.


CREATE TABLE `webmail`.`virtual_domains` (

 `id` INT NOT NULL AUTO_INCREMENT,

 `name` VARCHAR(50) NOT NULL,

 PRIMARY KEY (`id`)

 ) DEFAULT CHARSET=utf8;


CREATE TABLE `webmail`.`virtual_users` (

 `id` INT NOT NULL AUTO_INCREMENT,

 `password` VARCHAR(106) NOT NULL,

 `email` VARCHAR(80) NOT NULL,

 PRIMARY KEY (`id`),

 UNIQUE KEY `email` (`email`)

 ) DEFAULT CHARSET=utf8;


CREATE TABLE `webmail`.`virtual_aliases` (

 `id` INT NOT NULL AUTO_INCREMENT,

 `source` varchar(100) NOT NULL,

 `destination` varchar(100) NOT NULL,

 PRIMARY KEY (`id`)

 ) DEFAULT CHARSET=utf8;


INSERT INTO `webmail`.`virtual_domains` (`id` ,`name`) VALUES

 ('1', 'aaa.com'),

 ('2', 'bbb.com'),

 ('3', 'example.com');


INSERT INTO `webmail`.`virtual_users` (`id`, `password`, `email`) VALUES

 ('1', ENCRYPT('비밀번호', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'user1@aaa.com'),

 ('2', ENCRYPT('비밀번호', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'user2@aaa.com'),

 ('3', ENCRYPT('비밀번호', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'user1@bbb.com'),

 ('4', ENCRYPT('비밀번호', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'user2@bbb.com'),

 ('5', ENCRYPT('비밀번호', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'user1@example.com'),

 ('6', ENCRYPT('비밀번호', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'user2@example.com');


INSERT INTO `webmail`.`virtual_aliases` (`id`, `source`, `destination`) VALUES

 ('1', 'alias@aaa.com', 'user1@aaa.com'),

 ('2', 'user2@example.com', 'user1@example.com');

가상메일에서 사용될 테이블과 샘플데이터를 만듭니다.

default_pass_scheme = CRYPT 일때는 ENCRYPT('비밀번호')


# vi /etc/postfix/mysql-virtual-alias-maps.cf

hosts = 127.0.0.1

dbname = webmail

user = webmail

password = 비밀번호

query = SELECT destination FROM virtual_aliases WHERE source='%s'


# vi /etc/postfix/mysql-virtual-users.cf

hosts = 127.0.0.1

dbname = webmail

user = webmail

password = 비밀번호

query = SELECT email FROM virtual_users WHERE email='%s'


# vi /etc/postfix/mysql-virtual-mailbox-domains.cf

hosts = 127.0.0.1

dbname = webmail

user = webmail

password = 비밀번호

query = SELECT name FROM virtual_domains WHERE name='%s'


# vi /etc/postfix/mysql-virtual-mailbox-maps.cf

hosts = 127.0.0.1

dbname = webmail

user = webmail

password = 비밀번호

query = SELECT CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/') FROM virtual_users WHERE email='%s'


# chgrp postfix /etc/postfix/mysql-virtual-*.cf

# chmod o= /etc/postfix/mysql-virtual-*.cf

Postfix 에서 가상메일 계정의 정보를 가져올 쿼리파일을 만듭니다.


# vi /etc/postfix/main.cf

myhostname = mail.example.com

mydestination = localhost

mynetworks = 127.0.0.0/8

inet_interfaces = all

message_size_limit = 30720000

virtual_alias_domains =

virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual-alias-maps.cf, mysql:/etc/postfix/mysql-virtual-users.cf

virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf

virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf

virtual_mailbox_base = /home/vmail

virtual_uid_maps = static:5000

virtual_gid_maps = static:5000

smtpd_sasl_type = dovecot

smtpd_sasl_path = private/auth

smtpd_sasl_auth_enable = yes

broken_sasl_auth_clients = yes

smtpd_sasl_authenticated_header = yes

smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination

smtpd_use_tls = yes

smtpd_tls_cert_file = /etc/pki/dovecot/certs/dovecot.pem

smtpd_tls_key_file = /etc/pki/dovecot/private/dovecot.pem

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_transport = dovecot

dovecot_destination_recipient_limit = 1

Postfix main.cf 파일을 설정합니다.

postconf -e 'mydestination = localhost' 형태의 명령어로도 수정이 가능합니다.

그리고 postconf -n 명령어로 환경설정을 볼 수도 있습니다.

회색 부분을 굳이 설정하지 않아도 됩니다. ( 보안 사용시 필요 )

proxy_read_maps 은 없어도 될 것 같습니다.


# vi /etc/postfix/master.cf

dovecot   unix  -       n       n       -       -       pipe

    flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/deliver -f ${sender} -d ${recipient}


submission inet n       -       n       -       -       smtpd

  -o syslog_name=postfix/submission

  -o smtpd_tls_security_level=encrypt

  -o smtpd_sasl_auth_enable=yes

  -o smtpd_reject_unlisted_recipient=no

  #-o smtpd_client_restrictions=$mua_client_restrictions

  #-o smtpd_helo_restrictions=$mua_helo_restrictions

  #-o smtpd_sender_restrictions=$mua_sender_restrictions

  -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject

  -o milter_macro_daemon_name=ORIGINATING

smtps     inet  n       -       n       -       -       smtpd

  -o syslog_name=postfix/smtps

  -o smtpd_tls_wrappermode=yes

  -o smtpd_sasl_auth_enable=yes

  -o smtpd_reject_unlisted_recipient=no

  #-o smtpd_client_restrictions=$mua_client_restrictions

  #-o smtpd_helo_restrictions=$mua_helo_restrictions

  #-o smtpd_sender_restrictions=$mua_sender_restrictions

  -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject

  -o milter_macro_daemon_name=ORIGINATING

Postfix master.cf 파일을 설정합니다.

회색 부분을 설정하지 않아도 됩니다. ( 보안 사용시 필요 )


# systemctl restart postfix


# postmap -q alias@aaa.com mysql:/etc/postfix/mysql-virtual-alias-maps.cf

user1@aaa.com

# postmap -q user1@aaa.com mysql:/etc/postfix/mysql-virtual-users.cf

user1@aaa.com

# postmap -q aaa.com mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf

aaa.com

# postmap -q user1@aaa.com mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf

aaa.com/user1/

postfix 데몬을 재시작 하고 가상계정 정보를 잘 가져 오는지 확인합니다.


# mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.ori

# vi /etc/dovecot/dovecot.conf

protocols = imap pop3

listen = *

log_timestamp = "%Y-%m-%d %H:%M:%S "

mail_location = maildir:/home/vmail/%d/%n/Maildir


ssl_cert = </etc/pki/dovecot/certs/dovecot.pem

ssl_key = </etc/pki/dovecot/private/dovecot.pem


namespace {

    type = private

    separator = .

    prefix = INBOX.

    inbox = yes

}


service auth {

    unix_listener auth-master {

        mode = 0600

        user = vmail

    }

    unix_listener /var/spool/postfix/private/auth {

        mode = 0666

        user = postfix

        group = postfix

    }

    user = root

}


service auth-worker {

    user = root

}


protocol lda {

    log_path = /home/vmail/dovecot-deliver.log

    auth_socket_path = /var/run/dovecot/auth-master

    postmaster_address = postmaster@example.com

}


protocol pop3 {

    pop3_uidl_format = %08Xu%08Xv

}


passdb {

    driver = sql

    args = /etc/dovecot/dovecot-sql.conf.ext

}


userdb {

    driver = static

    args = uid=vmail gid=vmail home=/home/vmail/%d/%n allow_all_users=yes

}


# vi /etc/dovecot/dovecot-sql.conf.ext

driver = mysql

connect = host=127.0.0.1 dbname=webmail user=webmail password=비밀번호

default_pass_scheme = SHA512-CRYPT

password_query = SELECT email as user, password FROM virtual_users WHERE email='%u';


# chown vmail:dovecot /etc/dovecot

# chown vmail:dovecot /etc/dovecot/dovecot.conf

# chown vmail:dovecot /etc/dovecot/dovecot-sql.conf.ext

# chmod o= /etc/dovecot/dovecot-sql.conf.ext

기존 /etc/dovecot/dovecot.conf 파일을 백업해 놓고 새로운 파일로 설정을 합니다.

그리고 계정계정 로그인 정보에 사용될 /etc/dovecot/dovecot-sql.conf.ext 쿼리파일을 만듭니다.

dovecot 데몬에서 접근이 가능하도록 소유권한을 변경합니다.


# systemctl restart postfix

# systemctl restart dovecot

# tail -n15 /var/log/maillog

이제 postfix, dovecot 데몬을 재시작하고 메일이 잘 보내지고 잘 받아지는지 테스트 하면 됩니다.

telnet localhost smtp, telnet localhost pop3 명령어를 이용해서 메일 테스트를 합니다.


# mysql -u webmail -p webmail < /usr/share/roundcubemail/SQL/mysql.initial.sql


# cp -p /etc/roundcubemail/config.inc.php.sample /etc/roundcubemail/config.inc.php

# vi /etc/roundcubemail/config.inc.php

$config['db_dsnw'] = 'mysql://webmail:비밀번호@localhost/webmail';

$config['product_name'] = 'Webmail'; // HTML Title 부분에 나오는 내용, 적당한 이름으로 변경


# vi /etc/httpd/conf.d/roundcubemail.conf

<Directory /usr/share/roundcubemail/>

    <IfModule mod_authz_core.c>

        # Apache 2.4

        Require local

        Require all granted

    </IfModule>

</Directory>

<Directory /usr/share/roundcubemail/installer/>

    <IfModule mod_authz_core.c>

        # Apache 2.4

        Require local

        Require ip 허용아이피

    </IfModule>

</Directory>

# systemctl restart httpd

RoundCube 메일 환경을 설정합니다.

http://webmail.example.com/roundcubemail 페이지를 호출하여 확인합니다.

로그인시 사용자명에는 이메일 주소를 입력하여야 합니다.


위의 내용은 아래 첨부파일을 다운 받으면 됩니다.

postfix-dovecot-roundcube.txt


블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,

CentOS 7.x RoundCube 설치 방법


wget 으로 소스코드를 다운로드 받아서 설치할 수도 있지만 이번에는 yum install 로 설치하는 방법에 대해서 알아보겠습니다.


▶ RoundCube 설치

# yum install roundcubemail

1.1.12 가 설치 되는군요~


▶ 메일 DB 생성

# mysql -u root -p

CREATE DATABASE rcube;

GRANT ALL PRIVILEGES ON rcube.* TO rcube@localhost IDENTIFIED BY '비밀번호';

exit;

# mysql -u rcube -p rcube < /usr/share/roundcubemail/SQL/mysql.initial.sql


▶ RoundCube 설정

# cp -p /etc/roundcubemail/config.inc.php.sample /etc/roundcubemail/config.inc.php

# vi /etc/roundcubemail/config.inc.php

//$config['db_dsnw'] = 'mysql://roundcube:pass@localhost/roundcubemail';

// 위 부분을 찾아서 아래와 같이 수정

$config['db_dsnw'] = 'mysql://rcube:비밀번호@localhost/rcube';

$config['product_name'] = 'RCube Webmail'; // HTML Title 부분에 나오는 내용, 적당한 이름으로 변경


▶ 아파치 설정

# vi /etc/httpd/conf.d/roundcubemail.conf

<Directory /usr/share/roundcubemail/>

    <IfModule mod_authz_core.c>

        # Apache 2.4

        Require local

        Require all granted

    </IfModule>

</Directory>

<Directory /usr/share/roundcubemail/installer/>

    <IfModule mod_authz_core.c>

        # Apache 2.4

        Require local

        Require ip 허용아이피

    </IfModule>

</Directory>

# systemctl restart httpd

roundcubemail 과 roundcubemail/installer 두 곳 모두 수정하여야 합니다.

혹시 아파치 2.2 버전대라면 아랫쪽을 수정하시면 됩니다.


이제 완료되었습니다.

웹브라우저에서 http://domail.com/roundcubemail 로 웹메일 접속이 가능합니다.


여기에서 언급되지 않은 부분은 https://ivps.tistory.com/577 여기를 참고해서 설정하시면 됩니다.





블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,

RoundCube VirtualHost 도메인 연결


http://domain.com/roundcube 형태의 웹메일 url 이 보기 싫다면 .htaccess 파일에서 RewriteRule 을 이용한 리디렉션을 시켜도 되지만

아파치 설정의 VirtualHost 를 이용할 수도 있습니다.


▶ VirtualHost 도메인 연결

<VirtualHost *:80>

    ServerName mail.domain.com

    DocumentRoot /var/www/html/roundcube

    ErrorLog logs/mail.domain.com-error_log

    LogFormat "%V %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" vcommon

    CustomLog logs/mail.domain.com-access_log vcommon

</VirtualHost>

위 같이 하고 systemctl restart httpd 로 아파치를 재시작합니다.


이제 웹메일 접속을 http://mail.domain.com/ 으로 접속하면 됩니다.


라운드큐브 웹메일 설정은 https://ivps.tistory.com/577 여기를 참고하세요~



블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,

리눅스 웹메일 오픈소스 RoundCube


리눅스에서 메일서버를 구축하는 방법은 다람쥐메일 등 여러가지가 있겠지만 Postfix + DoveCot + RoundCube 로 웹메일을 구축 방법에 대해서 알아보겠습니다.

CentOS 7.x 기준입니다.

먼저 DNS 설정에서 MX 레코드 설정이 되어 있어야 합니다.


▶ MX 레코드 설정

mail            A            domain.com

domain.com    MX    10    mail.domain.com

이런 형태로 되면 됩니다.

보내는 건 되는데 받는게 안된다면 방화벽 또는 mx 레코드 설정을 의심해 보아야 합니다.


▶ Postfix 설치

# yum install postfix

# vi /etc/postfix/main.cf

myhostname = mail.domain.com

mydomain = domain.com

myorigin = $mydomain

inet_interfaces = all

mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

home_mailbox = Maildir/

# systemctl start postfix

# systemctl enable postfix

postfix 대신에 sendmail 을 사용하여도 됩니다.


▶ DoveCot 설치

# yum install dovecot

# vi /etc/dovecot/dovecot.conf

protocols = imap pop3 lmtp

# vi /etc/dovecot/conf.d/10-mail.conf

mail_location = maildir:~/Maildir

# vi /etc/dovecot/conf.d/10-auth.conf

disable_plaintext_auth = no

# systemctl start dovecot

# systemctl enable dovecot

pop3 나 imap 을 위해서 설치하여야 합니다.


▶ DB Database 생성

CREATE DATABASE roundcube;

GRANT ALL PRIVILEGES ON roundcube.* TO roundcube@localhost IDENTIFIED BY '비밀번호';

exit;

mysql -u root -p 로 접속해서 생성하면 됩니다.

roundcube 대신에 마음에 드는 계정명으로 생성


▶ RoundCube 소스 다운로드

# cd /var/www/html

# wget https://github.com/roundcube/roundcubemail/releases/download/1.3.7/roundcubemail-1.3.7-complete.tar.gz

# tar zxvf roundcubemail-1.3.7-complete.tar.gz

# ln -s roundcubemail-1.3.7 roundcube

# chown -R apache.apache roundcubemail-1.3.7

아파치의 DocumentRoot 에 맞게 받으면 됩니다.

참고로 yum install roundcubemail 로 설치하는 방법도 있습니다. ( https://ivps.tistory.com/580 참고 )


▶ RoundCube 설치

브라우저에서 자신의 도메인에 맞게 http://domain.com/roundcube/installer 페이지를 호출합니다.

required 관련된 부분이 OK 가 나오면 됩니다.

NEXT 버튼을 눌러서 다음으로 진행합니다.


mysql 계정 정보를 입력하고 다음으로 넘어갑니다.


Check DB config 에서 Initialize database 를 눌러서 DB 정보를 초기화 합니다.


▶ 메일계정 생성

# useradd roundcube

# passwd roundcube

Changing password for user roundcube.

New password:

Retype new password:

passwd: all authentication tokens updated successfully.

테스트에 사용될 계정을 생성합니다.


▶ 웹메일 접속 테스트

http://domain.com/roundcube 페이지를 열어서 로그인을 시도합니다.

로그인을 시도합니다.

저장소 서버에 연결을 실패했습니다. 에러가 뜨는군요~

DovoCot 이 설치가 되지 않았거나 설정이 잘 못 되면 나타나는 현상입니다.

설정을 재확인 합니다.

로그인이 정상적으로 되는지 좀전의 installer 페이지에서도 확인이 가능합니다.


▶ SMTP 포트 방화벽 설정

# firewall-cmd --permanent --zone=public --add-service=smtp

# firewall-cmd --reload

외부에서 메일을 받으려면 방화벽에서 smtp 포트가 열려 있어야 합니다.


이제 메일을 주고 받기 테스트 하시면 됩니다.


블로그 이미지

영은파더♥

가상서버호스팅 VPS 리눅스 서버관리 윈도우 IT

,