×
Community Blog How to Setup Postal Mail Server on Ubuntu 18.04

How to Setup Postal Mail Server on Ubuntu 18.04

In this tutorial, we will learn how to install and configure Postal on an Alibaba Cloud Elastic Compute Service (ECS) Ubuntu 18.04 server.

By Hitesh Jethva, Alibaba Cloud Community Blog author.

Introduction

Postal is a free, open source mail server written in Ruby and JavaScript. It is a feature-rich mail server application that can be used as an alternative to Sendgrid, Mailchimp, Mailgun and other similar SMTP servers.

In this tutorial, we will learn how to install and configure a Postal mail server on an Alibaba Cloud Elastic Compute Service (ECS) Ubuntu 18.04 server.

Prerequisites

  • A fresh Alibaba Cloud instance with Ubuntu 18.04 installed.
  • A root password is set up to your instance.
  • A valid domain name is configured with your instance.

Create a new ECS instance and connect to your instance as the root user.

Once you are logged into your Ubuntu 18.04 instance, run the following command to update your base system with the latest available packages.

apt-get update -y

Install and Configure MariaDB

First, you will need to install MariaDB database server to your server. You can install it with the following command:

apt-get install mariadb-server libmysqlclient-dev -y

After installing MariaDB, you will need to secure it first. You can secure it by running the following command:

mysql_secure_installation

Answer all the questions as shown below:

    Enter current password for root (enter for none):
    Set root password? [Y/n]: N
    Remove anonymous users? [Y/n]: Y
    Disallow root login remotely? [Y/n]: Y
    Remove test database and access to it? [Y/n]:  Y
    Reload privilege tables now? [Y/n]:  Y

Once the MariaDB server is secured, log in to MariaDB shell with the following command:

mysql -u root -p

Enter your root password, then create a database and user for Postal:

MariaDB [(none)]> CREATE DATABASE postal CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;
MariaDB [(none)]> CREATE USER 'postal'@'localhost' IDENTIFIED BY 'password';

Next, grant all the privileges to the postal database:

MariaDB [(none)]> GRANT ALL ON postal.* TO 'postal'@'localhost';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON `postal-%`.* to `postal`@`localhost` IDENTIFIED BY "password";

Next, flush the privileges and exit from the MariaDB shell with the following command:

MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> QUIT;

Install Ruby and RabbitMQ

Next, you will need to install Ruby to your server. By default, Ruby is not available in the Ubuntu 18.04 default repository. So, you will need to add Ruby repository to your server.

You can add the Ruby repository with the following command:

apt-get install software-properties-common
apt-add-repository ppa:brightbox/ruby-ng

Next, update the repository and install Ruby with the following command:

apt-get update -y
apt-get install ruby2.3 ruby2.3-dev build-essential -y

RabbitMQ is an open source message broker software that implements the Advanced Message Queuing Protocol. Postal uses RabbitMQ for queueing. So, you will also need to install RabbitMQ and Erlang to your server.

First, download and add the Erlang GPG key with the following command:

wget -O- https://packages.erlang-solutions.com/ubuntu/erlang_solutions.asc | apt-key add -

Next, add the Erlang repository with the following command:

echo "deb https://packages.erlang-solutions.com/ubuntu bionic contrib" | tee /etc/apt/sources.list.d/erlang.list

Next, update the repository and install Erlang with the following command:

apt-get update -y
apt-get install erlang -y

Once Erlang has been installed, you can proceed to install RabbitMQ.

First, download and add the RabbitMQ GPG key with the following command:

wget -O- https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc | apt-key add -
wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | apt-key add -

Next, add the RabbitMQ repository with the following command:

echo "deb https://dl.bintray.com/rabbitmq/debian $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/rabbitmq.list

Next, update the repository and install RabbitMQ with the following command:

apt-get update -y
apt-get install rabbitmq-server -y

Once the RabbitMQ has been installed, check the status of RabbitMQ service with the following command:

systemctl status  rabbitmq-server

Next, you will need to create RabbitMQ vhost and user for postal. You can do this with the following command:

rabbitmqctl add_vhost /postal
rabbitmqctl add_user postal password
rabbitmqctl set_permissions -p /postal postal ".*" ".*" ".*"

Install Nodejs

By default, the latest version of Nodejs is not available in the Ubuntu 18.04 default repository. So, you will need to add repository for that.

You can add the Nodejs repository with the following command:

curl -sL https://deb.nodesource.com/setup_10.x | bash

Next, update the repository and install Nodejs with the following command:

apt-get install nodejs -y

Install Postal

Before installing Postal, you will need to create a user for postal mail server. You can create it with the following command:

useradd -r -m -d /opt/postal -s /bin/bash postal

Next, allow ruby to listen on web ports with the following command:

setcap 'cap_net_bind_service=+ep' /usr/bin/ruby2.5

Next, install all the required gems with the following command:

gem install bundler

Next, install procodile with gem:

gem install procodile

Next, install nokogiri with gem:

gem install nokogiri -v '1.7.2'

Next, create a directory structure for Postal with the following command:

mkdir -p /opt/postal/app

Next, download the latest version of Postal with the following command:

wget https://postal.atech.media/packages/stable/latest.tgz

Once the download is completed, extract the downloaded file with the following command:

tar zxpv latest.tgz -C /opt/postal/app

Next, change ownership of postal directory with the following command:

chown -R postal:postal /opt/postal

Next, create a symlink for Postal binary with the following command:

