How Deploy Photogram Parse Server in Digital Ocean

Willian Ribeiro Angelo
Photogram
Published in
13 min readJan 2, 2017

Recently Parse announced they’re shutting down their mobile backend as a service. If you’ve built an app on top of Parse, this is probably bad news. The good news is that they’re giving developers 12 months to find a new solution, and they’re providing a path to running your own Parse-compatible service.

There’s a great migration guide that covers exporting Parse data to MongoDB, and running a Parse server built on Node.js and express.

This guide is going to cover how to install all dependencies that we need to run Parse Server on own server. We are going to use DigitalOcean to host our server, if you don’t have an account you can get one here and obtain $10 for free ;)

Receiving Github updates from Photogram

To receive fixes and enhancements to the Photogram Parse Server github, you must make the repository clone that is on your bitbucket for your local machine, then add the official Photogram Parse Server github repository and enter the git pull command to receive , Let’s look at the example below:

$ git clone your_server_bitbucket server
$ cd server
$ git remote add github https://github.com/photogram/server.git
$ git pull github master

I recommend you use the SourceTree program to check the changes and merge the files, if there is any conflict, this link has a tutorial on how you merge with git

Configure your server

Open the ecosystem.json file, and edit your server settings, then send the changes to bitbucket

There are some pre-defined settings to start Photogram Parse Server, but some items you need to edit at the time of setting up for your application.

From line 35, you can see that the Parse Dashboard has also been included, in which you can set the address, login and password to access after the service is started.

Once you’ve finalized your local changes, commit and submit the updates to your repository on Bitbucket

$ git add .
$ git commit -am "My changes"
$ git push origin master

Setup your Photogram Parse Server in Digital Ocean

DigitalOcean calls its virtual private servers Droplets; each Droplet that you spin up is a new VPS for your personal use.

The setup is very easy — the entire process can take as little as a minute! This tutorial will walk you through creating and accessing your new server.

Step One — Log In

To create your first Droplet go to the DigitalOcean Control Panel and log in with your email and password. The create button will be right there on the first page: click on “Create Droplet”:

Step Two — Select Droplet Image

You can create your Droplet image from 4 possible categories:

  • Distributions: Create from several operating systems, such as Ubuntu, Debian, CoreOS, and CentOS. After selecting your distribution, be sure to select the image of your choice (specifying the version and 64 bit or 32 bit).
  • One-click Apps: Create from images that have pre-installed and configured programs that will get your Droplet off to a strong start.
  • Snapshots: Create from a snapshot that you previously made, letting you create backup copies or scale quickly.
  • Backups: Create from a previously automatically generated backup, the option that you can enable on each Droplet individually with the “Backups” button.

32-bit vs. 64-bit Systems

A 32-bit operating system is recommended for cloud servers with less than 3 GB of RAM — this is especially true for servers with 1 GB, or less, of RAM. Processes can require significantly more memory on the 64-bit architecture. On servers with a limited amount of RAM, any performance benefits that one might gain from a 64-bit operating system would be diluted by having less memory available for buffers and caching.

Step Three — Select Your Droplet’s Size

Depending on your needs and budget, you can select the Droplet option that works best for you.

There is a wide spectrum for prices, power, and storage capacity. The smallest and least expensive option starts at 512MB of RAM with 1 CPU and 20GB of SSD storage. The size options grow larger from there, all the way up to 64GB of RAM with 20 CPUs and 640GB of SSD storage. Should your needs change at a future point, you can adjust your Droplet’s plan using the flexible and permanent resize options.

Step Four — Select Your Droplet Region

You may choose the most effective region for your Droplet location. Although equally powerful, the best region to choose is the one nearest to you and your customers or other possible users. Selecting a more distant server location may increase your server latency without serving any practical purpose.

Step Five — Select Additional Options

The Select additional options section allows you to select which features you would like your Droplet to have:

  • Private Networking: Enables a private networking interface, in addition to the default public interface, that can only be accessed via the private network of other Droplets within the same datacenter.
  • Backups: Enables automatic backups of the Droplet — for more information about the backup service, click here
  • IPv6: Enables IPv6 access for your Droplet.
  • User Data: Enables you to pass arbitrary data into the user-data key of the DigitalOcean Metadata service. This setting is required for CoreOS Droplets. To read more about user data, check out the tutorial on Droplet Metadata.

Select any of the settings you would like to enable. The Private Networking feature is very useful if you have multiple Droplets in the same datacenter that communicate with each other.

Step Six — Select SSH Keys (Optional)

Optional: Select which SSH keys you would like to add to your new Droplet.

It is recommended that you set up SSH keys to authenticate to your Droplets because it provides better security than a basic password. For more information about setting up SSH keys with your DigitalOcean Droplets, refer to this tutorial.

Step Seven — Select the Number and Names of the Droplets to Create

Next, you can choose the number and names of the Droplets you wish to create. Depending on the number of Droplets currently in your account, you can create up to five Droplets that will use the configuration that you have selected. By default, a single Droplet is set to be created. You can adjust the number of Droplets to create be clicking the plus or minus buttons.

Each Droplet must have a name. This name will be used in the DigitalOcean control panel and as the server’s hostname. A default name will be provided for your Droplet(s) based on the options you have selected, but you can modify the name(s) to best suit your needs. You may want to use a Fully Qualified Domain Name (e.g. droplet1.example.com).

Step Eight — Create Your Droplet

Once you have selected all of your preferred options, click on “Create”.

After your Droplet is created, its root password will arrive in your email inbox and the Droplet will be set up. If you included an SSH key in the previous steps, you will not be emailed a root password — use your SSH private key to authenticate as the root user instead.

With that, your server is ready!

Step Nine — Log In To Your Droplet

The process is slightly different for Mac and Windows Computers:

How to Log In With a Mac

  1. To log in from a computer running OS X, open the terminal program (in the utilities folder) and type in the following command. In the command use the IP address found in the DigitalOcean control panel: ssh root@droplet_ip_address
  2. Type “yes” if the prompt asks if you would like to connect to the host.
  3. If you did not use SSH keys, when prompted, type in the root password that was emailed to you and press ENTER. Although the password is entered when you type, it does not show up on the screen for security reasons.

You will then be connected to your DigitalOcean Droplet.

How to Log In With Windows

To log in to your Droplet on Windows, you will need to have PuTTY, an SSH client, installed on your computer.

  1. You can download the program here. Choose the Windows installer.
  2. Once PuTTY is downloaded and installed, starting the program will take you to the configuration screen.
  3. Fill in the “Host Name (or IP address)” field with the Droplet IP address from the DigitalOcean control panel. Make sure that the port number is 22 and that the connection type is SSH.
  4. Additionally, click on the “SSH” sidebar entry and select “2 only” as the preferred SSH protocol version.
  5. Once everything is configured, you can save these settings for future logins by clicking on the “Session” item in the side bar and entering a title into the “Saved Sessions” field. Click save to store your settings.
  6. Double-click on the session name to connect. Accept the subsequent pop up to confirm that you wish to connect to the host. After PuTTY starts up, type in the root password that was emailed to you.

You will then be connected to your DigitalOcean Droplet.

Install Git, Zsh Terminal, MongoDB and Nginx

To make it easier to install, I’ve created a script that automatically installs Zsh Terminal to make it easier to use the terminal in Ubuntu, MongoDB to be our Parse Server database and Nginx to route our application

$ curl -o- https://gist.githubusercontent.com/movibe/6b640e6c7003038df1e665f11bca3f2d/raw/15bb48ae84c3f246a54f3135b0a0cf9e718a2e15/photogram-server.bash | bash

Now, exit and enter the terminal again.

HTTPS Free: Install Let’s Encrypt and Retrieve a Certificate

Let’s Encrypt is a new Certificate Authority that provides an easy way to obtain free TLS/SSL certificates. Because a certificate is necessary to secure both the migration of data to MongoDB and your Parse Server API endpoint, we’ll begin by retrieving one with the letsencrypt client.

Install Let’s Encrypt and Dependencies

You must own or control the registered domain name that you wish to use the certificate with. If you do not already have a registered domain name, you may register one with one of the many domain name registrars out there (e.g. Namecheap, GoDaddy, etc.).

If you haven’t already, be sure to create an A Record that points your domain to the public IP address of your server. This is required because of how Let’s Encrypt validates that you own the domain it is issuing a certificate for. For example, if you want to obtain a certificate for example.com, that domain must resolve to your server for the validation process to work.

For more detail on this process, see How To Set Up a Host Name with DigitalOcean and How To Point to DigitalOcean Nameservers from Common Domain Registrars.

Begin by making sure that the git and bc packages are installed:

$ sudo apt-get -y install git bc

Next, clone the letsencrypt repository from GitHub to /opt/letsencrypt. The /opt/ directory is a standard location for software that's not installed from the distribution's official package repositories:

$ sudo git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt

Change to the letsencrypt directory:

$ cd /opt/letsencrypt

Retrieve Initial Certificate

Run letsencrypt with the Standalone plugin:

$ ./letsencrypt-auto certonly --standalone

You’ll be prompted to answer several questions, including your email address, agreement to a Terms of Service, and the domain name(s) for the certificate. Once finished, you’ll receive notes much like the following:

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/your_domain_name/fullchain.pem. Your cert will expire
on 2016-05-16. To obtain a new version of the certificate in the
future, simply run Let's Encrypt again.
- If you like Let's Encrypt, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

Note the path and expiration date of your certificate, highlighted in the example output. Your certificate files should now be available in /etc/letsencrypt/your_domain_name/.

Set Up Let’s Encrypt Auto Renewal

Warning: You can safely complete this guide without worrying about certificate renewal, but you will need to address it for any long-lived production environment.

