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

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 “[email protected]” to use it to send/receive emails easily instead of using services like Hotmail, Gmail, Yahoo Mail, etc.

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.

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

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

$ sudo hostnamectl set-hostname mail.tecmint.com

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.

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

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

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.

$ sudo apt-get update -y
$ sudo apt-get upgrade -y
$ 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 mailutils

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.

$ sudo apt -y install lsb-release apt-transport-https ca-certificates 
$ sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
$ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
$ sudo apt update
$ 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 mailutils

Step 3: Installing Postfix Mail Server on Ubuntu

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.

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

$ sudo apt-get install postfix

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

Install Postfix in Ubuntu
Install Postfix in Ubuntu

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

Set Postfix Mail Domain
Set Postfix Mail Domain

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.

$ postconf mail_version
$ sudo systemctl status postfix
Check Postfix Version
Check Postfix Version

Step 4: Testing Postfix Mail Server on Ubuntu

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

$ telnet gmail-smtp-in.l.google.com 25

Trying 74.125.200.27...
Connected to gmail-smtp-in.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP k12si849250plk.430 - gsmtp

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

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

$ mail [email protected]

Cc: 
Subject: Testing My Postfix Mail Server
I'm sending this email using the postfix mail server from Ubuntu machine

Step 5: Installing Dovecot IMAP and POP in Ubuntu

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

$ sudo apt-get install dovecot-imapd dovecot-pop3d
Install Dovecot in Ubuntu
Install Dovecot in Ubuntu

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

$ sudo systemctl restart dovecot
OR
$ sudo service dovecot restart
Start Dovecot in Ubuntu
Start Dovecot in Ubuntu

Step 6: Installing Roundcube Webmail in Ubuntu

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.

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

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.

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

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

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

Step 7: Create an Apache Virtual Host for Roundcube Webmail

14. Create an apache virtual host for Roundcube webmail.

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

Add the following configuration in it.

<VirtualHost *:80>
  ServerName tecmint.com
  DocumentRoot /var/www/html/roundcubemail/

  ErrorLog ${APACHE_LOG_DIR}/roundcube_error.log
  CustomLog ${APACHE_LOG_DIR}/roundcube_access.log combined

  <Directory />
    Options FollowSymLinks
    AllowOverride All
  </Directory>

  <Directory /var/www/html/roundcubemail/>
    Options FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
  </Directory>

</VirtualHost>

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

$ sudo a2ensite roundcube.conf
$ sudo systemctl reload apache2

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

Roundcube Webmail Installer
Roundcube Webmail Installer

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

Roundcube Webmail Database Settings
Roundcube Webmail Database Settings

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

Create Roundcube Configuration File
Create Roundcube Configuration File

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.

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

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

http://yourdomain.com/roundcubemail/
Roundcube Webmail Login
Roundcube Webmail Login

Step 8: Creating Mail Users

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

$ sudo useradd myusername

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

