1. 程式人生 > >Linux搭建postfix郵件服務器

Linux搭建postfix郵件服務器

amp lib64 服務器 安裝 用戶 fault 導入數據 mysq ide

-------啟動命令

systemctl start dovecot

service courier-authlib restart

systemctl status courier-authlib

systemctl enable courier-authlib

systemctl start saslauthd

systemctl enable saslauthd


-------postfix-------配置


一、 裝備工作

setenforce 0

systemctl stop firewalld

iptables -P INPUT ACCEPT

iptables -F

iptables -X

iptables -L


二、安裝mysql5.7 epel源

yum install -y wget

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

rpm -ivh mysql-community-release-el7-5.noarch.rpm

wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

rpm -ivh epel-release-latest-7.noarch.rpm


三、安裝postfix

1、卸載系統自帶的postfix,刪除postfix用戶,重新指定uid、gid創建新用戶postfix,postdrop

yum remove postfix -y

userdel postfix

groupdel postdrop

groupadd -g 2525 postfix

useradd -g postfix -u 2525 -s /sbin/nologin -M postfix

groupadd -g 2526 postdrop

useradd -g postdrop -u 2526 -s /sbin/nologin -M postdrop


2、下載源碼包並解壓編譯

wget http://mirrors.go-parts.com/postfix/source/official/postfix-3.0.1.tar.gz

tar xf postfix-3.0.1.tar.gz

cd postfix-3.0.1

make makefiles 'CCARGS=-DHAS_MYSQL -I/usr/include/mysql -DUSE_SASL_AUTH -DUSE_CYRUS_SASL -I/usr/include/sasl -DUSE_TLS ' 'AUXLIBS=-L/usr/lib64/mysql -lmysqlclient -lz -lrt -lm -L/usr/lib64/sasl2 -lsasl2 -lssl -lcrypto'

make && make install


3、更改目錄屬主屬組

chown -R postfix:postdrop /var/spool/postfix

chown -R postfix:postdrop /var/lib/postfix/

chown root /var/spool/postfix

chown -R root /var/spool/postfix/pid


4、修改postfix的配置文件

[root@localhost ~]# vim /etc/postfix/main.cf

myhostname = mail.everyoo.com //設置主機名

mydomain = everyoo.com //指定域名

myorigin = $mydomain //指明發件人所在的域名

inet_interfaces = //all指定postfix系統監聽的網絡接口

mydestination = $myhostname, localhost.$mydomain, localhost,$mydomain //指定postfix接收郵件時收件人的域名 [使用虛擬域需要禁用]

mynetworks_style = host //指定信任網段類型

mynetworks = 192.168.1.0/24, 127.0.0.0/8 //指定信任的客戶端

relay_domains = $mydestination //指定允許中轉郵件的域名

alias_maps = hash:/etc/aliases //設置郵件的別名


四、安裝dovecot

yum install -y dovecot dovecot-mysql

cd /etc/dovecot/

vim dovecot.conf

protocols = imap pop3

!include conf.d/*.conf

listen = *

base_dir = /var/run/dovecot/

cd conf.d/

vim 10-auth.conf

disable_plaintext_auth = no

vim 10-mail.conf

mail_location = maildir:~/Maildir

mail_location = maildir:/var/mailbox/%d/%n/Maildir

mail_privileged_group = mail

vim 10-ssl.conf

ssl = no

vim 10-logging.conf og_path = /var/log/dovecot.log

info_log_path = /var/log/dovecot.info

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

cp auth-sql.conf.ext auth-sql.conf

vim auth-sql.conf

passdb { driver = sql args = /etc/dovecot/dovecot-sql.conf.ext}# Path for SQL configuration file, see example-config/dovecot-sql.conf.ext

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


五、安裝courier-authlib

wget https://sourceforge.net/projects/courier/files/courier-unicode/1.2/courier-unicode-1.2.tar.bz2

tar xf courier-unicode-1.2.tar.bz2

cd courier-unicode-1.2

./configure

make && make install


wget https://sourceforge.net/projects/courier/files/authlib/0.66.2/courier-authlib-0.66.2.tar.bz2

tar xf courier-authlib-0.66.2.tar.bz2

cd courier-authlib-0.66.2

./configure \

--prefix=/usr/local/courier-authlib \

--sysconfdir=/etc \

--without-authpam \

--without-authshadow \

--without-authvchkpw \

--without-authpgsql \

--with-authmysql \

--with-mysql-libs=/usr/lib64/mysql \

--with-mysql-includes=/usr/include/mysql \

--with-redhat \

--with-authmysqlrc=/etc/authmysqlrc \

--with-authdaemonrc=/etc/authdaemonrc \

--with-mailuser=postfix

make && make install


2、配置courier-authlib

chmod 755 /usr/local/courier-authlib/var/spool/authdaemon

cp /etc/authdaemonrc.dist /etc/authdaemonrc

cp /etc/authmysqlrc.dist /etc/authmysqlrc

vim /etc/authdaemonrc

authmodulelist="authmysql"

authmodulelistorig="authmysql"

vim /etc/authmysqlrc

MYSQL_SERVER localhost

MYSQL_USERNAME extmail

MYSQL_PASSWORD extmail

MYSQL_SOCKET /var/lib/mysql/mysql.sock

MYSQL_PORT 3306

MYSQL_DATABASE extmail

MYSQL_USER_TABLE mailbox

MYSQL_CRYPT_PWFIELD password

DEFAULT_DOMAIN test.com

MYSQL_UID_FIELD '2525'

MYSQL_GID_FIELD '2525'

MYSQL_LOGIN_FIELD username

MYSQL_HOME_FIELD concat('/var/mailbox/',homedir)

MYSQL_NAME_FIELD name

MYSQL_MAILDIR_FIELD concat('/var/mailbox/',maildir)

3、courier-authlib添加服務啟動腳本及其他

cp courier-authlib.sysvinit /etc/init.d/courier-authlib

chmod +x /etc/init.d/courier-authlib

chkconfig --add courier-authlib

chkconfig courier-authlib on

echo "/usr/local/courier-authlib/lib/courier-authlib" >> /etc/ld.so.conf.d/courier-authlib.conf

ldconfig

service courier-authlib start


4、smtp以及虛擬用戶相關的設置

vim /usr/lib64/sasl2/smtpd.conf //文件不存在,要自己創建

pwcheck_method: authdaemond

log_level: 3

mech_list: PLAIN LOGIN

authdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket

vim /etc/postfix/main.cf

##postfix支持SMTP##

smtpd_sasl_auth_enable = yes

smtpd_sasl_local_domain = ''

smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination

broken_sasl_auth_clients=yes

smtpd_client_restrictions = permit_sasl_authenticated

smtpd_sasl_security_options = noanonymous

##postfix支持虛擬用戶##

virtual_mailbox_base = /var/mailbox

virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf //這裏的配置文件需在後面extman

裏復制過來

virtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cf

virtual_alias_domains =

virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cf

virtual_uid_maps = static:2525

virtual_gid_maps = static:2525

virtual_transport = virtual


六、安裝extmail

extmail和extman可通過這兩個鏈接下載

http://7xivyw.com1.z0.glb.clouddn.com/extmail-1.2.tar.gz

http://7xivyw.com1.z0.glb.clouddn.com/extman-1.1.tar.gz


mkdir -p /var/www/extsuite

tar xf extmail-1.2.tar.gz -C /var/www/extsuite/

mv /var/www/extsuite/extmail-1.2/ /var/www/extsuite/extmail


2、更改extmail的配置文件

cd /var/www/extsuite/extmail

cp webmail.cf.default webmail.cf

vim webmail.cf

SYS_SESS_DIR = /tmp/extmail

SYS_UPLOAD_TMPDIR = /tmp/extmail/upload

SYS_USER_LANG = zh_CN

SYS_MIN_PASS_LEN = 8

SYS_MAILDIR_BASE = /var/mailbox

SYS_MYSQL_USER = extmail

SYS_MYSQL_PASS = extmail

SYS_MYSQL_DB = extmail

SYS_MYSQL_HOST = localhost

SYS_MYSQL_SOCKET = /var/lib/mysql/mysql.sock

SYS_MYSQL_TABLE = mailbox

SYS_MYSQL_ATTR_USERNAME = username

SYS_MYSQL_ATTR_DOMAIN = domain

SYS_MYSQL_ATTR_PASSWD = password

SYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket


3、建立臨時文件目錄與session目錄

mkdir -p /tmp/extmail/upload

chown -R postfix.postfix /tmp/extmail/


七、安裝extman

tar xf extman-1.1.tar.gz -C /var/www/extsuite/

cd /var/www/extsuite/

mv extman-1.1/ extman


2、更改extman配置文件

cd extman/

cp webman.cf.default webman.cf


3、更改cgi目錄屬主屬組

chown -R postfix.postfix /var/www/extsuite/extman/cgi/

chown -R postfix.postfix /var/www/extsuite/extmail/cgi/


4、導入數據庫

vim docs/extmail.sql

:% s/TYPE/ENGINE/g

vim /etc/my.cnf

# Recommended in standard MySQL setup

#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES //將這行註視掉,重啟mysql,這裏需要註意的是,等數據庫導入成功後,這項是不可以去掉註釋的,不然mysql就啟動不起來了。

mysql -uroot < docs/extmail.sql

mysql -uroot < docs/init.sql


5、創建數據庫用戶extmail並授予權限

GRANT ALL ON extmail.* to extmail@'%' identified by 'extmail';

FLUSH PRIVILEGES;


6、復制四-4提到的配置文件

cd /var/www/extsuite/extman/docs/

cp mysql_virtual_* /etc/postfix/


mkdir /tmp/extman

chown -R postfix.postfix /tmp/extman/


7、啟動postfix、dovecot、saslauthd

ss -tnluo | grep :25

ps aux | grep dovecot

ps aux | grep saslauthd


八、測試

/usr/local/courier-authlib/sbin/authtest -s login [email protected] extmail

mkdir /var/mailbox

chown -R postfix.postfix /var/mailbox/


vim /etc/nginx/fcgi.conf

fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param QUERY_STRING $query_string;

fastcgi_param REQUEST_METHOD $request_method;

fastcgi_param CONTENT_TYPE $content_type;

fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

fastcgi_param REQUEST_URI $request_uri;

fastcgi_param DOCUMENT_ROOT $document_root;

fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param REMOTE_ADDR $remote_addr;

fastcgi_param REMOTE_PORT $remote_port;

fastcgi_param SERVER_ADDR $server_addr;

fastcgi_param SERVER_PORT $server_port;

fastcgi_param SERVER_NAME $server_name;


wget http://www.cpan.org/authors/id/M/MH/MHARNISCH/Unix-Syslog-1.1.tar.gz

tar xf Unix-Syslog-1.1.tar.gz

cd Unix-Syslog-1.1

perl Makefile.PL

make && make install


安裝證書到postfix,並配置postfix

vi /etc/postfix/main.cf

smtpd_use_tls = yes

smtpd_tls_key_file = /etc/postfix/mailkey.pem

smtpd_tls_cert_file = /etc/postfix/mail_signed_cert.pem

smtpd_tls_CAfile = /etc/postfix/cacert.pem


smtp_use_tls = yes

smtp_tls_note_starttls_offer = yes

smtp_tls_key_file = /etc/postfix/mailkey.pem

smtp_tls_cert_file = /etc/postfix/mail_signed_cert.pem

smtp_tls_CAfile = /etc/postfix/cacert.pem


service postfix restart


打開465(smtps)端口,則需要進行進行如下操作

修改/etc/postfix/master.cf文件

在smtp inet n - n - - smtpd行下,加入如下一行:

smtps inet n - n - - smtpd -o smtpd_tls_wrappermode=yes


vi /etc/dovecot/conf.d/10-ssl.conf

ssl = yes

ssl_cert = </etc/pki/tls/certs/server.crt

ssl_key = </etc/pki/tls/certs/server.key


Linux搭建postfix郵件服務器