Email marketing with MailTrain in Plesk Onyx

So I ran in to a few small issues getting an email marketing solution based on MailTrain running in Plesk Onyx. Nothing show stopping – but I figured I’d archive this here, so no one else has to waste a full day getting it all configured just right. 🙂

Email marketing – A problem in search of an answer

A little background…. I’m not the best at email marketing (or social media…. etc…), but figured I should really get a little more proactive. So the first problem was how to send out a large number of emails without totally destroying my email reputation. Day one was spent researching, looking at some saas services (MailChimp, SendInBlue etc), some WordPress plugins (Newsletter, MailPoet etc) and some paid apps (Sendy). The problem with all these solutions, was that none of them were cheap (most are subscription payment based), and worse some were downright extortionate. So whats a dev to do… get on GitHub of course! A couple of apps were discovered – a Rails app, and a Node.js app. Both were tested, but I settled on the Node.js app: MailTrain.

I fired it up on my dev machine, and had it running in a matter of minutes! so far so good. even better – it had all the features I wanted already – no custom code to write – awesome! and best of all it’s free to use.

Well, whats MailTrain?

MailTrain

shamelessly taken directly form the GitHub page
MailTrain is a self hosted newsletter application built on Node.js (v7+) and MySQL (v5.5+ or MariaDB).

FEATURES
Mailtrain supports subscriber list management, list segmentation, custom fields, email templates, large CSV list import files, etc.”

nice!

Installing on Plesk Onyx

I figured it would be super easy to install on Plesk, just like a rails app. Well, it was, mostly. But there was a couple of small issues I ran into which had me going round in circles for hours…
🙁

Enable Node.js

So ok, first of all we need to enable Node.js on Plesk. super easy:
Open the Tools and Settings tab

the Tools and Settings Panel in Plesk
the Tools and Settings Panel in Plesk

then click on Add/Remove Components

add new components into Plesk
add new components into Plesk

Scroll down and expand the Web Hosting menu, then enable NodeJS support

enabling Node.js support in Plesk
enabling Node.js support in Plesk

Crete a mysql database

Go back to the Websites & Domains tab and select your domain where you want to install MailTrain.
Click Add Database

adding a new database for MailTrain
adding a new database for MailTrain

Create a new database user

creating a new database in Plesk
creating a new database in Plesk

take note of the Database name, user and password – we’ll need these for configuring MailTrain.

Configure MailTrain

We need to give access to the database, and this is super easy.
If you haven’t done it yet, download MailTrain from GitHub
unarchive the zip file, and navigate to the config folder
duplicate the default.toml file and rename the new file production.toml
open the newly created production.toml file and replace the contents with this:

# production.toml
[mysql]
host=”127.0.0.1″
user=”your_username”
password=”your_password”
database=”your_database_name”

Ok, so normally this would be all we’d need to do. But I ran into an install issue, and had to add a custom script to to the package.json file.

Open up the file: package.json
add these two lines to the end of the scripts array:
“installposix”: “npm install posix”,
“stop”: “pkill –signal SIGINT mailtrain”

The installposix script is the one we need to get MailTrain to install correctly in Plesk Onyx. And the stop script is just a convenience script.

So your package.json will look something like this:

editing the package.json file
editing the package.json file

Cool, now we can upload MailTrain to our server.

Configure Node.js app in Plesk

click on the Node.js icon under our domain, and update the variables to this:
Document Root /”your_domain”/mailtrain/public
Application Mode production
Application URL http://”your_domain”
Application Root /”your_domain”/mailtrain

configuring the Node.js settings
configuring the Node.js settings

We have already enabled Node.js support globally, but now we need to enable Node.js on the domain where we want MailTrain running. This is super easy (like everything in Plesk Onyx). just click the Enable Node.js button

Install all the required dependencies for MailTrain

Normally we would just hit the NPM Install button, and our app would be running! but this is where those custom script we added to the package.json file come in handy.
Click the Run Script button, type installposix in the field, and hit Run. For some reason we need to install this manually or else the build fails. once it completes dismiss the script panel and hit the NPM Install button.

