Install Mailtrain on Ubuntu 18.04
Tags: mailtrain, ubuntu
Published: 2020-03-24 16:32:53

 Mailtrain is a free open-source, self-hosted, newsletter application which is built on NodeJs and MySQL/MariaDB. Mailtrain allows us to send bulk emails by uploading e-mailing lists, with that it has email triggers, segmentation, email template generation from drag and drop to HTML editor. All you need is the SMTP credentials of your ESP.  
In this tutorial, you will learn the steps required to successfully install and integrate Mailtrain Open Source Email Newsletter Application on a fresh Linux- Ubuntu 18.04 instance.  
Please go through these prerequisites  
  - A hosting server with a minimum of 1GB ram available for Mailtrain service. (To be at safer side, it is recommended to have slightly higher capacity)  
  - Node.js (v7+)
  - MySQL (v5.5+ or MariaDB)
  - Redis (for session storage)
  - MySQL (v5.5+ or MariaDB) 
With these requirements fulfilled get-go through the installation steps  

#Installation
###Step 1: Check Node version

```sh
$ sudo node -v 
```

You can install Node.js using these commands (Skip this step, if you already have NodeJs installed)
```sh
$ sudo apt install build-essential
$ sudo apt-get install manpages-dev
$ sudo apt-get install nodejs
```
###Step 2: Check MySQL version
```sh
$ sudo mysql -v
```
For the installation of MySQL server please refer to [this](https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/#apt-repo-fresh-install) article. (Skip, in case you have MySQL already installed):

###Step 3: Clone Mailtrain git repository on your server directory
```sh
$ sudo git clone git://github.com/Mailtrain-org/mailtrain.git
$ cd mailtrain
```
###Step 4: In this step you will create a new database and a new user, so log in to your MySQL instance and follow the given steps:

Log in to MySQL and create a new database.

    mysql> create database mailtrain;

Create a new user with its hostname and password 

    mysql> CREATE USER 'mailtrainuser'@'localhost' IDENTIFIED BY 'password';

Give user access and exit

    mysql> GRANT ALL PRIVILEGES ON * . * TO 'mailtrainuser'@'localhost';

    mysql> EXIT;

###Step 5: Copy the file config/default.toml to config/production.toml

    $ cp config/default.toml config/production.toml

###Step 6: Edit config file

    $ sudo vim config/production.toml

###Step 7: Now you will make changes to the following sections in production.toml file:

Change the MySQL details based on the database you have created.
```sh
[mysql]
host="localhost" // your mysql host
user="mailtrainuser" // username
password="password" // mysql password
database="mailtrain" // database
```
Enable Redis if you have it installed.
```sh
[redis]
enabled=true
host="localhost"
port=6379
db=5
```
For installation of Redis follow the given set of commands
```sh
$ sudo apt-get install redis-server
$ sudo systemctl enable redis-server.service
```
###Step 8: Run npm install

    $ npm install --production

###Step 9: For the final step, you will run the Mailtrain.

    $ NODE_ENV=production npm start

You can run Mailtrain like this or you can create a Mailtrain service. To do that, you can now end the currently running instance by pressing [Ctrl + c] and follow the next steps.

#Create and run Mailtrain service file

###Step 1: Create a new user group forMailtrain

    $ sudo adduser --system --group mailtrain

###Step 2: Add user group in the production.toml file

    $ sudo vim config/production.toml

Add these credentials

    user="mailtrain"
    group="mailtrain"

###Step 3: Add permissions for our Mailtrain user to execute our directory

    $ sudo chown mailtrain:mailtrain /var/www/html/mailtrain/ -R

###Step 4: Copy our Mailtrain service file to our systemd system directory

    $ sudo cp /var/www/html/mailtrain/setup/mailtrain.service /etc/systemd/system/

###Step 5: Change the working directory in our mailtrain.service file

    $ sudo vim /etc/systemd/system/mailtrain.service

Change "/opt/mailtrain" to whatever your Mailtrain folder path is for example "/var/www/html/mailtrain"

###Step 6: Start the Mailtrain service

    $ sudo service mailtrain start

And with this, you have the Mailtrain service up and running. You can check the status of the Mailtrain service by using this command

    $ sudo service mailtrain status

With this done, you have successfully installed Mailtrain service on your Linux server.

The next step is to set up a reverse proxy.

#Setup reverse proxy

###Step 1: Edit production.toml file

    $ sudo vim config/production.toml

Modify these lines
```sh
host="127.0.0.1" 
proxy=true
```
###Step 2: Server config setup

For Apache2, use the commands mentioned below:
```sh
$ sudo apt install apache2
$ sudo systemctl start apache2
$ sudo systemctl enable apache2
```
You can find the Mailtrain server configuration setting for Apache in the following path.

    $ vim /var/www/html/mailtrain/setup/mailtrain-apache.conf

Note:- You need to enable Proxy Mod on Apache2 (only if you haven't enabled it yet). This can be done by using the following commands:
```sh
$ sudo a2enmod proxy 
$ sudo a2enmod proxy_http
```
These are the settings you will be adding in your apache2.conf file:
```sh
$ vim /etc/apache2/apache2.conf
```
```sh
ProxyPreserveHost On
ProxyPass        "/" "http://127.0.0.1:3000/"
ProxyPassReverse "/" "http://127.0.0.1:3000/"
ServerName example.com
ServerAlias www.example.com
```
Restart the server

    $ sudo service apache2 restart

For Nginx, use the commands mentioned below:
```sh
$ sudo apt install nginx
$ sudo systemctl start nginx
$ sudo systemctl enable nginx
```
You can find the Mailtrain server configuration setting for Nginx in the following path.

    $ vim /var/www/html/mailtrain/setup/mailtrain-nginx.conf 

These are the settings you will be adding in your nginx.conf file :
```sh
$ vim /etc/nginx/nginx.conf
```
```sh
    server {
    listen 80;
    listen [::]:80;

    server_name mailtrain.org www.mailtrain.org;
    access_log /var/log/nginx/mailtrain.log;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://127.0.0.1:3000;
        proxy_redirect off;
    }
}
```
Restart the server:

    $ sudo service nginx restart

Step 3: Restart Mailtrain service

    $ sudo service mailtrain restart

With this, you will be able to access Mailtrain dashboard on your domain URL

http://{your-domain-address}
I hope with these simple steps you are now able to use the Mailtrain set up for your marketing campaigns. For setting up your SMTP credentials and other settings you can refer the following link which is the original post:

Mailtrain setup for Ubuntu 18.04 - https://pepipost.com/tutorials/how-to-install-mailtrain-on-ubuntu-18-04/

For installation on Centos 7 - https://pepipost.com/tutorials/how-to-install-mailtrain-on-centos-7/