Deploying Sails App to Digital Ocean Droplet with HTTPS

Noitidart
4 min readOct 12, 2019

--

THIS ARTICLE IS NOT YET COMPLETE I HAVE A TODO ON THE WEBPACK SECTION

I am using Sails version 1.2.3 for this article. Digital Ocean is short-formed to DO in this article.

This article will use Digital Ocean’s managed PostgreSQL and Redis services. (I will write an article in future on how to deploy without using them, in that article I will use docker-compose to run PostgreSQL and Redis locally within the container — however it is strongly recommended to use managed databases)

  1. Create Docker Hub account. (screenshots coming soon)
  2. Create a container repository where we can push our app to. Let’s call the repository the same name as our app, let’s call it “my-sails-do-app”. (screenshots coming soon)
  3. Install docker on your computer (login to docker with your Docker Hub account).
  4. sails new my-sails-do-app
  5. Install webpack because we want to build assets once when we create the Docker container, and then once the container is on the server, it should just run it. (TODO: )
  6. Install npm i sails-postgresql
  7. On DigitalOcean create Droplet using whatever Ubuntu is available, get it’s “Floating IP”. (mine is 134.209.5.127) (screenshots coming soon)
  8. On DigitalOcean create managed PostgreSQL instance and get the credentials. (screenshots coming soon)
  9. Within this managed PostgreSQL instance let’s create a database called “my-sails-data”. (screenshots coming soon)
  10. On DigitalOcean create managed Redis instance and get the credentials. (screenshots coming soon)
  11. On GoDaddy, buy a domain (we cannot setup SSL (https) on just Droplet’s Floating IP. Enter the Floating IP of your Droplet as the A name on GoDaddy. I bought hihihihihi.com (screenshots coming soon)
  12. In config/env/production.js let’s update the settings:
  • Set custom.baseUrl to https://hihihihihi.com
  • Set http.trustProxy to true
  • Set cookie.secure to true
  • Set Redis details to the managed DigitalOcean Redis details. Set session.pass should be password from DO’s Redis instance you created. Set session.host it should look something like redis-foo-foo-user-123123–0.db.ondigitalocean.com. Set session.port to what it is on the DO, it is probably 25061. Set session.tls to an empty object {} (this is because managed DO Redis uses the secure rediss protocol). Set session.db to a number of the database you want to use. Each DO managed Redis allows up to 16 databases, we have not used any yet, so let’s go ahead and put 0.
  • Set PostgreSQL details to the managed DigitalOcean PostgreSQL details. Set datastores.default.user to the do-user (this is the default DO user for managed instances, but you can create another user if you want). Set the datastores.default.port, it is probably 25060. Set datastores.default.database to the name of the database you created (we used "my-sails-data"). Set datastores.default.password to what DO gave you. Set datastores.default.host (it looks something like foo-foo-foo-12345–0.db.ondigitalocean.com). The set datastores.default.ssl to true as DigitialOcean uses the secure protocol.
  • Set sockets.onlyAllowOrigns to ['https://hihihihihi.com']

13. Create a file called Dockerfile in root of your project directory and set it to this:

FROM node:12-alpineARG WORKDIR=/opt/apps/my-sails-do-app/RUN mkdir -p $WORKDIRWORKDIR $WORKDIRCOPY package.json package-lock.json $WORKDIRRUN NODE_ENV=production npm iCOPY . $WORKDIRRUN NODE_ENV=production npx webpack --config webpack.config.jsEXPOSE 1337CMD npm start

14. Build the production container docker build . -t noitidart/my-sails-do-app:latest (be sure to use your username) (we can use whatever tag you want, I’m going to use “latest” and just keep overwriting it in future builds)

15. docker login with your Docker Hub details.

16. Push up the container to Docker Hub with docker push noitidart/my-sails-do-app:latest

17. Connect to your Droplet ssh root@134.209.5.127

18. Install Docker curl -L https://get.docker.com/ > getdocker.sh then chmod +x getdocker.sh then sudo ./getdocker.sh

19. Login to Docker Hub on the instance with docker login with your details.

20. Pull the container docker pull noitidart/my-sails-do-app:latest

21. Run the container so that it restarts whenever the Sails app crashes, and whenever the Droplet is restarted by DigitalOcean: docker run -p 1337:1337 -d --restart=always noitidart/my-sails-do-app:latest --name my-sails-do-app

Note: In future if you want to re-run this. You will have to first stop this container by doing docker stop my-sails-do-app && docker rm my-sails-do-app

22. Wait for the Sails to lift, to see what is going on in the container type docker logs -f my-sails-do-app

23. Install nginx with sudo apt-get update then sudo apt-get install nginx

24. Create nginx config file vim /etc/nginx/sites-available/hihihihihi.com and fill it with:

server {
listen 80;
server_name hihihihihi.com www.hihihihihi.com;
location / {
proxy_pass http://localhost:1337;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

25. Run sudo ln -s /etc/nginx/sites-available/themasjid.app /etc/nginx/sites-enabled

26. Run sudo nginx -t to make sure our config file we wrote has no typos, if not, correct typos. It should say:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

27. Reload nginx with sudo service nginx reload

28. Follow the steps here to setup certbot to add into our config file above HTTPS stuff — https://certbot.eff.org/lets-encrypt/ubuntubionic-nginx

  • For step 4, where it asks “ Choose how you’d like to run Certbot “ do the “ Either get and install your certificates… “ option which is sudo certbot --nginx
  • It will then ask you to enter numbers for the sites you want, I entered 1,2 to get themasjid.app and www.themasjid.app then hit enter
  • Then for if want to redirect http to https I said yes so I entered 2 then hit enter

29. Now go to https://hihihihihi.com and see your Sails app with SSL!

--

--