Skip to content

Instantly share code, notes, and snippets.

@Aebrathia
Created October 3, 2016 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aebrathia/c5f92858f5a92c86b4a590c4a2fb3291 to your computer and use it in GitHub Desktop.
Save Aebrathia/c5f92858f5a92c86b4a590c4a2fb3291 to your computer and use it in GitHub Desktop.
Setup Debian 8 server to work with Node, Nginx and MongoDB

Server setup

  1. Change root password. passwd root
  2. Follow steps Secure SSH
  3. Update Debian. apt-get update && apt-get upgrade
  4. Install sudo. apt-get install sudo
  5. Install dev tools (some npm packages need it). apt-get install build-essential
  6. Choose language. dpkg-reconfigure locales and pick fr_FR.UTF-8 if you're french
  7. Use UTC timezone
    1. rm /etc/localtime
    2. ln -s /usr/share/zoneinfo/UTC /etc/localtime
    3. date and date -u must have the same output

Secure SSH

Don't forget to change andrea and by your user's name.

  1. Add your user. adduser andrea
  2. Add him to sudoers if needed. usermod -G sudo andrea
  3. Find his public ssh key. https://www.github.com/myuser.keys or cat ~/.ssh/id_rsa.pub
  4. Add him to authorized keys (one per line). vim ~/.ssh/authorized_keys
  5. Enable access without password to wheel group. visudo : %sudo ALL = (ALL) NOPASSWD: ALL
  6. Prevent root connexion. Be sure you have another sudo user with ssh access. vim /etc/ssh/sshd_config & PermitRootLogin no
  7. Apply changes. systemctl reload sshd

Your Node project with Nginx and MongoDB

  1. Git. apt-get install git
  2. Node & npm. (Be sure to use appropriate version) curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - & sudo apt-get install -y nodejs
  3. Nginx. apt-get install nginx
  4. MongoDB. (Be sure to use appropriate version)
    1. sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
    2. echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
    3. apt-get install mongodb-org

Nginx

  1. Add your website conf. vim /etc/nginx/sites-available/mywebsite
  2. Enable it. ln -s /etc/nginx/sites-available/th.conf /etc/nginx/sites-enabled/
  3. It default poses problem, remove it. rm /etc/nginx/sites-enabled/default
  4. Restart Nginx to apply changes. /etc/init.d/nginx restart ou service nginx restart

Restart node app on crash or reboot with pm2

  1. Install PM2. npm install pm2 -g
  2. Enable pm2's launch on system boot. Replace systemd by the one your OS uses, see PM2. pm2 startup systemd
  3. Start app. pm2 start /var/www/mywebsite/index.js -n mywebsite
  4. Save pm2 process list. pm2 save

Sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment