News

  • Installing and configuring the mail server Postfix + Dovecot

    The system shall be declared the hostname and FQDN. You can check the host name with this command:

    hostname

    You can check FQDN with this command:

    hostname -f

    They must be valid and will likely differ from each other. We have it newsletterplus for host and newsletterplus.net for FQDN. If you do not set these parameters then no sense to continue. Better to contact our technical support.

    Installing server components

    If you don't have installed MySQL server - first to install MySQL:

    sudo apt-get install mysql-server

    Install the necessary components with all dependencies.

    sudo apt-get install postfix postfix-mysql dovecot-common dovecot-pop3d dovecot-imapd openssl

    Creating tables for the mail server with MySQL

    Enter to MySQL client:

    mysql -uroot -p -h localhost

    For an example, the password for the database take "123456".

    Create the database:

    CREATE DATABASE `mailserver` COLLATE 'utf8_general_ci';

    Choose a database:

    USE mailserver;

    Create the tables needed for the mail server:

    CREATE TABLE `virtual_domains` (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL
    ) ENGINE = InnoDB DEFAULT CHARSET=utf8;

    CREATE TABLE `virtual_users` (
    id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    domain_id INT(11) NOT NULL,
    user VARCHAR(40) NOT NULL,
    password VARCHAR(32) NOT NULL,
    CONSTRAINT UNIQUE_EMAIL UNIQUE (domain_id,user),
    FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE) ENGINE = InnoDB DEFAULT CHARSET=utf8;

    CREATE TABLE `virtual_aliases` (
    id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    domain_id INT(11) NOT NULL,
    source VARCHAR(40) NOT NULL,
    destination VARCHAR(80) NOT NULL,
    FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
    ) ENGINE = InnoDB DEFAULT CHARSET=utf8;

    CREATE VIEW view_users AS
    SELECT CONCAT(virtual_users.user, '@', virtual_domains.name) AS email,
    virtual_users.password
    FROM virtual_users
    LEFT JOIN virtual_domains ON virtual_users.domain_id=virtual_domains.id;

    CREATE VIEW view_aliases AS
    SELECT CONCAT(virtual_aliases.source, '@', virtual_domains.name) AS email,
    destination
    FROM virtual_aliases
    LEFT JOIN virtual_domains ON virtual_aliases.domain_id=virtual_domains.id;

    Now add your virtual domain (our domain is newsletterplus.net):

    INSERT INTO virtual_domains (id, name) VALUES (1, 'newsletterplus.net');

    and our virtual mail user (we have contact@newsletterplus.net):

    INSERT INTO virtual_users (id, domain_id, user, password) VALUES (1, 1, 'contact', MD5('email_password'));

    Exit from MySQL client:

    exit;

    Postfix mail server configuration

    Postfix config files are located in the /etc/postfix. If you suddenly lost or spoiled configuration, it is possible to copy the source code from here - /usr/share/postfix.

    Create a file /etc/postfix/mysql-virtual-mailbox-domains.cf with the contents:

    user = root
    password = 123456
    hosts = 127.0.0.1
    dbname = mailserver
    query = SELECT 1 FROM virtual_domains WHERE name='%s'

    Create a file /etc/postfix/mysql-virtual-mailbox-maps.cf:

    user = root
    password = 123456
    hosts = 127.0.0.1
    dbname = mailserver
    query = SELECT 1 FROM view_users WHERE email='%s'

    Create a file /etc/postfix/mysql-virtual-alias-maps.cf:

    user = root
    password = 123456
    hosts = 127.0.0.1
    dbname = mailserver
    query = SELECT destination FROM view_aliases WHERE email='%s'

    Create a file /etc/postfix/mysql-email2email.cf:

    user = root
    password = 123456
    hosts = 127.0.0.1
    dbname = mailserver
    query = SELECT email FROM view_users WHERE email='%s'

    Now set permissions to configs:

    sudo chgrp postfix /etc/postfix/mysql-*.cf
    sudo chmod u=rw,g=r,o= /etc/postfix/mysql-*.cf

    Require the user with home directory for mail and a separate group for it:

    sudo groupadd -g 5000 vmail
    sudo useradd -g vmail -u 5000 vmail -d /home/vmail -m

    Postfix configuration file is not yet available, create it using the package configuration wizard:

    sudo dpkg-reconfigure postfix

    Next in a wizard choose:

    General type of mail configuration — Internet Site
    System mail name — koteika.ru (if you have correct FQDN, the wizard fills itself)
    Root and postmaster mail recipient — Fill postmaster address on your taste, it will be necessary to create a box in this domain. (For example postmaster@newsletterplus.net)
    Other destinations to accept mail for — leave blank (still it overlaps virtual processors)
    Force synchronous updates on mail queue — No
    Local networks — 127.0.0.0/8 (subnet address, shipping address from which will go without authorization)
    Mailbox size limit — 0 (without limits)
    Local address extension character — empty field
    Internet protocols to use — IPv4

    Additional changes in the main postfix config:

    sudo postconf -e virtual_uid_maps=static:5000
    sudo postconf -e virtual_gid_maps=static:5000

    sudo postconf -e virtual_mailbox_domains=mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf
    sudo postconf -e virtual_mailbox_maps=mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf
    sudo postconf -e virtual_alias_maps=mysql:/etc/postfix/mysql-virtual-alias-maps.cf,mysql:/etc/postfix/mysql-email2email.cf

    sudo postconf -e virtual_transport=dovecot
    sudo postconf -e dovecot_destination_recipient_limit=1

    Edit the file /etc/postfix/master.cf

    In the end appends:

    dovecot unix - n n - - pipe
    flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -d ${recipient}

    Two spaces in front of «flags =» required! Next restart postfix:

    sudo service postfix restart

    Configuring a virtual transport dovecot

    First install dovecot-mysql package

    sudo apt-get install dovecot-mysql

    Getting editing config files.

    File /etc/dovecot/dovecot-sql.conf.ext:

    driver = mysql
    connect = host=127.0.0.1 dbname=mailuser user=root password=123456
    default_pass_scheme = MD5
    password_query = SELECT email as user, password FROM view_users WHERE email='%u';
    user_query = \
    SELECT 5000 AS uid, 5000 AS gid, '/home/vmail/%d/%n' AS home

    /etc/dovecot/conf.d/10-auth.conf

    disable_plaintext_auth = yes
    auth_mechanisms = plain login
    !include auth-sql.conf.ext

    /etc/dovecot/conf.d/auth-sql.conf.ext

    # Look up user passwords from a SQL database as
    # defined in /etc/dovecot/dovecot-sql.conf.ext
    passdb {
    driver = sql
    args = /etc/dovecot/dovecot-sql.conf.ext
    }
    # Look up user information from a SQL database as
    # defined in /etc/dovecot/dovecot-sql.conf.ext
    userdb {
    driver = sql
    args = /etc/dovecot/dovecot-sql.conf.ext
    }

    /etc/dovecot/conf.d/10-mail.conf

    mail_location = maildir:/home/vmail/%d/%n

    namespace {

    type = private
    separator = .
    prefix = INBOX.
    inbox = yes

    }

    mail_uid = vmail
    mail_gid = vmail

    /etc/dovecot/conf.d/10-master.conf — edit only auth section

    service auth {

    unix_listener auth-userdb {
    mode = 0600
    user = vmail
    group = vmail
    }

    unix_listener /var/spool/postfix/private/auth {
    mode = 0660
    user = postfix
    group = postfix
    }
    }

    /etc/dovecot/conf.d/10-ssl.conf

    ssl = yes
    ssl_cert = ssl_key =

    /etc/dovecot/conf.d/15-lda.conf — specify the e-mail address of the administrator

    postmaster_address = postmaster@newsletterplus.net

    Set permissions to config files:

    chown -R vmail:dovecot /etc/dovecot
    chmod -R o-rwx /etc/dovecot

    Make changes to postfix config:

    sudo postconf -e smtpd_recipient_restrictions=reject_unauth_pipelining,permit_mynetworks,permit_sasl_authenticated,reject_non_fqdn_recipient,reject_unknown_recipient_domain,reject_unauth_destination

    Configuring TLS / SSL certificates

    Create a certificate for dovecot:

    sudo openssl req -new -x509 -days 3650 -nodes -out /etc/ssl/certs/dovecot.pem \
    -keyout /etc/ssl/private/dovecot.pem

    When creating try to indicate the actual data:

    Country Name (2 letter code) [AU]: RU
    State or Province Name (full name) [Some-State]: Mary El
    Locality Name (eg, city) []: Yoshkar Ola
    Organization Name (eg, company) [Internet Widgits Pty Ltd]: Plus Software
    Organizational Unit Name (eg, section) []:
    Common Name (eg, YOUR name) []: Plus Software
    Email Address []: postmaster@newsletterplus.net

    Set permissions for a certificate:

    sudo chmod o= /etc/ssl/private/dovecot.pem

    Create a certificate for postfix:

    sudo openssl req -new -x509 -days 3650 -nodes -out /etc/ssl/certs/postfix.pem \
    -keyout /etc/ssl/private/postfix.pem

    Set permissions for a certificate:

    sudo chmod o= /etc/ssl/private/postfix.pem

    Now a little more set up postfix and add information about certificates and authorization settings:

    sudo postconf -e smtpd_tls_cert_file=/etc/ssl/certs/postfix.pem
    sudo postconf -e smtpd_tls_key_file=/etc/ssl/private/postfix.pem
    sudo postconf -e smtpd_use_tls=yes

    sudo postconf -e smtpd_sasl_type=dovecot
    sudo postconf -e smtpd_sasl_path=private/auth
    sudo postconf -e smtpd_sasl_auth_enable=yes
    sudo postconf -e smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination

    Restart postfix and dovecot:

    sudo service postfix restart
    sudo service dovecot restart

    Information about errors and other events can be found in the log files:

    /var/log/mail.log
    /var/log/mail.err
    /var/log/mail.warn
    /var/log/mail.info

    Our technical support team takes this instruction. If you could not set up postfix and dovecot yourself, please contact our technical support. Our experts will help you.

Post comment