CentOS 6配置Postfix+Mysql认证的邮件服务器步骤

发表时间:2016-03-31 11:43 | 分类:Linux | 浏览:3,492 次

在开始配置邮件服务器前,先了解下邮件发送的基本过程和几个概念。MUA:邮件用户代理,客户端的例如outlook,foxmail,网页版如163邮箱等。MTA:邮件传输代理,MUA写好邮件后通过MTA传输,可能会有多个MTA做relay,这里sendmail和postfix就是。MDA:邮件投递代理,MTA找到目标后把邮件发送指定地方等待用户取走。

一封邮件的基本流程类似如下:

发件人:MUA --发送--> MTA -> 若干个MTA... -> MTA -> MDA <--收取-- MUA:收件人

MUA到MTA,以及MTA到MTA之间使用的协议就是SMTP协议,而收邮件时,MUA到MDA之间使用的协议最常用的是POP3或IMAP。

CentOS中默认的MTA(邮件发送代理)用的是sendmail,但是这个配置比较复杂,这里我选择更加易用的Postfix。

准备

1、美国主机一台,系统CentOS 6

2、自有域名一个

步骤

1、修改hostname

# hostname mail.sijitao.net
# vi /etc/sysconfig/network

修改:HOSTNAME="mail.sijitao.net"

2、配置dns

正向解析到域名管理中分别添加MX指向sijitao.net,mail.sijitao.net的A记录指向主机的ip。

反向解析到主机商中修改,一般国外的主机都可以做。

配置好后检查是否生效,例如:

[root@mail ~]# dig -t mx sijitao.net
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.6 <<>> -t mx sijitao.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56108
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;sijitao.net. IN MX
;; ANSWER SECTION:
sijitao.net. 599 IN MX 10 mail.sijitao.net.
;; Query time: 303 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Thu Mar 31 10:43:40 2016
;; MSG SIZE rcvd: 50
[root@mail ~]# dig -t A mail.sijitao.net
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.6 <<>> -t A mail.sijitao.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 63586
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;mail.sijitao.net. IN A
;; ANSWER SECTION:
mail.sijitao.net. 599 IN A 107.161.16.181
;; Query time: 201 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Thu Mar 31 10:43:48 2016
;; MSG SIZE rcvd: 50
[root@mail ~]# dig -x 107.161.16.181
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.6 <<>> -x 107.161.16.181
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 14444
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;181.16.161.107.in-addr.arpa. IN PTR
;; ANSWER SECTION:
181.16.161.107.in-addr.arpa. 21599 IN PTR mail.sijitao.net.
;; Query time: 83 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Thu Mar 31 10:43:58 2016
;; MSG SIZE rcvd: 75

3、安装postfix

# yum groupinstall "Development Tools"
# yum install mysql-server mysql-devel cyrus-sasl cyrus-sasl-devel cyrus-imapd cyrus-sasl-plain
# 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
# wget https://archive.mgm51.com/mirrors/postfix-source/official/postfix-2.9.15.tar.gz
# tar xf postfix-2.9.15.tar.gz
# cd postfix-2.9.15
# 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 -lm -L/usr/lib64/sasl2 -lsasl2 -lssl -lcrypto'
# make
# make install
# chmod +x /etc/init.d/postfix
# chkconfig --add postfix
# chkconfig --list postfix
# newaliases

按照上面几个步骤操作,postfix很快就可以安装完成。这里采用自己编译的方式,主要是为了加入mysql和sasl等的支持。

4、配置postfix

# cd /etc/postfix
# vi main.cf

更具自己的实际情况,修改以下几项为您需要的配置。
myhostname = mail.sijitao.net
myorigin = sijitao.net
mydomain = sijitao.net
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
mynetworks = 127.0.0.0/8, 107.161.16.181/32

说明:
myorigin参数用来指明发件人所在的域名,即做发件地址伪装;
mydestination参数指定postfix接收邮件时收件人的域名,即您的postfix系统要接收到哪个域名的邮件;
myhostname 参数指定运行postfix邮件系统的主机的主机名,默认情况下,其值被设定为本地机器名;
mydomain 参数指定您的域名,默认情况下,postfix将myhostname的第一部分删除而作为mydomain的值;
mynetworks 参数指定你所在的网络的网络地址,postfix系统根据其值来区别用户是远程的还是本地的,如果是本地网络用户则允许其访问;
inet_interfaces 参数指定postfix系统监听的网络接口;

