Email Server on a Linux
Email Server on a Linux
A LINUX/UBUNTU
Prepared by LukuwiNET
Introduction
• Today, I will demonstrate how to configure an email server for your company, allowing
you to have your own official email address rather than using services like Gmail,
Yahoo, or Outlook. This setup will grant you full control over your email system,
without limitations on the size, bandwidth, or number of users.
• We will be using open-source technologies such as:
• - Postfix (for sending emails),
• - Dovecot(for retrieving emails),
• - Apache(as a web server),
• - MySQL (for the database),
• - Roundcube(as the webmail client),
• - Ubuntu(as the operating system).
Introduction…….
• By the end of this tutorial, you will have a fully functional
email server, which can handle both incoming and
outgoing emails with no third-party limitations. You’ll also
be able to manage emails via the web interface using
Roundcube.
Prerequisites
• Ubuntu Server: This tutorial uses Ubuntu 20.04 LTS, but the steps will work with
newer versions as well.
• Static IP Address: For your email server to be reachable on the web, you need a
public static IP address.
• Domain Name: It’s highly recommended to have a domain name to make your
email server professional (e.g., yourcompany.com).
• DNS Configuration: You’ll need to configure DNS records for your domain. This
includes an MX Record pointing to your mail server, and A Record for your
domain’s IP address.
• Basic Linux Knowledge: Familiarity with the command line, and installing and
configuring packages.
Step 1: Update Your Server and Install Required
Packages
The first step in setting up your email server is to ensure your server is
up-to-date and that you have the necessary packages installed.
1 Update and Upgrade Your System:
Before installing any packages, it’s always a good idea to update the
system:
• sudo apt update
• sudo apt upgrade
This will ensure all your software packages are current and any security
patches have been applied.
Step 1: Update Your Server and
Install Required Packages……..
2 Install Required Packages:
We need Apache (web server), MySQL (database), PHP (to
run Roundcube), and other PHP extensions required by
Roundcube. Run the following command to install
everything:
Command:
sudo apt install apache2 mysql-server php php-mysql php-
imap php-mbstring php-xml php-zip php-json libapache2-
mod-php
Step 1: Update Your Server and
Install Required Packages……..
Explanation:
Apache2: Our web server, used to host the Roundcube webmail interface.
MySQL: Used for storing email data, like users, emails, and Roundcube’s
settings.
PHP: A scripting language required for Roundcube’s web interface to
function.
PHP Extensions: These are additional features PHP needs to
communicate with the database and handle email-specific functions (like
IMAP for retrieving mail).
Step 2: Install and Configure Postfix (Mail Transfer
Agent)
Postfix is the software that will handle the sending and receiving of
emails. It is a widely used Mail Transfer Agent (MTA), known for its
performance and ease of configuration.
1 Install Postfix:
sudo apt install postfix
2 Configuring Postfix:
Choose "Internet Site" when asked for the mail server type.
For the system mail name, use your public IP address (or domain name
if you have one, e.g., mail.domain.com
Step 2: Install and Configure Postfix
(Mail Transfer Agent)…….
Postfix Configuration Details:
Once installed, you need to make some additional changes
to the Postfix configuration file. This file is where you define
how Postfix behaves when sending or receiving emails.
1 Edit the Postfix configuration file:
sudo nano /etc/postfix/main.cf
Step 2: Install and Configure Postfix
(Mail Transfer Agent)…….
2 Modify the following lines:
myhostname = management
mydomain = management.com
myorigin = $mydomain
inet_interfaces = all
inet_protocols = ipv4
myhostname = management.co.tz
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated,
reject_unauth_destination
Step 2: Install and Configure Postfix
(Mail Transfer Agent)…….
Explanation:
myhostname: The name of your mail server.
mydomain: Your domain name (e.g., domain.com)
myorigin: This specifies the domain name that will appear in the
"From" address when your server sends emails.
inet_interfaces: Specifies which network interfaces Postfix listens to
for incoming mail. “ all “ means it listens on all network interfaces.
inet_protocols: Set to ipv4 to force Postfix to use IPv4 only.
Step 2: Install and Configure Postfix
(Mail Transfer Agent)…….
3 Restart Postfix to apply the changes:
sudo systemctl restart postfix
Step 3: Install and Configure Dovecot (IMAP/POP3
Server)
Dovecot is the software that will allow users to retrieve their emails using
protocols like IMAP or POP3. While Postfix handles sending emails, Dovecot
handles receiving and managing them.
1 Install Dovecot:
Install Dovecot and the required protocols:
sudo apt install dovecot-core dovecot-imapd dovecot-pop3d
2 Configure Dovecot to Use Maildir Format:
To store emails efficiently, we’ll configure Dovecot to use the Maildir format,
which stores each email as a separate file (more efficient than the older mbox
format).
Step 3: Install and Configure
Dovecot (IMAP/POP3 Server)…..
• Open the Dovecot configuration file:
sudo nano /etc/dovecot/conf.d/10-mail.conf
Now that Postfix and Dovecot are handling email transport and
retrieval, we need to install Roundcube, a web-based email client
that will allow you to manage your emails through a user-friendly
web interface.
1 Install Roundcube:
sudo apt install roundcube roundcube-core roundcube-mysql
2 During the installation, you’ll be asked to configure the Roundcube
database. Select "Yes" and enter the MySQL root password when
prompted.
Step 5: Configure Roundcube to Work with Postfix and
Dovecot
You will need to create system users for each person who will
have an email account. These users will have a corresponding
email address on your domain (e.g. [email protected])
1 Create system users:
sudo adduser user1
sudo adduser user2
Each of these commands will create a new user (with a home
directory), which can be used for email accounts.
Step 6: Create Email Accounts…..
2 Create Maildir directories for the users:
sudo mkdir -p /home/user1/Maildir
sudo mkdir -p /home/user2/Maildir
sudo chown -R user1:user1 /home/user1/Maildir
sudo chown -R user2:user2 /home/user2/Maildir
sudo chmod -R 700 /home/user1/Maildir
sudo chmod -R 700 /home/user2/Maildir
Step 7: Configure Apache to Serve Roundcube
then uncomment
service pop3-login {
inet_listener pop3 {
port = 110
}
inet_listener pop3s {
port = 995
ssl = yes
}
}
Step 7: Configure Apache to Serve Roundcube
…..
service imap-login {
inet_listener imap {
port = 143
}
inet_listener imaps {
port = 993
ssl = yes
}
Step 7: Configure Apache to Serve Roundcube
…..