GithubHelp home page GithubHelp logo

jamesthehacker / nodejs-server-ansible-playbook Goto Github PK

View Code? Open in Web Editor NEW
40.0 6.0 8.0 7 KB

NodeJS server provisioning and application deployment using Ansible: HAProxy, Let's Encrypt, HTTPS, Secure Firewall ...

nodejs-server-ansible-playbook's Introduction

NodeJS Server: Digital Ocean Ansible Playbook

Network Architecutre

Deploy a NodeJS application to Digital Ocean with ease! Don't have a Digital Ocean account? Get one here and get $10 free (enough to pay for 2 servers for a month).

This play will set up a single server that is well suited to small/medium NodeJS applications on Digital Ocean. I use this same playbook for setting A/B testing for my marketing campaigns and have used it on servers that recieve large traffic loads without any issues.

The playbook is an extension to a tutorial I write on setting up a small production ready secure NodeJS server. It performs exactly the same tasks detailed in my tutorial, except it automates the entire process using Ansible.

Feautres:

  • NodeJS 8
  • HAProxy
  • Automatic application deployment
  • Let's Encrypt dns-01 domain verification and certificate renewal
  • Daemonized NodeJS application using systemd
  • Secured Firewall (Only ports 22, 80 and 443 are open)

How It Works

In this playbook the node application lives under /var/www. The default entry point for the application is /var/www/server.js. If your entry point differs this can be changed in group_vars/all.yml.

I use haproxy to act like a revere proxy that load balances 3 instances of server.createServer, each running on port 5001, 5002 and 5003 respectively. Ideally your application should do the same to avoid having to make configuration changes to haproxy.j2 and nodeapp.j2. If you know what you're doing and would like to make modifications for a more advanced architecture you will need to edit roles/templates/haproxy.j2 and roles/templates/nodeapp.j2.

HTTPS is enabled by default. SSL/TLS certificates are provided by Let's Encrypt using dns-01 and auto certificate renewal.

Prequisites

  • Ansible 2.4
  • A Digital Ocean account
  • A domain name

This playbook requires Ansible version 2.4. As of writing this is currently the development version. For instructions on installing 2.4 see here.

Configuring Playbook

Before running the playbook a couple of configuration options need to be set. Configuration options are stored in group_vars/all.yml. An example configuration is below:

# Domain name for your application
domain: yourdomain.com

# Git repository for your NodeJS application
app_repo: https://github.com/JamesTheHacker/nodebox-testapp

# Application entry point
entry_point: server.js

# Digital Ocean API key
api_token: 308ddfb93a32a22ef222de98496e981ef247d5c1f6fe17d76d8f9db30a7d5f23

# SSH key id (fingerprint) to existing SSH key on Digital Ocean
ssh_key_id: xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx

# Unprivileged username and password
user: web
user_pass: ChangeThisPasswordToSomethingSecure

# Your email address. Required for Let's Encrypt
email: [email protected]

# 2 character country code. Required for Let's Encrypt
country: GB

Variables

  • domain:: Set this to the domain name you want to link to the application. Do not include www.. Before running the playbook update the domains nameservers to: ns1.digitalocean.com, ns2.digitalocean.com and ns3.digitalocean.com.
  • app_repo: The playbook will automatically clone an application from a git repository (Github, Bitbucket etc).
  • entry_point: Entry point for your NodeJS application
  • api_token: Set this to your Digital Ocean API token. You can generate a token here.
  • ssh_key_id: This should contain the SSH fingerprint from Digital Ocean. You can find the fingerprint here
  • user: Unprivilaged username
  • password: Unprivilaged user password
  • email: A valid email address. Required for Let's Encrypt
  • country: 2 digit country code. Required for Let's Encrypt

Note: I recommend using Vault to encrypt all.yml to ensure passwords are not stored in plain text.

Go!

Setting up the server and deploying your application is simple! Once you've set the required configuration variables simple run the following:

cd nodejs-server-ansible-playbook
ansible-playbook playbook.yml

If all runs successfully you should now be able to visit your domain in the browser and see your application running :)

## Help and Support

If you have an problems please file an issue. You can also catch me in the #ansible channel on freenode (username jj15), or tweet me at @JamesTheHaxor

Contributions

I'm human, and mistakes/errors/issues happen. If you would like to fix any issues, or improve this playbook please submit a pull request and I will happily merge :)

Shoutouts

I'd like to thank my good friend @Radar for his help and support. I would also like to thank the amazing people in the #ansible channel for answering my numerous questions. You guys are awesome!

nodejs-server-ansible-playbook's People

Contributors

jamesthehacker avatar undergroundlabs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

nodejs-server-ansible-playbook's Issues

Error Running playbook

This is a cool looking porject. Unfortunately, I cannot seem to get it working.

ansible-playbook playbook.yml produces the following error:

ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

The error appears to have been in '/home/egordon/nodejs-server-ansible-playbook/roles/node-servers/tasks/letsencrypt-dns.yml': line 25, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: Generating Certificate Signing Request
  ^ here


The error appears to have been in '/home/egordon/nodejs-server-ansible-playbook/roles/node-servers/tasks/letsencrypt-dns.yml': line 25, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: Generating Certificate Signing Request
  ^ here

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.