This tutorial is now outdated. If you want to stay up to date with the MEAN Stack you will want to check out this Complete Guide instead.
Moving over a MEAN application to production
For this setup, I am using the following stack to serve my application to users.
- Ubuntu 14.04
- SSL Certificates
Now you have to run through part 1 again to set up the development server after you do this you then have to clone the repository into the server
That will pull over our source code and just like before back in part one
Now that we have all of those files on the server we have to be able to setup our development credentials on the server lets edit this file.
and make the following changes.
If you can tell we are running our application on a different port 3080 instead of the regular 3000 port, remember we do have our Ghost application on 3000 so we can’t reuse that port. Now make sure you save your changes.
How to setup apache and node.js with an SSL certificate
Here is the tricky part that took a bit of asking around in the mean community, I wanted to force all of the traffic arriving at my application to be sent over HTTPS and the way you would o this is by first purchasing an SSL certificate from a provider I used godaddy, then you have to generate key with OpenSSL, more on that later. Here is what the apache configuration would look like.
That’s it all you have to do is enable the site and you are ready to go from a server standpoint, if you don’t recall how to do that go back to part 1.
How to create an init.d script for a node.js application
Now we need to test all of those configurations, from the MEAN.IO documentation you can easily do this by running the following command.
This presents a problem, though? How can we make this command as a service? or even better a daemon if we have to restart the server? If you never built a Linux daemon before a really good boilerplate code cane be found at
/etc/init.d/skeleton so we will be using this skeleton to build our own service.
Now just make the changes to that file accordingly to your environment, where the files are located what user is going to be running it. Of course, this leads more into system administration which this tutorial is not about.
Now that we have this handy script running all we have to do is start the application by running
start knowrick and at any point if you want to stop it just run
Separation of concerns with the source code
Whenever you are working on a project that anyone can see your source code, it’s important not to publish secret tokens, passwords and the like on a public repository like Github, not only because bots are constantly scanning for keys, passwords, and username on that response so it’s not a good idea. How do we keep using our git repo in the development and in production? We have to remove files that we don’t want to be tracked in the repository we do thy by running the following commands.
This will only remove the files from the repository but we still have to tell git not to track those files you can do this by running the following command.
Now that we have ignored those files just check in those changes
That’s it don’t forget to register those changes to node by doing a restart of the service.
Now we can quickly push code changes right to the repository without doing any configurations. AWESOME!
What’s next for this project?
That will do it for this application if we call back to part 1 of this tutorial the entire point of this was for me to be able to track and manage my goals and learn MEAN. Check and check again, now that the application is in production it’s time to start putting it to use, I will be tracking my goals all of 2015 and see if we get some results and possibly tackle this project again in 2016 with new and improved changes.
Now what are you waiting for? go and give it a try!
I always had a passion for the field of STEM (Science, Technology, Engineering, and Math) and I knew I wanted to do something to make a difference in the world. I just didn’t know where to start. I was an immigrant in a new country, grew up in a tough environment, and wasn’t sure how… Read More