Postfix Mail System - Step by Step Guide Part 1: Install Postfix with Mysql Support and remove Sendmail

snottysurfsideΔιακομιστές

9 Δεκ 2013 (πριν από 3 χρόνια και 8 μήνες)

319 εμφανίσεις

Postfix Mail System
-

Step by Step Guide

Part 1:


Install Postfix with Mysql Support and remove
Sendmail



Installing of Postfix, Postfixadmin, Dovecot and Squirrelmail is not an easy task. There are some
customization works you need to be done in order t
o get it works. I hope the following guide step
by step instruction will be handy for you :)



My mail server system information:

hostname : example.centos.local

IP address: 192.168.139.128

Netmask


: 255.255.255.0

Gateway

: 192.168.139.2



There are 2

domains that I will setup for this guide. There are

domain name : abc.local

user mailbox


: alex@abc.local



domain name : xyz.local

user mailbox


: yasmin@xyz.local



The postfix admin login we will be using is post@example.local, where example.local wil
l not be
created in Postfixadmin's virtual domain. This is to show you that, the email system we setup later
is solely running on Postfixadmin's virtual domain




#vim /etc/hosts

114.130.27.3

ns1.asianetbd.net ns1



Step 0: Install prerequisite packages

#y
um
-
y install system
-
switch
-
mail, dovecot, squirrelmail, mysql
-
server, php
-
imap


Step1: Backup the current Sendmail configuration

CentOS Linux default MTA is Sendmail. Since we are swtching to new MTA
-
Postfix. It is
advisable to backup current sendmail con
fig and bin to new location before we proceed.



First, I will create new folder call backup. Next, I backup all the files and record the
sendmail path to a text.


#mkdir /backup

#touch

/backup/sendmail.config.location.txt

#which sendmail mailq newaliases
> /backup/sendmail.config.location.txt



#which sendmail mailq newaliases

#cp
-
var /usr/sbin/sendmail /backup

#cp
-
var /usr/bin/mailq /backup

#cp
-
var /usr/bin/newaliases /backup


Step5: Install postfix from centosplus repo

#yum install
-
y
--
enablerepo=cen
tosplus postfix

Step6: verify the postfix package installed

Now, let us check the packages information been installed. Good, that is the package that we
need.


#rpm
-
qa postfix

#postconf
-
m

btree

cidr

environ

hash

ldap

mysql


<
--

this means the postfix pa
ckage support the mysql database

nis

pcre

pgsql

proxy

regexp

static

unix


Step8 : Install system
-
switch
-
mail, mysql, system
-
switch
-
mail,dovecot, squirrelmail

#yum
-
y install


php
-
mysql php
-
imap, php
-
ldap,


php
-
mbstring


Step9: Switching default Sendmail to

Postfix Linux mail system

#system
-
switch
-
mail


Step10: verify the setting of our new MTA

#alternatives
--
display mta

Step10: Remove Sendmail packages

#
yum
-
y remove sendmail





Postfix Mail System
-

Step by Step Guide

Part 2:


Create Virtual User, Confi
gure Postfix, Dovecot and
Mysql



This is the Part2 of the Posft Mail System, we are going to configure the following:
-

1. MySQL Server, create a postfixadmin database

2. Postfix Main.cf and create a virtual user file and virtual domain configuration files

3. Dovecot



Create Mysql Username, Password and Postfixadmin database

Step1:


Start your mysql

#service mysqld start

#chkconfig mysqld on

#chkconfig
--
list mysqld


Step2:


Create mysql root user password

First we need to create a password for our root ac
cess. I also perform a new test to ensure that
no blank password is accepted.


#mysqladmin
-
u root password 'your_password'

#mysql
-
u root
-
p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or
\
g.

Your MySQL connection id is 4

Server vers
ion: 5.0.77 Source distribution


Type 'help;' or '
\
h' for help. Type '
\
c' to clear the buffer.


Mysql>

Step3:


Create Postfixadmin database

Next,we create a database named postfix and the create a user called 'mail'. Then
we will give full access rights fo
r 'mail' on postfix database.


mysql> CREATE DATABASE postfix;

mysql> CREATE USER mail@localhost IDENTIFIED BY 'your_password';

mysql> GRANT ALL PRIVILEGES ON postfix.* TO mail;


mysql> show databases;

+
--------------------
+

| Database


|

+
---
-----------------
+

| information_schema |

| mysql


|

| postfix


|

| test


|

+
--------------------
+

4 rows in set (0.00 sec)

Create Postfixadmin User

Step1: Create a non
-
root user for postfixadmin

Let u
s create a non
-
root user for our postfixa & pop administration purposes.



#useradd vmail


-
s /sbin/nologin

#id vmail

uid=501(vmail) gid=501(vmail) groups=501(vmail)


Configure Postfix

Step1:


Configure the postfix main.cf file

First modify the /etc/post
fix/main.cf. We change the following lines

Note: Number in blue are line numbers. You can active it using :set nu under vi command mode

#vim
/etc/postfix/main.cf

93 myorigin = $mydomain


110 inet_interfaces = all


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


255 mynetworks = 192.168.139.0/24, 127.0.0.0/8


<
--
this is the ip that can used to sent email

:wq


Next, Add in the following lines, this will tell the system to look for postfix configuration, its virtual
domains, virtual users and
aliases.



The virtual_uid_maps : static:501. This 501 is the id of my vmail. Change this if you have diffrent
id for your uid and gid.

#vim /etc/postfix/main.cf

virtual_mailbox_domains = mysql:/etc/postfix/mysql
-
domains.cf

virtual_mailbox_maps = mysql:/et
c/postfix/mysql
-
users.cf

virtual_alias_maps = mysql:/etc/postfix/mysql
-
aliases.cf

virtual_mailbox_base = /home/vmail

virtual_uid_maps = static:501

virtual_gid_maps = static:501

:wq

Step2:


Configure the postfix mysql
-
domians.cf file

The following configura
tion will create mysql
-
domains.cf. This will enable postfixadmin to create
virtual domain



#vim /etc/postfix/mysql
-
domains.cf

host = localhost


user = mail


password = your_password


dbname = postfix


table = domain


select_field = domain


where_field =
domain


additional_conditions = and backupmx = '0' and active = '1'


:wq

Step3:


Configure the postfix mysql
-
users.cf file

The following configuration will create mysql
-
users.cf. This will enable postfixadmin create virtual
users





#vim /etc/postfix/mysq
l
-
users.cf

host = localhost

user = mail

password = your_password

dbname = postfix

table = mailbox

select_field = maildir

where_field = username

additional_conditions = and active = '1'

result_format = %sMaildir/

:wq


Configure the postfix mysql
-
alias.cf f
ile

Great, the last part we should do now is to connect the postfixadmin to mysql



#vim /etc/postfix/mysql
-
aliases.cf

host = localhost

user = mail

password = your_password

dbname = postfix

table = alias

select_field = goto

where_field = address

additiona
l_conditions = and active = '1'

:wq

#service postfix start

#chkconfig postfix on

#chkconfig
--
level postfix


Configuring Dovecot


Step 1 Check if the dovecot packages installed

#rpm
-
qa dovecot

Step 2 Create mysql config file for postfixadmin

Create the fi
le
/etc/dovecot
-
mysql.conf

containing the lines below. You might need to change your
username, password and db user.


#vim
/etc/dovecot
-
mysql.conf


driver = mysql


connect = host=localhost dbname=postfix user=mail password=your_password


default_pass_sche
me = PLAIN


password_query = SELECT password FROM mailbox WHERE username = '%u'

:wq

#cp /etc/dovecot.conf /etc/dovecot.ori


Next edit the file
/etc/dovecot.conf

