Rob's web

Mailserver

Introduction

We setup the server for multiple virtual domains so we can extend without limits. The only limit is the disk storage space.

An mailsystem is build with multiple parts. The main parts are:

We start with the basic that all three parts use.

Creating vmail user and group

To begin, let's create a user and a group to handle emails (we will need this as our email accounts are not associated with a system user). You can use another UID and GID (other than 5000 as we do below) as long as it's not in use and is a high number:

# groupadd -g 5000 vmail 
# useradd -g vmail -u 5000 vmail -d /srv/mail/vhosts -M -s /usr/sbin/nologin

Creating vmail data directories

The directory structure for user@example.com wil be: /srv/mail/vhosts/example.com/user/.

# cd /srv
# mkdir -p mail/vhosts/
# cd mail/vhosts/
# mkdir example.com (example.org ...)
# cd /srv/
# chown -R vmail:vmail mail/
# chmod -R 770 mail/
# chcon -R system_u:object_r:mail_spool_t:s0 mail/

Creating vmail accounts database

The sql server needs to be running for we can go further.

The mailsystem get its own database, so lets create one.

Create mariadb vmail database

# mariadb -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 31
Server version: 10.5.22-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database mail;
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mail               |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)

MariaDB [(none)]>

Create mariadb vmail tables

We need 3 tables wich we create now.

MariaDB [(none)]> use mail;

MariaDB [mail]> create table 'domains' (
	'DomainId' int not null auto_increment ,
	'DomainName' varchar(50) not null ,
	primary key ('DomainId')) engine = InnoDB;

MariaDB [mail]> create table 'users' ( 
    'UserId' int not null auto_increment,  
    'DomainId' int not null,  
    'password' varchar(110) not null,  
    'Email' varchar(100) not null,  
    primary key ('UserId'),  
    unique key 'Email' ('Email'),  
    foreign key (DomainId) references domains(DomainId) on delete cascade 
) engine = InnoDB;

MariaDB [mail]> create table 'aliases' (
    'AliasId' int not null auto_increment, 
    'DomainId' int not null, 
    'Source' varchar(100) not null, 
    'Destination' varchar(100) not null, 
    primary key ('AliasId'), 
    foreign key (DomainId) references domains(DomainId) on delete cascade
) engine = InnoDB;

MariaDB [mail]> show tables;
+----------------+
| Tables_in_mail |
+----------------+
| aliases        |
| domains        |
| users          |
+----------------+
3 rows in set (0.000 sec)

MariaDB [mail]> quit
Bye
# 

Structuur database

Create mariadb user

Never use the root account for the sql server. Make for every SQL-client its own user account.

# mariadb -u root -p
MariaDB [(none)]> create user 'vmail'@'localhost' identified by 'YourPasswordHere';
MariaDB [(none)]> grant all privileges on mail.* to 'vmail'@'localhost';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> quit
Bye
#

Creating a vmail domain

Enter at least one domain.

# mariadb -u root -p
MariaDB [(none)]> use mail;
MariaDB [mail]> insert into domains (DomainName) values ('example.com'); 

Creating a vmail user

Create at least one user for each domain. User is the email address.

To find de DomainId enter:

MariaDB [mail]> select * from domains;
+----------+--------------+
| DomainId | DomainName   |
+----------+--------------+
|        1 | example.com  |
+----------+--------------+

Now we have all the information to add a user.

MariaDB [mail]> insert into users (DomainId, password, Email) values (1, encrypt('PasswordForEmailAccount',
	concat('$6$', substring(sha(rand()), -16))), 'johndoe@example.com');

Creating a email alias

Aliases are usefull for creating functional email addresses, which can be routed to the person in controll. When this person change you only need to change the alias for that address to the new person.

This is optional but create at least one so we can test that as well.

MariaDB [mail]> insert into aliases (DomainId, Source, Destination) values (1, 'info@example.com', 'johndoe@example.com');
MariaDB [mail]> quit
Bye
# 

Testing the tables

First we start testing if the tables show what we expect.

# mariadb -u root -p
MariaDB [(none)]> use mail;
MariaDB [mail]> select * from domains;
+----------+--------------+
| DomainId | DomainName   |
+----------+--------------+
|        1 | example.com  |
+----------+--------------+
MariaDB [mail]> select * from users;
.....
MariaDB [mail]> select * from aliases;
.....
MariaDB [mail]> quit
Bye
# 

When there are correct tables shown we can continue.

Having added and tested our domain, user and alias tables, we are ready to continue setting up our email server.

Installing dovecot and postfix

Dovecot can do the authentication for postfix. We need it later when we start testing smtp.

Now install, configure and test dovecot.

When dovecot works continue with postfix.

Data flow mail

Links