We’re almost, but not quite there, and this step tripped me up for hours…

One last thing

Well actually two things….
drop back out of the Node.js settings to your domain name, and open up the Apache & nginx Settings panel. Add the following code in Both the HTTP, and HTTPS fields:

ProxyPass http://”your_server_IP_address”:3000/
ProxyPassReverse http://”your_server_IP_address”:3000/

hit Apply

settings custom Apache Directives
settings custom Apache Directives

Head back to the Node.js panel one last time. Open the Run Script panel, enter start, then hit Run. then dismiss the panel

running custom scripts in Plesk for our custom Email marketing setup
running custom scripts in Plesk for our custom Email marketing setup

Success – affordable and reliable email marketing

MailTrain should now be running on your domain!
you can get it configured to send out your newsletter by following the tutorials in the Wiki.

Rails – access a subdomain app via the root domain in Plesk Onyx

I ran into just this problem the other day. Migrating my Rails store from a CPanel VPS over to the current Plesk Onyx VPS proved to be a little tricky. It took me a while to figure out a solution. So I figured I’d write a blog outlining the steps I took to get my desired result.

The problem

For the sake of simplicity, I like to host my Store as a subfolder of my root domain. ie… sweetpproductions.com/store. In Cpanel, this was pretty easy to set up. Just add a symlink from sweetpproductions/store to the Rails app directory. Soon I found out, this is not so simple in Plesk. Trying to get symlinks working lead me on a path of frustration ultimately leading to a dead end.

Rails on Plesk

Plesk Onyx takes the pain out of setting up Rails apps. It was in fact almost too easy, and I wasn’t sure if I was missing something. In contrast, setting up my Rails app in CPanel involved delving into manually installing various components via the command line. This ease of use however comes at a price. I quickly discovered that I simply could not get the Rails app installed into a subfolder, and still have the root domain accessible. ie.. sweetpproductions.com. So after many attempts and duckduckgo searches, I decided that my best bet was to…

Run the app on a subdomain!

Simple enough, right. Installing Rails in the root of a domain(or subdomain) is a breeze in Plesk. Just take a look at the tutorial over here. OK nice, but now I need to set up some kind of rewrite so that my app is accessed from sweetpproductions.com/store, and NOT store.sweetpproductions.com.

Rails installation on subdomain in Plesk Onyx
Rails installation on subdomain in Plesk Onyx

Apache Alias to the rescue

Let’s add an Alias on the root domain to the subdomain. This will map the requests for sweetpproductions.com/store to store.sweetpproductions.com.
We do this from the Apache & nginx Settings tab.
Scroll to the bottom of the page, and paste the following code into both Additional directives for HTTP and Additional directives for HTTPS

Alias /store /var/www/vhosts/sweetpproductions.com/store.sweetpproductions.com/rails_store/public

So far so good, now we have the site structure we want. But the rails app just won’t load 🙁 We still need to configure a few more things to get it working correctly.

Configure Ruby on the root domain!

This may seem counter intuitive, but this is what we need to do to get our setup working. We won’t actually install the app on the root domain, but we do need a Ruby instance running. Let’s configure Ruby with the same settings as the Ruby install on the subdomain. Take care to make sure that the Ruby version and application mode are identical on the subdomain and root domain.

Rails installation on root domain in Plesk Onyx
Rails installation on root domain in Plesk Onyx

Now we need to grab some information from the system to enable our app to run correctly. To do this, we need to…

Enable SSH access

This is super easy on Plesk. In the root domain manager, click on FTP Access.
Then on the FTP Accounts tab – add a new user (if you dont have one already) and enable Access to the server over SSH via bin/sh. Now let’s log into the server and get the info we need. We need to configure Ruby on the root domain to load our subdomain hosted rails app with the correct Ruby and Passenger installs. First of all let’s get the location of our Passenger install with the following command:
which passenger-config
taking note of the result:
/usr/bin/passenger-config

