文档参考http://5146324.blog.51cto.com/5136324/1551743
一.安装LAMP。
yum -y install httpd mysql mysql-devel mysql-server php php-pecl-Fileinfo php-mcrypt php-devel php-mysql php-common php-mbstring php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc pcre pcre-devel dovecot dovecot-devel dovecot-mysql
这里dovecot dovecot-devel dovecot-mysql包是为了防止创建邮箱管理员用户的时候报错。文档下面会有提示说明
/etc/init.d/dovecot start
新建用户
useradd -u 2000 -d /var/vmail -m -s /sbin/nologin vmail
修改php.ini文件,融合HTTP和php,并且新建index.php测试
在etc/httpd/conf/httpd.conf 设置启动用户和组都为vmail
vim /etc/php.ini
short_open_tag = Off 改为short_open_tag = On
/etc/init.d/httpd start
在http目录下新建index.php
<?
phpinfo();
?>
测试如下http://192.168.33.20,查看imap是否安装上了
二.安装postfixadmin
正常的情况下需要DNS来域名解析。在这里并没有做。所以最好在服务器的hosts和PC机上做解析
192.168.33.10 mail.wu.com
下载postfixadmin
http://sourceforge.net/projects/postfixadmin/files/postfixadmin/postfixadmin-2.3.7/
解压postfixadmin
tar -xzf postfixadmin-2.3.7.tar.gz mv postfixadmin-2.3.7 postfixadmin rm -rf postfixadmin-2.3.7.tar.gz
3. 配置postfixadmin
在配置postfixadmin,为了防止修改出现问题。先备份配置文件
[root@mail postfixadmin]# cp config.inc.php config.inc.php.bak [root@mail postfixadmin]# cp setup.php setup.php.bak [root@mail postfixadmin]# cat config.inc.php 修改如下 $CONF['configured'] = true; $CONF['database_type'] = 'mysql'; 数据库类型 $CONF['database_host'] = 'localhost'; 数据库位置(远程的话为IP,需要授权) $CONF['database_user'] = 'postfix'; 数据库访问用户 $CONF['database_password'] = 'postfix'; 数据库访问密码 $CONF['database_name'] = 'postfix'; 数据库名 $CONF['admin_email'] = 'postmaster@test.com'; 邮箱管理员邮件 $CONF['encrypt'] = 'dovecot:CRAM-MD5'; dovencotMD5---$CONF['setup_password']这个设置有关 $CONF['dovecotpw'] = "/usr/bin/doveadm pw"; $CONF['domain_path'] = 'YES'; $CONF['domain_in_mailbox'] = 'NO'; $CONF['aliases'] = '1000'; $CONF['mailboxes'] = '1000'; $CONF['maxquota'] = '1000'; $CONF['quota'] = 'YES'; $CONF['fetchmail'] = 'NO'; $CONF['used_quotas'] = 'YES'; $CONF['new_quota_table'] = 'YES';
4. 启动mysql,并且创建用户授权
mysql> grant all on *.* to postfix@'localhost' identified by 'postfix'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> create database postfix; Query OK, 1 row affected (0.00 sec)
5. postfixadmin目录设置用户和所组
[root@mail html]# chown -R vmail:vmail postfixadmin [root@mail html]# ll 总用量 8 -rw-r--r-- 1 root root 19 9月 12 20:34 index.php drwxrwxr-x 14 vmail vmail 4096 9月 12 21:15 postfixadmin
6. 访问postfixadmin 创建密码(数据库写入,密码是用来创建管理员的)
http://192.168.33.20/postfixadmin/setup.php
创建密码(admin),修改config.inc.php 这个密码可以随意创建
$CONF['setup_password'] = '486c851513c1bab4a922e842eaf81452:4b909dc045d47d54dc402146f7f788adcf980b04';
这里可以创建邮箱的管理员用户了
这里报错。。是因为没有安装启动。dovecot dovecot-devel dovecot-mysql
Everything seems fine... attempting to create/update database structure
Database is up to date
can't encrypt password with dovecotpw, see error log for details
如果在创建之前已经启动了dovecot的话,如下
然后http://192.168.33.20/postfixadmin/login.php 登入试试
7. 创建脚本来实现创建虚拟邮件目录。postfixadmin不会自动新建
postfixadmin数据库和脚本目录存放都我自己设定在/data/postfix里的data和sh目录下
mkdir -p /data/postfix/{data,sh}
vim /usr/local/bin/maildir-creation.sh
#!/bin/bash # HOME_DIR="/var/vmail" USER_NAME="vmail" GROUP_NAME="vmail" if [ ! -d ${HOME_DIR}/$1 ] ; then mkdir ${HOME_DIR}/$1 chown -R ${USER_NAME}.${GROUP_NAME} ${HOME_DIR}/$1 fi mkdir ${HOME_DIR}/$1/$2 chown -R ${USER_NAME}.${GROUP_NAME} ${HOME_DIR}/$1/$2
vim /usr/local/bin/maildir-deletion.sh
#!/bin/bash # # vmta ALL = NOPASSWD: /usr/local/bin/maildir-deletion.sh # if [ $# -ne 2 ] ; then exit 127 fi DOMAIN="$1" USER="$2" HOME_DIR="/var/vmail" USER_DIR="${HOME_DIR}/${DOMAIN}/${USER}" TRASH_DIR="${HOME_DIR}/deleted-maildirs" DATE=`date "+%Y%m%d_%H%M%S"` if [ ! -d "${TRASH_DIR}/${DOMAIN}" ] ; then mkdir -p "${TRASH_DIR}/${DOMAIN}" fi if [ -d "${USER_DIR}" ] ; then mv ${USER_DIR} ${TRASH_DIR}/${DOMAIN}/${USER}-${DATE} fi
chown vmail:vmail /usr/local/bin/maildir-* chmod +x /usr/local/bin/maildir-*
新建目录
[root@mail sh]# mkdir /var/vmail/deleted-maildirs [root@mail sh]# chown -R vmail:vmail /var/vmail/deleted-maildirs
8. 配置sudo
vim /etc/sudoers 在最后加入
vmail ALL = NOPASSWD: /data/postfix/sh/maildir-creation.sh vmail ALL = NOPASSWD: /data/postfix/sh/maildir-deletion.sh
Defaults requiretty 注释掉在这行的最前面加#
9. 修改postfixadmin配置文件
vim create-mailbox.php
在229行的前面加入 system("sudo /data/postfix/sh/maildir-creation.sh $fDomain ".$_POST['fUsername']); 如下
system("sudo /data/postfix/sh/maildir-creation.sh $fDomain ".$_POST['fUsername']); db_log ($SESSID_USERNAME, $fDomain, 'create_mailbox', "$fUsername");
vim delete.php
在146行db_log ($SESSID_USERNAME, $fDomain, 'delete_mailbox', $fDelete);下加入,如下
db_log ($SESSID_USERNAME, $fDomain, 'delete_mailbox', $fDelete); $userarray=explode("@",$fDelete); $user=$userarray[0]; $domain=$userarray[1]; system("sudo /data/postfix/sh/maildir-deletion.sh $domain $user");
三. 配置postfix邮件代理
1. 配置POSTFIX
cp main.cf main.cf.bak 修改main.cf myhostname = mail.test.com mydomain = test.com myorigin = $mydomain inet_interfaces = all mynetworks_style = host mynetworks = 192.168.33.20, 127.0.0.0 在最后增加 virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql_virtual_domains_maps.cf virtual_alias_maps = proxy:mysql:/etc/postfix/mysql_virtual_alias_maps.cf virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql_virtual_mailbox_maps.cf # Additional for quota support virtual_create_maildirsize = yes virtual_mailbox_extended = yes virtual_mailbox_limit_maps = mysql:/etc/postfix/mysql_virtual_mailbox_limit_maps.cf virtual_mailbox_limit_override = yes virtual_maildir_limit_message = Sorry, this user has exceeded their disk space quota, please try again later. virtual_overquota_bounce = yes #Specify the user/group that owns the mail folders. I'm not sure if this is strictly necessary when using Dovecot's LDA. virtual_uid_maps = static:2000 virtual_gid_maps = static:2000 #Specifies which tables proxymap can read: http://www.postfix.org/postconf.5.html#proxy_read_maps proxy_read_maps = $local_recipient_maps $mydestination $virtual_alias_maps $virtual_alias_domains $virtual_mailbox_maps $virtual_mailbox_domains $relay_recipient_maps $relay_domains $canonical_maps $sender_canonical_maps $recipient_canonical_maps $relocated_maps $transport_maps $mynetworks $virtual_mailbox_limit_maps
检查配置文件是否有问题
postconf
2. 创建postfix的mysql文件
cd /etc/postfix/
[root@mail postfix]# cat mysql_virtual_domains_maps.cf user = postfix password = postfix hosts = localhost dbname = postfix query = SELECT domain FROM domain WHERE domain='%s' AND active = '1' #optional query to use when relaying for backup MX #query = SELECT domain FROM domain WHERE domain='%s' AND backupmx = '0' AND active = '1' [root@mail postfix]# cat mysql_virtual_alias_maps.cf user = postfix password = postfix hosts = localhost dbname = postfix query = SELECT goto FROM alias WHERE address='%s' AND active = '1' [root@mail postfix]# cat mysql_virtual_mailbox_maps.cf user = postfix password = postfix hosts = localhost dbname = postfix query = SELECT CONCAT(domain,'/',maildir) FROM mailbox WHERE username='%s' AND active = '1' [root@mail postfix]# cat mysql_virtual_mailbox_limit_maps.cf user = postfix password = postfix hosts = localhost dbname = postfix query = SELECT quota FROM mailbox WHERE username='%s' AND active = '1'
3. SMTP 认证
1).查看POSTFIX支持认证
[root@mail postfix]# postconf -a cyrus dovecot
2).修改配置文件main.cf,在文件的最后增加
cd /etc/postfix
#SASL SUPPORT FOR CLIENTS # Turns on sasl authorization smtpd_sasl_auth_enable = yes #Use dovecot for authentication smtpd_sasl_type = dovecot # Path to UNIX socket for SASL smtpd_sasl_path = /var/run/dovecot/auth-client #Disable anonymous login. We don't want to run an open relay for spammers. smtpd_sasl_security_options = noanonymous #Adds support for email software that doesn't follow RFC 4954. #This includes most versions of Microsoft Outlook before 2007. broken_sasl_auth_clients = yes # smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination # TRANSPORT MAP virtual_transport = dovecot dovecot_destination_recipient_limit = 1
3).修改配置文件master.cf,文件最后增加(注意flags之前有2个空格)
dovecot unix - n n - - pipe flags=DRhu user=vmail:vmail argv=/usr/libexec/dovecot/dovecot-lda -f ${sender} -d ${recipient}
四. 安装配置dovecot
1.修改主配置文件
cd /etc/dovecot/
vim dovecot.conf
protocols = imap pop3 listen = * dict { quota = mysql:/etc/dovecot/dovecot-dict-sql.conf.ext } !include conf.d/*.conf
2.修改文件conf.d/10-auth.conf
disable_plaintext_auth = no auth_mechanisms = plain login cram-md5 !include auth-sql.conf.ext
3.修改文件conf.d/10-mail.conf
mail_location = maildir:%hMaildir mbox_write_locks = fcntl
4.修改文件conf.d/10-master.conf
service auth { unix_listener auth-userdb { mode = 0600 user = vmail group = vmail } 新加下面一段,为smtp做认证 unix_listener auth-client { mode = 0600 user = postfix group = postfix } } service auth-worker { } service dict { unix_listener dict { mode = 0600 user = vmail group = vmail } }
5.修改文件conf.d/15-lda.conf
protocol lda { # Space separated list of plugins to load (default is global mail_plugins). mail_plugins = quota postmaster_address = postfixmaster@test.com 管理员邮箱 }
6.修改文件conf.d/20-imap.conf
protocol imap { mail_plugins = quota imap_quota }
7.修改文件conf.d/20-pop3.conf
protocol pop3 { pop3_uidl_format = %08Xu%08Xv mail_plugins = quota }
8.修改文件conf.d/90-quota.conf
plugin { quota_rule = *:storage=1G } plugin { } plugin { quota = dict:User quota::proxy::quota } plugin { }
9.新增加文件/etc/dovecot/dovecot-sql.conf.ext
driver = mysql connect = host=localhost dbname=postfix user=postfix password=postfix default_pass_scheme = CRAM-MD5 user_query = SELECT CONCAT('/var/vmail/', maildir) AS home, 2000 AS uid, 2000 AS gid, CONCAT('*:bytes=', quota) as quota_rule FROM mailbox WHERE username = '%u' AND active='1' password_query = SELECT username AS user, password, CONCAT('/var/vmail', maildir) AS userdb_home, 2000 AS userdb_uid, 2000 AS userdb_gid, CONCAT('*:bytes=', quota) as userdb_quota_rule FROM mailbox WHERE username = '%u' AND active='1'
10.新增加文件/etc/dovecot/dovecot-dict-sql.conf.ext
connect = host=localhost dbname=postfix user=postfix password=postfix map { pattern = priv/quota/storage table = quota2 username_field = username value_field = bytes } map { pattern = priv/quota/messages table = quota2 username_field = username value_field = messages }
五.使用管理员登入创建域
和新建虚拟用户
五.使用Foxmail来发送接收查看是否能正常收发邮件
在Foxmail登入邮件用户,并且发送邮件接收
这样,就可以正常域内发送接收了。但是要达到外网发送。最好是DNS能解析到。必须域名和外网了
转载于:https://blog.51cto.com/wujuntl/1551743