(原創)Install Postfix On CentOS 6.0(Minimal)

vi /etc/selinux/config
change
SELINUX=enforcing
to
SELINUX=disabled

reboot

update
yum -y update

useradd -u 1000 jason

passwd jason

vi /etc/ssh/sshd_config
Port 61093
AllowUsers jason

install mysql
yum -y install mysql-server
chkconfig --levels 2345 mysqld on

install apache + php
yum -y install httpd
chkconfig --levels 2345 httpd on
vi /etc/httpd/conf/httpd.conf
ServerName www.jason-tang.com:80

yum -y install php php-gd php-imap php-mbstring php-mysql php-pear php-xml

install named
yum -y install bind
chkconfig --levels 2345 named on
vi /etc/resolv.conf
change to
nameserver 127.0.0.1

vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 61093 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

reboot

install cyrus-sasl
yum -y install cyrus-sasl cyrus-sasl-plain

install Postfix
yum -y install postfix

cp -rp /etc/sasl2/smtpd.conf /etc/sasl2/smtpd.conf.bak
vi /etc/sasl2/smtpd.conf
pwcheck_method:authdaemond
log_level:3
mech_list: PLAIN LOGIN
authdaemond_path:/var/spool/authdaemon/socket

useradd vmail -u 500 -s /sbin/nologin -d /dev/null
mkdir /home/domains
chown -R vmail:vmail /home/domains
chmod -R ug+rwx,o-rwx /home/domains

