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

发布时间:2026-01-10 点击:2
在开始配置邮件服务器前,先了解下邮件发送的基本过程和几个概念。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: 753、安装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 25helo mail.sijitao.netmail from: root@sijitao.netrcpt to: abc@sijitao.netdatasubject:hellohello.quit正常的话一封邮件就发abc@sijitao.net到去了。
5、添加sasl认证模块# vi /etc/sysconfig/saslauthd修改:mech=shadow
# vi /etc/sasl2/smtpd.conf log_level:3pwcheck_method:saslauthdmech_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 = yessmtpd_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_destinationsmtpd_sasl_auth_enable = yessmtpd_sasl_local_domain = $myhostnamesmtpd_sasl_security_options = noanonymoussmtpd_sasl_path= smtpdsmtpd_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/authdaemonrcauthmodulelist=\authmysql\authmodulelistorig=\authmysql\daemons=10# vi?/etc/authmysqlrcmysql_server xxxmysql_username xxxmysql_password xxxmysql_port xxxmysql_database xxxmysql_user_table mailboxmysql_crypt_pwfield passwordmysql_uid_field 2525mysql_gid_field 2525mysql_login_field usernamemysql_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 startb、修改smtp认证配置之前smtp认证我们用的是shadow,即linux系统的用户名密码。现在需要改成courier-authlib。
# mkdir -pv /var/mailbox# chown -r postfix /var/mailbox/重新配置smtp 认证,编辑 /etc/sasl2/smtpd.conf ,确保其为以下内容:
pwcheck_method: authdaemondlog_level: 3mech_list:plain loginauthdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socketc、postfix配置添加########################virtual mailbox settings########################virtual_mailbox_base = /var/mailboxvirtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cfvirtual_mailbox_domains = mysql:/etc/postfix/mysql_virtual_domains_maps.cfvirtual_alias_domains =virtual_alias_maps = mysql:/etc/postfix/mysql_virtual_alias_maps.cfvirtual_uid_maps = static:2525virtual_gid_maps = static:2525virtual_transport = virtual##########################quota settings########################message_size_limit = 14336000virtual_mailbox_limit = 20971520d、导入mysqlmysql认证管理这里使用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.pidnginx配置
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。


百度云服务器上怎么部署网站
电脑麦克风显示未插入的详细处理办法【图文】
买云服务器还是树莓派好
站长入门服务器 HostEase美国服务器45美元促销
给别人操作了怕他乱弄东西
收到非法信息提示这个文件包含非法关键字
为什么备案是上传资料负责人扫码进行上传资料是
阿里云服务器和腾讯服务器哪个好