Node.js + MongoDB in production
THIS PROJECT IS DEPRECATED.
Get the foundation to start developing Node.js applications and the tools to put it into production.
During development you will get a Vagrant box with:
- Genguis: an amazing MongoDB admin UI;
- knife-solo: utility to provision your production boxes.
And in production you will get a box with:
- Nginx as the web server (acting as a proxy to the application);
- MongoDB setup only to accept local connections;
- Upstart service for the Node.js application using node-supervisor;
- SSH setup with authorized_keys;
- Application deployment support using git hooks.
All the cookbooks presented are very simple (not maxing 30 lines of code), demostrating that it is possible to provision machines using Chef without the use of complex third-party cookbooks (managed by Berkshelf or Librarian).
You can check how each cookbook work and see how little code is required to install something like Nginx by simply relying on the default Ubuntu package.
The Node.js server must listen to port 3000 for the Nginx proxy and Vagrant port-forwarding to work.
Development with Vagrant
Once Vagrant is installed, you can start the development machine with:
This will download a base box from the internet and provision the machine using the recipes that are specified at the
Once completed, you can log in the machine and start working.
For more information on Vagrant, please check its excelent documentation.
Server provisioning with knife-solo
Once you've got the first working version of your application, you can put it into production using the knife-solo utility that is already installed inside the Vagrant development box.
You will need a Ubuntu box with root access. A good way of getting a machine is using a VPS provider (like Digital Ocean).
Once you've got the machine, we need to prepare it before cooking the recipes. Using the knife-solo tool it is easy as:
knife solo prepare root@myRemoteMachineAddress
Pay attention to the
myRemoteMachineAddress, change it to the machine address.
Configuring the data bags
Data bags is Chef's way of storing sensitive information, and it is usually not a good idea to commit its contents.
The only data bag in this project is the one used to hold you public ssh key. Make sure to use the sample file as a reference to create your own
Cooking the recipes
Once chef is installed, we can cook the recipes.
knife solo cook root@myRemoteMachineAddress -r "role[application]"
role[application] represents the role of this machine, or in other words, which cookbooks we will be running in it. You can check the
roles/application.rb file to see how it works.
Once completed, you will have the machine ready to host the application.
The deployment works via git-hooks. So once the server detects the push, it installs the dependencies (
npm install) and restarts the server.
To deploy a application in this shiny new server you need to add a new remote to your application repository.
git remote add deploy deploy@myRemoteMachineAddress:/opt/application/.git
As you can see, the application is located at the
/opt/application server folder.
Then you can push the code and watch it works!
git push deploy master
The server expects that the application server is executed via a
index.js file in
production environment. But you can change that in the
roles/application.rb file at:
default_attributes 'node_env' => 'production', 'node_start_script' => '/opt/application/index.js'