Deploying a Meteor app on an AWS production server
with Passenger in Standalone mode on Ubuntu 18.04 LTS (with APT)
On this page you will learn how you can deploy your app to a server that is running Passenger. You can either follow these instructions with your own app, or you can use the sample Meteor app we prepared.
Table of contents
- Loading...
1 Transferring the Meteor package to the server
1.1 Build package
To deploy your Meteor app to production, we need to create a packaged version of it. A packaged Meteor app contains the Meteor runtime and various other necessary things for running a Meteor app in production.
In order to provide you appropriate instructions, please choose your Meteor version:
Inside your application's code directory, on your local computer, use the meteor bundle
meteor build
command to create a Meteor package tarball.
$ meteor bundle package.tar.gz
$ meteor build --server-only ../new_package && mv ../new_package/*.tar.gz package.tar.gz
Why create a package?
Wondering why we instruct you to create a package, instead of just using the Meteor app in unpackaged form? Learn more at About Meteor support.
"meteor bundle" deprecated?
Meteor will probably tell you that meteor bundle
is deprecated in favor of meteor build
. Please ignore that message, because for the purpose of running a Meteor web application on Passenger, only meteor bundle
does what we want.
meteor bundle
creates a packaged web application, in the form of a Node.js web app with the Meteor runtime included. meteor build
is a more comprehensive tool that not only does what meteor bundle
does, but also builds iOS and Android app packages. However, Passenger is a web application server, so iOS and Android packages are not relevant to us, which is why we recommend using meteor bundle
instead of meteor build
.
1.2 Upload package to the server
Copy the package to your production server, for example using scp
:
local-computer$ scp -i your_ec2_key.pem package.tar.gz adminuser@yourserver.com:
Replace adminuser
with the name of an account with administrator privileges or sudo privileges.
This is usually admin
, ec2-user
, root
or ubuntu
.
1.3 Login to your server, create a user for the app
Login to your server with SSH:
$ ssh -i your_ec2_key.pem adminuser@yourserver.com
Replace adminuser
with the name of an account with administrator privileges or sudo privileges.
This is usually admin
, ec2-user
, root
or ubuntu
.
Now that you have logged in, you should create an operating system user account for your app. For security reasons, it is a good idea to run each app under its own user account, in order to limit the damage that security vulnerabilities in the app can do. Passenger will automatically run your app under this user account as part of its user account sandboxing feature.
You should give the user account the same name as your app. But for demonstration purposes, this tutorial names the user account myappuser
.
$ sudo adduser myappuser
We also ensure that that user has your SSH key installed:
$ sudo mkdir -p ~myappuser/.ssh $ touch $HOME/.ssh/authorized_keys $ sudo sh -c "cat $HOME/.ssh/authorized_keys >> ~myappuser/.ssh/authorized_keys" $ sudo chown -R myappuser: ~myappuser/.ssh $ sudo chmod 700 ~myappuser/.ssh $ sudo sh -c "chmod 600 ~myappuser/.ssh/*"
1.4 Extract package
You need extract the package to a permanent location on the server. A good location is /var/www/APP_NAME
. Let us create that directory.
$ sudo mkdir -p /var/www/myapp
Replace myapp
and myappuser
with your app's name and your app user account's name.
Now let us extract the package:
$ cd /var/www/myapp $ tar xzf ~/package.tar.gz $ chown -R myappuser: .
Your extract app package directory now lives on the server at /var/www/myapp/bundle
.
2 Preparing the app's environment
2.1 Install MongoDB
During development, the Meteor runtime takes care of starting MongoDB for you. MongoDB is the database engine that Meteor uses. But a packaged Meteor app does not start MongoDB for you. Instead, a packaged Meteor app expects that MongoDB is already running somewhere, and that you tell the app where that MongoDB instance is.
You can install MongoDB from www.mongodb.org. While you can also install MongoDB via a package manager (apt-get), please note that this might be an outdated or even unsupported version.
2.2 Login as the app's user
All subsequent instructions must be run under the application's user account. While logged into your server, login under the application's user account as follows:
$ sudo -u myappuser -H bash -l
2.3 Install app dependencies
Your application has various dependencies.
They must be installed. Most of these dependencies are Javascript libraries, managed by npm. You can install them by running npm install
in your app's package directory, under the programs/server
subdirectory:
$ cd /var/www/myapp/bundle/programs/server $ npm install --production
Your app may also depend on services, such as Redis etc. With the exception of MongoDB, installing services that your app depends on is outside of this tutorial's scope.
3 Starting the app in Passenger
Now that you are done with transferring your app's code to the server and setting up an environment for your app, it is time to start your app in Passenger.
3.1 Create a Passenger config file
Since this is a production environment, we need to customize Passenger a little bit. Go to your application's
package directory
and create a file named Passengerfile.json
:
$ cd /var/www/myapp/bundle $ nano Passengerfile.json
Insert:
{ // Tell Passenger that this is a Meteor app. "app_type": "node", "startup_file": "main.js", "envvars": { // Tell your app where MongoDB is "MONGO_URL": "mongodb://localhost:27017/myappdb", // Tell your app what its root URL is "ROOT_URL": "http://yourserver.com", }, // Store log and PID file in parent directory "log_file": "../passenger.log", "pid_file": "../passenger.pid" // Run the app in a production environment. The default value is "development". "environment": "production", // Run Passenger on port 80, the standard HTTP port. "port": 80, // Tell Passenger to daemonize into the background. "daemonize": true, // Tell Passenger to run the app as the given user. Only has effect // if Passenger was started with root privileges. "user": "myappuser" }
Replace myappuser
with your app's user account name.
Replace myappdb
with an appropriate MongoDB database name. Also be sure to set ROOT_URL
to an appropriate value.
3.2 Go back to the admin account
You have previously logged into your app's user account in order to prepare the app's environment. That user does not have sudo access. In the next steps, you need to start Passenger with root privileges, for which sudo access is needed. So you need to switch back to the admin account.
This can be done by simply exiting the shell that was logged into the app's user account. You will then be dropped back to the admin account. For example:
# This is what you previously ran: admin$ sudo -u myappuser -H bash -l myappuser$ ... # Type `exit` to go back to the account you were before myappuser$ exit admin$ _
3.3 Start Passenger Standalone
While in your application's code directory, start Passenger. As configured, it will start on port 80 and will daemonize into the background.
Note that, because Passenger is configured to listen on port 80, this command must be run with root privileges. Only root privileged processes can listen on ports lower than 1024.
$ cd /var/www/myapp/bundle $ sudo passenger start
3.4 Test drive
You should now be able to access your app through the server's host name! Try running this from your local computer. Replace yourserver.com
with your server's hostname.
$ curl http://yourserver.com/ ...your app's front page HTML...
If you do not see your app's front page HTML, then the most likely cause is that you did not setup DNS records. Setting up DNS is outside the scope of this tutorial. In the mean time, we recommend that you use your server's IP address as the server name.
3.5 Make sure Passenger Standalone starts on system boot
Passenger is now running and serving your app, but that only lasts until you reboot your server. So you must configure your server to start Passenger Standalone on system boot.
The easiest way to do that is to add it to the file /etc/rc.local
. This script is called during system boot. Ensure that it's executable and open it in a text editor:
$ sudo chmod +x /etc/rc.local $ sudo nano /etc/rc.local
Here is an example of what you may want to add to /etc/rc.local
. If there is an exit
command in rc.local, make sure you add these before the exit command.
#!/bin/sh # Change working directory to your webapp. cd /var/www/myapp/bundle # Start Passenger Standalone in daemonized mode. Passenger will be started as # root when run from this file, but Passengerfile.json tells it to drop its # privileges to a normal user. passenger start
Next step
Congratulations, you have successfully deployed your app!
Continue: Deploying updates »