配置好后重启postfix,然后可以用telnet测试下。

telnet mail.sijitao.net 25
helo mail.sijitao.net
mail from: root@sijitao.net
rcpt to: abc@sijitao.net
data
Subject:hello
hello
.
quit

正常的话一封邮件就发abc@sijitao.net到去了。

5、添加sasl认证模块

# vi /etc/sysconfig/saslauthd

修改:MECH=shadow

# vi /etc/sasl2/smtpd.conf 
log_level:3
pwcheck_method:saslauthd
mech_list:PLAIN LOGIN
# cd /usr/lib64/sasl2/
# ln -s /etc/sasl2/smtpd.conf smtpd.conf

测试命令:testsaslauthd -u username -p password。模块没问题的话会显示ok,这里用户名密码是linux系统的账号密码。

接下来再次修改postfix的main.cf配置,增加如下内容:

############################CYRUS-SASL############################
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_invalid_hostname,reject_non_fqdn_hostname,reject_unknown_sender_domain,reject_non_fqdn_sender,reject_non_fqdn_recipient,reject_unknown_recipient_domain,reject_unauth_pipelining,reject_unauth_destination
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
smtpd_sasl_path= smtpd
smtpd_banner = Welcome to our $myhostname ESMTP,Warning: Version not Available!

修改后重启postfix,再次测试后可以发现发邮件前必须认证,用户名密码就是linux系统的用户名密码。

使用outlook发信报错:Helo command rejected: need fully-qualified hostname

去掉:reject_non_fqdn_hostname

6、结合mysql认证

这里postfix和mysql的认证是通过courier-authlib来实现的。

a、courier-authlib安装

这里安装可能会出现各种问题,如果提示那个库没有,安装即可。如果确定都在了,那么请试着换下版本号。

# yum install libtool-ltdl libtool-ltdl-devel

依赖安装完成,下面是我碰到的错误和解决办法。

# yum install libtool-ltdl libtool-ltdl-devel
# tar xf courier-authlib-0.66.1.tar.bz2
# cd courier-authlib-0.66.1
# ./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 \
    --with-mailgroup=postfix \
    --with-ltdl-lib=/usr/lib64 \
    --with-ltdl-include=/usr/include

报错: configure: error: invalid ltdl library directory: `/usr/lib64/'

# wget http://ftpmirror.gnu.org/libtool/libtool-2.4.6.tar.gz
# tar zxvf libtool-2.4.6.tar.gz 
# ./configure --prefix=/usr/local/libtool-2.4.6 --enable-ltdl-install
# make
# make install

再次编译安装成功。

# ./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 \
    --with-mailgroup=postfix \
    --with-ltdl-lib=/usr/local/libtool-2.4.6/lib \
    --with-ltdl-include=/usr/local/libtool-2.4.6/include

安装

# make
# make install
# chmod 755 /usr/local/courier-authlib/var/spool/authdaemon
# cp /etc/authdaemonrc.dist /etc/authdaemonrc
# cp /etc/authmysqlrc.dist /etc/authmysqlrc

修改courier-authlib的配置,类似如下:

# vi /etc/authdaemonrc
authmodulelist="authmysql"
authmodulelistorig="authmysql"
daemons=10

# vi /etc/authmysqlrc
MYSQL_SERVER xxx
MYSQL_USERNAME xxx
MYSQL_PASSWORD xxx
MYSQL_PORT xxx
MYSQL_DATABASE xxx
MYSQL_USER_TABLE mailbox
MYSQL_CRYPT_PWFIELD password
MYSQL_UID_FIELD 2525
MYSQL_GID_FIELD 2525
MYSQL_LOGIN_FIELD username
MYSQL_HOME_FIELD concat('/var/mailbox/',homedir)
MYSQL_MAILDIR_FIELD concat('/var/mailbox/',maildir)

配置修改好后开启courier-authlib服务。

# cp courier-authlib.sysvinit /etc/rc.d/init.d/courier-authlib
# chmod 755 /etc/init.d/courier-authlib
# chkconfig --level 2345 courier-authlib on
# service courier-authlib start

b、修改smtp认证配置

之前smtp认证我们用的是shadow,即linux系统的用户名密码。现在需要改成courier-authlib。

# mkdir -pv /var/mailbox
# chown -R postfix /var/mailbox/

重新配置SMTP 认证,编辑 /etc/sasl2/smtpd.conf ,确保其为以下内容:

pwcheck_method: authdaemond
log_level: 3
mech_list:PLAIN LOGIN
authdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket

c、postfix配置添加

########################Virtual Mailbox Settings########################
virtual_mailbox_base = /var/mailbox
virtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf
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
##########################QUOTA Settings########################
message_size_limit = 14336000
virtual_mailbox_limit = 20971520

d、导入mysql

mysql认证管理这里使用extman来完成,从网上下载extman。添加合适的数据库名后导入输入extman中docs/extmail.sql 和 docs/init.sql两个sql、创建mysql的用户名密码。

复制mysql_virtual_alias_maps.cf、mysql_virtual_domains_maps.cf、mysql_virtual_limit_maps.cf、mysql_virtual_mailbox_maps.cf、mysql_virtual_sender_maps.cf 这几个文件到/etc/postfix目录,根据实际情况修改数据库连接相关信息。

e、修改postfix,去掉中心域

注销myhostname、mydomain、myorigin、mydestination ,重启postfix。

7、nginx extman 安装

到这里,postfix和mysql的认证已经配置完成,不过为了管理方便,还是建议安装extman的web界面。

# yum install spawn-fcgi fcgi-devel fcgi

如果无法安装可以执行一下命令

yum install epel-release
或
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

安装fcgiwrap,fcgiwrap下载地址 http://github.com/gnosek/fcgiwrap/tarball/master。版本用的是fcgiwrap-1.0.1 ,其他我这边都出错了。

# tar zxvf 你下载的版本.tar.gz 
# cd gnosek-fcgiwrap-1328862/ 
# autoreconf -i 
# ./configure 
# make 
# cp fcgiwrap /usr/local/bin/

启动fcgiwrap

spawn-fcgi -f /usr/local/bin/fcgiwrap -a 127.0.0.1 -p 9001 -F 3 -P /var/run/fastcgi-c.pid

nginx配置

log_format extmanlog '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';

server
{
    listen       80;
    server_name  extman.sijitao.net;
    index index.html index.htm index.php index.cgi;
    root  /home/wwwroot/extsuite/extman/html/;

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        rewrite ^/extman/default/images/(.*) /default/images/$1 break;
        expires     30d;
    }

    location ~ .*\.(js|css)?$
    {
        rewrite ^/extman/default/(.*) /default/$1 break;
        expires     1h;
    }

    location /extman/cgi/ {
        root  /home/wwwroot/extsuite/extman/cgi;
        fastcgi_pass          127.0.0.1:9001;
        fastcgi_index         index.cgi;
        rewrite ^/extman/cgi/(.*)\.cgi /$1.cgi break;
        include            fastcgi.conf;
    }

   access_log  /home/wwwlogs/extmanlog.log  extmanlog;
}

nginx安装完成,打开页面后碰到几个问题。

a、报错:Can't locate FCGI.pm in
yum install fcgi-perl

b、报错:Can't locate DBD/mysql.pm
yum install perl-DBD-MySQL

以下perl也安装

yum -y install perl-CGI

yum -y install perl-CGI-Session

yum -y install perl-Unix-Syslog

c、报错:Can't open /tmp/extman//sid_68bed5adf6321a50972fecd642e94805, No such file or directory
mkdir -pv /tmp/extman
chown www:www /tmp/extman/

最后,登录extman,添加域名和用户,发送一封邮件测试下吧。如果需要接受邮件,可以再配置一个dovecot。

本文标签:,

本文链接:https://www.sijitao.net/2396.html

欢迎您在本博客中留下评论,如需转载原创文章请注明出处,谢谢!

已经有5个回复
Comment (5)
Trackbacks (0)
  1. 米粒博客  ( 2016.04.2 22:16 ) : #-9

    明天放假 没事 折腾折腾 谢谢分享

  2. 薛峰博客  ( 2016.04.5 22:03 ) : #-8

    尽力了,还是看不懂

    • 章郎虫  ( 2016.04.5 22:29 ) :

      我也不懂,慢慢学习中。

      • 薛峰博客  ( 2016.04.19 02:13 ) :

        我一直想接触点其它内容的东西,SEO不打算怎么写了,打算重点写网络推广方面的内容,这种内容对用户的价值更加的高一些。

  3. 五冶大学  ( 2016.06.2 10:35 ) : #-7

    看不懂。。。。。。。。

  1. 还没有Trackbacks
一键脚本 博客历程 留言联系 文章归档 网站地图 谷歌地图
Copyright © 2010-2024 章郎虫博客 All Rights Reserved.