Week 8
Deployment
And we're off. Today we'll be deploying the web apps that you've been working so hard on! Follow the guide (with intentionally vague instructions) below to successfully set up a live environment.
Prerequisites
- [ ] Milestone 2
- [ ] Sign up for GCP account at console.cloud.google.com.
- [ ] Set up the GCP CLI
gcloudon your machine: Mac, Ubuntu. - [ ] Configure the
PRODprofile for your application, which will be active when you run your app live. Steps below:
PROD Profile
- Sign up for a free tier database service instance:
- Postgres users: check out ElephantSQL.
- MySQL users: check out Google Cloud MySQL.
- Mongo users: check out MongoLab.
- Once you have signed up for the service, update your database config for
PRODwith the relevant database URI, username, password, etc. - If you are using Postgres or MySQL: execute your
schema.sqlon your instance via the CLI e.g.mysql -h [IP_ADDRESS] -u root -p < schema.sql- this will set up your production instance with the database, user, and tables you'll need. - Run your app locally with the
PRODprofile and see that you successfully connect to your new database instance.
Deployment
- Your database config should be in a separate config file (eg
db.config.js), which will give you the info you need based on the profile you're running (TESTorDEV). We will now - Create a new project on GCP.
- On the sidebar, go to Compute Engine > VM instances. Spin up a brand new server (by default it will run Debian 9 Stretch, if you want it to run Ubuntu 16.04 or something else you will need to figure out how to do that).
- SSH into your instance using
gcloud. - On your server, check that Git is installed. If not, install Git.
- Install languages required to run your app:
- node and npm (Do the PPA one)
- go
- elm - you're on your own
- Set up and run your app with the
PRODprofile. - Discover the external IP address of your server, and access your app at [IP_ADDRESS]:[PORT].
Domain Name
It's no good asking people to go to 51.42.73.33:3000 to access your app - most won't remember that address. So get a domain name to go with your app:
- Buy a domain name on Amazon/Google (if you really like your app), or get one for free at freenom.com.
- Configure the Domain Name System (DNS) for your domain name. You will need to set up the Name Server (NS) if it not set up, as well as add an A-record to your domain.
- Wait a few seconds for servers to update with the new information. Access your app using your brand new domain name!
Nginx
But wait, it doesn't work. The domain name only points to your IP address, but your app runs on a specific port. When you go to shawty.cf for example, it will redirect you to http://35.196.132.69/, (implicitly, the port is 80, which the port that listens for http connections (https connections are on port 443)). Shawty-the-app runs on port 9001.
So we need something that will stand in the middle, listens for connections to port 80, and decides: "Oh, you are coming in from shawty.cf, that means I need to point you to port 9001 instead, not 80.". Enter Nginx (pronounced engine-x by the way).
- Install
nginxon your server. - Start
nginxwithsudo /etc/init.d/nginx start. This command might not work for everyone, depending on your server's configuration. - Visit your app at the IP address alone (no port). You should see
nginx's welcome page. This is port 80 you're accessing. - Go to
nginx's configuration directory at/etc/nginx/, and briefly read throughnginx.conf. This is the defaultnginxconfiguration - you don't need to understand everything, but you should probably read up on how nginx is configured. At the bottom, notice theincludestatement, which points to the directory/etc/nginx/conf.d. This means all config files in/etc/nginx/conf.dare also read to the main config file. - Add a new file
/etc/nginx/conf.d/[your-domain-name].conf, and write your first nginx configuration file - we will leave this up to you to figure out. - Once you're done, run
sudo /etc/init.d/nginx restartfor your file to take effect. Now visit your app at the domain name you "bought", and voila.
https
This section is under construction, as we don't think anyone will get to this point on Monday :)