We use the output from this command, to find our Ruby location:
/usr/bin/passenger-config –ruby-command
passenger-config was invoked through the following Ruby interpreter:
Command: /opt/plesk/ruby/2.3.1/bin/ruby
Version: ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
To use in Apache: PassengerRuby /opt/plesk/ruby/2.3.1/bin/ruby
To use in Nginx : passenger_ruby /opt/plesk/ruby/2.3.1/bin/ruby
To use with Standalone: /opt/plesk/ruby/2.3.1/bin/ruby /usr/bin/passenger start
 
The following Ruby interpreter was found first in $PATH:
Command: /var/www/vhosts/sweetpproductions.com/.rbenv/versions/2.3.1/bin/ruby
Version: ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
To use in Apache: PassengerRuby /var/www/vhosts/sweetpproductions.com/.rbenv/versions/2.3.1/bin/ruby
To use in Nginx : passenger_ruby /var/www/vhosts/sweetpproductions.com/.rbenv/versions/2.3.1/bin/ruby
To use with Standalone: /var/www/vhosts/sweetpproductions.com/.rbenv/versions/2.3.1/bin/ruby /usr/bin/passenger start

Configuring Ruby and Passenger

From these two commands, we have now have all the info we need to build our Apache rules.

  • The result from our first command is our PassengerRoot variable.
  • PassengerRuby is found in the output from the second command.
  • In our case we want our store loading at sweetpproductions.com/store, so both PassengerBaseURI and RailsBaseURI will be /store
  • PassengerAppRoot is the root location of our app
  • Directory is the location of the public folder in our app

So now lets re-edit our Apache rules on the root domain to get the rails app working.
Alias /store /var/www/vhosts/mrqwirk.com/store.mrqwirk.com/rails_store/public
PassengerRuby /opt/plesk/ruby/2.3.1/bin/ruby
PassengerRoot /usr/share/passenger/phusion_passenger/locations.ini
RailsEnv development
PassengerEnabled on
<Location /store >
PassengerBaseURI /store
PassengerAppRoot /var/www/vhosts/mrqwirk.com/store.mrqwirk.com/rails_store
</Location>
<Directory /var/www/vhosts/mrqwirk.com/store.mrqwirk.com/rails_store/public>
RailsBaseURI /store
AllowOverride all
Options -MultiViews
</Directory>

Apache & nginx Settings on Plesk Onyx for root domain
Apache & nginx Settings on Plesk Onyx for root domain

Success! now our store is loading just fine when we navigate to sweetpproductions.com/store 🙂
We have just one final touch for completion, we need to…

Redirect requests from the subdomain to the root domain

To do this we open up the subdomain in Plesk and add the following code into Additional directives for HTTP and Additional directives for HTTPS on the Apache & nginx Settings tab.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.sweetpproductions\.com
RewriteRule ^(.*)$ http://sweetpproductions.com/%1 [L,NC,QSA]

Edited 17 Oct 2017
Using the previous rewrite rule created an issue with updating the Let’s Encrypt SSL/TLS Certificate. The new rewrite rule below, adds an exception for the .well-known/acme-challenge/ folder, so that Let’s Encrypt can correctly validate:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.sweetpproductions\.com
RewriteCond %{REQUEST_FILENAME} !\/\.well-known\/acme-challenge\/
RewriteRule ^(.*)$ http://sweetpproductions.com/%1 [L,NC,QSA]

Apache & nginx Settings on Plesk Onyx for subdomain redirect to root folder
Apache & nginx Settings on Plesk Onyx for subdomain redirect to root folder

Done!

Now when anyone visit’s our site at sweetpproductions.com/store they will be shown our rails store. And also if anyone tries to visit store.sweetpproductions.com they will be redirected to our preferred loaction at sweetpproductions.com/store.

Posted originally posted Aug 19, 2017 – updated Oct 17, 2017