Tech notes

Javascript, HTML, CSS

Jun 10, 2018 | AWS Meteor deploy

Deploy Meteor app on AWS from scratch


Meteor a platform for building web apps on pure JS becomes more and more popular. I spent some time deploying my recent Meteor project on AWS and I found this experience a little confusing and time-consuming. So here is a steps to set up everything from scratch. Hope this manual will help to save time and to avoid most common issues.

I use AWS EC2 with Linux Fedora.

  1. SSH to your remote instance

  2. Copy your project to a server

    My project was on github, so I generated SSH key on my instance and added this key to gitHub settings.

    $ ssh-keygen to generate SSH key

    $ cat /home/ec2-user/.ssh/id_rsa.pub copy key and add to the github settings

    $ git clone git@github.com:project_name.git clone git project

  3. Install Meteor

    Use a simple curl command, as explained on the official Meteor site:
    $ curl https://install.meteor.com/ | sh

  4. Install Node

    Node.js is available from the NodeSource Enterprise Linux and Fedora binary distributions repository.

    $ curl -sL https://deb.nodesource.com/setup_0.10 | sudo -E bash -
    $ sudo yum -y install nodejs install package
  5. Setup mongoDB

    It is not necessary step because Meteor will automatically install and run its own instance of MongoDB. However in my case I want to run separate independent from Meteor instance. Install MongoDb (3.6 in my case) and create new database for the project.

    • Create a repo file so you can install MongoDB using yum.

      $ touch /etc/yum.repos.d/mongodb-org-3.6.repo

      Copy paste the folowing content:

      [mongodb-org-3.6]
      name=MongoDB Repository
      baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.6/x86_64/
      gpgcheck=1
      enabled=1
      gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
      
    • $ sudo yum install -y mongodb-org to install packages
    • $ sudo mkdir -p /data/db/ create directory for data
    • $ sudo chown `id -u` /data/db set directory permissions
    • $ sudo mongod run mongo to check that everything works correct
    • $ mongod --storageEngine inMemory run mongo in memory
    • $ use database_name create new database Note: need to create new collection and insert at least 1 document.
    • export MONGO_URL=mongodb://localhost:27017/your_db Force Meteor to use database that was created on the previous step. Change directory to the folder that contains Meteor project and export MONGO_URL
  6. Install nginx server

    I use Nginx to route incoming requests to port 80 and 443 to Meteor port (by default 3000).
    • In the home directory of the server create nginx_signing.key file with content from nginx docs. To verify the signing key use
      $ sudo apt-key add nginx_signing.key You should see an OK response.
    • $ sudo yum install nginx install nginx
  7. Configure nginx to process incoming requests.

    • Go to nginx directory. $ cd /etc/ngnix
    • Check all available files (ls) and add 2 directories
      $ mkdir sites-available
      $ mkdir sites-enabled
    • In folder 'sites-available' create new file 'app' or update existing one with the following content
      # this section is needed to proxy web-socket connections
      map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
      }
      
      # HTTP
      server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
      
        location = /favicon.ico {
            root /home/USERNAME/portal/programs/web.browser/app;
            access_log off;
        }
      
        location ~* "^/[a-z0-9]{40}\.(css|js)$" {
          gzip_static on;
          root /home/USERNAME/portal/programs/web.browser;
          access_log off;
        }
      
        location ~ "^/packages" {
          root /home/USERNAME/portal/programs/web.browser;
          access_log off;
        }
      
        # pass requests to Meteor
        location / {
            proxy_pass http://127.0.0.1:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade; #for websockets
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
        }
      }
    • $ sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/app create a symbolic link to a file with the same name in “sites/enabled”.
    • Add reference to the site-enabled file to the bottom of the /etc/nginx/nginx.conf file to include your changes:
      include /etc/nginx/sites-enabled/*;
    • To test the config
      $ sudo nginx -t
      You should see
      $ nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
      $ nginx: configuration file /etc/nginx/nginx.conf test is successful
      Then restart nginx
      sudo service nginx restart
  8. Run Meteor

    Go to your project branch and run Meteor. Some developers prefer to use tools like forever to run programms in the background. I personally prefer to use screens.

Note: if your application runs on the server but it is not accessible through public IP you may consider taking a look at AWS security groups and load balancers.


Back to blog