mkdir /etc/postfix/TLS
cd /etc/postfix/TLS
openssl req -new -x509 -nodes -out smtpd.pem -keyout smtpd.pem -days 3650

Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Hong Kong
Locality Name (eg, city) [Default City]:Hong Kong
Organization Name (eg, company) [Default Company Ltd]:Jason Tang Ltd
Organizational Unit Name (eg, section) []:IT
Common Name (eg, your name or your server's hostname) []:Jason Tang
Email Address []:Email住址會使用灌水程式保護機制。你需要啟動Javascript才能觀看它

cd /root

vi /etc/postfix/main.cf
myhostname = mail.jason-tang.com
mydomain = jason-tang.com
myorigin = $mydomain
mydestination = $myhostname localhost localhost.$mydomain
mynetworks = 127.0.0.1/32
inet_interfaces = all

virtual_mailbox_base = /home/domains/
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:500
virtual_gid_maps = static:500

virtual_transport = maildrop
maildrop_destination_recipient_limit = 1
maildrop_destination_concurrency_limit = 1

message_size_limit = 51200000
virtual_mailbox_limit = 209715200
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, the user's maildir has overdrawn his diskspace quota, please try again later.
virtual_overquota_bounce = yes

broken_sasl_auth_clients = yes
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination
smtpd_sasl_local_domain = $mydomain
smtpd_sasl_security_options = noanonymous
smtpd_sasl_application_name = smtpd
smtpd_banner=$myhostname ESMTP $mail_name ($mail_version)

smtp_use_tls = yes
smtpd_use_tls = yes
smtp_tls_note_starttls_offer = yes
smtpd_tls_key_file = /etc/postfix/TLS/smtpd.pem
smtpd_tls_cert_file = /etc/postfix/TLS/smtpd.pem
smtpd_tls_CAfile = /etc/postfix/TLS/smtpd.pem
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
tls_random_source = dev:/dev/urandom

mime_header_checks = regexp:/etc/postfix/mime_header_checks.regexp

vi /etc/postfix/master.cf
smtps     inet  n       -       n       -       -       smtpd
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING

vi /etc/postfix/mysql_virtual_alias_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = alias
select_field = goto
where_field = address
additional_conditions = AND active = '1'

vi /etc/postfix/mysql_virtual_domains_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = domain
select_field = domain
where_field = domain
additional_conditions = AND active = '1'

vi /etc/postfix/mysql_virtual_mailbox_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = mailbox
select_field = maildir
where_field = username
additional_conditions = AND active = '1'

vi /etc/postfix/mysql_virtual_mailbox_limit_maps.cf
user = postfix
password = postfix
hosts = localhost
dbname = postfix
table = mailbox
select_field = quota
where_field = username
additional_conditions = AND active = '1'

vi /etc/postfix/mime_header_checks.regexp
/filename=\"?(.*)\.(bat|cmd|com|pif|exe)\"?$/ REJECT

chkconfig --levels 2345 postfix on

install Postfixadmin
yum -y install wget
wget http://downloads.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-2.3.4/postfixadmin_2.3.4.tar.gz
tar -zxvf postfixadmin_2.3.4.tar.gz
mv postfixadmin-2.3.4 /var/www/html/postfixadmin
chown -R apache:apache /var/www/html/postfixadmin
rm -rf postfixadmin_2.3.4.tar.gz

1. Create the MySQL Tables
mysql -u root
CREATE DATABASE postfix;
GRANT ALL ON postfix.* TO postfix@localhost IDENTIFIED BY "postfix";
exit

2. Configure PostfixAdmin
vi /var/www/html/postfixadmin/config.inc.php
$CONF['configured'] = true;
$CONF['setup_password'] = '856d5eb384038a8bb9293adbd81ee487:8bd0475c6edee8aed00b6b5f336d74e45bc51276';
$CONF['postfix_admin_url'] = 'http://mail.jason-tang.com/postfixadmin';
$CONF['database_type'] = 'mysqli';
$CONF['database_password'] = 'postfix';

$CONF['admin_email'] = Email住址會使用灌水程式保護機制。你需要啟動Javascript才能觀看它';

$CONF['domain_path'] = 'YES';
$CONF['domain_in_mailbox'] = 'NO';

$CONF['quota'] = 'YES';

$CONF['mailbox_postcreation_script']='/usr/bin/sudo -u vmail /usr/local/bin/postfixadmin-mailbox-postcreation.sh';
$CONF['mailbox_postdeletion_script']='/usr/bin/sudo -u vmail /usr/local/bin/postfixadmin-mailbox-postdeletion.sh';
$CONF['domain_postdeletion_script']='/usr/bin/sudo -u vmail /usr/local/bin/postfixadmin-domain-postdeletion.sh';

3.http://IP/postfixadmin/setup.php
You should see a list of 'OK' messages.

Setup password      postfixadmin   <-------- Input your Setup password
Admin:              <-------- Input your Email address(Just for postfixadmin)
Password:           <-------- Input your Password
Password (again):   <-------- Input your Password again

And then click "Add Admin"

Admin has been added!

mv /var/www/html/postfixadmin/setup.php /var/www/html/postfixadmin/setup.php.disabled
chmod 000 /var/www/html/postfixadmin/setup.php.disabled

4. Copy .sh files
cp -rp /var/www/html/postfixadmin/ADDITIONS/postfixadmin*.sh /usr/local/bin
chmod 755 /usr/local/bin/postfixadmin*.sh

vi /usr/local/bin/postfixadmin-mailbox-postcreation.sh
change
basedir=/var/spool/maildirs
to
basedir=/home/domains

change
maildirmake "$maildir"
to
/usr/lib/courier-imap/bin/maildirmake "$maildir"

vi /usr/local/bin/postfixadmin-mailbox-postdeletion.sh
change
basedir=/var/spool/maildirs
trashbase=/var/spool/deleted-maildirs
to
basedir=/home/domains
trashbase=/home/domains

vi /usr/local/bin/postfixadmin-domain-postdeletion.sh
change
basedir=/var/spool/maildirs
trashbase=/var/spool/deleted-maildirs
to
basedir=/home/domains
trashbase=/home/domains

5.install sudo
yum -y install sudo fprintd-pam
visudo
#Defaults    requiretty
apache ALL=(vmail) NOPASSWD: /usr/local/bin/postfixadmin-mailbox-postcreation.sh, /usr/local/bin/postfixadmin-mailbox-postdeletion.sh, /usr/local/bin/postfixadmin-domain-postdeletion.sh

install courier-authlib courier-imap maildrop
yum -y install rpm-build make libtool openldap-devel mysql-devel postgresql-devel gdbm-devel pam-devel expect gcc-c++ redhat-rpm-config libtool-ltdl-devel libidn-devel gamin-devel pcre-devel
mkdir /home/jason/courier
cd /home/jason/courier
wget http://downloads.sourceforge.net/project/courier/authlib/0.63.0/courier-authlib-0.63.0.tar.bz2
wget http://downloads.sourceforge.net/project/courier/imap/4.9.3/courier-imap-4.9.3.tar.bz2
wget http://downloads.sourceforge.net/project/courier/maildrop/2.5.4/maildrop-2.5.4.tar.bz2
chown -R jason:jason /home/jason/courier
rpmbuild -ta /home/jason/courier/courier-authlib-0.63.0.tar.bz2
rpm -ivh /root/rpmbuild/RPMS/x86_64/courier-authlib-0.63.0-1.el6.x86_64.rpm
rpm -ivh /root/rpmbuild/RPMS/x86_64/courier-authlib-debuginfo-0.63.0-1.el6.x86_64.rpm
rpm -ivh /root/rpmbuild/RPMS/x86_64/courier-authlib-devel-0.63.0-1.el6.x86_64.rpm
rpm -ivh /root/rpmbuild/RPMS/x86_64/courier-authlib-mysql-0.63.0-1.el6.x86_64.rpm
su jason
rpmbuild -ta /home/jason/courier/courier-imap-4.9.3.tar.bz2
exit
rpm -ivh /home/jason/rpmbuild/RPMS/x86_64/courier-imap-4.9.3-1.x86_64.rpm
rpm -ivh /home/jason/rpmbuild/RPMS/x86_64/courier-imap-debuginfo-4.9.3-1.x86_64.rpm
rm -rf /home/jason/rpmbuild
rpmbuild -ta maildrop-2.5.4.tar.bz2
rpm -ivh /root/rpmbuild/RPMS/x86_64/maildrop-2.5.4-1.x86_64.rpm
rpm -ivh /root/rpmbuild/RPMS/x86_64/maildrop-devel-2.5.4-1.x86_64.rpm
rpm -ivh /root/rpmbuild/RPMS/x86_64/maildrop-debuginfo-2.5.4-1.x86_64.rpm

rm -rf /root/rpmbuild

chmod +x /var/spool/authdaemon

vi /etc/authlib/authdaemonrc
authmodulelist="authmysql"
authmodulelistorig="authmysql"

vi /etc/authlib/authmysqlrc
MYSQL_SERVER            localhost
MYSQL_USERNAME          postfix
MYSQL_PASSWORD          postfix
MYSQL_SOCKET            /var/lib/mysql/mysql.sock
MYSQL_DATABASE          postfix
MYSQL_USER_TABLE        mailbox
MYSQL_CRYPT_PWFIELD     password
MYSQL_UID_FIELD         '500'
MYSQL_GID_FIELD         '500'
MYSQL_LOGIN_FIELD       username
MYSQL_HOME_FIELD        '/home/domains/'
MYSQL_NAME_FIELD        name
MYSQL_MAILDIR_FIELD     maildir
MYSQL_QUOTA_FIELD       concat(quota,'S')
MYSQL_WHERE_CLAUSE      active='1'

chkconfig --levels 2345 courier-authlib on
chkconfig --levels 2345 courier-imap on

vi /etc/postfix/master.cf
maildrop  unix  -       n       n       -       -       pipe
  flags=DRhu user=vmail argv=/usr/bin/maildrop -d ${recipient}

vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 25 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 465 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 110 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 995 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 143 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 993 -j ACCEPT

install squirrelmail
cd /home/jason/courier
wget http://downloads.sourceforge.net/project/squirrelmail/stable/1.4.22/squirrelmail-webmail-1.4.22.tar.gz
wget http://downloads.sourceforge.net/project/squirrelmail/locales/1.4.18-20090526/all_locales-1.4.18-20090526.tar.gz
tar -zxvf squirrelmail-webmail-1.4.22.tar.gz
rm -rf squirrelmail-webmail-1.4.22.tar.gz
mv squirrelmail-webmail-1.4.22 /var/www/html/webmail
tar -zxvf all_locales-1.4.18-20090526.tar.gz
rm -rf all_locales-1.4.18-20090526.tar.gz
./install
Please enter path to your squirrelmail installation:/var/www/html/webmail
cp: overwrite `/var/www/html/webmail/help/en_US/options.hlp'? y
cp: overwrite `/var/www/html/webmail/help/en_US/compose.hlp'? y
cp: overwrite `/var/www/html/webmail/help/en_US/search.hlp'? y
cp: overwrite `/var/www/html/webmail/help/en_US/addresses.hlp'? y
cp: overwrite `/var/www/html/webmail/help/en_US/read_mail.hlp'? y
cp: overwrite `/var/www/html/webmail/help/en_US/folders.hlp'? y
cp: overwrite `/var/www/html/webmail/help/en_US/main_folder.hlp'? y
cp: overwrite `/var/www/html/webmail/help/en_US/FAQ.hlp'? y
cp: overwrite `/var/www/html/webmail/help/en_US/basic.hlp'? y

chown -R apache:apache /var/www/html/webmail
/var/www/html/webmail/configure
10->1->zh_TW->2->big5->S->Q

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

rm -rf  /home/jason/courier

vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
#::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

reboot

test:
http://IP/postfixadmin
http://IP/webmail

wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
rpm -K rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
rpm -ivh rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
rm -rf rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm

install clamav
yum -y install clamd clamav-milter
vi /etc/clamav-milter.conf
MilterSocketMode 666
AddHeader Replace

chkconfig --levels 2345 sendmail off
chkconfig --levels 2345 postfix on
chkconfig --levels 2345 clamd on
chkconfig --levels 2345 clamav-milter on

install SpamAssassin
yum -y install spamassassin spamass-milter
chkconfig --levels 2345 spamassassin on
chkconfig --levels 2345 spamass-milter on

vi /etc/mail/spamassassin/local.cf
required_hits 5
report_safe 0
rewrite_header Subject *****SPAM*****
use_bayes 1
bayes_auto_learn 1
ok_locales              en zh

vi /etc/postfix/main.cf
milter_connect_macros = b j _ {daemon_name} {if_name} {if_addr}
smtpd_milters =
      unix:/var/clamav/clmilter.socket
      unix:/var/run/spamass.sock
milter_default_action = accept

vi /etc/sysconfig/spamassassin
SPAMDOPTIONS="-u nobody -d -c -m5 -H"

sa-update

chown -R nobody:nobody /root/.spamassassin

vi /etc/rc.d/rc.local
chmod 777 /var/run/spamass.sock
su - root -c "/etc/init.d/spamassassin restart"

reboot

yum -y install perl-MIME-EncWords perl-Email-Valid perl-Mail-Sender perl-Log-Log4perl

wget http://download.fedora.redhat.com/pub/epel/6/i386/perl-MIME-Charset-1.006.2-3.el6.noarch.rpm
rpm -ivh perl-MIME-Charset-1.006.2-3.el6.noarch.rpm
rm -rf perl-MIME-Charset-1.006.2-3.el6.noarch.rpm

1. Create a local account
groupadd vacation -g 501
useradd vacation -u 501 -g 501 -s /sbin/nologin -d /nonexistent -c "Virtual Vacation"

2. Create a directory
mkdir /var/spool/vacation

3. Copy the files
cp -rp /var/www/html/postfixadmin/VIRTUAL_VACATION/vacation.pl /var/spool/vacation
vi /var/spool/vacation/vacation.pl
my $db_type = 'mysql';

my $db_username = 'postfix';
my $db_password = 'postfix';
my $db_name     = 'postfix';

our $vacation_domain = 'autoreply.jason-tang.com';

chown -R vacation:vacation /var/spool/vacation
chmod -R 700 /var/spool/vacation

4. Setup the transport type
vi /etc/postfix/master.cf
vacation  unix  -       n       n       -       -       pipe
  flags=Rq user=vacation argv=/var/spool/vacation/vacation.pl -f ${sender} -- ${recipient}

vi /etc/postfix/main.cf
transport_maps = hash:/etc/postfix/transport

cp -rp /etc/postfix/transport /etc/postfix/transport.bak
vi /etc/postfix/transport
autoreply.jason-tang.com       vacation

postmap /etc/postfix/transport
/etc/init.d/postfix restart

5. Postfixadmin Setting
vi /var/www/html/postfixadmin/config.inc.php
$CONF['vacation'] = 'YES';
$CONF['vacation_domain'] = 'autoreply.jason-tang.com';
$CONF['vacation_control'] ='YES';
$CONF['vacation_control_admin'] = 'YES';