Skip to content

Instantly share code, notes, and snippets.

@airportyh
Created April 13, 2017 16:38
Show Gist options
  • Select an option

  • Save airportyh/f5adcfa667c627778f72a0ddc54aec49 to your computer and use it in GitHub Desktop.

Select an option

Save airportyh/f5adcfa667c627778f72a0ddc54aec49 to your computer and use it in GitHub Desktop.

Deploying Express App on AWS

Stuff to install on Ubuntu

  • nvm - curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash
  • Node.js - nvm install v6.0.0
  • pm2 - npm install pm2 -g

Stuff to configure on Ubuntu

  • Apache Site

Setting up an Express app

cd /var/www
git clone YOUR_PROJECT_URL
cd YOUR_PROJECT_DIR
npm install

Setup your database if needed.

Setting up Apache site

cd /etc/apache2/sites-available
nano YOUR_PROJECT_NAME.conf

Put in this contents:

<VirtualHost *:80>
        ServerAdmin YOUR_EMAIL
        ServerName YOUR_HOST_NAME

        DocumentRoot /var/www/YOUR_PROJECT_DIR/public/
        ProxyRequests on
        ProxyPass / http://localhost:YOUR_APP_PORT/      
</VirtualHost>
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2ensite YOUR_PROJECT_NAME
sudo service apache2 reload

Start (Test only) Your app

cd /var/www/draw_together
npm install
node YOUR_BACKEND_JS_SCRIPT

Test by editing local computer (your mac) /etc/hosts file

Add an entry in the file that points YOUR_HOST_NAME to your Ubuntu server's IP address.

PM2 Commands

  • pm2 start YOUR_SCRIPT
  • pm2 list
  • pm2 show ID

Bonus: getting socket.io to work

sudo a2enmod proxy_wstunnel
sudo a2enmod rewrite

Add the four lines in the middle

<VirtualHost *:80>
        ServerAdmin YOUR_EMAIL
        ServerName YOUR_HOST_NAME

        RewriteEngine On
        RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
        RewriteCond %{QUERY_STRING} transport=websocket    [NC]
        RewriteRule /(.*)           ws://localhost:YOUR_APP_PORT/$1 [P,L]

        DocumentRoot /var/www/YOUR_PROJECT_DIR/public/
        ProxyRequests on
        ProxyPass / http://localhost:YOUR_APP_PORT/

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