MikeTeo.net

A Software Developer’s Blog (Wanna Email Me?)

This blog post documents the steps required to setup email hosting for multiple domains on a Fedora Core 7 box. At the end of the setup exercise, you will have a email hosting system that uses

  • MySQL for account storage information
  • Postfix as mail transfer agent (MTA) with SMTP/SSL and STARTTLS capability
  • Dovecot for POP3 and IMAP (including SSL for security)
  • PostfixAdmin for account management
  • Lighttpd as a light-weight HTTP daemon service for PostfixAdmin
  • Postgrey as a deterrence against spams.

If you are familiar with system administration and using yum, you should be able to complete this within 30 minutes.

Step 0: Preparation

Create the vmail user that is necessary for holding the received mails.

$> groupadd -g 5000 vmail
$> useradd -u 5000 -g 5000 -m -d /var/vmail -s /sbin/nologin vmail

Step 1: Setup MySQL Database

$> yum install mysql mysql-server php-mysql
$> service mysqld start
$> mysqladmin -u root -p password '<your new password>'

Step 2: Lighttpd

2.1. Install lighttpd

$> yum install lighttpd

2.2. Modify /etc/lighttpd/lighttpd.conf.

You need to uncomment one of the server.modules, mod_cgi.
Then locate the cgi.assign section and change it to the following.

cgi.assign = ( ".php" => "/usr/bin/php-cgi" )

2.3. Start lighttpd

$> service lighttpd start

If you encounter an error that the port is in use, stop Apache service.

$> service httpd stop

and then try starting lighttpd again.

Step 3: PostfixAdmin

3.1. Download PostfixAdmin-2.2 (local mirror) from its download page on SourceForge.

3.2. Untar or unzip the downloaded source file into /srv/www/lighttpd and rename the folder to postfixadmin. The absolute path of the postfixadmin folder should be /srv/www/lighttpd/postfixadmin.

3.3. Login to mysql as root and run the following SQL statements

$> mysql -u root -p
mysql> CREATE DATABASE postfix;
mysql> CREATE USER 'postfix'@'localhost' IDENTIFIED BY 'postfix';
mysql> GRANT ALL PRIVILEGES ON `postfix` . * TO 'postfix'@'localhost';
mysql> FLUSH PRIVILEGES;

3.4. Locate the following lines in config.inc.php in postfixadmin folder and modify them to match your database settings.

$CONF['database_type'] = 'mysqli';
$CONF['database_host'] = 'localhost';
$CONF['database_user'] = 'postfix';
$CONF['database_password'] = 'postfix';
$CONF['database_name'] = 'postfix';
$CONF['encrypt'] = 'md5';
$CONF['domain_path'] = 'YES';
$CONF['domain_in_mailbox'] = 'NO';

3.5. Use your browser to access http://<your_hostname.tld>/postfixadmin/setup.php. You should see a list of ‘OK’ messages.

3.6. If everything is ok, you should see a form prompting you to create the superadmin for the postfixadmin system.

Step 4: Postfix

4.1. Check if you can find any binary RPMs for postfix-2.5, or you can use the binary RPM which I have built. If not, you need to rebuild it yourself from the src rpm (local mirror).

If you don’t have rpmbuild installed, you can install it through yum

$> yum install rpmbuild

Now, rebuild the src rpm.

$> rpmbuild --rebuild  postfix-2.5.1-2.fc9.src.rpm

You may need to yum install some other devel RPMs that are required for the build. Once the rpmbuild is done, the postfix-2.5 RPMs will be in /usr/src/redhat/RPMS/i386/.

4.2. Install the postfix RPM using yum

$> rpm -U postfix-2.5.1.rpm

4.3. Download this postfix-config.zip and unzip in your /etc/postfix folder.

$> unzip -d /etc/postfix postfix-config.zip

4.4. Edit /etc/postfix/main.cf. Change the myhostname and mydomain parameters to match your machine. Avoid changing the rest of the settings unless you know what each of these parameters does.

4.5. If you have modified the MySQL database settings in Step 3, please change the corresponding settings in mysql_XXX_maps.cf.

4.6. Generate the required DH params.

$> openssl dhparam -out /etc/postfix/dh_1024.pem 1024
$> openssl dhparam -out /etc/postfix/dh_512.pem 512

If you don’t have openssl installed,

$> yum install openssl

Step 5: Postgrey

5.1. Install the dependencies for Postgrey.

$> yum install perl-BerkeleyDB perl-Net-Server perl-IO-Multiplex

5.2. Download and install the RPM for Postgrey. (local mirror)

$> rpm -i postgrey-1.27-0.noarch.rpm

5.3. Start postgrey

$> service postgrey start

Step 6: Dovecot

6.1. Install dovecot

$> yum install dovecot

6.2. Download this dovecot-config.zip and unzip in your /etc folder.

$> unzip -d /etc dovecot-config.zip

6.3. Create the log file for dovecot delivery

$> touch /var/log/dovecot-deliver.log
$> chown vmail.mail /var/log/dovecot-deliver.log

6.4. If you have modified the database settings in Step 3, please change the connect line in /etc/dovecot/sql.conf to match your new settings.

6.5. Add the following lines to /etc/logrotate.conf so that the above log file will be rotated with the rest of your system logs.

/var/log/dovecot-deliver.log {
weekly
create 0640 vmail mail
rotate 8
}

6.6. Start dovecot

$> service dovecot start

Step 7: Test

7.1. First shutdown sendmail. This service is usually activated and installed by default on Fedora Core systems.

$> service sendmail stop

7.2. Start postfix

$> service postfix start

7.3. Telnet to port 25 on localhost to see if everything works

$> telnet 127.0.0.1 25

You should see something like this.

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 miketeo.net ESMTP Postfix

The hostname after 220 should be the same as what you have configured in /etc/postfix/main.cf in Step 4.

7.4. You are strongly advised to use Abuse.net open relay test to check if postfix is configured properly as an obedient MTA and not as an open relay. You just need to key in your IP in the address field to test. The test will take a few minutes to complete.

Finally…

At this stage, you can start creating domains and email accounts using the postfixadmin management interface. The URL should be http://<your_hostname.tld>/postfixadmin/login.php. You should need to login first using the superadmin account which you have created in Step 3.

Note that the postfix on your system will accept emails for the email addresses that you have created on postfixadmin. The logins for these email accounts are the email addresses themeselves (e.g. loginID for test@test.com is test@test.com). To access the emails, simply configure either POP3 or IMAP for incoming mail server and enter the login-password information.

Your users can also send email through your system to other domains like hotmail and gmail. However, they must enable SMTP authentication on their email clients. The login-password information for the outgoing mail server is the same as the login-password for the incoming mail service.

Do not enable secure authentication on your email clients. If you need security, access or send your email through SSL

Your services are currently started manually. To ensure that they are started whenever they are rebooted, enter the following commands:

$> chkconfig --level 35 sendmail off
$> chkconfig --level 35 postfix on
$> chkconfig --level 35 mysqld on
$> chkconfig --level 35 dovecot on
$> chkconfig --level 35 postgrey on
$> chkconfig --level 35 lighttpd on

That’s all folks. Hope you have an easy time setting up your email hosting. :-)
If you think this blog post has failed to explain certain points clearly, feel free to drop in your comments to help me improve on it.

Add A Comment