How to Setup a Complete Mail Server (Postfix) using ‘Roundcube’ (Webmail) on Ubuntu/Debian

[‘

n

Creating a mail server on Linux powered machines can be one of the most essential things that every system administrator needs to do while configuring the servers for the first time, if you don’t know what it means; it’s simple, if you have a website like “example.com”, you can create an email account like “[emailxa0protected]” to use it to send/receive emails easily instead of using services like Hotmail, Gmail, Yahoo Mail, etc.

n

In this article, we’ll learn how to do so by installing the Postfix mail server with the “Roundcube” webmail application and its dependencies on Debian 10/9 and Ubuntu 20.04/18.04/16.04 LTS releases.

n

n

Step 1: Set a Hostname and Create DNS Records for Domain

n

1. First, set a valid FQDN (Fully Qualified Domain Name) hostname for your Ubuntu server using the hostnamectl command as shown.

n

$ sudo hostnamectl set-hostname mail.tecmint.comrn

n

2. Next, you need to add a MX and A records for your domain in your DNS control panel that guides other MTAs that your mail server mail.yourdomain.com domain is responsible for email delivery.

n

MX record    @           mail.tecmint.comrnmail.tecmint.com        <IP-address>rn

n

Step 2: Installing Apache, MariaDB, and PHP on Ubuntu

n

3. In order to create a running mail server using “Roundcube”, we’ll have to install Apache2, MariaDB, and PHP packages first, to do so, run.

n

$ sudo apt-get update -yrn$ sudo apt-get upgrade -yrn$ sudo apt install apache2 apache2-utils mariadb-server mariadb-client php7.4 libapache2-mod-php7.4 php7.4-mysql php-net-ldap2 php-net-ldap3 php-imagick php7.4-common php7.4-gd php7.4-imap php7.4-json php7.4-curl php7.4-zip php7.4-xml php7.4-mbstring php7.4-bz2 php7.4-intl php7.4-gmp php-net-smtp php-mail-mime php-net-idna2 mailutilsrn

n

On Debian 10/9, you need to download and install the SURY PHP PPA repository to install PHP 7.4 on Debian 10/9 as shown.

n

$ sudo apt -y install lsb-release apt-transport-https ca-certificates rn$ sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpgrn$ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.listrn$ sudo apt updatern$ sudo apt install apache2 apache2-utils mariadb-server mariadb-client php7.4 libapache2-mod-php7.4 php7.4-mysql php-net-ldap2 php-net-ldap3 php-imagick php7.4-common php7.4-gd php7.4-imap php7.4-json php7.4-curl php7.4-zip php7.4-xml php7.4-mbstring php7.4-bz2 php7.4-intl php7.4-gmp php-net-smtp php-mail-mime php-net-idna2 mailutilsrn

n

Step 3: Installing Postfix Mail Server on Ubuntu

n

4. Postfix is a mail transfer agent (MTA) which is the responsible software for delivering & receiving emails, it’s essential in order to create a complete mail server.

n

To install it on Ubuntu/Debian or even Mint, run:

n

$ sudo apt-get install postfixrn

n

During installation, you will be asked to choose the type of mail configuration, choose “Internet Site”.

n

Install Postfix in Ubuntu
Install Postfix in Ubuntu

n

5. Now enter the fully qualified domain name that you want to use for send and receive emails.

n

Set Postfix Mail Domain
Set Postfix Mail Domain

n

6. Once Postfix installed, it will automatically start and creates a new /etc/postfix/main.cf file. You can verify the Postfix version and status of the service using the following commands.

n

$ postconf mail_versionrn$ sudo systemctl status postfixrn

n

Check Postfix Version
Check Postfix Version

n

Step 4: Testing Postfix Mail Server on Ubuntu

n

7. Now try to check your mail server is connecting on port 25 using the following command.

n

$ telnet gmail-smtp-in.l.google.com 25rnrnTrying 74.125.200.27...rnConnected to gmail-smtp-in.l.google.com.rnEscape character is '^]'.rn220 mx.google.com ESMTP k12si849250plk.430 - gsmtprn

n

The above message indicates that the connection is successfully established. Type quit to close the connection.

n

8. You can also use a mail program to send and read emails using the following command.

n

$ mail [emailxa0protected]rnrnCc: rnSubject: Testing My Postfix Mail ServerrnI'm sending this email using the postfix mail server from Ubuntu machinern

n

Step 5: Installing Dovecot IMAP and POP in Ubuntu

n

9. Dovecot is a mail delivery agent (MDA), it delivers the emails from/to the mail server, to install it, run the following command.

n

$ sudo apt-get install dovecot-imapd dovecot-pop3drn

n

Install Dovecot in Ubuntu
Install Dovecot in Ubuntu

n

10. Next, restart the Dovecot service using the following command.

n

$ sudo systemctl restart dovecotrnORrn$ sudo service dovecot restartrn

n

Start Dovecot in Ubuntu
Start Dovecot in Ubuntu

n

Step 6: Installing Roundcube Webmail in Ubuntu

n

11. Roundcube is the webmail server that you’ll be using to manage emails on your server, it has a simple web interface to do the job, it can be customized by installing more modules & themes.

n

$ wget https://github.com/roundcube/roundcubemail/releases/download/1.4.8/roundcubemail-1.4.8.tar.gzrn$ tar -xvf roundcubemail-1.4.8.tar.gzrn$ sudo mv roundcubemail-1.4.8 /var/www/html/roundcubemailrn$ sudo chown -R www-data:www-data /var/www/html/roundcubemail/rn$ sudo chmod 755 -R /var/www/html/roundcubemail/rn

n

12. Next, you need to create a new database and user for Roundcube and grant all permission to a new user to write to the database.

n

$ sudo mysql -u rootrnMariaDB [(none)]> CREATE DATABASE roundcube DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;rnMariaDB [(none)]> CREATE USER [emailxa0protected] IDENTIFIED BY 'password';rnMariaDB [(none)]> GRANT ALL PRIVILEGES ON roundcube.* TO [emailxa0protected];rnMariaDB [(none)]> flush privileges;rnMariaDB [(none)]> quit;rn

n

13. Next, import the initial tables to the Roundcube database.

n

$ sudo mysql roundcube < /var/www/html/roundcubemail/SQL/mysql.initial.sqlrn

n

Step 7: Create an Apache Virtual Host for Roundcube Webmail

n

14. Create an apache virtual host for Roundcube webmail.

n

$ sudo nano /etc/apache2/sites-available/roundcube.confrn

n

Add the following configuration in it.

n

<VirtualHost *:80>rn  ServerName tecmint.comrn  DocumentRoot /var/www/html/roundcubemail/rnrn  ErrorLog ${APACHE_LOG_DIR}/roundcube_error.logrn  CustomLog ${APACHE_LOG_DIR}/roundcube_access.log combinedrnrn  <Directory />rn    Options FollowSymLinksrn    AllowOverride Allrn  </Directory>rnrn  <Directory /var/www/html/roundcubemail/>rn    Options FollowSymLinks MultiViewsrn    AllowOverride Allrn    Order allow,denyrn    allow from allrn  </Directory>rnrn</VirtualHost>rn

n

15. Next, enable this virtual host and reload the apache for the changes.

n

$ sudo a2ensite roundcube.confrn$ sudo systemctl reload apache2rn

n

16. You can now access the webmail by going to http://yourdomain.com/roundcubemail/installer/.

n

Roundcube Webmail Installer
Roundcube Webmail Installer

n

16. Next, go to the Database settings and add the database details.

n

Roundcube Webmail Database Settings
Roundcube Webmail Database Settings

n

17. After making all the changes, create a config.inc.php file.

n

Create Roundcube Configuration File
Create Roundcube Configuration File

n

18. After finishing the installation and the final tests please delete the installer folder and make sure that enable_installer option in config.inc.php is disabled.

n

$ sudo rm /var/www/html/roundcubemail/installer/ -rrn

n

19. Now go to the login page and enter the user name and the password of the user.

n

http://yourdomain.com/roundcubemail/rn

n

Roundcube Webmail Login
Roundcube Webmail Login

n

Step 8: Creating Mail Users

n

20. In order to start using the Roundcube webmail, you’ll have to create a new user, to do so, run.

n

$ sudo useradd myusernamern

n

Replace “myusername” with the user name you want, create a password for the new user by running.

n

$ sudo passwd myusernamern

n

21. Now go back to the login page and enter the user name and the password of the newly created user.

n

Have you tried to create an email server before? How did it go? Have you used Roundcube or any other mail server before? What do you think about it?

n

‘]