Installing Mailtrain on an Ubuntu Instance
- compute
- newsletter
- Mailtrain
- Ubuntu
Mailtrain is a self-hosted open-source newsletter application built in Node.js and using MariaDB as datastore.
You may need certain IAM permissions to carry out some actions described on this page. This means:
- you are the Owner of the Scaleway Organization in which the actions will be carried out, or
- you are an IAM user of the Organization, with a policy granting you the necessary permission sets
- You have an account and are logged into the Scaleway console
- You have configured your SSH key
- You have created an Instance running Ubuntu Bionic or later
- You have sudo privileges or access to the root user
- You have unlocked the SMTP ports
Preparing the server
- Connect to your server via SSH.
- Install sudo on the server:
apt install sudo
- Create a regular user account with sudo access and switch into it. Replace my_new_user with your username.
adduser my_new_user --gecos "My New User"usermod -aG sudo my_new_usersu - my_new_user
- Update the already installed software on the system to the latest version:
sudo apt update && apt upgrade -y
- Install the required tools:
sudo apt install -y build-essential unzip
- Install Node.js and NPM:
sudo curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -sudo apt install -y nodejs
Installing Postfix
To send Mail from your server, an SMTP server is required.
- Install Postfix via apt:
sudo apt install -y postfix
- When asked choose Internet Site in the list of options:
- Enter the mail name for your Instance and confirm. The mail name should be a fully qualified domain name (FQDN) resolving to your Instance’s IP address:
Installing MariaDB
-
Download the MariaDB repositories to make sure you have the latest version of the database engine available:
sudo curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash -
Install MariaDB, an open source MySQL fork:
sudo apt install -y mariadb-server -
Initialize the MariaDB server:
sudo mysql_secure_installation -
Connect to the database server as root user with the password you have set previously:
mysql -u root -p -
Create an empty database for Mailtrain:
CREATE DATABASE mailtrain; -
Create a database user for Mailtrain and grant all privileges of the database to the user. Then create another user with read-only access to the database:
GRANT ALL PRIVILEGES ON mailtrain.* TO mailtrain_user@localhost IDENTIFIED BY 'user_password';GRANT SELECT ON mailtrain.* TO mt_readonly@localhost IDENTIFIED BY 'readonly_password';Note:Replace the demo user and password in the commands above with your preferred username and password.
-
Flush the new privileges and leave the database prompt:
FLUSH PRIVILEGES;EXIT;
Installing Nginx
-
Install nginx via apt:
sudo apt install -y nginx -
Create three configuration files for Nginx:
sudo nano /etc/nginx/sites-available/mailtrain.example.com.confsudo nano /etc/nginx/sites-available/sbox-mailtrain.example.com.confsudo nano /etc/nginx/sites-available/newsletter.example.com.confand put the following content into it:
- mailtrain.example.com.conf
server {listen [::]:80;listen 80;server_name mailtrain.example.com;charset utf-8;client_max_body_size 50M;location / {proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_pass http://127.0.0.1:3000;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";proxy_next_upstream error timeout http_502 http_503 http_504;}}- sbox-mailtrain.example.com.conf
server {listen 80;server_name sbox-mailtrain.example.com;access_log /var/log/nginx/sbox-mailtrain.access;error_log /var/log/nginx/sbox-mailtrain.error;location / {proxy_pass http://127.0.0.1:3003;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}- newsletter.example.com.conf*
server {listen 80;server_name newsletter.example.com;access_log /var/log/nginx/newsletter.access;error_log /var/log/nginx/newsletter.error;location / {proxy_pass http://127.0.0.1:3004;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}} -
Activate the configuration by linking the files in the sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/mailtrain.example.com.conf /etc/nginx/sites-enabled/sudo ln -s /etc/nginx/sites-available/sbox-mailtrain.example.com.conf /etc/nginx/sites-enabled/sudo ln -s /etc/nginx/sites-available/newsletter.example.com.conf /etc/nginx/sites-enabled/ -
Reload the nginx configuration to enable the new virtual host:
sudo systemctl reload nginx.service
Remember to replace mailtrain.example.com
with the domain name you want to use for your Mailtrain installation. Optionally, you can encrypt the HTTP traffic by using a free SSL certificate from Let’s Encrypt.
Installing Mailtrain
-
Enter the
www
directory:cd /var/www/ -
Download and unzip Mailtrain:
wget https://github.com/Mailtrain-org/mailtrain/archive/refs/tags/v2.20210609.0.zipunzip v2.20210609.0.ziprm v2.20210609.0.zipmv mailtrain-2.20210609.0 mailtrain -
Change the ownership of the directory to your Mailtrain user:
chown -R my_new_user:my_new_user /var/www/mailtrain -
Enter the Mailtrain directory:
cd mailtrain -
Import the database scheme into the Mailtrain database using the following command. Remember to replace
mailtrain_user
with your database user.mysql -u mailtrain_user -p mailtrain < ./server/setup/sql/mailtrain.sql -
Create a
production.yaml
file by opening it in a text editor. This file allows you to overwrite the default configuration values in the file./server/config/default.yaml
.nano ./server/config/production.yaml -
Copy the following configuration into the file and edit it towards your requirements. Then save the file and exit the text editor:
user: my_new_usergroup: my_new_userroUser: nobodyroGroup: nobodywww:host: 127.0.0.1proxy: truesecret: "Enter secret and random text here"trustedUrlBase: http://mailtrain.example.comsandboxUrlBase: http://sbox-mailtrain.example.compublicUrlBase: http://newsletter.example.commysql:user: mailtrain_userpassword: user_passworddatabase: mailtrainredis:enabled: truelog:level: infobuiltinZoneMTA:enabled: falsequeue:processes: 5Note:The Mailtrain configuration requires you to specify the following URL endpoints.
mailtrain.example.com
: The configuration interface for logged-in users.sbox-mailtrain.example.com
: This is used to host template editors. This URL is not shown to any user.newsletter.example.com
: The public URL as seen by visitors.
-
Create a configuration file for the report worker by opening it in a text editor like nano.
sudo nano ./server/services/workers/reports/config/production.yaml -
Copy the following configuration in the editor, save the file and exit.
log:level: warnmysql:user: mailtrain_userpassword: user_passworddatabase: mailtrain -
Install the required Node packages by running the following command:
for idx in client shared server mvis/client mvis/server mvis/test-embed mvis/ivis-core/client mvis/ivis-core/server mvis/ivis-core/shared mvis/ivis-core/embedding; do(cd $idx && npm install)doneTip:Do not use
sudo
when running the command above.
Building Mailtrain
- Change into the
client
directory from themailtrain
main directory:cd client - Run the following command to build the application:
npm run build
- Set the permissions of the
config
directory:sudo chmod o-rwx /var/www/mailtrain/server/config/ - Create a
systemd
service file for Mailtrain by opening it in a text editor like nano:sudo nano /etc/systemd/system/mailtrain.service - Copy the following configuration into the file, save it and exit the text editor:
[Unit]Description=Mailtrain serverAfter=syslog.target network.target mariadb.service redis-server.service[Service]Environment="NODE_ENV=production"WorkingDirectory=/var/www/mailtrain/serverExecStart=/usr/bin/node index.jsType=simpleRestart=alwaysRestartSec=10[Install]WantedBy=multi-user.target
- Enable and start the Mailtrain service:
sudo systemctl enable --now mailtrain.service
Accessing the Mailtrain web interface
- Open your web browser and type the domain name of your Mailtrain Instance.
- Click Login in the top bar to connect to Mailtrain.
- Login with the username
admin
and passwordtest
. _Do not forget to change the password after your first login._2. Click Login in the top bar to connect to Mailtrain. - Login with the username
admin
and passwordtest
. Do not forget to change the password after your first login.
You have now specified other parameters like your company name, service URL etc. before starting your first e-marketing campaign. For more information on how to plan your campaigns, refer to the official documentation.
Encrypt connections to your Mailtrain, by enabling HTTPS and by using a Let’s Encrypt TLS certificate.