$ sudo passwd myusername

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

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?

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

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

  1. In step 2, many packages could not be found (I use a Debian 10 server).

    Terminal info:

    [email protected]:/etc/apache2/sites-enabled# 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 mailutils
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Package php-net-ldap2 is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    Package php-net-ldap3 is not available but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source

    E: Unable to locate package php7.4
    E: Couldn’t find any package by glob ‘php7.4’
    E: Couldn’t find any package by regex ‘php7.4’
    E: Unable to locate package libapache2-mod-php7.4
    E: Couldn’t find any package by glob ‘libapache2-mod-php7.4’
    E: Couldn’t find any package by regex ‘libapache2-mod-php7.4’
    E: Unable to locate package php7.4-mysql
    E: Couldn’t find any package by glob ‘php7.4-mysql’
    E: Couldn’t find any package by regex ‘php7.4-mysql’
    E: Package ‘php-net-ldap2’ has no installation candidate
    E: Package ‘php-net-ldap3’ has no installation candidate
    E: Unable to locate package php7.4-common
    E: Couldn’t find any package by glob ‘php7.4-common’
    E: Couldn’t find any package by regex ‘php7.4-common’
    E: Unable to locate package php7.4-gd
    E: Couldn’t find any package by glob ‘php7.4-gd’
    E: Couldn’t find any package by regex ‘php7.4-gd’
    E: Unable to locate package php7.4-imap
    E: Couldn’t find any package by glob ‘php7.4-imap’
    E: Couldn’t find any package by regex ‘php7.4-imap’
    E: Unable to locate package php7.4-json
    E: Couldn’t find any package by glob ‘php7.4-json’
    E: Couldn’t find any package by regex ‘php7.4-json’
    E: Unable to locate package php7.4-curl
    E: Couldn’t find any package by glob ‘php7.4-curl’
    E: Couldn’t find any package by regex ‘php7.4-curl’
    E: Unable to locate package php7.4-zip
    E: Couldn’t find any package by glob ‘php7.4-zip’
    E: Couldn’t find any package by regex ‘php7.4-zip’
    E: Unable to locate package php7.4-xml
    E: Couldn’t find any package by glob ‘php7.4-xml’
    E: Couldn’t find any package by regex ‘php7.4-xml’
    E: Unable to locate package php7.4-mbstring
    E: Couldn’t find any package by glob ‘php7.4-mbstring’
    E: Couldn’t find any package by regex ‘php7.4-mbstring’
    E: Unable to locate package php7.4-bz2
    E: Couldn’t find any package by glob ‘php7.4-bz2’
    E: Couldn’t find any package by regex ‘php7.4-bz2’
    E: Unable to locate package php7.4-intl
    E: Couldn’t find any package by glob ‘php7.4-intl’
    E: Couldn’t find any package by regex ‘php7.4-intl’
    E: Unable to locate package php7.4-gmp
    E: Couldn’t find any package by glob ‘php7.4-gmp’
    E: Couldn’t find any package by regex ‘php7.4-gmp’

    Reply
    • @Michael,

      I think PHP 7.4 not included in the official Debian repository. If you still want to install PHP 7.4 on Debian 10, you need to enable SURY PHP PPA repository and then install PHP 7.4 on Debian 10 or Debian 9 as shown.

      Install PHP 7.4 on Debian 10

      $ sudo apt -y install lsb-release apt-transport-https ca-certificates 
      $ sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
      $ echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
      $ sudo apt update
      $ 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 mailutils
      
      Reply
  2. Thanks for this tutorial. It was really helpful.

    However, I can get emails from Roundcube but I cannot send emails from that interface. I get the following error: SMTP Error (-1): Connection to server failed.

    Reply
  3. Pretty much every package failed to load / could not be found when running step 2

    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package php7.4
    E: Couldn’t find any package by glob ‘php7.4’
    E: Couldn’t find any package by regex ‘php7.4’
    E: Unable to locate package libapache2-mod-php7.4
    E: Couldn’t find any package by glob ‘libapache2-mod-php7.4’
    E: Couldn’t find any package by regex ‘libapache2-mod-php7.4’
    E: Unable to locate package php7.4-mysql
    E: Couldn’t find any package by glob ‘php7.4-mysql’
    E: Couldn’t find any package by regex ‘php7.4-mysql’
    E: Unable to locate package php7.4-common
    E: Couldn’t find any package by glob ‘php7.4-common’
    E: Couldn’t find any package by regex ‘php7.4-common’
    E: Unable to locate package php7.4-gd
    E: Couldn’t find any package by glob ‘php7.4-gd’
    E: Couldn’t find any package by regex ‘php7.4-gd’
    E: Unable to locate package php7.4-imap
    E: Couldn’t find any package by glob ‘php7.4-imap’
    E: Couldn’t find any package by regex ‘php7.4-imap’
    E: Unable to locate package php7.4-json
    E: Couldn’t find any package by glob ‘php7.4-json’
    E: Couldn’t find any package by regex ‘php7.4-json’
    E: Unable to locate package php7.4-curl
    E: Couldn’t find any package by glob ‘php7.4-curl’
    E: Couldn’t find any package by regex ‘php7.4-curl’
    E: Unable to locate package php7.4-zip
    E: Couldn’t find any package by glob ‘php7.4-zip’
    E: Couldn’t find any package by regex ‘php7.4-zip’
    E: Unable to locate package php7.4-xml
    E: Couldn’t find any package by glob ‘php7.4-xml’
    E: Couldn’t find any package by regex ‘php7.4-xml’
    E: Unable to locate package php7.4-mbstring
    E: Couldn’t find any package by glob ‘php7.4-mbstring’
    E: Couldn’t find any package by regex ‘php7.4-mbstring’
    E: Unable to locate package php7.4-bz2
    E: Couldn’t find any package by glob ‘php7.4-bz2’
    E: Couldn’t find any package by regex ‘php7.4-bz2’
    E: Unable to locate package php7.4-intl
    E: Couldn’t find any package by glob ‘php7.4-intl’
    E: Couldn’t find any package by regex ‘php7.4-intl’
    E: Unable to locate package php7.4-gmp
    E: Couldn’t find any package by glob ‘php7.4-gmp’
    E: Couldn’t find any package by regex ‘php7.4-gmp’

    Reply

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.