server_setup

New System Configuration

I am always looking for ways to remember how the frell I setup my server. I do it so infrequently that I need to document it somewhere so I don’t forget. So you get to be the next victim for hearing how it was done…

So here goes:

First things first, server configuration. The current server is a base system with the following:

  • CentOS
  • MySql
  • postfix
  • dovecot
  • mailman

Postfix with MySQL in RHEL/CentOS 5

The version of Postfix that comes with Red Hat Enterprise Linux 5 or CentOS 5 does not support MySQL lookups. You can build your own custom Postfix RPM or get a prebuilt RPM from the CentOS Plus repository. You can use the CentOS repository in RHEL since CentOS is binary compatible with RHEL. This article describes how to install Postfix with MySQL lookup table support from the CentOS Plus repository.

  1. Edit the file /etc/yum.repos.d/CentOS-Base.repo or its equivalent in RHEL and edit the lines below.
    [base]
    exclude=postfix
    
    [update]
    exclude=postfix
    
    [centosplus]
    enabled=1
    includepkgs=postfix

    If you are using RHEL, add the lines below under the centosplus section.

    name=CentOS-$releasever - Plus
    mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
    gpgcheck=1
    gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
     
  2. Type yum remove postfix in a terminal window to remove the standard version of Postfix.
  3. Type yum install postfix to install the version of Postfix found in the CentOS Plus repository.
  4. To verify MySQL support, type postconf -m and check if mysql is found in the output.

Now that we have a version of postfix that will support our, mysql based, virtual users for mail services, lets continue…

How to Install Postfix Admin in RHEL/CentOS 5

Postfix Admin is a web based interface used to manage mailboxes, virtual domains and aliases. This section of the article will show you how to install Postfix Admin to simplify management of mailboxes, domains and aliases.
Install the Postfix Admin requirements using the command below.
yum install mysql-server php-mysql php-imap

Download the latest stable version of Postfix Admin in .tar.gz format from the online repository. Assuming you got the file postfixadmin-2.2.1.1.tar.gz and it is located on your Desktop, type in the commands below to extract and to put it into its proper directory.

cd /usr/share
tar -xvzf ~/Desktop/postfixadmin-2.2.1.1.tar.gz
mv postfixadmin-2.2.1.1 postfixadmin

Edit the file /usr/share/postfixadmin/config.inc.php and update the following lines below.

$CONF['configured'] = true;
$CONF['postfix_admin_url'] = '/postfixadmin';
$CONF['database_type'] = 'mysqli';
$CONF['database_host'] = 'localhost';
$CONF['database_user'] = 'postfix';
$CONF['database_password'] = 'your_password';
$CONF['database_name'] = 'postfix';
$CONF['domain_path'] = 'YES';
$CONF['domain_in_mailbox'] = 'NO';
$CONF['encrypt'] = 'cleartext';
$CONF['emailcheck_resolve_domain] = 'NO';

Creating the Postfix Admin Database
Start the mysqld service and launch the MySQL command line tool using the command below.

mysql -u root -p

The default root password of MySQL is a blank password. NOTE: You should set a password for the root user in mysql. Next, create a new MySQL database for Postfix Admin using the commands below.

mysql> CREATE DATABASE postfix;
mysql> CREATE USER postfix@localhost IDENTIFIED BY 'your_password';
mysql> GRANT ALL PRIVILEGES ON postfix.* TO postfix;

Configuring the Postfix Admin Web Application

  1. Create the file /etc/httpd/conf.d/postfixadmin.conf containing the line below.
  2. Start or restart the httpd service.
  3. Go to the Postfix Admin setup page at http://localhost/postfixadmin/setup.php and fill in the setup password. Next, click the Generate password hash.
  4. Get the generated setup password hash and put it into the file /usr/share/postfixadmin/config.inc.php. Next, fill in the Setup password, Admin and Password and Password (again). Finally, click Add Admin to create a new admin account.
  5. Go to the Postfix Admin login page at http://localhost/postfixadmin/ and login using your newly created admin account.
  6. Congratulations, it works.

If you encounter any problems, check the log file at /var/log/httpd/error_log.

Handling Multiple Domains with Postfix Admin in RHEL/CentOS 5

This section of the article will show you how to configure Postfix and Dovecot to handle multiple domains stored in MySQL. Postfix Admin is used to manage the domains, mailboxes and aliases in MySQL. See How to Install Postfix Admin, in the previous section, to setup the MySQL database that will be used by Postfix and Dovecot. Since the user names will be stored in MySQL, we will have to create a user that will be the owner for all the files belonging to the MySQL user names.

# /usr/sbin/useradd vmail

Configure postfix

  1. Install a version of Postfix with MySQL support. (See previous section)
  2. Create the file /etc/postfix/mysql-domains.cf containing the lines below.
    hosts = localhost
    user = postfix
    password = your_password
    dbname = postfix
    table = domain
    select_field = domain
    where_field = domain
    additional_conditions = and backupmx = '0' and active = '1'
  3. Test /etc/postfix/mysql-domains.cf using the command below.
    postmap -q acme.com mysql:/etc/postfix/mysql-domains.cf

    Replace acme.com with your own domain name that you have created in the postfix admin tool. It should echo your domain.

  4. Create the file /etc/postfix/mysql-users.cf containing the lines below.
    hosts = localhost
    user = postfix
    password = your_password
    dbname = postfix
    table = mailbox
    select_field = maildir
    where_field = username
    additional_conditions = and active = '1'
    result_format = %sMaildir/
  5. Test /etc/postfix/mysql-users.cf using the command below.
    postmap -q johndoe@acme.com mysql:/etc/postfix/mysql-users.cf

    Replace johndoe@acme.com with your own email address. You should see the mailbox path.

  6. Create the file /etc/postfix/mysql-aliases.cf containing the lines below.
    hosts = localhost
    user = postfix
    password = your_password
    dbname = postfix
    table = alias
    select_field = goto
    where_field = address
    additional_conditions = and active = '1'
  7. Test /etc/postfix/mysql-aliases.cf using the command below.
    postmap -q john@acme.com mysql:/etc/postfix/mysql-aliases.cf

    Replace john@acme.com with your own alias address. You should see the destination email.

  8. Create the file /etc/postfix/mysql-aliases.cf containing the lines below.
    hosts = localhost
    user = postfix
    password = your_password
    dbname = postfix
    table = alias
    select_field = goto
    where_field = address
    additional_conditions = and active = '1'
  9. Test /etc/postfix/mysql-aliases.cf using the command below.
    postmap -q john@acme.com mysql:/etc/postfix/mysql-aliases.cf

    Replace john@acme.com with your own alias address. You should see the destination email.

  10. Edit the postfix configuration file /etc/postfix/main.cf and edit the line below.
    mydestination = $myhostname, localhost.$mydomain, localhost

    and add the lines below

    virtual_mailbox_domains = mysql:/etc/postfix/mysql-domains.cf
    virtual_mailbox_maps = mysql:/etc/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

    virtual_mailbox_base, virtual_uid_maps and virtual_gid_maps should contain the home directory, user id and group id of vmail respectively. Make sure $mydomain in mydestination has been removed, otherwise the lookup will not work and you will get a “User unknown in local recipient table” error.

  11. Restart the Postfix or MailScanner service if you have installed it.
    service postfix restart
  12. You should now be able to send email to addresses found in MySQL. See Test Postfix using Telnet and try using MySQL email addresses instead of the system user names.

Configuring Dovecot

  1. Create the file /etc/dovecot-mysql.conf containing the lines below.
    driver = mysql
    connect = host=localhost dbname=postfix user=postfix password=password
    default_pass_scheme = PLAIN
    password_query = SELECT password FROM mailbox WHERE username = '%u'
  2. Edit the file /etc/dovecot.conf and change the value of the following keys below.
    auth_username_format = %Lu
    passdb sql {
      args = /etc/dovecot-mysql.conf
    }
    userdb static {
      args = uid=501 gid=501 home=/home/vmail/%d/%n
    }

    uid, gid and home should contain the user id, group id and home directory respectively of the vmail user account. Comment out all the other passdb and userdb sections except for those specified above to ensure that nothing will conflict with our MySQL virtual accounts.

  3. Restart the dovecot service.
  4. You should now be able to login using the user names found in MySQL. See Test Dovecot using Telnet and use MySQL user names (johndoe@acme.com) instead of the system user names (johndoe).

If you encounter any problems, check the log file at /var/log/maillog.

Share

Comments are closed.

Previous Post
«
Next Post
»