Set Up And Configure Postfix E-Mail Server with Dovecot
With this tutorial, we assume that you have already installed Postfix, an open-source mail transfer agent. After that, we install and configure Dovecot, an open source IMAP and POP3 email server for Linux/UNIX-like systems.
Finally, we shall install SquirrelMail, a mail user interface, to show that Postfix and Dovecot really work.
What We Are Going To Cover
- How to install Apache and PHP 7.3
- Install Postfix mail server
- Installing Dovecot as a mail client
- Install and configure SquirrelMail
- Annuling errors in SquirrelMail installation
- Creating a user which will send and receive mail
- Send mail from SquirrelMail
Prerequisites
We use Ubuntu 16.04:
- Starting with a clean VPS with
- At least 512Mb of RAM and
- 15Gb of free disk space.
- You will need root user access and
- DNS records for your domain must be already in place, especially PTR and MX.
- In this post we assume that you have worked through “How To Install And Configure Postfix“, and that you have Postfix up and running as instructed.
We start from scratch and install all that we need to finish up with a running SquirrelMail.
Step 1: Install Apache
First, update your package manager’s cache:
sudo apt update -y
Install the Apache web server:
sudo apt install apache2 -y
Enable its service to make it run on every system boot:
sudo systemctl enable apache2
Finally, start it:
sudo systemctl start apache2
To verify that Apache was installed successfully, access it from your local browser by navigating to http://YOUR_DOMAIN/. If that does not work, try adding :80 in the end, like this:
http://YOUR_DOMAIN:80
You should see a welcome page for Apache, which means that you now have Apache running.
Step 2: Install PHP 7.3
First, install the prerequisite packages:
sudo apt install software-properties-common python-software-properties
Then, add the ondrej PPA:
sudo add-apt-repository -y ppa:ondrej/php
and update your sources by running:
sudo apt update
Install PHP 7.3 using the following command:
sudo apt install php7.3 php7.3-cli php7.3-common
Step 3: Install PHP Extensions
These are the usual extensions that many programs expect to be there:
- session support, the Standard PHP Library (SPL) extension, hash, ctype, and JSON support
- mbstring, zip, gd. openssl, libxml, curl
Install them:
sudo apt install php7.3-curl php7.3-gd php7.3-json php7.3-mbstring php7.3-intl php7.3-mysql php7.3-xml php7.3-zip
Restart Apache to activate:
sudo systemctl restart apache2
Step 4: Install Postfix Mail Server
We have gone over installation of Postfix in some length in article “How To Install And Configure Postfix“. Here we only repeat the commands:
sudo apt-get install postfix
Select Internet site, and enter FQDN site name, for instance, aleksasavic.com. Next restart Postfix:
sudo service postfix restart
Step 5: Install Dovecot
Postfix is the mail server while Dovecot is a mail delivery agent (MDA). They cooperate as Dovecot delivers the emails from/to Postfix.
Dovecot is a secure IMAP server. It silently indexes email messages in the background, and will replace most other IMAP clients. Besides Postfix, it works with Exim as well, and will even offer workarounds for some bugs present in other IMAP and POP3 clients
For the basic installation of Dovecot, only two commands are needed:
sudo apt-get install dovecot-imapd dovecot-pop3d
sudo service dovecot restart
Step 6: Install SquirrelMail
SquirrelMail is a standards-based webmail package written in PHP. It includes built-in pure PHP support for the IMAP and SMTP protocols, and all pages render in HTML 4.0 (with no JavaScript required) for maximum compatibility across browsers. It has few requirements and is easy to configure and install. SquirrelMail also supports MIME, address books, and folder manipulation.
Here is how to install it:
sudo apt-get install squirrelmail
sudo squirrelmail-configure
Step 7: Configuring SquirellMail
Now we need to configure SquirrelMail, through a special command:
sudo squirrelmail-configure
We are met with a series of textual menus.
Enter 2 for server settings and another menu:
Now select 1 to change domain name to your domain name.
To go back, enter R and press Enter on the keyboard.
Next to change is option 4 for General Options.
We go after option “Allow server-side sorting”; enter “11” and change it from “false” to “true” by entering “y”. Press Enter on the keyboard, and enter the “S” key to save the configuration file.
Finally, Q to quit.
Now we copy squirrel configuration files to apache:
sudo cp /etc/squirrelmail/apache.conf /etc/apache2/sites-available/squirrelmail.conf
sudo a2ensite squirrelmail.conf && sudo service apache2 reload
Restart Apache to activate:
sudo systemctl restart apache2
We have installed Postfix, Dovecot, and SquirrelMail. Now we should be able to see SquirrelMail in action, by navigating to
aleksasavic.com/squirrelmail
in the browser.
Step 8: Create Email User
Let us now define a user which will have access to the mail:
sudo useradd squser
sudo passwd squser
sudo mkdir -p /var/www/html/squser
We also have to state permissions for the aquser:
sudo chown -R squser:squser /var/www/html/squser
Login into your mail again. If there are directories without permissions, execute these commands:
sudo chmod 777 /var/mail
sudo chmod 777 /var/www
sudo chmod 777 /home
Step 9: Resolving Errors
If there are errors, have a look at errors log:
sudo nano /var/log/mail.err
and find what the complaint is about. In this case:
mkdir(/home/squser/mail) failed: Permission denied (
euid=1002(squser) egid=1002(squser) missing +w perm: /home,
so we need to give writing permission to folder /home:
sudo chmod 777 /home
Execute command
/etc/init.d/postfix reload
to activate the changes we have just created. Restart Postfix:
sudo service postfix restart
Step 10: Encrypt Mail with Standard TLS
Email started out as sending plain text from sender to recipient. That is not safe, so the messages should be encrypted — protected while in transfer. Only the intended recipient should be able to read them.
We shall now protect the transfer with standard protocol STARTTLS:
sudo postconf -e 'smtptlssecuritylevel = may' sudo postconf -e 'smtpdtlssecuritylevel = may' sudo postconf -e 'smtptlsnotestarttlsoffer = yes' sudo service postfix restart
Step 11: Send Email From SquirrelMail
Let us return to the browser, we should enter user name and password. User name is the name of the user that we created, such as squser and password to enter is its password from Ubuntu.
Enter SquirrelMail. If everything goes well, we shall see good old SquirrelMail on the screen. You should be able to send messages immediately, while to receive them, you would have to set up an MX parameter at your registrar’s site. You might also need to configure Postfix a bit more, which is out of scope of this article.
What To Do Next
SquirrelMail is a mature application and its development has stopped five years ago. You can still run it in production environment and it won’t fail you. You may also want to install another email client such as RoundCube, which is much harder to install but is well maintained and more powerful compared to SquirrelMail.
Dusko Savic is a technical writer and Flutter programmer.
Related Posts:
- World Host Group Expands Again with Nimbus Hosting Acquisition - October 30, 2024
- OVHcloud’s 2024 Annual Performance: The Growth Continues - October 29, 2024
- Looking for a VPN?We’ve Got a Thread for You - December 7, 2021
Broken link “How To Install And Configure Postfix”
http://lowendbox.com/blog/how-to-install-and-configure-postfix-on-ubuntu-16-04
I like mailinabox better since it has roundcube
Any SMTP and IMAP/POP server can use roundcube. Just replace the steps for squirrelmail with install steps for roundcube, it can be done.
Are these steps valid for Ubuntu 18.04 and PHP 7.4?
Thanks for the article. Very helpful.