and change the value of the following keys below. For line 917
please
make sure that your uid a
nd gid is the same as your vmail user
. Make sure you remove #
sign for all the following lines.


Also, comment out all passdb and userdb setting.


Then, make a
dovecot.log to ease any dovecot troubleshooting.

#vim
/etc/dovecot.conf


61 log_path = /var/log/
dovecot.log

721


auth_username_format = %Lu


869


passdb sql {

870


args = /etc/dovecot
-
mysql.conf

871


}


913

userdb static {

917


args = uid=501 gid=501 home=/home/vmail/%d/%n

927


}


Step3 Start you dovecot services

Finally startup your dovecot ser
vices.

#service dovecot start

#service dovecot on



Postfix Mail System
-

Step by Step Guide
-


Part 3:


Postfixadmin Installation on CentOS Linux

Step1: Download and extract Postfixadmin

Current version postfix version is 2.3. For me, I always create a fo
lder called 'software' and dump
all extra packages inside.

Note: For your information, I have already turn off my SELinux. If you wish to implement SELinux,
you can install posftadmin under /usr/share


#mkdir /software

#cd /software

#wget
http://downloads
.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin_2
.3.tar.gz?use_mirror=nchc

#tar


-
xvzf postfixadmin
-
2.3.tar.gz

#mv postfixadmin
-
2.3 postfixadmin




Step2:


Configure postfixadmin configuration file

Make a original copy of the file
/softwar
e/postfixadmin/config.inc.php


before editing

#cp
/software/postfixadmin/config.inc.php /software/postfixadmin/config.inc.php.ori

#vim

/software/postfixadmin/config.inc.php

26 $CONF['configured'] = true;

36 $CONF['postfix_admin_url'] = '/postfixadmin';



4
9 $CONF['database_type'] = 'mysqli';

50 $CONF['database_host'] = 'localhost';

51 $CONF['database_user'] = 'mail';

52 $CONF['database_password'] = 'your_password';



53 $CONF['database_name'] = 'postfix';

100 $CONF['encrypt'] = 'cleartext';