You may have noticed that your Let’s Encrypt certificate is due to expire in 90 days. This is a deliberate feature of the Let’s Encrypt approach, intended to minimize the amount of time that a compromised certificate can exist in the wild if something goes wrong.

Let’s Encrypt is still in beta. Better auto-renewal features are planned, but in the meanwhile you will either have to repeat the certificate retrieval process by hand, or use a scheduled script to handle it for you. The details of automating this process are covered in How To Secure Nginx with Let’s Encrypt on Ubuntu 14.04, particularly the section on setting up auto renewal.

Setting Up MongoDB

Ok, now we have MongoDB installed on our server, but we have to config it with a couple of parameters and need a user to access to MongoDB remotely.

The first thing that we are going to do is allow remotely connections to MongoDB. It is because we need to import Parse Data from theirs servers and Parse will be using this DB every time that an old user send information to their servers.

$ vi /etc/mongod.conf

And comment next line, To comment a line you have to put `#` at the begin of the line.

# bindIp: 127.0.0.1

Then we only have to restart mongo daemon and it will allow remote connections.

$ sudo service mongod restart

Now we are going to create a new database, new user and disable an option that Parse need. The user is going to be `parseuser` and password `password`. You must change it.

$ mongo
> use parse
> db.createUser({ user: "parseuser", pwd: "password", roles: [ { role: "userAdmin", db: "parse" } ] })
> db.getSiblingDB('admin').runCommand( { setParameter: 1, failIndexKeyTooLong: false } )

To go out from `mongo shell` we have to press `ctrl+d`.
We have our MongoDB configured and we can start to import data from Parse. The Uri that Parse ask us is:

mongodb://parseuser:password@IP_SERVER:27017/parse

Configuring your server Environments

PM2 is a NodeJS process manager that helps you monitor the execution of the Parse Server, as well as making custom configurations through your JSON configuration file.

Inside the folder of photogram-server that we cloned in the previous step, there is a file called ecosystem.json, open it in Visual Studio Code or WebStorm to view basic and advanced settings you can do before you start Server.

Change the information in the ecosyste.json file locally, including your DATABASE_URI address we generated in the installation of the previous MongoDB step, after commit locally and then send it to the bi…

mongodb://parseuser:password@IP_SERVER:27017/parse

Install NodeJS with NVM

Parse Server works over NodeJS 4.1 and, like MongoDB, NodeJS is already included in Ubuntu package repositories but it is not the version that we need. We are going to download and install the version that we need.

$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash

Now exit and enter the terminal again to install NodeJS

$ nvm install 6.9.2
$ nvm alias default 6.9.2
$ node -v

Install PM2

$ npm install -g pm2

Configure and install yourPhotogram Server

Now we will need to clone our Photogram Parse Server repository from Bitbucket to our Droplet, if you have not already set up on Bitbucket, just follow this introductory tutorial

Now we are going to download the project and configure it

$ git clone your_bitbucket_parse_server server
$ cd server
$ npm install

Starting the Server with PM2

$ pm2 start ecosystem.json

After you type this command, you will receive a similar image on your terminal (depending on your OS):

It means that the ecosystem.json file has been successfully validated and the Parsi-server application started with the status online

To make sure everything is working let’s type the following command

$ pm2 logs

If you receive a message Parse LiveQuery Server starts running on your terminal, it means that your server is already running!

Install and Configure Nginx with HTTPS free

We’ll use the Nginx web server to provide a reverse proxy to parse-server, so that we can serve the Parse API securely over TLS/SSL.

Install the nginx package:

$ sudo apt-get install -y nginx

Open /etc/nginx/sites-enabled/default in nano (or your editor of choice):

$ sudo nano /etc/nginx/sites-enabled/default

Replace its contents with the following: /etc/nginx/sites-enabled/default

# HTTP - redirect all requests to HTTPS
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
return 301 https://$host$request_uri;
}
# HTTPS - serve HTML from /usr/share/nginx/html, proxy requests to /parse/
# through to Parse Server
server {
listen 443 ssl;
server_name posernetwork.com ;
root /usr/share/nginx/html;
index index.html index.htm;
ssl on;
# Use certificate and key provided by Let's Encrypt:
ssl_certificate /etc/letsencrypt/live/posernetwork.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/posernetwork.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# Pass requests for /parse/ to Parse Server instance at localhost:1337
location / {
# prevents 502 bad gateway error
proxy_buffers 8 32k;
proxy_buffer_size 64k;
# redirect all HTTP traffic to localhost:8088;

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://0.0.0.0:1337/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_redirect off;
# enables WS support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 999999999;
}
}

Exit the editor and save the file. Restart Nginx so that changes take effect:

$ sudo service nginx restart

Test Parse Server

Access the address of your server and verify that it is working, if you have a problem, repeat the configuration or comment so I can help you

https://your_domain_name/

References

--

--