Note: This repo is no longer maintained. I would advise a more modern ES6 boilerplate setup such as MEVN with webpack.
A boilerplate MEAN app on a single server.
This boilerplate is designated to the beginner/intermediate web developer who wishes to get a jump start into deploying a cloud MEAN web app. Many of the features included are required by most production deployments, and therefore I hope I may help you get your to scalability on AWS. This software is free to use under MIT.
-
MEAN (MongoDB, Express, Angular, node.js) full-stack framework - A blazing fast stack for efficient modern web applications, using NoSQL key-value storage with MongoDB, Express for route handling, Angular for a clean and responsive frontend, and node.js to tie the API together.
-
AWS CodeDeploy Scripts - Allows your cloud servers to listen for commits to a Github repo's master branch, and gracefully update. This can be used among a load-balanced network, allowing you to build at scale,
-
User Authentication with Satellizer - A reliable end-to-end token-based authenticator for Angular that provides security and simplicity to your application,
-
AWS S3 Uploads - A staple in web applications, allowing users to upload large files without bogging down your expensive mongo store,
-
Error handling and API logging to Loggly - Production logging is an important aspect to solve unforeseen bugs or unstable components of your API,
-
GULP minify support - Gulp and minification of your JS and CSS files can speed up your load times and obfuscate your code from hackers,
-
User Validation/Recaptcha - Google reCaptcha2 provides excellent protection from potentially malicious bots trying to create accounts on your system,
-
Maintenance Mode - Sometimes, downtime is required to rebuild databases or stop production systems while maintenance is conducted,
-
MailChimp - Upon signup, users can be automatically placed into a Mailchimp mailing list to start receiving marketing emails,
-
Password Reset - Password reset functionality through email using Nodemailer.
This guide assumes you're on a Mac. The process should be similar on a PC.
-
Download the zip file and extract to your local machine,
-
Using terminal, install the node.js runtime locally,
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update
brew doctor
export PATH="/usr/local/bin:$PATH"
brew install node
npm install -g grunt-cli
- Navigate to the extracted folder and install necessary node modules,
cd "/Users/*USER*/Desktop/Single Machine"
npm install
- Install and run MongoDB locally
brew install mongodb
mongod
- In another terminal window, run server.js
node server
- In Identity & Access Management (IAM), create a new role with AmazonEC2FullAccess, AWSCodeDeployFullAccess, and AWSCodeDeployRole policies,
- Create a new user called "GitHub" and grant AWSCodeDeployFullAccess, and save their Access Token & Secret,
- Launch a new Amazon Linux AMI EC2 Instance (Free-Tier, 8GB) with the IAM role from (1), include all HTTP traffic and open SSH for your IP,
- In AWS Code Deploy, create a new application and ensure app name and group are alphanumeric with _, no dashes or other characters,
- Create a new deployment group called "production".
- Create GitHub token repo_deployment token in GitHub personal access settings,
- Create a new public or private repo,
- In repo settings, webhooks and services, set up GitHub Auto-Deployment,
- Then, set up AWS CodeDeploy, and use the IAM User "GitHub" AWS Access Key & Secret, and CodeDeploy app name and deployment group set previously.
- SSH to instance,
sudo chmod 600 *KEYNAME*.pem
ssh -i keys/*KEYNAME*.pem ec2-user@*EC2PUBLICDNS*
- Upgrade System (YUM),
sudo yum update
- Install NodeJS,
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
. ~/.nvm/nvm.sh
nvm install --lts
sudo ln -s /usr/local/bin/node /usr/bin/node
sudo ln -s /usr/local/bin/npm /usr/bin/npm
sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node"
sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm"
- Install Code Deploy Agent,
sudo yum install ruby
sudo yum install wget
cd /home/ec2-user
wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
sudo chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent status
- Install PM2,
npm install pm2 -g
sudo su -
sudo npm install pm2 -g
exit
sudo ln -s /usr/local/bin/pm2 /usr/bin/pm2
- Install GULP,
npm install gulp -g
sudo su -
sudo npm install gulp -g
exit
sudo ln -s /usr/local/bin/gulp /usr/bin/gulp
- Open Port 80 Listening,
sudo yum install libcap2-bin
sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``
- (Optional) Set up HTTPS/Load Balancing by using Route 53, EC2 Load-Balancer, EC2 Auto-Scale Group.
- Create a new account and MongoDB sandbox at mLab,
- Copy the
mongodb://
url in the tools/config.json file underproduction.mongo.uri
- Push commit to master branch,
- In CodeDeploy, deploy a new revision,
- Select "My application is stored in GitHub", and connect,
- Enter your repository name and last commit id,
- Your server should now be accessible at your EC2 DNS.
These features can be switched on/off by their respective booleans in tools/config.json
- Amazon Web Services S3: Create a new S3 bucket for photos AWS, and include your access key and secret key in tools/config.json,
- Loggly: Add your token and subdomain to tools/config.json,
- Google Recaptcha: Add your secret to tools/config.json and your public key to /app/controllers/validate.js,
- MailChimp: Adding your API key and list to tools/config.json,
- HTTPS: May be added to your load-balancer, and node will automatically redirect HTTP traffic to the HTTPs endpoint,
- Gulp: Gulp is automatically run on production, you can comment out the .js and .css files in main.ejs and include the two generated ones in the asset folder (May cause artifacts).
- Every commit to the master branch done locally will automatically deploy and restart on your EC2 (if CodeDeploy is set up correctly),
- There should not be a need to ever SSH back into the EC2, PM2 handles all crashes/faults,
- Develop locally! It's much easier and quicker to keep things local by running
mongod
in a terminal, - WebStorm is a great tool to work locally, alongside Chrome for inspecting.
- One might think that MongoDB should run on the single machine as well, and the mongo uri is localhost. That would make the entire app actually exist on a single machine. While it works locally, and it might look cool on paper within the cloud, there is no easy way of scaling such a setup.
- MongoDB
- Express
- AngularJS
- Node.js
- Amazon Web Services
- MailChimp
- MLab
- Google Recaptcha
- PM2
- Satellizer
- Mongoose
- Gulp
- Nodemailer
- Sitemap.js
- Underscore.js
- Async
- WrapKit
EOF