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.
-
SSH to your remote instance
-
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-keygento generate SSH key$ cat /home/ec2-user/.ssh/id_rsa.pubcopy key and add to the github settings$ git clone git@github.com:project_name.gitclone git project -
Install Meteor
Use a simple curl command, as explained on the official Meteor site:
$ curl https://install.meteor.com/ | sh -
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 nodejsinstall package -
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.repoCopy 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-orgto install packages$ sudo mkdir -p /data/db/create directory for data$ sudo chown `id -u` /data/dbset directory permissions$ sudo mongodrun mongo to check that everything works correct$ mongod --storageEngine inMemoryrun mongo in memory$ use database_namecreate new database Note: need to create new collection and insert at least 1 document.export MONGO_URL=mongodb://localhost:27017/your_dbForce Meteor to use database that was created on the previous step. Change directory to the folder that contains Meteor project and export MONGO_URL
-
-
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.keyYou should see an OK response. -
$ sudo yum install nginxinstall nginx
- In the home directory of the server create nginx_signing.key file with content from
nginx docs.
To verify the signing key use
-
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/appcreate 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
- Go to nginx directory.
-
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.