ln -s /opt/postal/app/bin/postal /usr/bin/postal

Next, install all the required dependencies with the following command:

postal bundle /opt/postal/vendor/bundle

Next, generate Postal Configuration files with the following command:

postal initialize-config

Next, open Postal configuration file and make some changes:

nano /opt/postal/config/postal.yml

Make the following changes as per your environment:

web:
  host: postal.example.com
  # The protocol that requests to the management interface should happen on
  protocol: https

main_db:
  # Specify the connection details for your MySQL database
  host: localhost
  username: postal
  password: password
  database: postal

message_db:
  # Specify the connection details for your MySQL server that will be house the
  # message databases for mail servers.
  host: localhost
  username: postal
  password: password
  prefix: postal

rabbitmq:
  # Specify the connection details for your RabbitMQ server.
  host: 127.0.0.1
  username: postal
  password: password
  vhost: /postal
  
dns:
  # Specifies the DNS record that you have configured. Refer to the documentation at
  # https://github.com/atech/postal/wiki/Domains-&-DNS-Configuration for further
  # information about these.
  mx_records:
    - mx.postal.example.com
  smtp_server_hostname: postal.example.com
  spf_include: spf.postal.example.com
  return_path: rp.postal.example.com
  route_domain: routes.postal.example.com
  track_domain: track.postal.example.com

smtp:
  # Specify an SMTP server that can be used to send messages from the Postal management
  # system to users. You can configure this to use a Postal mail server once the
  # your installation has been set up.
  host: 127.0.0.1
  port: 2525
  username: # Complete when Postal is running and you can
  password: # generate the credentials within the interface.
  from_name: Postal
  from_address: postal@yourdomain.com

Save and close the file. Then, initialize database with the following command:

postal initialize

Next, you will need to create a admin user for Postal. You can do this with the following command:

postal make-user

Answer all the questions as shown below:

Postal User Creator
Enter the information required to create a new Postal user.
This tool is usually only used to create your initial admin user.

E-Mail Address      : admin@example.com
First Name          : Admin
Last Name           : Postal
Initial Password:   : *********

User has been created with e-mail address admin@example.com

Finally, start the Postal application with the following command:

postal start

You can check the status of Postal with the following command:

postal status

You can stop the Postal service any time with the following command:

postal stop

Create Systemd Service file for Postal

Next, you will need to create a systemd service file to manage Postal service. You can do this with the following command:

nano /etc/systemd/system/postal.service

Add the following lines:

[Unit]
Description=Postal Mail Platform
After=mysql.service rabbitmq-server.service
Wants=mysql.service rabbitmq-server.service

[Service]
ExecStart=/usr/bin/postal start
ExecStop=/usr/bin/postal stop
ExecReload=/usr/bin/postal restart
User=postal
Restart=on-failure
Type=forking

[Install]
WantedBy=mysql.service rabbitmq-server.service

Save and close the file. Then, reload the systemd with the following command:

systemctl daemon-reload

Next, start Postal service and enable it to start on boot with the following command:

systemctl start postal 
systemctl enable postal 

You can check the status of Postal service with the following command:

systemctl status postal

Install and Configure Nginx

Next, you will need to install Nginx to access Postal mail server. First, install Nginx with the following command:

apt-get install nginx openssl -y

Next, copy Nginx configuration file with the following command:

cp /opt/postal/app/resource/nginx.cfg /etc/nginx/sites-available/default

Next, create a self-signed SSL certificate with the following command:

mkdir /etc/nginx/ssl/
openssl req -x509 -newkey rsa:4096 -keyout /etc/nginx/ssl/postal.key -out /etc/nginx/ssl/postal.cert -days 365 -nodes

Answer all the questions as shown below:

Generating a 4096 bit RSA private key
...............................++
.................++
writing new private key to '/etc/nginx/ssl/postal.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:IN
State or Province Name (full name) [Some-State]:Gujarat
Locality Name (eg, city) []:Ahmedabad
Organization Name (eg, company) [Internet Widgits Pty Ltd]:IT
Organizational Unit Name (eg, section) []:IT
Common Name (e.g. server FQDN or YOUR name) []:Hitesh
Email Address []:admin@example.com

Next, open the Nginx default virtual host file and define your domain:

nano /etc/nginx/sites-available/default

Make the following changes:

server_name postal.example.com;

Save and close the file. Then, restart Nginx service with the following command:

systemctl restart nginx

Access Postal Web Interface

Now, open your web browser and type the URL https://postal.example.com. You will be redirected to the following page:

1

Provide your administrator email and password which you have created earlier. Then, click on the Login button. You should see the following page:

2

Next, click on the Create the first organization. You should see the following page:

3

Next, provide your organization name and click on the Create organization button. You should see the following page:

4

That's it! Now, you can easily build your own mail server for your organization.

0 0 0
Share on

Hiteshjethva

38 posts | 4 followers

You may also like

Comments

Hiteshjethva

38 posts | 4 followers

Related Products

  • Web App Service

    Web App Service allows you to deploy, scale, adjust, and monitor applications in an easy, efficient, secure, and flexible manner.

    Learn More
  • Alibaba Mail

    Alibaba Mail is one of the only email service providers in the industry that supports public cloud services and provides fast, secure, and stable services.

    Learn More
  • Web Hosting

    Explore how our Web Hosting solutions help small and medium sized companies power their websites and online businesses.

    Learn More
  • Web Hosting Solution

    Explore Web Hosting solutions that can power your personal website or empower your online business.

    Learn More