145 $CONF['dom
ain_path'] = 'YES';

151 $CONF['domain_in_mailbox'] = 'NO';


272 $CONF['emailcheck_resolve_domain']='NO';

:wq

Step3:


creating apache aliase for Postfixadmin

In the following example we are creating an aliase for postfixadmin





echo "Alias /postfixadmin
/software/postfixadmin" > /etc/httpd/conf.d/postfixadmin.conf

Step4:


start up your apache services

#service httpd start

#chkconfig httpd on


Step6:


Check your postfix


services

The postfix services should startup together with apache service if not try t
o start it

#service postfix status

Step7:


Initial setup Postfixadmin using web browser

Launching your web browser and type in the following address



http://your
-
ip
-
address
-
here/postfixadmin/setup.php

#
service postfix status

Step7:


Initial setup Postfix
admin using web browser

Launching your web browser and type in the following address



http://your
-
ip
-
address
-
here/postfixadmin/setup.php

The Postfixadmin checker will run a series of checking and will attempt to create/update database
structure. If any th
e check is fail, kindly check on the dependencies and install it via yum
command. If everything is okay you will see the following appear at the bottom of the web
browser.




Step8:


Insert the hash password

After you have put in your setup password, you

will se the following screen, this are the hash key
that we need to put in our /software/postfixadmin/config.inc.php file

#Edit you config.inc.php


#vim


/software/postfixadmin/config.inc.php

Remove line 31


#$CONF['setup_password'] = 'changeme';

Add
in


the has key generated by the setup process just now into $CONF['setup_password'] =
'your
-
own
-
hash
-
password';



28 // In order to setup Postfixadmin, you MUST specify a hashed password here.

29 // To create the hash, visit setup.php in a browser and typ
e a password into the field,

30 // on submission it will be echoed out to you as a hashed value.

31 #$CONF['setup_password'] = 'changeme';


<
----
delete this line

32
$CONF['setup_password'] = 'your
-
own
-
hash
-
password '
;


<
---
add in this line


:wq

Next, fill

in the Setup password, Admin and Password. Finally, click Add Admin to create a new
admin account.


you see the following information, "Admin has been added!", The you have sucessfully create a
admin account.



Important!

Remove the setup.php after the

installed postfix

cd /software/posftadmin

mv setup.php setup.ori

Now Go to the Postfix Admin login page at

http://your
-
ip
-
address/postfixadmin/


login using
your newly created admin account.



This is the main page that you after you login to the Postfi
xadmin. You are now ready to create
virtual users and multiple domains.



Postfix Mail System
-

Step by Step Guide
-


Linux WebMail
-

SquirrelMail


Installation on CentOS Linux


Step1: Check if squirrelmail packages already install


First, you can check if

squirrelmail already installed


in your system.

#
rpm
-
qi squirrelmail

#
yum
-
y install squirrelmail


Step2: configure


squirrelmail packages

Now, we are ready to start configure our squirrelmail. Run the following command


#
/usr/share/squirrelmail/config/
conf.pl


SquirrelMail Configuration : Read: config.php (1.4.0)

---------------------------------------------------------

Main Menu
--

1. Organization Preferences

2. Server Settings

3. Folder Defaults

4. General Options

5. Themes

6. Address Books

7. Message

of the Day (MOTD)

8. Plugins

9. Database

10. Languages


D. Set pre
-
defined settings for specific IMAP servers


C Turn color off

S Save data

Q Quit


Command >> 2


Step3: Check your configuration

Open your browser. Then enter the address
http://your
-
ip
-
addr
ess/src/configtest.php
. This
will run a test on your squirrelmail configuration. For security concern, you might need to move
the configtest.php to some other location after the installation. The example below shows the
result page of the test


Step4: Res
tart Apache server

Now, let us restart the apache server and make sure the apache services automatic start
-
up when
system boot.

#service httpd start

#chkconfig httpd on


Step5:


Login your webmail

You can now access your webmail using
http://ipaddress/webmail




Optional:


URL Redirection

If you prefer to access SquirrelMail from the root of your domain, we need to do a little trick called
"URL redirection".

Create the file
/etc/httpd/conf.d/sqroot.conf



#vim
/etc/
httpd/conf.d/sqroot.conf


<VirtualHost *:80>

DocumentRoot /usr/share/squirrelmail

</VirtualHost>

:wq


#
service httpd restart


Postfix Mail System
-

Step by Step Guide

Part 5:


Create Virtual Domains and Users using Postfixadmin


Step 1: login to PostfixAdm
in Page

In our example, my administrator is "post@example.local". Key in your password and click on
login password to continue





Step2 Setup Multiple Domains

Next, Go to


Domain List> New Domain to add new domain. Let us add the following domains
now. F
irst add in "abc.local" then click on Add Domain button. Continue to create another domian
"xyz.local"



Step3 Create User Mailbox of different domains

Now, create user mailbox for 2 different domains. Go to Virtual List > Add Mailbox. First create
alex f
or abc.local and yasmin for xyz.local.









Postfix Mail System
-

Step by Step Guide

Part 6:


Verify the PostfixAdmin installation




In Part 5, let us have a quick check on all our previous configuration


Check1: Verify the virtual domains created b
y Postfixadmin

Let us check on the virtual domains that we created earlier exist in MySQL database. Change your
own domain name created earlier in blue text below. You should able to see the the domain reply
from the command.



#postmap
-
q xyz.local mysql:
/etc/postfix/mysql
-
domains.cf

xyz.local


#postmap
-
q abc.local

mysql:/etc/postfix/mysql
-
domains.cf

abc.local

Check2: Verify the virtual domains created by Postfixadmin

Now,let us check if the virtual domain created by postfix are ok.





#cd /home/vmail/

#
ls
-
l

abc.local xyz.local


<
--

2 virtual domains created


Check3: Check inside the Mysql Database (optional).

If both of the verification on Check1 and 2 fails. You might need to check if the domain created is
exisit in your database.


# mysql
-
u mail
-
p
postfix


mysql> select * from domain;


Check5 : Verify the virtual users' password created by PostfixAdmin in Mysql

Dovecot can only read support clear text password created. If you the password created are in
encrypted, you might hit Authentication error!

Let us have a quick check on alex@abc.local's


password.


# mysql
-
u mail
-
p postfix

mysql> SELECT password FROM mailbox WHERE username = 'alex@abc.local';



+
----------
+

| password |

+
----------
+

| alex


|

+
----------
+

1 row in set (0.00 sec)



G
reat! the password created in not encrypted :P .This will ensure that our POP3 can
verify the password. Else, go to /software/postfixadmin/config.inc.php again check on
line 100.


#vim /software/postfixadmin/config.inc.php


26 $CONF['configured'] = true;

3
6 $CONF['postfix_admin_url'] = '/postfixadmin';



49 $CONF['database_type'] = 'mysqli';

50 $CONF['database_host'] = 'localhost';

51 $CONF['database_user'] = 'mail';

52 $CONF['database_password'] = 'your_password';



53 $CONF['database_name'] = 'postfix';

1
00 $CONF['encrypt'] = 'cleartext';



145 $CONF['domain_path'] = 'YES';

151 $CONF['domain_in_mailbox'] = 'NO';


272 $CONF['emailcheck_resolve_domain']='NO';


Also, go to /etc/dovecot.conf again check if


following setting is correct. Make sure that you also

commented out all other passdb and userdb other than passdb sql and userdb static.


#vim
/etc/dovecot.conf



61 log_path = /var/log/dovecot.log

721


auth_username_format = %Lu


869


passdb sql {

870


args = /etc/dovecot
-
mysql.conf

871


}


913

userdb sta
tic {

917


args = uid=501 gid=501 home=/home/vmail/%d/%n

927


}



Check6 : SMTP Relay test

You are advised to perform 2 test both on local host and use another pc. This is to ensure that
there are no connection issue such as Firewalls or relay deny fro
m Postfix


Check6 : SMTP Relay test


Check6 : SMTP Relay test

You are advised to perform 2 test both on local host and use another pc. This is to ensure that
there are no connection issue such as Firewalls or relay deny from Postfix



telnet centos.example
.local 25

220 centos.example.local ESMTP Postfix

helo server

250 centos.example.local

mail from:<alex@abc.local>

250 2.1.0 Ok

rcpt to:<yasmin@xyz.local>

250 2.1.5 Ok

data

354 End data with <CR><LF>.<CR><LF>

this is a mail from alex. How are you?

.

250 2.0.
0 Ok: queued as 9602D4A80A2

quit

221 2.0.0 Bye


If the connection fail when performing smtp relay test on other pc. Please go to
/etc/postfix/main.cf


then check on the following


#vim /etc/postfix/main.cf



93 myorigin = $mydomain


110 inet_interfaces = a
ll


<
--

make sure this is not inet_interface=localhost


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


255 mynetworks = 192.168.139.0/24, 127.0.0.0/8


<
--
this is the ip that can used to
sent email

:wq



Check9 : SquirrelMail Check

Login t
o


http://you
-
mailserver
-
ipaddress/webmail

now. Login to SquirrelMail


using
alex@abc.local and yasmin@xyz.local must be in user@domainname format.


Try to sent email to
each other to verify the SquirrelMail is handling the email correctly. If you faced an
y issue run the
http://you
-
mailserver
-
ipaddress/src/configtest.php
to verify your SquirrelMail setting.



Note: If you are setting Mailserver for your orgnization to communicate to external organization.
Please also make sure that you have:



Valid FQDN doma
in name




Valid MX records




Firewall rules to allow POP, SMTP, and IMAP




Perform a Open Relay test to avoid Spam Attack