Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Meteor: Full-Stack Web Application Development

You're reading from   Meteor: Full-Stack Web Application Development Rapidly build web apps with Meteor

Arrow left icon
Product type Course
Published in Nov 2016
Publisher Packt
ISBN-13 9781787287754
Length 685 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Fabian Vogelsteller Fabian Vogelsteller
Author Profile Icon Fabian Vogelsteller
Fabian Vogelsteller
Marcelo Reyna Marcelo Reyna
Author Profile Icon Marcelo Reyna
Marcelo Reyna
Arrow right icon
View More author details
Toc

Chapter 10. Deploying Our App

Our app is now ready to be deployed. In this chapter, we will see how we can deploy our app on different servers to make it public and show the world what we built.

Meteor makes it easy to deploy applications on its own server infrastructure. It's free and quick to do, but probably not the right place for a production environment. Therefore, we will take a look at manual deployment as well as some great tools built to deploy on any Node.js server.

In this chapter, we will cover the following topics:

Deploying on meteor.com

Meteor provides its own hosting environment, where everybody can deploy apps with a single command, for free. In order to deploy apps, Meteor creates a developer account for us so that we can manage and deploy our apps later. To start, let's perform the following steps to deploy our app on meteor.com:

  1. Deploying on a subdomain of meteor.com is as simple as running the following command in the terminal from our app's folder:
    $ meteor deploy myCoolNewBlog
    

    We can freely choose the subdomain we want to deploy on. If myCoolNewBlog.meteor.com is already taken, Meteor will ask us to log in to the owner's account to overwrite the currently deployed app, or we will have to choose another name.

  2. If the domain name is free, Meteor will ask us to provide an e-mail address so that it can create a developer account for us. After entering the e-mail address, we will receive an e-mail with a link to set up our Meteor Developer account, as shown in the following screenshot:
    Deploying on meteor.com
  3. To create our account, we need to follow the link given by Meteor so that we can fully set up our account by adding a username and a password, as shown in the next screenshot:
    Deploying on meteor.com
  4. After we have done that, we get access to our developer account's page, where we can add e-mail addresses, check our last login, and authorize other Meteor developers to log in to our apps (though we have to add the accounts-meteor-developer package first).
  5. Now, to finally deploy our app, we need to log in with our Meteor Developer account in the terminal by using $ meteor login, entering our credentials, and running the deploy command again:
    $ meteor deploy myCoolNewBlog
    
  6. Using the $ meteor authorized –add <username> command, we can allow other Meteor developers to deploy to our app's subdomain, as shown in the following screenshot:
    Deploying on meteor.com
  7. If we want to update our deployed app, we can simply run $ meteor deploy from inside our app's folder. Meteor will ask us for our credentials, and we can then deploy our app.

If we're on a friend's computer and want to use our Meteor account, we can do so using $ meteor login. Meteor will keep us logged in and everybody can redeploy any of our apps. We need to make sure we use $ meteor logout when we're finished.

Deploying on meteor.com using a domain name

We can also host our app on meteor.com, but can define our own domain name.

To do this, we simply deploy using our domain name, as follows:

$ meteor deploy mydomain.com

This will host the app on meteor.com, but with no direct URL such as myapp.meteor.com.

To point our domain to the app on the Meteor servers, we need to change the A record of our domain to the IP address of origin.meteor.com (which is 107.22.210.133 at the time of writing this book), or the CNAME record to origin.meteor.com. You can do this at the provider where you registered your domain under the DNS configuration.

Meteor will then get a request from our domain and redirect internally to the server where our app is located.

Backup and restore databases hosted on meteor.com

If you ever need to back up your database or move it to another server, you can get temporary Mongo database credentials for the deployed database using the following command:

$ meteor mongo myapp.meteor.com –url

This will get something like the following credentials:

mongodb://client-ID:xyz@production-db-b1.meteor.io:27017/yourapp_meteor_com

You can then use the credentials from the preceding output to back up your database using mongodump:

$ mongodump -h production-db-b1.meteor.io --port 27017 --username client-ID --password xyz --db yourapp_meteor_com

This will create a folder named dump/yourapp_meteor_com where you are and put the dump files of the database inside.

To restore it to another server, use mongorestore, with the last argument being the folder where you put the database dump:

$ mongorestore -h mymongoserver.com --port 27017 --username myuser --password xyz --db my_new_database dump/yourapp_meteor_com

If you simply want to put the data into your local Meteor app's database, start the Meteor server using $ meteor and run the following command:

$ mongorestore --port 3001

Deploying on other servers

Meteor's free hosting is great, but when it comes to using an app in production, we want to be in control of the server we're using.

Meteor allows us to create a bundle of our application so that we can deploy it on any Node.js server. The only downside to this is that we need to install certain dependencies ourselves. Additionally, there are two packages out there that make deploying apps almost as simple as Meteor itself, though their configuration is still needed.

Bundling our app

In order to deploy our app on our own server, we need to have a Linux server with the latest version of Node.js and NPM installed. The server should have the same platform as our local machine on which we will create the bundle. If you want to deploy your app on another platform, take a look at the next section. Now let's build the app by performing the following steps:

  1. If our server fits the aforementioned requirements, we can go to our app's folder on our local machine and run the following command:
    $ meteor build myAppBuildFolder
    
  2. This will create myAppBuildFolder with a *.tar.gz file inside. We can then upload this file to our server and extract it under ~/Sites/myApp for example. Then we go to the extracted folder and run the following commands:
    $ cd programs/server
    $ npm install
    
  3. This will install all the NPM dependencies. After they're installed, we set the necessary environment variables:
    $ export MONGO_URL='mongodb://user:password@host:port/databasename'
    $ export ROOT_URL='http://example.com'
    $ export MAIL_URL='smtp://user:password@mailhost:port/'
    $ export PORT=8080
    

    The export commands will set the MONGO_URL, ROOT_URL, and MAIL_URL environment variables.

  4. As this manual deployment doesn't come with preinstalled MongoDB, we need to either install it on our machine or use a hosted service such as Compose (http://mongohq.com). If we rather want to install MongoDB on our server ourselves, we can follow the guide at http://docs.mongodb.org/manual/installation.
  5. The ROOT_URL variable should be the URL of the domain pointing to our server. If our app sends e-mails, we can additionally set our own SMTP server or use a service such as Mailgun (http://mailgun.com) and change the SMTP host in the MAIL_URL variable.

    We can also specify the port on which we want our app to run using the PORT environment variable. If we don't set the PORT variable, it will use port 80 by default.

  6. After we set these variables, we go to the root folder of our app and start the server using the following command:
    $ node main.js
    

    Tip

    If you want to make sure your application is restarted in case it crashes or when the server is rebooted, take a look at the forever NPM package, which is explained at https://github.com/nodejitsu/forever.

If everything goes fine, our app should be reachable at <your server's ip>:8080.

In case we run into trouble by manually deploying our app, we can use the next approach.

Deploying using Demeteorizer

The disadvantage of using $ meteor build is that most node modules are already compiled, and therefore can cause problems in the server's environment. Hence comes Demeteorizer, which is very similar to $ meteor build but will additionally unpack the bundle and create a package.json file with all the node dependencies and the correct node version for the project. Here is how we deploy using Demeteorizer:

  1. Demeteorizer comes as an NPM package, which we can install using the following command:
    $ npm install -g demeteorizer
    

    Note

    If the npm folder doesn't have the right permissions, use sudo before the command.

  2. Now we can go to our app's folder and type the following command:
    $ demeteorizer -o ~/my-meteor-blog-converted
    
  3. This will output the ready-to-distribute app to the my-meteor-blog-converted folder. We just copy this folder to our server, set the same environment variables as described earlier, and run the following command:
    $ cd /my/server/my-meteor-blog-converted
    $ npm install
    $ node main.js
    

This should start our app on the port we specified.

Deploying using Meteor Up

The previous steps help us to deploy our app on our own server, but this method still requires us to build, upload, and set the environment variables.

Meteor Up (mup) aims to make deploying as easy as running $ meteor deploy. However, if we want to use Meteor Up, we need to have full admin rights on the server.

Additionally, this allows us to auto-restart the app in case it crashes, using the forever NPM package, as well as start the app when the server reboots, using the upstart NPM package. We can also revert to the previously deployed version, which gives us a good basis for deployment on the production environment.

Note

The next steps are for more advanced developers, as they require setting up sudo rights on the server machine. Therefore, if you're inexperienced in deployment, consider using a service such as Modulus (http://modulus.io), which offers online Meteor deployment using its own command-line tool, available at https://modulus.io/codex/meteor_apps.

Meteor Up will set up the server and deploy our app as follows:

  1. To install mup on our local machine, we type the following command:
    $ npm install -g mup
    
  2. Now we need to create a folder for our deployment configuration, which could be in the same folder where our app is located:
    $ mkdir ~/my-meteor-blog-deployment
    $ cd ~/my-meteor-blog-deployment
    $ mup init
    
  3. Meteor Up creates a configuration file for us, which will look like the following:
    {
      "servers": [
        {
          "host": "hostname",
          "username": "root",
          "password": "password"
          // or pem file (ssh based authentication)
          //"pem": "~/.ssh/id_rsa"
        }
      ],
      "setupMongo": true,
      "setupNode": true,
      "nodeVersion": "0.10.26",
      "setupPhantom": true,
      "appName": "meteor",
      "app": "/Users/arunoda/Meteor/my-app",
      "env": {
        "PORT": 80,
        "ROOT_URL": "http://myapp.com",
        "MONGO_URL": "mongodb://arunoda:fd8dsjsfh7@hanso.mongohq.com:10023/MyApp",
        "MAIL_URL": "smtp://postmaster%40myapp.mailgun.org:adj87sjhd7s@smtp.mailgun.org:587/"
      },
      "deployCheckWaitTime": 15
    }
  4. Now we can edit this file to work for our server environment.
  5. First, we will add the SSH server authentication. We can provide either our RSA key file or a username and a password. If we want to use the latter, we need to install sshpass, a tool used to provide SSH passwords without using the command line:
    "servers": [
        {
          "host": "myServer.com",
          "username": "johndoe",
          "password": "xyz"
          // or pem file (ssh based authentication)
          //"pem": "~/.ssh/id_rsa"
        }
    ],

    Note

    To install sshpass for our environment, we can follow the steps at https://gist.github.com/arunoda/7790979, or if you're on Mac OS X, take a look at http://www.hashbangcode.com/blog/installing-sshpass-osx-mavericks.

  6. Next, we can set some options, such as choosing to install MongoDB on the server. If we use a service such as Compose, we will set it to false:
    "setupMongo": false,

    If we already have Node.js installed on our server, we will also set the next option to false:

    "setupNode": false,

    If we want to mention a specific Node.js version, we can set it as follows:

    "nodeVersion": "0.10.25",

    Meteor Up can also install PhantomJS for us, which is necessary if we use Meteor's spiderable package, which makes our app crawlable by search engines:

    "setupPhantom": true,

    In the next option, we will set the name of our app, which can be the same as our app's folder name:

    "appName": "my-meteor-blog",

    Finally, we point to our local app folder so that Meteor Up knows what to deploy:

    "app": "~/my-meteor-blog",
  7. Meteor Up also lets us preset all the necessary environment variables, such as the correct MONGO_URL variable:
    "env": {
        "ROOT_URL": "http://myServer.com",
        "MONGO_URL": "mongodb://user:password@host:port/databasename",
        "PORT": 8080
    },
  8. The last option sets the time Meteor Up will wait for before checking whether the app started successfully:
    "deployCheckWaitTime": 15

Setting up the server

In order to set up the server using Meteor Up, we need a no-password access to sudo. Perform the following steps to set up the server:

  1. To enable no-password access, we need to add our current user to the server's sudo group:
    $ sudo adduser <username> sudo
    
  2. Then add NOPASSWD to the sudoers file:
    $ sudo visudo
    
  3. Now replace the %sudo ALL=(ALL) ALL line with the following line:
    %sudo ALL=(ALL) NOPASSWD:ALL
    

Deploying with mup

If everything has worked fine, we can set up our server. The following steps explain how we can deploy with mup:

  1. Run the following command from inside the local my-meteor-blog-deployment folder:
    $ mup setup
    

    This will configure our server and install all requirements chosen in our configuration file.

    Once this process is done, we can deploy our app any time by running the following command from the same folder:

    $ mup deploy
    

This way, we can also create production and staging environments by creating two separate Meteor Up configurations with two distinct app names, and deploy it to the same server.

Bundling our app

In order to deploy our app on our own server, we need to have a Linux server with the latest version of Node.js and NPM installed. The server should have the same platform as our local machine on which we will create the bundle. If you want to deploy your app on another platform, take a look at the next section. Now let's build the app by performing the following steps:

  1. If our server fits the aforementioned requirements, we can go to our app's folder on our local machine and run the following command:
    $ meteor build myAppBuildFolder
    
  2. This will create myAppBuildFolder with a *.tar.gz file inside. We can then upload this file to our server and extract it under ~/Sites/myApp for example. Then we go to the extracted folder and run the following commands:
    $ cd programs/server
    $ npm install
    
  3. This will install all the NPM dependencies. After they're installed, we set the necessary environment variables:
    $ export MONGO_URL='mongodb://user:password@host:port/databasename'
    $ export ROOT_URL='http://example.com'
    $ export MAIL_URL='smtp://user:password@mailhost:port/'
    $ export PORT=8080
    

    The export commands will set the MONGO_URL, ROOT_URL, and MAIL_URL environment variables.

  4. As this manual deployment doesn't come with preinstalled MongoDB, we need to either install it on our machine or use a hosted service such as Compose (http://mongohq.com). If we rather want to install MongoDB on our server ourselves, we can follow the guide at http://docs.mongodb.org/manual/installation.
  5. The ROOT_URL variable should be the URL of the domain pointing to our server. If our app sends e-mails, we can additionally set our own SMTP server or use a service such as Mailgun (http://mailgun.com) and change the SMTP host in the MAIL_URL variable.

    We can also specify the port on which we want our app to run using the PORT environment variable. If we don't set the PORT variable, it will use port 80 by default.

  6. After we set these variables, we go to the root folder of our app and start the server using the following command:
    $ node main.js
    

    Tip

    If you want to make sure your application is restarted in case it crashes or when the server is rebooted, take a look at the forever NPM package, which is explained at https://github.com/nodejitsu/forever.

If everything goes fine, our app should be reachable at <your server's ip>:8080.

In case we run into trouble by manually deploying our app, we can use the next approach.

Deploying using Demeteorizer

The disadvantage of using $ meteor build is that most node modules are already compiled, and therefore can cause problems in the server's environment. Hence comes Demeteorizer, which is very similar to $ meteor build but will additionally unpack the bundle and create a package.json file with all the node dependencies and the correct node version for the project. Here is how we deploy using Demeteorizer:

  1. Demeteorizer comes as an NPM package, which we can install using the following command:
    $ npm install -g demeteorizer
    

    Note

    If the npm folder doesn't have the right permissions, use sudo before the command.

  2. Now we can go to our app's folder and type the following command:
    $ demeteorizer -o ~/my-meteor-blog-converted
    
  3. This will output the ready-to-distribute app to the my-meteor-blog-converted folder. We just copy this folder to our server, set the same environment variables as described earlier, and run the following command:
    $ cd /my/server/my-meteor-blog-converted
    $ npm install
    $ node main.js
    

This should start our app on the port we specified.

Deploying using Meteor Up

The previous steps help us to deploy our app on our own server, but this method still requires us to build, upload, and set the environment variables.

Meteor Up (mup) aims to make deploying as easy as running $ meteor deploy. However, if we want to use Meteor Up, we need to have full admin rights on the server.

Additionally, this allows us to auto-restart the app in case it crashes, using the forever NPM package, as well as start the app when the server reboots, using the upstart NPM package. We can also revert to the previously deployed version, which gives us a good basis for deployment on the production environment.

Note

The next steps are for more advanced developers, as they require setting up sudo rights on the server machine. Therefore, if you're inexperienced in deployment, consider using a service such as Modulus (http://modulus.io), which offers online Meteor deployment using its own command-line tool, available at https://modulus.io/codex/meteor_apps.

Meteor Up will set up the server and deploy our app as follows:

  1. To install mup on our local machine, we type the following command:
    $ npm install -g mup
    
  2. Now we need to create a folder for our deployment configuration, which could be in the same folder where our app is located:
    $ mkdir ~/my-meteor-blog-deployment
    $ cd ~/my-meteor-blog-deployment
    $ mup init
    
  3. Meteor Up creates a configuration file for us, which will look like the following:
    {
      "servers": [
        {
          "host": "hostname",
          "username": "root",
          "password": "password"
          // or pem file (ssh based authentication)
          //"pem": "~/.ssh/id_rsa"
        }
      ],
      "setupMongo": true,
      "setupNode": true,
      "nodeVersion": "0.10.26",
      "setupPhantom": true,
      "appName": "meteor",
      "app": "/Users/arunoda/Meteor/my-app",
      "env": {
        "PORT": 80,
        "ROOT_URL": "http://myapp.com",
        "MONGO_URL": "mongodb://arunoda:fd8dsjsfh7@hanso.mongohq.com:10023/MyApp",
        "MAIL_URL": "smtp://postmaster%40myapp.mailgun.org:adj87sjhd7s@smtp.mailgun.org:587/"
      },
      "deployCheckWaitTime": 15
    }
  4. Now we can edit this file to work for our server environment.
  5. First, we will add the SSH server authentication. We can provide either our RSA key file or a username and a password. If we want to use the latter, we need to install sshpass, a tool used to provide SSH passwords without using the command line:
    "servers": [
        {
          "host": "myServer.com",
          "username": "johndoe",
          "password": "xyz"
          // or pem file (ssh based authentication)
          //"pem": "~/.ssh/id_rsa"
        }
    ],

    Note

    To install sshpass for our environment, we can follow the steps at https://gist.github.com/arunoda/7790979, or if you're on Mac OS X, take a look at http://www.hashbangcode.com/blog/installing-sshpass-osx-mavericks.

  6. Next, we can set some options, such as choosing to install MongoDB on the server. If we use a service such as Compose, we will set it to false:
    "setupMongo": false,

    If we already have Node.js installed on our server, we will also set the next option to false:

    "setupNode": false,

    If we want to mention a specific Node.js version, we can set it as follows:

    "nodeVersion": "0.10.25",

    Meteor Up can also install PhantomJS for us, which is necessary if we use Meteor's spiderable package, which makes our app crawlable by search engines:

    "setupPhantom": true,

    In the next option, we will set the name of our app, which can be the same as our app's folder name:

    "appName": "my-meteor-blog",

    Finally, we point to our local app folder so that Meteor Up knows what to deploy:

    "app": "~/my-meteor-blog",
  7. Meteor Up also lets us preset all the necessary environment variables, such as the correct MONGO_URL variable:
    "env": {
        "ROOT_URL": "http://myServer.com",
        "MONGO_URL": "mongodb://user:password@host:port/databasename",
        "PORT": 8080
    },
  8. The last option sets the time Meteor Up will wait for before checking whether the app started successfully:
    "deployCheckWaitTime": 15

Setting up the server

In order to set up the server using Meteor Up, we need a no-password access to sudo. Perform the following steps to set up the server:

  1. To enable no-password access, we need to add our current user to the server's sudo group:
    $ sudo adduser <username> sudo
    
  2. Then add NOPASSWD to the sudoers file:
    $ sudo visudo
    
  3. Now replace the %sudo ALL=(ALL) ALL line with the following line:
    %sudo ALL=(ALL) NOPASSWD:ALL
    

Deploying with mup

If everything has worked fine, we can set up our server. The following steps explain how we can deploy with mup:

  1. Run the following command from inside the local my-meteor-blog-deployment folder:
    $ mup setup
    

    This will configure our server and install all requirements chosen in our configuration file.

    Once this process is done, we can deploy our app any time by running the following command from the same folder:

    $ mup deploy
    

This way, we can also create production and staging environments by creating two separate Meteor Up configurations with two distinct app names, and deploy it to the same server.

Deploying using Demeteorizer

The disadvantage of using $ meteor build is that most node modules are already compiled, and therefore can cause problems in the server's environment. Hence comes Demeteorizer, which is very similar to $ meteor build but will additionally unpack the bundle and create a package.json file with all the node dependencies and the correct node version for the project. Here is how we deploy using Demeteorizer:

  1. Demeteorizer comes as an NPM package, which we can install using the following command:
    $ npm install -g demeteorizer
    

    Note

    If the npm folder doesn't have the right permissions, use sudo before the command.

  2. Now we can go to our app's folder and type the following command:
    $ demeteorizer -o ~/my-meteor-blog-converted
    
  3. This will output the ready-to-distribute app to the my-meteor-blog-converted folder. We just copy this folder to our server, set the same environment variables as described earlier, and run the following command:
    $ cd /my/server/my-meteor-blog-converted
    $ npm install
    $ node main.js
    

This should start our app on the port we specified.

Deploying using Meteor Up

The previous steps help us to deploy our app on our own server, but this method still requires us to build, upload, and set the environment variables.

Meteor Up (mup) aims to make deploying as easy as running $ meteor deploy. However, if we want to use Meteor Up, we need to have full admin rights on the server.

Additionally, this allows us to auto-restart the app in case it crashes, using the forever NPM package, as well as start the app when the server reboots, using the upstart NPM package. We can also revert to the previously deployed version, which gives us a good basis for deployment on the production environment.

Note

The next steps are for more advanced developers, as they require setting up sudo rights on the server machine. Therefore, if you're inexperienced in deployment, consider using a service such as Modulus (http://modulus.io), which offers online Meteor deployment using its own command-line tool, available at https://modulus.io/codex/meteor_apps.

Meteor Up will set up the server and deploy our app as follows:

  1. To install mup on our local machine, we type the following command:
    $ npm install -g mup
    
  2. Now we need to create a folder for our deployment configuration, which could be in the same folder where our app is located:
    $ mkdir ~/my-meteor-blog-deployment
    $ cd ~/my-meteor-blog-deployment
    $ mup init
    
  3. Meteor Up creates a configuration file for us, which will look like the following:
    {
      "servers": [
        {
          "host": "hostname",
          "username": "root",
          "password": "password"
          // or pem file (ssh based authentication)
          //"pem": "~/.ssh/id_rsa"
        }
      ],
      "setupMongo": true,
      "setupNode": true,
      "nodeVersion": "0.10.26",
      "setupPhantom": true,
      "appName": "meteor",
      "app": "/Users/arunoda/Meteor/my-app",
      "env": {
        "PORT": 80,
        "ROOT_URL": "http://myapp.com",
        "MONGO_URL": "mongodb://arunoda:fd8dsjsfh7@hanso.mongohq.com:10023/MyApp",
        "MAIL_URL": "smtp://postmaster%40myapp.mailgun.org:adj87sjhd7s@smtp.mailgun.org:587/"
      },
      "deployCheckWaitTime": 15
    }
  4. Now we can edit this file to work for our server environment.
  5. First, we will add the SSH server authentication. We can provide either our RSA key file or a username and a password. If we want to use the latter, we need to install sshpass, a tool used to provide SSH passwords without using the command line:
    "servers": [
        {
          "host": "myServer.com",
          "username": "johndoe",
          "password": "xyz"
          // or pem file (ssh based authentication)
          //"pem": "~/.ssh/id_rsa"
        }
    ],

    Note

    To install sshpass for our environment, we can follow the steps at https://gist.github.com/arunoda/7790979, or if you're on Mac OS X, take a look at http://www.hashbangcode.com/blog/installing-sshpass-osx-mavericks.

  6. Next, we can set some options, such as choosing to install MongoDB on the server. If we use a service such as Compose, we will set it to false:
    "setupMongo": false,

    If we already have Node.js installed on our server, we will also set the next option to false:

    "setupNode": false,

    If we want to mention a specific Node.js version, we can set it as follows:

    "nodeVersion": "0.10.25",

    Meteor Up can also install PhantomJS for us, which is necessary if we use Meteor's spiderable package, which makes our app crawlable by search engines:

    "setupPhantom": true,

    In the next option, we will set the name of our app, which can be the same as our app's folder name:

    "appName": "my-meteor-blog",

    Finally, we point to our local app folder so that Meteor Up knows what to deploy:

    "app": "~/my-meteor-blog",
  7. Meteor Up also lets us preset all the necessary environment variables, such as the correct MONGO_URL variable:
    "env": {
        "ROOT_URL": "http://myServer.com",
        "MONGO_URL": "mongodb://user:password@host:port/databasename",
        "PORT": 8080
    },
  8. The last option sets the time Meteor Up will wait for before checking whether the app started successfully:
    "deployCheckWaitTime": 15

Setting up the server

In order to set up the server using Meteor Up, we need a no-password access to sudo. Perform the following steps to set up the server:

  1. To enable no-password access, we need to add our current user to the server's sudo group:
    $ sudo adduser <username> sudo
    
  2. Then add NOPASSWD to the sudoers file:
    $ sudo visudo
    
  3. Now replace the %sudo ALL=(ALL) ALL line with the following line:
    %sudo ALL=(ALL) NOPASSWD:ALL
    

Deploying with mup

If everything has worked fine, we can set up our server. The following steps explain how we can deploy with mup:

  1. Run the following command from inside the local my-meteor-blog-deployment folder:
    $ mup setup
    

    This will configure our server and install all requirements chosen in our configuration file.

    Once this process is done, we can deploy our app any time by running the following command from the same folder:

    $ mup deploy
    

This way, we can also create production and staging environments by creating two separate Meteor Up configurations with two distinct app names, and deploy it to the same server.

Deploying using Meteor Up

The previous steps help us to deploy our app on our own server, but this method still requires us to build, upload, and set the environment variables.

Meteor Up (mup) aims to make deploying as easy as running $ meteor deploy. However, if we want to use Meteor Up, we need to have full admin rights on the server.

Additionally, this allows us to auto-restart the app in case it crashes, using the forever NPM package, as well as start the app when the server reboots, using the upstart NPM package. We can also revert to the previously deployed version, which gives us a good basis for deployment on the production environment.

Note

The next steps are for more advanced developers, as they require setting up sudo rights on the server machine. Therefore, if you're inexperienced in deployment, consider using a service such as Modulus (http://modulus.io), which offers online Meteor deployment using its own command-line tool, available at https://modulus.io/codex/meteor_apps.

Meteor Up will set up the server and deploy our app as follows:

  1. To install mup on our local machine, we type the following command:
    $ npm install -g mup
    
  2. Now we need to create a folder for our deployment configuration, which could be in the same folder where our app is located:
    $ mkdir ~/my-meteor-blog-deployment
    $ cd ~/my-meteor-blog-deployment
    $ mup init
    
  3. Meteor Up creates a configuration file for us, which will look like the following:
    {
      "servers": [
        {
          "host": "hostname",
          "username": "root",
          "password": "password"
          // or pem file (ssh based authentication)
          //"pem": "~/.ssh/id_rsa"
        }
      ],
      "setupMongo": true,
      "setupNode": true,
      "nodeVersion": "0.10.26",
      "setupPhantom": true,
      "appName": "meteor",
      "app": "/Users/arunoda/Meteor/my-app",
      "env": {
        "PORT": 80,
        "ROOT_URL": "http://myapp.com",
        "MONGO_URL": "mongodb://arunoda:fd8dsjsfh7@hanso.mongohq.com:10023/MyApp",
        "MAIL_URL": "smtp://postmaster%40myapp.mailgun.org:adj87sjhd7s@smtp.mailgun.org:587/"
      },
      "deployCheckWaitTime": 15
    }
  4. Now we can edit this file to work for our server environment.
  5. First, we will add the SSH server authentication. We can provide either our RSA key file or a username and a password. If we want to use the latter, we need to install sshpass, a tool used to provide SSH passwords without using the command line:
    "servers": [
        {
          "host": "myServer.com",
          "username": "johndoe",
          "password": "xyz"
          // or pem file (ssh based authentication)
          //"pem": "~/.ssh/id_rsa"
        }
    ],

    Note

    To install sshpass for our environment, we can follow the steps at https://gist.github.com/arunoda/7790979, or if you're on Mac OS X, take a look at http://www.hashbangcode.com/blog/installing-sshpass-osx-mavericks.

  6. Next, we can set some options, such as choosing to install MongoDB on the server. If we use a service such as Compose, we will set it to false:
    "setupMongo": false,

    If we already have Node.js installed on our server, we will also set the next option to false:

    "setupNode": false,

    If we want to mention a specific Node.js version, we can set it as follows:

    "nodeVersion": "0.10.25",

    Meteor Up can also install PhantomJS for us, which is necessary if we use Meteor's spiderable package, which makes our app crawlable by search engines:

    "setupPhantom": true,

    In the next option, we will set the name of our app, which can be the same as our app's folder name:

    "appName": "my-meteor-blog",

    Finally, we point to our local app folder so that Meteor Up knows what to deploy:

    "app": "~/my-meteor-blog",
  7. Meteor Up also lets us preset all the necessary environment variables, such as the correct MONGO_URL variable:
    "env": {
        "ROOT_URL": "http://myServer.com",
        "MONGO_URL": "mongodb://user:password@host:port/databasename",
        "PORT": 8080
    },
  8. The last option sets the time Meteor Up will wait for before checking whether the app started successfully:
    "deployCheckWaitTime": 15

Setting up the server

In order to set up the server using Meteor Up, we need a no-password access to sudo. Perform the following steps to set up the server:

  1. To enable no-password access, we need to add our current user to the server's sudo group:
    $ sudo adduser <username> sudo
    
  2. Then add NOPASSWD to the sudoers file:
    $ sudo visudo
    
  3. Now replace the %sudo ALL=(ALL) ALL line with the following line:
    %sudo ALL=(ALL) NOPASSWD:ALL
    

Deploying with mup

If everything has worked fine, we can set up our server. The following steps explain how we can deploy with mup:

  1. Run the following command from inside the local my-meteor-blog-deployment folder:
    $ mup setup
    

    This will configure our server and install all requirements chosen in our configuration file.

    Once this process is done, we can deploy our app any time by running the following command from the same folder:

    $ mup deploy
    

This way, we can also create production and staging environments by creating two separate Meteor Up configurations with two distinct app names, and deploy it to the same server.

Setting up the server

In order to set up the server using Meteor Up, we need a no-password access to sudo. Perform the following steps to set up the server:

  1. To enable no-password access, we need to add our current user to the server's sudo group:
    $ sudo adduser <username> sudo
    
  2. Then add NOPASSWD to the sudoers file:
    $ sudo visudo
    
  3. Now replace the %sudo ALL=(ALL) ALL line with the following line:
    %sudo ALL=(ALL) NOPASSWD:ALL
    

Deploying with mup

If everything has worked fine, we can set up our server. The following steps explain how we can deploy with mup:

  1. Run the following command from inside the local my-meteor-blog-deployment folder:
    $ mup setup
    

    This will configure our server and install all requirements chosen in our configuration file.

    Once this process is done, we can deploy our app any time by running the following command from the same folder:

    $ mup deploy
    

This way, we can also create production and staging environments by creating two separate Meteor Up configurations with two distinct app names, and deploy it to the same server.

Deploying with mup

If everything has worked fine, we can set up our server. The following steps explain how we can deploy with mup:

  1. Run the following command from inside the local my-meteor-blog-deployment folder:
    $ mup setup
    

    This will configure our server and install all requirements chosen in our configuration file.

    Once this process is done, we can deploy our app any time by running the following command from the same folder:

    $ mup deploy
    

This way, we can also create production and staging environments by creating two separate Meteor Up configurations with two distinct app names, and deploy it to the same server.

Outlook

Currently, Meteor limits native deployment to its own servers, with limited control over the environment. Planned is an enterprise-grade server infrastructure called Galaxy, which will make deploying and scaling Meteor apps as simple as Meteor itself.

Nonetheless, with Meteor's simplicity and great community, we already have a rich set of tools available to deploy to any Node.js-based hosting and PaaS environment.

Note

For example, if we wanted to deploy on Heroku, we can take a look at the build pack by Jordan Sissel at https://github.com/jordansissel/heroku-buildpack-meteor.

Summary

In this chapter, we learned how to deploy Meteor and how simple deploying on Meteor's own server infrastructure can be. We also used tools such as Demeteorizer and Meteor Up to deploy on our own server infrastructure.

To read more about the specific deployment methods, take a look at the following resources:

You can find the full example code of this app, ready for deployment, at https://www.packtpub.com/books/content/support/17713 or on GitHub at https://github.com/frozeman/book-building-single-page-web-apps-with-meteor/tree/chapter10.

In the next chapter, we will create a package of our previously created ReactiveTimer object and publish it to Meteor's official package repository.

You have been reading a chapter from
Meteor: Full-Stack Web Application Development
Published in: Nov 2016
Publisher: Packt
ISBN-13: 9781787287754
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image