Meteor is constantly evolving, and so are the ways to host it. I couldn’t find any single source that explained how to deploy a Meteor site to Webfaction using the latest technology (ie. as of March 2014). So here is how I did it for my site, Signup Zone.
First, follow Webfaction’s instructions to install a MongoDB app and add a
data directory. I have called the app
mongodb1. Note down its port number, eg. 18000.
The instructions say to add a user with the
["userAdminAnyDatabase"] role. I have also added a second user with the roles
['clusterAdmin', 'userAdminAnyDatabase', 'readAnyDatabase'], based on this Stackoverflow answer.
The instructions then say to run this in an SSH window:
./mongodb-linux-x86_64-2.4.9/bin/mongod --auth --dbpath $HOME/webapps/mongodb1/data/ --port 18000
Use Webfaction’s one-click installer to install a node.js app, and note down its port number too, eg. 17000. Upgrade it to the latest stable version using the “n” package, following Webfaction’s instructions for node.
As mentioned in the Webfaction instructions, in the
nodeapp directory, type
export PATH=$PWD/bin/:$PATH. Then
cd demeteoredapp and type
npm install in that directory.
In another SSH shell, enter (the DB name is “admin”):
export MONGO_URL="mongodb://mongo_user:mongo_password@localhost:18000/admin?autoReconnect=true" # 2 export MAIL_URL='smtp://user:firstname.lastname@example.org' export PORT="17000" export ROOT_URL="http://example.com"
You can adjust the default
bin/start script to do this too.
Having installed the app this way, I have developed a process to update the code. I rename the previously demeteorized app directory, eg. to
old; go into my Meteor app directory and type:
demeteorize -o ../demeteoredapp, then
mv ../old/.git ../demeteoredapp. At that point I can do
git add --all and commit and push the new version up to my git repo on Webfaction. I then
ssh onto the server and type:
cd webapps/nodeapp/demeteoredapp/appname git pull; ../bin/stop ; ../bin/start
to pull in the new changes and launch them.
Note there is no need for the
forever package when you do it this way. I’m not sure we need the
demeteorizer package either, or if the Meteor bundler is sufficient.
I found some comments online that there can be memory problems if you use Meteor and MongoDB on Webfaction. I haven’t experienced this problem. (I’m keeping a close eye on it – I have even built an admin panel which includes a call to a Meteor method that executes the
memory_usage.py script provided by Webfaction.)
You will then want to set up cron jobs to start your MongoDB app if it fails for any reason. I have modelled this script off Webfaction’s
start script for node.js:
#!/bin/sh mkdir -p $HOME/webapps/mongoapp/run mkdir -p $HOME/webapps/mongoapp/log pid=$(/sbin/pidof $HOME/webapps/mongoapp/mongodb-linux-x86_64-2.4.9/bin/mongod) if echo "$pid" | grep -q " "; then pid="" fi if [ -n "$pid" ]; then user=$(ps -p $pid -o user | tail -n 1) if [ $user = "your-username" ]; then exit 0 fi fi nohup $HOME/webapps/mongoapp/mongodb-linux-x86_64-2.4.9/bin/mongod --auth --fork --logpath $HOME/webapps/mongoapp/log/mongo.log --dbpath $HOME/webapps/mongoapp/data/ --port 18000 > /dev/null 2>&1 & /sbin/pidof $HOME/webapps/mongoapp/mongodb-linux-x86_64-2.4.9/bin/mongod > $HOME/webapps/mongoapp/run/mongo.pid
Put this into your scheduled tasks using
You will also want to back up your database. A simple command to back up the database to a directory called
$HOME/webapps/mongoapp/mongodb-linux-x86_64-2.4.9/bin/mongodump --port 18000 -o $HOME/dbbackup
You will need to add the admin username and password.
I like to have daily, weekly and monthly backups, so I make three directories
monthly and have a script for each:
The daily script is:
#!/bin/sh # $HOME/scripts/cron_daily_mongo_backup.sh # add this to crontab, e.g. to run at 15:45 server time every day: # 45 15 * * * ~/scripts/cron_daily_mongo_backup.sh # back up the database $HOME/webapps/mongoapp/mongodb-linux-x86_64-2.4.9/bin/mongodump --port 18000 -o $HOME/mongo_backup/daily/dump-`date +\%Y\%m\%d` 2>> $HOME/mongo_backup/daily/cron.log # delete backup directories older than 7 days find $HOME/mongo_backup/daily -type d -ctime +7 | xargs -r rm -rf # Be careful using -rf
The weekly script is:
#!/bin/sh # add this to crontab as, e.g. to run at 16:20 server time every Sunday: # 20 16 * * 0 ~/scripts/cron_weekly_mongo_backup.sh cp -R $HOME/mongo_backup/daily/dump-`date +\%Y\%m\%d` $HOME/mongo_backup/weekly find $HOME/mongo_backup/weekly -type d -ctime +32 | xargs -r rm -rf
And the monthly script is:
#!/bin/sh # add this to crontab as, e.g. to run at 16:25 server time every 1st of the month: # 25 16 1 * * ~/scripts/cron_monthly_mongo_backup.sh # cp -R $HOME/mongo_backup/daily/dump-`date +\%Y\%m\%d` $HOME/mongo_backup/monthly find $HOME/mongo_backup/monthly -type d -ctime +370 | xargs -r rm -rf
If you’ve read this far, please check out the finished product at signup.zone!