1. Update the system
sudo apt-get update && sudo apt-get -y upgrade
2. Install necessary packages
sudo apt-get -y install wget nano dbconfig-common sqlite3
3. Create system user
For security reasons, we will create a new system user who will be the owner of all mailboxes.
sudo useradd -r -u 150 -g mail -d /var/vmail -s /sbin/nologin -c "Virtual Mail User" vmail
sudo mkdir -p /var/vmail
sudo chmod -R 770 /var/vmail
sudo chown -R vmail:mail /var/vmail
4. Install PHP 7.0 and all required PHP modules
If you don’t have PHP installed on your server you can install the latest stable version of PHP 7.0 and all necessary modules, with the following command:
sudo apt-get -y install php-fpm php-cli php7.0-mbstring php7.0-imap php7.0-sqlite3
5. Install and configure Nginx
If you don’t have a web server installed on your machine, install Nginx from the official Ubuntu repositories:
sudo apt-get -y install nginx
Create a new Nginx server block with the following content:
sudo nano /etc/nginx/sites-available/postfixadmin.your_domain.com
server {
listen 80;
server_name postfixadmin.your_domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name postfixadmin.your_domain.com;
root /var/www/postfixadmin-3.1;
index index.php;
charset utf-8;
ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem;
ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key;
ssl_protocols TLSv1.2;
ssl_ciphers "EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS:!RC4";
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_ecdh_curve secp521r1;
location / {
try_files $uri $uri/ index.php;
}
location ~* \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
}
Activate the server block by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/postfixadmin.your_domain.com /etc/nginx/sites-enabled/postfixadmin.your_domain.com
Test the Nginx configuration and restart nginx:
sudo nginx -t
sudo service nginx restart
6. PostfixAdmin
Download the PostfixAdmin archive from SourceForge and extract it in the /var/www/ directory:
wget -q -O - "http://downloads.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-3.1/postfixadmin-3.1.tar.gz" | sudo tar -xzf - -C /var/www
Open the mail configuration file and edit the following values:
sudo nano /var/www/postfixadmin-3.1/config.inc.php
$CONF['configured'] = true;
$CONF['database_type'] = 'sqlite';
$CONF['database_name'] = '/var/vmail/postfixadmin.db';
// $CONF['database_host'] = 'localhost';
// $CONF['database_user'] = 'postfix';
// $CONF['database_password'] = 'postfixadmin';
// $CONF['database_name'] = 'postfix';
$CONF['domain_path'] = 'NO';
$CONF['domain_in_mailbox'] = 'YES';
sudo chown -R www-data: /var/www/postfixadmin-3.1
Create the SQLite database:
sudo touch /var/vmail/postfixadmin.db
sudo chown vmail:mail /var/vmail/postfixadmin.db
sudo usermod -a -G mail www-data
To populate the database go to https://postfixadmin.your_domain.com/setup.php
and you should see something like below:
Testing database connection - OK - sqlite://:xxxxx@//var/vmail/postfixadmin.db
Create a new admin user:
bash /var/www/postfixadmin-3.1/scripts/postfixadmin-cli admin add admin@your_domain.com --password strong_password --password2 strong_password --superadmin 1 --active 1
7. Install and configure postfix
Install postfix with the command bellow:
sudo apt-get install postfix
Create the following files:
sudo nano /etc/postfix/sqlite_virtual_alias_maps.cf
dbpath = /var/vmail/postfixadmin.db
query = SELECT goto FROM alias WHERE address='%s' AND active = '1'
sudo nano /etc/postfix/sqlite_virtual_alias_domain_maps.cf
dbpath = /var/vmail/postfixadmin.db
query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = printf('%u', '@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'
sudo nano /etc/postfix/sqlite_virtual_alias_domain_catchall_maps.cf
dbpath = /var/vmail/postfixadmin.db
query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = printf('@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'
sudo nano /etc/postfix/sqlite_virtual_domains_maps.cf
dbpath = /var/vmail/postfixadmin.db
query = SELECT domain FROM domain WHERE domain='%s' AND active = '1'
sudo nano /etc/postfix/sqlite_virtual_mailbox_maps.cf
dbpath = /var/vmail/postfixadmin.db
query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'
sudo nano /etc/postfix/sqlite_virtual_alias_domain_mailbox_maps.cf
dbpath = /var/vmail/postfixadmin.db
query = SELECT maildir FROM mailbox,alias_domain WHERE alias_domain.alias_domain = '%d' and mailbox.username = printf('%u', '@', alias_domain.target_domain) AND mailbox.active = 1 AND alias_domain.active='1'
Edit the main.cf
file:
postconf -e "myhostname = $(hostname -A)"
postconf -e "virtual_mailbox_domains = sqlite:/etc/postfix/sqlite_virtual_domains_maps.cf"
postconf -e "virtual_alias_maps = sqlite:/etc/postfix/sqlite_virtual_alias_maps.cf, sqlite:/etc/postfix/sqlite_virtual_alias_domain_maps.cf, sqlite:/etc/postfix/sqlite_virtual_alias_domain_catchall_maps.cf"
postconf -e "virtual_mailbox_maps = sqlite:/etc/postfix/sqlite_virtual_mailbox_maps.cf, sqlite:/etc/postfix/sqlite_virtual_alias_domain_mailbox_maps.cf"
postconf -e "smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem"
postconf -e "smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.key"
postconf -e "smtpd_use_tls = yes"
postconf -e "smtpd_tls_auth_only = yes"
postconf -e "smtpd_sasl_type = dovecot"
postconf -e "smtpd_sasl_path = private/auth"
postconf -e "smtpd_sasl_auth_enable = yes"
postconf -e "smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination"
postconf -e "mydestination = localhost"
postconf -e "mynetworks = 127.0.0.0/8"
postconf -e "inet_protocols = ipv4"
postconf -e "virtual_transport = lmtp:unix:private/dovecot-lmtp"
Open the master.cf
file, find submission inet n
and smtps inet n
sections and edit as follows:
sudo nano /etc/postfix/master.cf
smtp inet n - y - - smtpd
#smtp inet n - y - 1 postscreen
#smtpd pass - - y - - smtpd
#dnsblog unix - - y - 0 dnsblog
#tlsproxy unix - - y - 0 tlsproxy
submission inet n - y - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
# -o smtpd_reject_unlisted_recipient=no
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
# -o smtpd_helo_restrictions=$mua_helo_restrictions
# -o smtpd_sender_restrictions=$mua_sender_restrictions
# -o smtpd_recipient_restrictions=
# -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING
smtps inet n - y - - smtpd
-o syslog_name=postfix/smtps
# -o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
# -o smtpd_reject_unlisted_recipient=no
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
# -o smtpd_helo_restrictions=$mua_helo_restrictions
# -o smtpd_sender_restrictions=$mua_sender_restrictions
# -o smtpd_recipient_restrictions=
# -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING
Enable and restart the postfix service
systemctl enable postfix
systemctl restart postfix
8. Install and Configure Dovecot
Install dovecot with sqlite support using the command bellow:
sudo apt-get install dovecot-imapd dovecot-lmtpd dovecot-pop3d dovecot-sqlite
Open the /etc/dovecot/conf.d/10-mail.conf
file and change the following values:
mail_location = maildir:/var/vmail/%d/%n
mail_privileged_group = mail
mail_uid = vmail
mail_gid = mail
first_valid_uid = 150
last_valid_uid = 150
Open the /etc/dovecot/conf.d/10-auth.conf
file and change the following values:
auth_mechanisms = plain login
#!include auth-system.conf.ext
!include auth-sql.conf.ext
Create a new dovecot-sql.conf.ext
file:
sudo nano /etc/dovecot/dovecot-sql.conf.ext
driver = sqlite
connect = /var/vmail/postfixadmin.db
default_pass_scheme = MD5-CRYPT
password_query = \
SELECT username as user, password, '/var/vmail/%d/%n' as userdb_home, \
'maildir:/var/vmail/%d/%n' as userdb_mail, 150 as userdb_uid, 8 as userdb_gid \
FROM mailbox WHERE username = '%u' AND active = '1'
user_query = \
SELECT '/var/vmail/%d/%n' as home, 'maildir:/var/vmail/%d/%n' as mail, \
150 AS uid, 8 AS gid, printf('dirsize:storage=', quota) AS quota \
FROM mailbox WHERE username = '%u' AND active = '1'
In the /etc/dovecot/conf.d/10-ssl.conf
file enable SSL support:
ssl = yes
.
Open the /etc/dovecot/conf.d/15-lda.conf
file and set the postmaster_address
email address.
postmaster_address = [email protected]_domain.com
Open the /etc/dovecot/conf.d/10-master.conf
file, find the service lmtp section and change it to:
service lmtp {
unix_listener /var/spool/postfix/private/dovecot-lmtp {
mode = 0600
user = postfix
group = postfix
}
}
find the service auth section and change it to:
service auth {
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
unix_listener auth-userdb {
mode = 0600
user = vmail
#group = vmail
}
user = dovecot
}
Change the service auth-worker section to the following:
service auth-worker {
user = vmail
}
Set the permissions:
chown -R vmail:dovecot /etc/dovecot
chmod -R o-rwx /etc/dovecot
Enable and restart the dovecot service
systemctl enable dovecot
systemctl restart dovecot
9. SpamAssassin
SpamAssassin is an open source tool written in Perl which helps filter out unwanted messages. If you want to enable and configure SpamAssassin please continue with the following steps.
To install SpamAssassin, run:
sudo apt-get install spamassassin
sudo adduser spamd --disabled-login
Open the /etc/default/spamassassin
file and make the following changes:
ENABLED=1
OPTIONS="--create-prefs --max-children 5 -d 127.0.0.1 --username spamd --helper-home-dir /home/spamd/ -s /home/spamd/spamd.log"
PIDFILE="/home/spamd/spamd.pid"
CRON=1
To integrate SpamAssassin with Postfix, append the following at the end of the /etc/postfix/master.cf
file:
smtp inet n - - - - smtpd
-o content_filter=spamassassin
spamassassin unix - n n - - pipe
user=nobody argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f ${sender} ${recipient}
Enable and restart the spamassassin service
systemctl enable spamassassin
systemctl restart spamassassin
systemctl restart postfix
If everything is setup correctly now you should be able to login to your PostfixAdmin back-end by going to https://postfixadmin.your_domain.com/
and create your first virtual domain and mailbox.