Skip to content

Instantly share code, notes, and snippets.

@ravituvar
Created December 24, 2018 09:44
Show Gist options
  • Save ravituvar/05d7883c62c1aab7d1d2acc12fcc597e to your computer and use it in GitHub Desktop.
Save ravituvar/05d7883c62c1aab7d1d2acc12fcc597e to your computer and use it in GitHub Desktop.
Mailserver on Linux (Postfix and Dovecot)

Postfix + Dovecot on Linux

Inhalt:

Prerequisites

  1. Debian x86_64 >=8 oder Ubuntu >= 16.04
  2. Webserver installed (Apache, nginx, Caddyserver, etc.)
  3. PHP >= 5.6 and phpmyadmin
  4. A mail client such as Mozillas Thunderbird
  5. A working MX, as well as a rDNS-Record

Software

Installation

We begin by installing the dependencies, as well as the software required for this tutorial.

$ sudo apt update
$ sudo apt install php7.2-imap dovecot-common dovecot-imapd dovecot-pop3d dovecot-mysqli postfix postfix-mysqli

Then we create the user vmail, whose home directory is located at /var/vmail.

$ useradd -u 6000 vmail -d /var/vmail
$ mkdir /var/vmail
$ chown vmail:vmail /var/vmail

Now we log into our phpmyadmin environment and create the user mail with a database of the same name. After that, the value EUER_PASSWORT in the perpared files must be changed to the password of the database user mail.

We continue by installing the postfixadmin webfrontend.

$ cd /path/to/www-folder
$ wget https://github.com/postfixadmin/postfixadmin/archive/postfixadmin-3.1.zip
$ unzip postfixadmin-3.1.zip
$ mv postfixadmin-3.1 postfixfolder && mv postfixfolder/postfixadmin-3.1 postfixadmin
$ rm postfixadmin-3.1.zip
$ rm -r postfixfolder
$ chmod -R 755 postfixadmin && chown -R www-data:www-data postfixadmin

We edit the config.inc.php file in the postfixadmin folder to fit our needs.

$ cd postfixadmin && nano config.inc.php

We are going to change the following parameters in the config file.

$CONF['configured'] = 'true';
$CONF['default_language'] = 'de';
$CONF['database_type'] = 'mysqli';
$CONF['database_host'] = 'localhost';
$CONF['database_user'] = 'mail';
$CONF['database_password'] = 'YOUR_PASSWORD';
$CONF['database_name'] = 'mail';
$CONF['admin_email'] = 'admin@yourdomain.de';
$CONF['encrypt'] = 'md5';

We add the following parameters.

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

And finally, we save the file and give it its needed privileges.

$ chown -R www-data:www-data *

After that, visit postfixadmin in your browser pointing to yourdomain.de/postfixadmin/setup.php. From there on, follow the instructions given on the weppage by creating a setup password. When finished, a $CONF string will appear, that we need to put into our config file again.

$ nano config.inc.php

And change

$CONF['setup_password'] = 'changeme';
to
$CONF['setup_password'] = 'PutHereYourRandomStringOfTheSetupPassword';

Go back to the setup page of postfixadmin and set an admin user, as well as an admin Mailaccount. After that, we can log into the Webinterface by typing yourdomain.de/postfixadmin/login.php in the browser. Once logged in, we create the new domain in the domain list section, don't forget to uncheck the create aliases option.

Now change the directory to postfix and create the ssl folder.

$ cd /etc/postfix && mkdir ssl && cd ssl/

In the created ssl folder, the certificate, as well as the private key are both put in. Additionally we give the private key restrictive rights for security concerns.

$ chmod 600 private.key && cd ../

Next, we upload all files beginning with mysql-* into the postfix folder and give them the following permissions

$ chmod 600 mysql-* && chmod g+r mysql-*

Edit the main.cf to fit your needs by editing the path to your cert and private key and append the content of the main.cf provided by the prepared files at the end. Save the file after that happened.

Then edit the master.cf and comment in the following lines.

$ nano master.cf

submission
smtps
-o wrappermode

Then, again, append the content of the prepaired master.cf to the end of the file and save it. Now the following permission has to be set. After that, a healthcheck of the postfix configuration will be performed.

$ chown -R root:postfix * && postfix check
$ service postfix restart

If the postfix check returns no error, the Mailserver is configured properly.

Now we change the directory to dovecot and edit the dovecot.conf.

$ cd ../dovecot && nano dovecot.conf

We append the content from the prepaired file again into dovecot.conf and change the Cert and Key Directory for our TLS Configurtion, as well as edit the Postmaster Mail Address to fit our needs (e.g. by using the admin mail address)

Then again save the file and open a new one called dovecot-mysql.conf

$ nano dovecot-mysql.conf

...and paste the content of the prepaired file into the newly created one and save it as well. Last but not least, assign the appropriate rights to the file by executing the following commands.

$ chmod 600 dovecot-mysql.conf
$ chmod g+r dovecot-mysql.conf
$ chown :vmail dovecot-mysql.conf

Finally restart the dovecot and postfix services.

$ service dovecot restart
$ service postfix restart

Congratulations!! Now you can add mailaccounts for your customers by logging into the postfixadmin webinterface with the superuser credentials, visiting the virtual list and adding a mailbox.

At that point, you can already setup the user at thunderbird or on any mail client.

Thank you for reading through!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment