In this tutorial, we will cover the steps needed for installing a mail server using Postfix, Dovecot, SQLite, and PostfixAdmin on Ubuntu 18.04.
- PostfixAdmin is a free web-based interface. With PostfixAdmin, we can configure and manage a Postfix-based email server for many users.
- Postfix is a Mail Transfer Agent (MTA) that relays mail between different mail servers across the Internet.
- Dovecot is a popular Local Delivery Agent (LDA) that deliver mail received from postfix into their final destinations (mailboxes, programs, etc..)
With these three packages combined, your server will be ready to send and receive emails, along with a web-based interface for managing your email server. Let’s begin.
Requirements:
- For the purposes of this tutorial, we will use an Ubuntu 18.04 VPS.
- Full SSH root access or a user with sudo privileges is also required.
Step 1 – Getting Started
Connect to your server via SSH as the root user using the following command:
ssh root@IP_ADDRESS -p PORT_NUMBER
and replace IP_ADDRESS and PORT_NUMBER with your actual server IP address and SSH port number.
Before starting with the installation you will need to update your system packages to their latest version.
You can do this by running the following command:
apt-get update apt-get upgrade
Step 2 – Create a System User
Before we create a system user, we need to install some necessary packages.
sudo apt-get -y install wget nano dbconfig-common sqlite3
Now we will create a new system user with the following commands. This user 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
Step 3 – Install PHP 7.3 and All Required PHP Modules
We will install the latest stable version of PHP 7.3 and all required PHP modules with the following commands:
apt install software-properties-common python-software-properties add-apt-repository ppa:ondrej/php apt update sudo apt-get install php7.3 php7.3-cli php7.3-common php-fpm php-cli php7.3-mbstring php7.3-imap php7.3-sqlite3
Once these are installed, we can then install Nginx, which will serve as our web server for PostfixAdmin.
Step 4 – Install and configure Nginx
To install Nginx from the official Ubuntu repositories, we will run the following command:
sudo apt-get install nginx
Now we need to create a new Nginx server block with the following content. First, open the file:
sudo nano /etc/nginx/sites-available/postfixadmin.your_domain.com
Then add the following:
server { listen 80; server_name postfixadmin.your_domain.com; root /var/www/postfixadmin-3.0; index index.php; charset utf-8; location / { try_files $uri $uri/ index.php; } location ~* \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_pass unix:/run/php/php7.3-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; } }
Make sure you replace your_domain.com with your actual registered domain name.
To activate the server block, we will create a symbolic link:
sudo ln -s /etc/nginx/sites-available/postfixadmin.your_domain.com /etc/nginx/sites-enabled/postfixadmin.your_domain.com
To make Nginx aware of this, we’ll need to restart it.
sudo service nginx restart
Step 5 – Install and Configure PostfixAdmin
PostfixAdmin supports MySQL, PostgreSQL and SQLite databases. In this tutorial, we will use SQLite as our database system.
Now we will download PostfixAdmin and extract it to the /var/www/
directory:
wget -q -O - "http://downloads.sourceforge.net/project/postfixadmin/postfixadmin/postfixadmin-3.0/postfixadmin-3.0.tar.gz" | sudo tar -xzf - -C /var/www
Open the mail configuration file and edit the following values:
sudo nano /var/www/postfixadmin-3.0/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';
Now, change the owner for the folder so that it belongs to the web server:
sudo chown -R www-data: /var/www/postfixadmin-3.0
We will then 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
If everything is set up correctly, you should open your browser and go to:
https://postfixadmin.your_domain.com/setup.php
You should see something like below:
- Depends on: SQLite – OK
- Testing database connection – OK – sqlite://:xxxxx@//var/vmail/postfixadmin.db
To create a new admin user, run the following command:
bash /var/www/postfixadmin-3.0/scripts/postfixadmin-cli admin add admin@your_domain.com --password strong_password --password2 strong_password --superadmin 1 --active 1
Make sure to replace strong_password with an actual strong password.
If you don’t want to do this using the command line, you can also create a new admin user from the PostfixAdmin Web Interface.
Step 6 – Install and Configure Postfix
We can install Postfix with the following command:
sudo apt-get install postfix
We need to create the following files:
sudo nano /etc/postfix/sqlite_virtual_alias_maps.cf
Add the following to the file:
dbpath = /var/vmail/postfixadmin.db
query = SELECT goto FROM alias WHERE address='%s' AND active = '1'
Now the next file:
sudo nano /etc/postfix/sqlite_virtual_alias_domain_maps.cf
Add this to it:
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'
Save that file, then add the next one:
sudo nano /etc/postfix/sqlite_virtual_alias_domain_catchall_maps.cf
Insert this content into it:
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'
Save and close the file. Let’s create the next one:
sudo nano /etc/postfix/sqlite_virtual_domains_maps.cf
Add this to the file:
dbpath = /var/vmail/postfixadmin.db
query = SELECT domain FROM domain WHERE domain='%s' AND active = '1'
Save and close again. Once again, let’s create the next file that we need.
sudo nano /etc/postfix/sqlite_virtual_mailbox_maps.cf
This is what should be added as the content:
dbpath = /var/vmail/postfixadmin.db
query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'
Onto the last file. Create it with this command:
sudo nano /etc/postfix/sqlite_virtual_alias_domain_mailbox_maps.cf
Then add this to it:
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'
Finally, save and close the file.
Now we will edit the main.cf configuration file by executing the following command:
postconf -e "myhostname = $(hostname -A)"
We need to edit the file so that it looks like this. We’re adding all of the files that we just created:
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"
Save and close the file once you are done.
Also, we need to edit the master.cf file. Open the file like so:
sudo nano /etc/postfix/master.cf
Find the submission inet n
and smtps inet n
sections, and make sure the file looks like this:
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
Save and close the file. Now restart the Postfix service and enable it.
systemctl enable postfix systemctl restart postfix
Step 7 – Install and Configure Dovecot
Now that everything else is set up, we need to install Dovecot with SQLite support:
sudo apt-get install dovecot-imapd dovecot-lmtpd dovecot-pop3d dovecot-sqlite
We need to 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
We then need to open the /etc/dovecot/conf.d/10-auth.conf
file and change the following values so that it looks like this:
auth_mechanisms = plain login #!include auth-system.conf.ext !include auth-sql.conf.ext
We will create a new dovecot-sql.conf.ext
file:
sudo nano /etc/dovecot/dovecot-sql.conf.ext
Add this as the content:
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
We will open the /etc/dovecot/conf.d/15-lda.conf
file and set the postmaster_address
email address.
postmaster_address = postmaster@vps.your_domain.com
Make sure you replace your_domain.com with your domain name.
Also, we need to open the /etc/dovecot/conf.d/10-master.conf
file, find the service lmtp
section and change it so that it looks like this:
service lmtp { unix_listener /var/spool/postfix/private/dovecot-lmtp { mode = 0600 user = postfix group = postfix } }
Then find the service auth
section and change it to this:
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 }
We will also change the service auth-worker
section to the following:
service auth-worker { user = vmail }
Close the file, and 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
If everything is set up correctly, you should be able to log in to your PostfixAdmin Web interface and create your first virtual domain and mailbox.
Of course, you don’t have to set up and configure a mail server with PostfixAdmin on your Ubuntu 18.04 server if you use one of our Managed Server Support services, in which case you can simply ask our expert Linux admins to set up and configure a mail server with PostfixAdmin for you. They are available 24×7 and will take care of your request immediately. This can be especially helpful since setting up a mail server is not the simplest thing.
PS. If you liked this post, on how to Set up a mail server with PostfixAdmin on Ubuntu 18.04, please share it with your friends on the social networks using the buttons below or simply leave a comment in the Comments Section below. Thanks.