CentOS7下搭建postfix邮件服务器实现extmail的web访问
转自
http://linuxu.blog.iyunv.com/9471357/1641436
CentOS7.1最新1503版本最小化安装,基于lnmp,nginx与mysql为yum安装,当然没有安装php,其他编译所需要的全是官网上下载的最新稳定版,搭建的过程中出了很多错误,经过几天的实验,最后终于搭建成了,这次博客也是重新搭建,边写边搭建,如果再次出现错误的话,会将错误一并写出来。
一、 装备工作
1、防止不必要的麻烦关闭Selinux
1setenforce 0关闭防火墙
1systemctl stop firewalld2、清空iptables
1234iptables -P INPUT ACCEPTiptables -Fiptables -Xiptables -L查看是否全部为ACCEPT
3、由于CentOS7默认安装的是MariaDB,所以要添加MySQL的yum源,有些编译需要的devel包只有epel有,所以我们把epel源也一并添加
12345yum install -y wgetwget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpmrpm -ivh mysql-community-release-el7-5.noarch.rpm wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpmrpm -ivh epel-release-latest-7.noarch.rpm二、安装postfix
1、安装编译及其他所需要的包,我这总共有124个包需要安装,mysql-server比较大,速度也比较慢
1yum install nginx vim gcc gcc-c++ openssl openssl-devel db4-devel ntpdate mysql mysql-devel mysql-server bzip2 php-mysql cyrus-sasl-md5 perl-GD perl-DBD-MySQL perl-GD perl-CPAN perl-CGI perl-CGI-Session cyrus-sasl-lib cyrus-sasl-plain cyrus-sasl cyrus-sasl-devel libtool-ltdl-devel telnet mail libicu-devel -y2、编译安装postfix
1、卸载系统自带的postfix,删除postfix用户,重新指定uid、gid创建新用户postfix,postdrop
1234567yum remove postfix -yuserdel postfixgroupdel postdropgroupadd -g 2525 postfixuseradd -g postfix -u 2525 -s /sbin/nologin -M postfixgroupadd -g 2526 postdropuseradd -g postdrop -u 2526 -s /sbin/nologin -M postdrop2、下载源码包并解压编译
12345wget http://mirrors.go-parts.com/postfix/source/official/postfix-3.0.1.tar.gztar xf postfix-3.0.1.tar.gzcd postfix-3.0.1make 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 installmake install的时候会有个交互式的界面,自定义一些目录,我们这里只更改第二项临时文件目录,其他的全部默认。
123456789101112131415Please specify the prefix for installed file names. Specify this ONLYif you are building ready-to-install packages for distribution to OTHERmachines. See PACKAGE_README for instructions.install_root: [/] Please specify a directory for scratch files while installing Postfix. Youmust have write permission in this directory.tempdir: [/root/postfix-3.0.1] /tmp/extmail……………………………………………………shlib_directory: [no] Please specify the final destination directory for non-executable filesthat are shared among multiple Postfix instances, such as postfix-files,dynamicmaps.cf, as well as the multi-instance template files main.cf.protoand master.cf.proto.meta_directory: [/etc/postfix]3、更改目录属主属组
1234chown -R postfix:postdrop /var/spool/postfixchown -R postfix:postdrop /var/lib/postfix/chown root /var/spool/postfixchown -R root /var/spool/postfix/pid4,修改postfix的配置文件
12345678910[iyunv@localhost ~]# vim /etc/postfix/main.cfmyhostname = 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
1、yum安装
1[iyunv@localhost ~]# yum install -y dovecot dovecot-mysql2、配置dovecot
12345678910111213141516171819202122[iyunv@localhost ~]# cd /etc/dovecot/[iyunv@localhost dovecot]# vim dovecot.conf //直接在配置文件最后添加即可protocols = imap pop3!include conf.d/*.conflisten = *base_dir = /var/run/dovecot/[iyunv@localhost dovecot]# cd conf.d/[iyunv@localhost conf.d]# vim 10-auth.confdisable_plaintext_auth = no[iyunv@localhost conf.d]# vim 10-mail.confmail_location = maildir:~/Maildirmail_location = maildir:/var/mailbox/%d/%n/Maildirmail_privileged_group = mail[iyunv@localhost conf.d]# vim 10-ssl.confssl = no[iyunv@localhost conf.d]# vim 10-logging.conf og_path = /var/log/dovecot.loginfo_log_path = /var/log/dovecot.infolog_timestamp = "%Y-%m-%d %H:%M:%S "[iyunv@localhost conf.d]# cp auth-sql.conf.ext auth-sql.conf[iyunv@localhost conf.d]# vim auth-sql.confpassdb { driver = sql # Path for SQL configuration file, see example-config/dovecot-sql.conf.ext args = /etc/dovecot/dovecot-sql.conf.ext}userdb { driver = sql args = /etc/dovecot/dovecot-sql.conf.ext}3、编辑dovecot通过mysql认证的配置文件
123456[iyunv@localhost conf.d]# vim /etc/dovecot-mysql.confdriver = mysqlconnect = host=localhost dbname=extmail user=extmail password=extmaildefault_pass_scheme = CRYPTpassword_query = SELECT username AS user,password AS password FROM mailbox WHERE username = '%u'user_query = SELECT maildir, uidnumber AS uid, gidnumber AS gid FROM mailbox WHERE username = '%u'四、安装courier-authlib
1、下载解压并并编译
1234567891011121314151617[iyunv@localhost ~]# wget https://sourceforge.net/projects/courier/files/authlib/0.66.2/courier-authlib-0.66.2.tar.bz2[iyunv@localhost ~]# tar xf courier-authlib-0.66.2.tar.bz2[iyunv@localhost ~]# cd courier-authlib-0.66.2[iyunv@localhost ~]# ./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这里会报错,configure: error: The Courier Unicode Library 1.2 appears not to be installed.提示Courier Unicode Library没有安装,我们下载courier-unicode-1.2并编译安装。
12345[iyunv@localhost ~]# wget https://sourceforge.net/projects/courier/files/courier-unicode/1.2/courier-unicode-1.2.tar.bz2[iyunv@localhost ~]# tar xf courier-unicode-1.2.tar.bz2 [iyunv@localhost ~]# cd courier-unicode-1.2[iyunv@localhost courier-unicode-1.2]# ./configure[iyunv@localhost courier-unicode-1.2]# make && make install再次编译courier-authlib就没问题了,别忘了make && makeinstall
2、配置courier-authlib
12345678910111213141516171819202122[iyunv@localhost courier-authlib-0.66.2]# chmod 755 /usr/local/courier-authlib/var/spool/authdaemon[iyunv@localhost courier-authlib-0.66.2]# cp /etc/authdaemonrc.dist /etc/authdaemonrc[iyunv@localhost courier-authlib-0.66.2]# cp /etc/authmysqlrc.dist /etc/authmysqlrc[iyunv@localhost courier-authlib-0.66.2]# vim /etc/authdaemonrc //配置文件里的验证方法比较多,我们这里只使用authmysqlauthmodulelist="authmysql"authmodulelistorig="authmysql"[iyunv@localhost courier-authlib-0.66.2]# vim /etc/authmysqlrc //直接添加到配置文件尾部,然后去上面将响应系统默认的注视掉,或者删除即可MYSQL_SERVER localhostMYSQL_USERNAME extmailMYSQL_PASSWORD extmailMYSQL_SOCKET /var/lib/mysql/mysql.sockMYSQL_PORT 3306MYSQL_DATABASE extmailMYSQL_USER_TABLE mailboxMYSQL_CRYPT_PWFIELD passwordDEFAULT_DOMAIN test.comMYSQL_UID_FIELD '2525'MYSQL_GID_FIELD '2525'MYSQL_LOGIN_FIELD usernameMYSQL_HOME_FIELD concat('/var/mailbox/',homedir)MYSQL_NAME_FIELD nameMYSQL_MAILDIR_FIELD concat('/var/mailbox/',maildir)3、courier-authlib添加服务启动脚本及其他
12345678[iyunv@localhost courier-authlib-0.66.2]# cp courier-authlib.sysvinit /etc/init.d/courier-authlib[iyunv@localhost courier-authlib-0.66.2]# chmod +x /etc/init.d/courier-authlib[iyunv@localhost courier-authlib-0.66.2]# chkconfig --add courier-authlib[iyunv@localhost courier-authlib-0.66.2]# chkconfig courier-authlib on[iyunv@localhost courier-authlib-0.66.2]# echo "/usr/local/courier-authlib/lib/courier-authlib" >> /etc/ld.so.conf.d/courier-authlib.conf[iyunv@localhost courier-authlib-0.66.1]# ldconfig[iyunv@localhost courier-authlib-0.66.1]# service courier-authlib startStarting Courier authentication services: authdaemond
4、smtp以及虚拟用户相关的设置
1234567891011121314151617181920212223[iyunv@localhost ~]# vim /usr/lib64/sasl2/smtpd.conf //文件不存在,要自己创建pwcheck_method: authdaemondlog_level: 3mech_list: PLAIN LOGINauthdaemond_path:/usr/local/courier-authlib/var/spool/authdaemon/socket[iyunv@localhost ~]# vim /etc/postfix/main.cf##postfix支持SMTP##smtpd_sasl_auth_enable = yessmtpd_sasl_local_domain = ''smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destinationbroken_sasl_auth_clients=yessmtpd_client_restrictions = permit_sasl_authenticatedsmtpd_sasl_security_options = noanonymous##postfix支持虚拟用户##virtual_mailbox_base = /var/mailboxvirtual_mailbox_maps = mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf //这里的配置文件需在后面extman里复制过来virtual_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五、安装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
1、创建目录并解压
123[iyunv@localhost ~]# mkdir -p /var/www/extsuite[iyunv@localhost ~]# tar xf extmail-1.2.tar.gz -C /var/www/extsuite/[iyunv@localhost ~]# mv /var/www/extsuite/extmail-1.2/ /var/www/extsuite/extmail2、更改extmail的配置文件
123456789101112131415161718[iyunv@localhost ~]# cd /var/www/extsuite/extmail[iyunv@localhost extmail]# cp webmail.cf.default webmail.cf[iyunv@localhost extmail]# vim webmail.cfSYS_SESS_DIR = /tmp/extmailSYS_UPLOAD_TMPDIR = /tmp/extmail/uploadSYS_USER_LANG = zh_CNSYS_MIN_PASS_LEN = 8SYS_MAILDIR_BASE = /var/mailboxSYS_MYSQL_USER = extmailSYS_MYSQL_PASS = extmailSYS_MYSQL_DB = extmailSYS_MYSQL_HOST = localhostSYS_MYSQL_SOCKET = /var/lib/mysql/mysql.sockSYS_MYSQL_TABLE = mailboxSYS_MYSQL_ATTR_USERNAME = usernameSYS_MYSQL_ATTR_DOMAIN = domainSYS_MYSQL_ATTR_PASSWD = passwordSYS_AUTHLIB_SOCKET = /usr/local/courier-authlib/var/spool/authdaemon/socket3、建立临时文件目录与session目录
12[iyunv@localhost extmail]# mkdir -p /tmp/extmail/upload[iyunv@localhost extmail]# chown -R postfix.postfix /tmp/extmail/六、安装extman
1、解压extman
123[iyunv@localhost ~]# tar xf extman-1.1.tar.gz -C /var/www/extsuite/[iyunv@localhost ~]# cd /var/www/extsuite/[iyunv@localhost extsuite]# mv extman-1.1/ extman2、更改extman配置文件
12[iyunv@localhost extsuite]# cd extman/[iyunv@localhost extman]# cp webman.cf.default webman.cf3、更改cgi目录属主属组
12[iyunv@localhost extman]# chown -R postfix.postfix /var/www/extsuite/extman/cgi/[iyunv@localhost extman]# chown -R postfix.postfix /var/www/extsuite/extmail/cgi/4、导入数据库
由于数据库不能识别TYPE=MyISAM,所以这里直接导入会出错,先编辑extmail.sql数据库文件,将TYPE=MyISAM更改为ENGINE=MyISAM
12[iyunv@localhost extman]# vim docs/extmail.sql :% s/TYPE/ENGINE/g共有五处修改
导入还是会报错
12[iyunv@localhost extman]# mysql -uroot < docs/extmail.sql ERROR 1364 (HY000) at line 31: Field 'ssl_cipher' doesn't have a default value此处需要修改my.cnf配置文件
12345[iyunv@localhost extman]# vim /etc/my.cnf# Recommended in standard MySQL setup#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES //将这行注视掉,重启mysql,这里需要注意的是,等数据库导入成功后,这项是不可以去掉注释的,不然mysql就启动不起来了。再次导入数据库[iyunv@localhost extman]# mysql -uroot < docs/extmail.sql [iyunv@localhost extman]# mysql -uroot < docs/init.sql5、创建数据库用户extmail并授予权限
1234mysql> GRANT ALL ON extmail.* to extmail@'%' identified by 'extmail'; //这里直接在授权任何权限在任意地址上了Query OK, 0 rows affected (0.00 sec)mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)6、复制四-4提到的配置文件
12[iyunv@localhost ~]# cd /var/www/extsuite/extman/docs/[iyunv@localhost docs]# cp mysql_virtual_* /etc/postfix/6、为extman创建临时目录
12[iyunv@localhost extman]# mkdir /tmp/extman[iyunv@localhost extman]# chown -R postfix.postfix /tmp/extman/7、启动postfix、dovecot、saslauthd
123456789101112131415[iyunv@localhost extman]# ss -tnluo | grep :25tcp LISTEN 0 100 *:25 *:* [iyunv@localhost extman]# ps aux | grep dovecotroot 2220 0.1 0.3 15616 1508 ? Ss 03:02 0:00 /usr/sbin/dovecot -Fdovecot 2223 0.0 0.1 9312 1004 ? S 03:02 0:00 dovecot/anvilroot 2224 0.0 0.2 9440 1172 ? S 03:02 0:00 dovecot/logroot 2226 0.0 0.4 12428 2184 ? S 03:02 0:00 dovecot/configroot 2240 0.0 0.1 112640 972 pts/0 R+ 03:02 0:00 grep --color=auto dovecot[iyunv@localhost extman]# ps aux | grep saslauthdroot 2230 0.0 0.1 71944 916 ? Ss 03:02 0:00 /usr/sbin/saslauthd -m /run/saslauthd -a pamroot 2231 0.0 0.1 71944 676 ? S 03:02 0:00 /usr/sbin/saslauthd -m /run/saslauthd -a pamroot 2232 0.0 0.1 71944 676 ? S 03:02 0:00 /usr/sbin/saslauthd -m /run/saslauthd -a pamroot 2233 0.0 0.1 71944 676 ? S 03:02 0:00 /usr/sbin/saslauthd -m /run/saslauthd -a pamroot 2234 0.0 0.1 71944 676 ? S 03:02 0:00 /usr/sbin/saslauthd -m /run/saslauthd -a pamroot 2242 0.0 0.1 112640 668 pts/0 R+ 03:03 0:00 grep --color=auto saslauthd七、测试
1、测试虚拟用户
1234567891011[iyunv@localhost courier-authlib-0.66.2]# /usr/local/courier-authlib/sbin/authtest -s login postmaster@extmail.org extmailAuthentication succeeded. //显示这个表示成功,测试时使用的是postmaster@extmail.org,因为我们导入的数据库init.sql里面自带了这个。Authenticated: postmaster@extmail.org (uid 2525, gid 2525)Home Directory: /var/mailbox/extmail.org/postmaster //这里需要注意/var/mailbox这个目录现在我们还没有创建,后面web访问的时候如果没有会报错,所以提前创建。 Maildir: /var/mailbox/extmail.org/postmaster/Maildir/ Quota: (none) Encrypted Password: $1$phz1mRrj$3ok6BjeaoJYWDBsEPZb5C0 Cleartext Password: extmail Options: (none)[iyunv@localhost courier-authlib-0.66.2]# mkdir /var/mailbox[iyunv@localhost courier-authlib-0.66.2]# chown -R postfix.postfix /var/mailbox/不创建的话会报错
2、测试smtp发信
1234567891011121314151617181920[iyunv@localhost ~]# printf "postmaster@extmail.org" | openssl base64cG9zdG1hc3RlckBleHRtYWlsLm9yZw==[iyunv@localhost ~]# printf "extmail" | openssl base64ZXh0bWFpbA==[iyunv@localhost ~]# telnet localhost 25Trying ::1...telnet: connect to address ::1: Connection refusedTrying 127.0.0.1...Connected to localhost.Escape character is '^]'.220 mail.daen.com ESMTP Postfixauth login334 VXNlcm5hbWU6cG9zdG1hc3RlckBleHRtYWlsLm9yZw==334 UGFzc3dvcmQ6ZXh0bWFpbA==235 2.7.0 Authentication successful //成功quit221 2.0.0 ByeConnection closed by foreign host.八、启动nginx实现web访问
1、nginx本身并不能解析cgi,extmail自带了解析cgi的程序,但是有些地方需要修改下
123[iyunv@localhost ~]# vim /var/www/extsuite/extmail/dispatch-initSU_UID=postfixSU_GID=postfix启动dispatch-init
12[iyunv@localhost ~]# /var/www/extsuite/extmail/dispatch-init startStarting extmail FCGI server...启动cmdserver,不启动会出现Connection refused
12[iyunv@localhost ~]# /var/www/extsuite/extman/daemon/cmdserver -v -d loaded ok2、添加nginx虚拟主机
1[iyunv@localhost ~]# vim /etc/nginx/conf.d/extmail.conf12345678910111213141516171819202122232425server { listen 8080; server_name mail.everyoo.com; index index.html index.htm index.php index.cgi; root /var/www/extsuite/extmail/html/; location /extmail/cgi/ { fastcgi_pass 127.0.0.1:8888; fastcgi_index index.cgi; fastcgi_param SCRIPT_FILENAME /var/www/extsuite/extmail/cgi/$fastcgi_script_name; include fcgi.conf; } location /extmail/ { alias /var/www/extsuite/extmail/html/; } location /extman/cgi/ { fastcgi_pass 127.0.0.1:8888; fastcgi_index index.cgi; fastcgi_param SCRIPT_FILENAME /var/www/extsuite/extman/cgi/$fastcgi_script_name; include fcgi.conf; } location /extman/ { alias /var/www/extsuite/extman/html/; } access_log /var/log/extmail_access.log;}生成fcgi.conf
12345678910111213141516[iyunv@localhost ~]# vim /etc/nginx/fcgi.conffastcgi_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;重启nginx,访问192.168.1.105:8080
出错,需要安装Unix::Syslog
安装Unix::Syslog
1234[iyunv@localhost ~]# wget http://www.cpan.org/authors/id/M/MH/MHARNISCH/Unix-Syslog-1.1.tar.gz[iyunv@localhost ~]# tar xf Unix-Syslog-1.1.tar.gz [iyunv@localhost ~]# cd Unix-Syslog-1.1[iyunv@localhost Unix-Syslog-1.1]# perl Makefile.PL[iyunv@localhost Unix-Syslog-1.1]# make && make install再次访问就可以了,extman的登录账户为root@extmail.org密码为extmail*123*,首次使用需要先添加域,添加之后再修改域,改为可自由注册,再注册用户就可以登录发邮件了
九、问题集锦
图形日志无法查看,后续补充
另外参考了两篇文章,非常感谢!
http://hypocritical.blog.iyunv.com/3388028/1403385
http://www.programaloco.com/blog/MIzMyADMwATz.html
|