Keep Meteor Running on Amazon Ec2

Keep meteor running on amazon EC2

Install forever and use a start script.

$ npm install -g forever

I have several scripts for managing my production environment - the start script looks something like:

#!/bin/bash

forever stopall

export MAIL_URL=...
export MONGO_URL=...
export MONGO_OPLOG_URL=...
export PORT=3000
export ROOT_URL=...
forever start /home/ubuntu/apps/myapp/bundle/main.js

exit 0

Conveniently, it will also append to a log file in ~/.forever which will show any errors encountered while running your app. You can get the location of the log file and other stats about your app with:

$ forever list

To get your app to start on startup, you'd need to do something appropriate for your flavor of linux. You can maybe just put the start script in /etc/rc.local. For ubuntu see this question.

Also note you really should be bundling your app if using it in production. See this comparison for more details on the differences.

How To Manage Meteor App Running On AWS AMAZON EC2

You need to refer to the "Accessing the database" section in mup docs.

https://github.com/arunoda/meteor-up#accessing-the-database

When logged to your EC2 instance, simply type mongo appName to access the mongo admin prompt.

Then you can use this command to clear the database in a similar way meteor reset would do :

db.dropDatabase();

Meteor: Deploying to Amazon EC2

You need to start your own instance of mongodb. What you are seeing when running your project using the meteor command is just the in-place mongo db that meteor provides you with for development. In production you just start your own mongodb (install it via your linux package manager) and then set your MONGO_URL to that -- you can use the local IP for that.

On Ubuntu on AWS, for instance, if you installed mongodb using apt-get install mongodb, it will run on this URL: MONGO_URL='mongodb://localhost:27017/yourdbname'. If you use a separate AWS instance to run the db then you'll just replace localhost with the IP of that instance.

BTW: you should avoid running anything as root, incl. your bundled app. I'm assuming you are doing that only in order to be able to bind to port 80. A perhaps safer way of doing so is to allow your user to bind to that port also using the following command prior to invoking node:

sudo setcap 'cap_net_bind_service=+ep' /usr/bin/nodejs

Update:
The easiest way to set these environment variables is to just use env:

sudo env PORT=80 MONGO_URL=mongodb://localhost:27017/sidebar ROOT_URL=http://ec2-23-20-113-59.compute-1.amazonaws.com/ node ../bundle/main.js

Update deployed meteor app while running with minimum downtime - best practice

You can't do this without killing the node process, but I haven't found that really matters. What's actually more annoying is the browser refresh on the client, but there isn't much you can do about that.

First, let's assume the application is already running. We start our app via forever with a script like the one in my answer here. I'd show you my whole upgrade script but it contains all kinds of Edthena-specific stuff, so I'll outline the steps we take below:

  1. Build a new bundle. We do this on the server itself, which avoids any missing fibers issues. The bundle file is written to /home/ubuntu/apps/edthena/edthena.tar.gz.

  2. We cd into the /home/ubuntu/apps/edthena directory and rm -rf bundle. That will blow away the files used by the current running process. Because the server is still running in memory it will keep executing. However, this step is problematic if your app regularly does uncached disk operatons like reading from the private directory after startup. We don't, and all of the static assets are served by nginx, so I feel safe in doing this. Alternatively, you can move the old bundle directory to something like bundle.old and it should work.

  3. tar xzf edthena.tar.gz

  4. cd bundle/programs/server && npm install

  5. forever restart /home/ubuntu/apps/edthena/bundle/main.js

There really isn't any downtime with this approach - it just restarts the app in the same way it would if the server threw an exception. Forever also keeps the environment from your original script, so you don't need to specify your environment variables again.

Finally, you can have a look at the log files in your ~/.forever directory. The exact path can be found via forever list.

Error: EACCES: permission denied - running Meteor app (OHIF) on AWS EC2 Instance

I was able to help Craig with that specific problem and it was done as follows:

The problem is meteor runs with root user (not recommended)

To get rid of this problem, stop the app servers, which in this case was tomcat with the command:

sudo systemctl stop tomcat.service

Optional Step: Just do it if you want for security reasons

  • cp -a local/home/ec2-user/meteor_local_copy

Go to your project folder:

  • cd myProject/.meteor/local
  • Warn: Delete only the /local directory (not /.meteor/local):
  • There is no problem deleting, it will be re-created:
    • sudo rm -rf ../myProject/.meteor/local

Now update your meteor:

meteor update

Wait until a message like this appears:

"Installed. Run 'meteor update --release 2.0' inside of a particular..."

In your project directory run:

meteor npm rebuild

Start the aplication server again:

systemctl start tomcat.service

Check if the problem was resolved!

AWS SES with Meteor

To send email in Meteor the easiest thing to do is to set the SMTP server and username information using process.env.MAIL_URL variable. The Accounts and Email packages will automatically use it to access the SMTP server whenever you send a message.

To enable SMTP access with AWS SES:

  1. Log into the AWS Management Console
  2. Select the appropriate region where you set up SES.
  3. Select SES from the list of Application Services
  4. Click the SMTP Settings menu item in the left menu
  5. Click the Create My SMTP Credentials button
  6. Use the default IAM username that's provided. This is a special IAM user that will only have access to the SMTP server. You shouldn't use your main IAM username/password when accessing the SMTP server.
  7. Make sure to note the SMTP server name. You'll need it later.
  8. After the account is created download the credentials.

Now that you have an account set up, simply copy and paste the following line to the top of your Meteor startup file and replace the username and password from the values in the credential file that you just downloaded and the server name that was provided.

process.env.MAIL_URL = 'smtp://username:password@smtp-server-name:465';

Please note that both the sending and receiving email addresses must be verified in the AWS SES management console if you haven't received production access yet. If you don't do this Meteor will throw an error in the console and the message will not be sent.

meteor vs meteor bundle for production

This won't be an exhaustive list, but here are some things that the meteor command does:

  1. creates a local database
  2. watches on every dependent file in your app or in your packages
  3. sends every file separately and unminified to the client (this is super inefficient unless you are developing locally)

In contrast, bundling an app:

  1. does not create a local database
  2. does not spend CPU watching your files for changes
  3. creates two minified files (js and css) which is perfect for putting on a CDN or hosting from a reverse proxy. These are also efficient for clients to download and are highly cacheable.

In general, deploying shouldn't be a huge pain if you use a good set of scripts.

Run Meteor as a Daemon process

Meteor Up creates a daemon with these features:

  • Auto-Restart if the app crashed (using forever)

  • Auto-Start after the server reboot (using upstart)



Related Topics



Leave a reply



Submit