Skip to content

Instantly share code, notes, and snippets.

@HenryHengZJ
Last active October 19, 2022 23:48
Show Gist options
  • Save HenryHengZJ/f8fb7dae0300d18a104cc4a29ec51a7a to your computer and use it in GitHub Desktop.
Save HenryHengZJ/f8fb7dae0300d18a104cc4a29ec51a7a to your computer and use it in GitHub Desktop.
Guide on how to host Outerbridge on AWS EC2 instance

Launch EC2 Instance

This walkthrough will skip how to launch an EC2 instance. There's plenty of resources and tutorial on how to do that. In this case we are launching a EC2 instance with t2.micro.

After EC2 instance has successfully spinned up, SSH into EC2 instance.

Install Node

  1. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
    
  2. . ~/.nvm/nvm.sh
    
  3. nvm install --lts
    

Install Yarn

npm install yarn -g

Install MongoDB

Follow instruction: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-amazon/

  1. sudo nano /etc/yum.repos.d/mongodb-org-6.0.repo
    
  2. Copy paste the following into the file you just created above:
    [mongodb-org-6.0]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/6.0/x86_64/
    gpgcheck=1
    enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
    
    Ctrl+X to exit, press Y to save the file.
  3. sudo yum install -y mongodb-org
    
  4. sudo systemctl start mongod
    
  5. sudo systemctl status mongod
    
  6. sudo systemctl enable mongod
    

Install Git

sudo yum install git -y

Install pm2

npm install pm2 -g

Setup the project

Two ways to setup:

Install Outerbridge via NPM:

  1. npm install -g outerbridge
    
  2. npx outerbridge start
    
  3. App is now ready on port 3000.
  4. To view the app, create 2 new inbound rules on the EC2 instance. Custom TCP Port 3000 that allows anywhere to access.
  5. You can then view the app: "Your Public IPv4 DNS":3000. Example: http://ec2-18-222-246-22.eu-west-1.compute.amazonaws.com:3000

Install via Github

  1. git clone https://github.com/Outerbridgeio/Outerbridge.git
    
  2. cd Outerbridge
    
  3. yarn setup
    
  4. yarn bootstrap
    
    Depending on your RAM size, for t2.micro this could take a while.
  5. yarn build
    
  6. yarn start
    
  7. App is now ready on port 3000.
  8. To view the app, create 2 new inbound rules on the EC2 instance. Custom TCP Port 3000 that allows anywhere to access.
  9. You can then view the app: "Your Public IPv4 DNS":3000. Example: http://ec2-18-222-246-22.eu-west-1.compute.amazonaws.com:3000

Troubleshoot yarn bootstrap

In cases where t2.micro is taking forever on yarn bootstrap, here is the workaround:

  1. Install the dependencies of each package independently. cd into each package: ui, components and server and do yarn install for each of them.
  2. Back to the Outerbridge root path, do yarn bootstrap. Because the dependencies have been installed, the operation could be faster.

Troubleshoot yarn build

Most of the time, it is stuck at building the ui folder. As a temporary workaround:

  1. Back to your EC2 home path (/home/ec2-user/) and do
    git clone https://github.com/Outerbridgeio/OuterbridgeUIBuild.git
    
  2. Copy the build folder to Outerbridge ui folder:
    cp -r OuterbridgeUIBuild/build/ Outerbridge/packages/ui/
    
  3. cd Outerbridge
    
  4. Build server and components independently. cd into package: components and server and do yarn build for each of them.
  5. Back to the Outerbridge root path, do yarn start to see if app can be started.

Using NGINX

If you want to get rid of the :3000 on the url and have a custom domain, you can use NGINX to reverse proxy port 80 to 3000. So user will be able to open the app using your domain. Example: http://yourdomain.com

  1. Create 2 new inbound rules on the EC2 instance. Custom HTTP Port 80 and HTTPS Port 443 that allows anywhere IVP4 to access. You can proceed to delete the 2 Custom port 3000 TCP inbound rules created earlier.

  2. sudo amazon-linux-extras install -y nginx1
    
  3. nginx -v

  4. sudo systemctl start nginx
    
  5. sudo nano /etc/nginx/conf.d/outerbridge.conf
    
  6. Copy paste the following and change to your domain:

    server {
        listen 80;
        listen [::]:80;
        server_name yourdomain.com; #Example: demo.outerbridge.io
    
        location / {
            proxy_pass http://localhost:3000;
            proxy_http_version 1.1;
            proxy_set_header Host $host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_cache_bypass $http_upgrade;
        }
    }
    
  7. sudo systemctl restart nginx
    
  8. Go to your DNS provider, and add a new A record with IP address using Public IPv4 address from EC2 instance.

  9. You should now be able to open the app: http://yourdomain.com

Install Certbot to have HTTPS

If you like your app to have https://yourdomain.com. Here is how:

  1. sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    
  2. sudo yum-config-manager --enable epel 
    
  3. sudo yum install certbot python2-certbot-nginx
    
  4. certbot --version
  5. sudo certbot --nginx
    
  6. If cert expired, simply renew:
    sudo certbot renew --dry-run
    
  7. You can now open the app: https://yourdomain.com

Backup MongoDB

You can backup MongoDB database into S3 bucket. Here is the article that can be followed:

  1. sudo yum --enablerepo epel install s3cmd
    
  2. s3cmd --configure
    
  3. sudo nano mongo_backup.sh
    
  4. Copy paste and replace values:
    #!/bin/bash
    
    #Force file syncronization and lock writes
    mongo admin --eval "printjson(db.fsyncLock())"
    
    MONGODUMP_PATH="/usr/bin/mongodump"
    MONGO_DATABASE="dbname_here" #replace with your database name
    
    TIMESTAMP=`date +%F-%H%M`
    S3_BUCKET_NAME="bucketname_here" #replace with your bucket name on Amazon S3
    S3_BUCKET_PATH="mongodb-backups"
    
    # Create backup
    $MONGODUMP_PATH -d $MONGO_DATABASE
    
    # Add timestamp to backup
    mv dump mongodb-$HOSTNAME-$TIMESTAMP
    tar cf mongodb-$HOSTNAME-$TIMESTAMP.tar mongodb-$HOSTNAME-$TIMESTAMP
    
    # Upload to S3
    s3cmd put mongodb-$HOSTNAME-$TIMESTAMP.tar s3://$S3_BUCKET_NAME/$S3_BUCKET_PATH/mongodb-$HOSTNAME-$TIMESTAMP.tar
    
    #Unlock database writes
    mongo admin --eval "printjson(db.fsyncUnlock())"
    #Delete local files
    rm -rf mongodb-*
    
  5. bash mongo_backup.sh
    
  6. Go to S3 bucket, you should see a tar file.
  7. Setup cron
    crontab -e
    
  8. Copy paste:
    #1st of every month at 9 am
    00 09 1 * * /bin/bash /home/ec2-user/mongo_backup.sh
    
  9. To get the backup:
     s3cmd get <s3://link>
    
  10. Unzip it:
    tar xvf <file.tar>
    
  11. Restore it:
    mongorestore -d <dbname> -c <collectionname> <dir/file.bson>
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment