CentOS Linux安装postfix邮件服务器

postfix是一个高效的邮件服务器,配置比sendmail简单,这里使用LAMP+postfix+dovecot+SquirrelMail搭建提供SMTP/POP3/IMAP的简单邮件服务。系统是CentOS6.2。

简单说明:
postfix是一个MTA即邮件传输代理。作为收件服务器。
dovecot是一个开源的IMAP和POP3邮件服务器。作为发件服务器。

准备工作:
1、卸载sendmail

yum remove sendmail

2、安装LAMP

yum install httpd mysql mysql-devel mysql-server php php-mysql php-bcmath php-gd php-mbstring php-xml php-imap

配置LAMP过程略过。

一、安装postfix
使用源码编译安装。
1、建立postfix所需的用户和组
建立postfix、postdrop组和postfix用户:

groupadd -g 2001 postfix
useradd -g postfix -u 1001 -s /sbin/nologin -M postfix
groupadd -g 2002 postdrop

2、安装必备软件包

yum install db*-devel

3、到官网http://www.postfix.org/ 找一个下载点下载postfix,目前稳定版为Postfix 2.9

cd /tmp
wget ftp://ftp.reverse.net/pub/postfix/official/postfix-2.9.3.tar.gz
tar -zxvf postfix-2.9.3.tar.gz
cd postfix-2.9.3

更新makefile文件,添加mysql、sasl认证支持:

make -f Makefile.init makefiles \
'CCARGS=-DHAS_MYSQL -I/usr/include/mysql \
-DUSE_SASL_AUTH \
-DDEF_SERVER_SASL_TYPE=\"dovecot\"' \
'AUXLIBS=-L/usr/lib/mysql -lmysqlclient -lz -lm'

备注:-DUSE_SASL_AUTH表示启用SASL认证,-DDEF_SERVER_SASL_TYPE=\”dovecot\”表示使用dovecot做SASL模块。

继续安装:

make
make install

4、make install执行后会调用postfix-install脚本
这里设置如下,空白为直接按回车。

install_root: [/]
tempdir: [/tmp/postfix-2.9.3] /tmp/postfix
config_directory: [/etc/postfix]
command_directory: [/usr/sbin]
daemon_directory: [/usr/libexec/postfix]
data_directory: [/var/lib/postfix]
html_directory: [no] /var/www/html/postfix
mail_owner: [postfix]
mailq_path: [/usr/bin/mailq]
manpage_directory: [/usr/local/man]
newaliases_path: [/usr/bin/newaliases]
queue_directory: [/var/spool/postfix]
readme_directory: [no]
sendmail_path: [/usr/sbin/sendmail]
setgid_group: [postdrop]

postfix安装在/usr/sbin/postfix,随后可以访问http://IP/postfix,查看Postfix Documentation。

5、检验postfix是否支持Dovecot SASL

postconf -a

结果显示dovecot,说明支持。

其它postconf命令:
postconf -n
postconf -m
postconf -d

二、安装dovecot
官网:http://dovecot.org,目前稳定版为dovecot 2.1.8。

1、安装必备软件包

yum install pam pam-devel

2、编译安装dovecot

cd /tmp
wget http://dovecot.org/releases/2.1/dovecot-2.1.8.tar.gz
tar -zxvf dovecot-2.1.8.tar.gz
cd dovecot-2.1.8
./configure --with-sql --with-mysql --with-ssl=openssl --with-pam
make
make install

dovecot安装在/usr/local/sbin/dovecot。

三、配置postfix
这里域名为live-in.org,FQDN(fully-qualified domain name)为mail.live-in.org。

0、修改主机名

hostname mail.live-in.org


vim /etc/hosts
127.0.0.1 mail.live-in.org localhost

1、编辑配置文件/etc/postfix/main.cf:

cp /etc/postfix/main.cf /etc/postfix/main.cf.bak
vim /etc/postfix/main.cf

1)设置邮件系统的主机名(FQDN)
找到:

#myhostname = host.domain.tld

修改为相应的域名:

myhostname = mail.live-in.org

2)设置本地域名
默认是将myhostname的第一部分去掉作为mydomain。
找到:

#mydomain = domain.tld

修改为相应的域名:

mydomain = live-in.org

3)设置发件人所在域名
找到:

#myorigin = $mydomain

去掉注释:

myorigin = $mydomain

这里和mydomain设置成一样。

4)设置postfix要接收邮件的域名
找到:

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

去掉注释:

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

5)不传递来自其它主机的邮件
找到:

#mynetworks_style = host

去掉注释:

mynetworks_style = host

6)设置postfix监听的网络接口
找到:

#inet_interfaces = all

去掉注释:

inet_interfaces = all

7)找到:

#alias_maps = hash:/etc/aliases

去掉注释:

alias_maps = hash:/etc/aliases

是为了防止出现警告信息:warning: dict_nis_init: NIS domain name not set – NIS lookups disabled。

8)设置邮件存储位置和格式
找到:

#home_mailbox = Maildir/

去掉注释:

home_mailbox = Maildir/

2、设置使用dovecot作为SASL认证,在最后加入:

smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination

3、启动postfix

/usr/sbin/postfix start

四、配置dovecot
1、复制样例配置文件

cd /usr/local/etc/dovecot
cp -R /usr/local/share/doc/dovecot/example-config/* /usr/local/etc/dovecot/

2、编辑配置文件dovecot.conf

vim dovecot.conf

找到以下几句将前面的注释去掉:

protocols = imap pop3 lmtp
listen = *, :: 修改为 listen = *
base_dir = /var/run/dovecot/

3、编辑配置文件10-master.conf

vim conf.d/10-master.conf

1)找到service auth块中的以下两句:

#unix_listener /var/spool/postfix/private/auth {
#mode = 0666

修改为:

unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}

2)找到:

default_login_user = dovenull
default_internal_user = dovecot

将注释去掉。

4、编辑配置文件10-auth.conf

vim conf.d/10-auth.conf

1)找到:

#disable_plaintext_auth = yes

修改为:

disable_plaintext_auth = no

允许明文密码验证。

2)找到:

auth_mechanisms = plain

修改为:

auth_mechanisms = plain login

使Outlook能使用与接收邮件服务器相同的设置认证SMTP。

5、编辑配置文件10-mail.conf

vim conf.d/10-mail.conf

找到:

#mail_location = maildir:~/Maildir

去掉注释:

mail_location = maildir:~/Maildir

6、建立dovecot用户

useradd -M -s /sbin/nologin dovenull
useradd -M -s /sbin/nologin dovecot

7、建立PAM认证文件

vim /etc/pam.d/dovecot

添加:

auth    required        pam_unix.so
account required        pam_unix.so


8、查看配置文件是否有错

dovecot -n

没有报错即可。

9、启动dovecot

dovecot

报错:

doveconf: Fatal: Error in configuration file /usr/local/etc/dovecot/conf.d/10-ssl.conf line 12: ssl_cert: Can’t open file /etc/ssl/certs/dovecot.pem: No such file or directory

是找不到/etc/ssl/certs/dovecot.pem文件。

进入dovecot解压后的源代码目录:

cd /tmp/dovecot-2.1.8/doc
mkdir -p /etc/ssl/certs
mkdir -p /etc/ssl/private
sh mkcert.sh

再次启动dovecot

dovecot

10、日志文件
这里postfix和dovecot日志都在/var/log/maillog里。

11、在域名的DNS解析处添加一个mail二级域名的A记录,和MX邮件交换记录。
自建的邮箱发给gmail和hotmail认为是垃圾邮件。还要做一个邮箱域名反向解析。即mail.xxx.com的A记录指向邮件服务器的IP,还要把该IP的PRT指向到mail.xxx.com。IP反向解析需要联系上级的ISP来做了。因为我们对邮件域名有管理权,但对这个IP没有管理权。

12、整理下思路
这里设置的邮件用户为Linux系统内的实际用户,使用pam验证密码,邮件存储在~/Maildir(家目录下的Maildir目录中)。

但是root用户没有/home/root/Maildir这个目录如何收发邮件呢?

vim /etc/aliases

在最后加入:

root:     test3


做一个别名重定向,随后更新资料库:

newaliases

建立test3用户:

useradd test3

另外好像root用户不能用POP3、IMAP登录发邮件?

五、安装SquirrelMail
SquirrelMail是一个基于PHP的webmail邮件系统。目前稳定版为squirrelmail-webmail-1.4.22。

1、下载SquirrelMail

cd /tmp
wget http://www.squirrelmail.org/countdl.php?fileurl=http%3A%2F%2Fprdownloads.sourceforge.net%2Fsquirrelmail%2Fsquirrelmail-webmail-1.4.22.tar.gz
tar -zxvf squirrelmail-webmail-1.4.22.tar.gz
mv squirrelmail-webmail-1.4.22 /var/www/html/webmail
chown -R apache:apache /var/www/html/webmail

2、配置参数

cd /var/www/html/webmail
./configure

会看到一个可选择的菜单,要修改几个地方:
输入D(Set pre-defined settings for specific IMAP servers),再输入dovecot
输入2(Server Settings),再输入1,设置Domain
输入10(Languages),再输入1,设置Default Language为zh_CN
输入10(Languages),再输入2,设置Default Charset为UTF8

建立SquirrelMail Data和Attachment:

mkdir -p /var/local/squirrelmail/data/
mkdir -p /var/local/squirrelmail/attach/
chown -R apache:apache /var/local/squirrelmail

访问:http://IP/webmail 登录SquirrelMail。

参考资料:
http://www.postfix.org/STANDARD_CONFIGURATION_README.html
http://wiki2.dovecot.org/HowTo/PostfixAndDovecotSASL
http://www.postfix.org/MYSQL_README.html
http://blog.csdn.net/stuartjing/article/details/6792508
http://hi.baidu.com/delphiss/blog/item/38571c94f7a7d50e7af48052.html

《CentOS Linux安装postfix邮件服务器》上的15个想法

  1. 你好,请问我照着您到经验配置,为什么我在更新makefile文件到时候会报错呢:
    make -f Makefile.in MAKELEVEL= Makefiles
    (echo “# Do not edit — this file documents how Postfix was built for your machine.”; /bin/sh makedefs) >makedefs.tmp
    makedefs: line 602: gcc: command not found
    make: *** [Makefiles] 错误 1
    make: *** [makefiles] 错误 2
    之前系统自带的有postfix2.6.6,我用yum -y remove postfix删除后就按照文章说到开始配置的.还有,如果postfix采用yum安装到话,怎么启动dovecot sasl认证呢?最近在学习在centos6.2下配置postfix服务器,由于是新手,遇到不少问题,之前用yum安装时,基本配置完后,telnet localhost 25 :connection closed by foreign host。然后在maillog里说,localhost postfix/smtpd :fatal: no sasl authentication mechanisms.是不是因为没有启动dovecot sasl到认证呢?我在dovecot.conf和main.cf中都进行了相应到配置到,不知道您能不能帮我分析分析呢?非常感谢,希望您能给予帮助.

    1. 1、更新makefile报错是没有安装gcc编译器,执行yum install gcc gcc-c++
      2、网上查了说CentOS默认源yum安装的postfix不包括对mysql的支持,所以我使用了编译安装postfix
      3、“no sasl authentication mechanisms”,应该是postfix没有使用到dovecot sasl认证,因为postfix和dovecot版本不同配置的地方也会有不同,你找找yum安装的这个版本的配置方法,其实要改的项都是差不多的

    2. 其实我这里只有SquirrelMail用到mysql,postfix没有用到mysql,本来想搞虚拟域和虚拟用户的,后来没搞,所以用yum安装postfix也是可以的

  2. 不添加虚拟域及其虚拟用户,SquirrelMail的默认用户名和密码是什么?通过postfixadmin创建的用户名和密码登陆总是报错

    1. SquirrelMail只是一个web的邮件客户端,如果邮件系统上没有添加过用户当然是登录不了的。要查下虚拟域和虚拟用户设置,验证用户的地方有问题

  3. * BAD [ALERT] Plaintext authentication not allowed without SSL/TLS, but your client did it anyway. If anyone was listening, the password was exposed.
    C2 NO [PRIVACYREQUIRED] Plaintext authentication disallowed on non-secure (SSL/TLS) connections

    1. outlook用的是什么版本?看日志是客户端设置需要ssl支持,而服务器没有,在outlook里去掉收发邮件需要ssl的设置

  4. 请问webmail测试出现这个错误:ERROR:
    Message not sent. Server replied:
    Transaction failed
    554 5.7.1 : Relay access denied 怎么解决呢

    1. 你好,SquirrelMail是一个收发邮件的工具,相当于outlook、foxmail的作用,不用配置DNS吧,只要web上有IP或域名能够访问

  5. 楼主,你好。想咨询一下,想咨询一下postfix和squirreMail的关系是什么,如果squirreMail只是一个收发邮件的客户端,那么如果我不安装这个的话,能否直接通过终端 来发邮件呢? 求指导终端如何创建邮箱用户和发邮件的步骤 :wink: ,新手学习,谢谢

    1. 你好,squirreMail是一个邮件客户端,搭建了postfix后,可以用其它邮件客户端如outlook,foxmail等连接。创建步骤我也没研究过,应该搭建好postfix后,主要是用户认证上的设置

评论已关闭。