How To Migrate a Parse App to Parse Server on Ubuntu 14.04

index.html 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <!DOCTYPE html>
  2. <html lang=en>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>How To Migrate a Parse App to Parse Server on Ubuntu 14.04</title>
  6. <link rel=stylesheet href="fake-it.css" />
  7. </head>
  8. <body>
  9. <p>This is the source for the DigitalOcean guide <a href="https://www.digitalocean.com/community/tutorials/how-to-migrate-a-parse-app-to-parse-server-on-ubuntu-14-04">How To Migrate a Parse App to Parse Server on Ubuntu 14.04</a>, by Brennen Bearnes. It&rsquo;s best read there, with comments.</p>
  10. <p>Copyright © 2016 DigitalOcean™ Inc. This work is licensed under a <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.</p>
  11. <h3>Introduction</h3>
  12. <p>Parse is a Mobile Backend as a Service platform, owned by Facebook since 2013. In January of 2016, Parse announced that its hosted services would shut down completely on January 28, 2017.</p>
  13. <p>Fortunately, Parse has also released <a href="https://github.com/ParsePlatform/parse-server">an open source API server</a>, compatible with the hosted service&rsquo;s API, called <strong>Parse Server</strong>. Parse Server is under active development, and seems likely to attract a large developer community. It can be be deployed to a range of environments running Node.js and MongoDB.</p>
  14. <p>This guide focuses on migrating a pre-existing Parse application to a standalone instance of Parse Server running on Ubuntu 14.04. It uses TLS/SSL encryption for all connections, using a certificate provided by Let&rsquo;s Encrypt, a new Certificate Authority which offers free certificates. It includes a few details specific to DigitalOcean and Ubuntu 14.04, but should be broadly applicable to systems running recent Debian-derived GNU/Linux distributions.</p>
  15. <p>&lt;$>[warning]
  16. <strong>Warning:</strong> It is strongly recommended that this procedure first be tested with a development or test version of the app before attempting it with a user-facing production app. It is also strongly recommended that you read this guide in conjunction with the <a href="https://parse.com/docs/server/guide#migrating">official migration documentation</a>.
  17. &lt;$></p>
  18. <h2>Prerequisites</h2>
  19. <p>This guide builds on <a href="https://www.digitalocean.com/community/tutorials/how-to-run-parse-server-on-ubuntu-14-04">How To Run Parse Server on Ubuntu 14.04</a>. It requires the following:</p>
  20. <ul>
  21. <li>An Ubuntu 14.04 server, configured with a non-root <code>sudo</code> user</li>
  22. <li>Node.js 5.6.x</li>
  23. <li>MongoDB 3.0.x</li>
  24. <li>A domain name pointing at the server</li>
  25. <li>A Parse App to be migrated</li>
  26. </ul>
  27. <p>The target server should have enough storage to handle all of your app&rsquo;s data. Since Parse compresses data on their end, they officially recommend that you provision at least 10 times as much storage space as used by your hosted app.</p>
  28. <h2>Step 1 – Install Let&rsquo;s Encrypt and Retrieve a Certificate</h2>
  29. <p>Let&rsquo;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&rsquo;ll begin by retrieving one with the <code>letsencrypt</code> client.</p>
  30. <h3>Install Let&rsquo;s Encrypt and Dependencies</h3>
  31. <p>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.).</p>
  32. <p>If you haven&rsquo;t already, be sure to create an <strong>A Record</strong> that points your domain to the public IP address of your server. This is required because of how Let&rsquo;s Encrypt validates that you own the domain it is issuing a certificate for. For example, if you want to obtain a certificate for <code>example.com</code>, that domain must resolve to your server for the validation process to work.</p>
  33. <p>For more detail on this process, see <a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-a-host-name-with-digitalocean">How To Set Up a Host Name with DigitalOcean</a> and <a href="https://www.digitalocean.com/community/tutorials/how-to-point-to-digitalocean-nameservers-from-common-domain-registrars">How To Point to DigitalOcean Nameservers from Common Domain Registrars</a>.</p>
  34. <p>Begin by making sure that the <code>git</code> and <code>bc</code> packages are installed:</p>
  35. <pre><code class="command">sudo apt-get -y install git bc
  36. </code></pre>
  37. <p>Next, clone the <code>letsencrypt</code> repository from GitHub to <code>/opt/letsencrypt</code>. The <code>/opt/</code> directory is a standard location for software that&rsquo;s not installed from the distribution&rsquo;s official package repositories:</p>
  38. <pre><code class="command">sudo git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt
  39. </code></pre>
  40. <p>Change to the <code>letsencrypt</code> directory:</p>
  41. <pre><code class="command">cd /opt/letsencrypt
  42. </code></pre>
  43. <h3>Retrieve Initial Certificate</h3>
  44. <p>Run <code>letsencrypt</code> with the Standalone plugin:</p>
  45. <pre><code class="command">./letsencrypt-auto certonly --standalone
  46. </code></pre>
  47. <p>You&rsquo;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&rsquo;ll receive notes much like the following:</p>
  48. <pre><code>IMPORTANT NOTES:
  49. - Congratulations! Your certificate and chain have been saved at
  50. /etc/letsencrypt/live/<span style="color: red;">your_domain_name</span>/fullchain.pem. Your cert will expire
  51. on <span style="color: red;">2016-05-16</span>. To obtain a new version of the certificate in the
  52. future, simply run Let's Encrypt again.
  53. - If you like Let's Encrypt, please consider supporting our work by:
  54. Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
  55. Donating to EFF: https://eff.org/donate-le
  56. </code></pre>
  57. <p>Note the path and expiration date of your certificate, highlighted in the example output. Your certificate files should now be available in <code>/etc/letsencrypt/<span style="color: red;">your_domain_name</span>/</code>.</p>
  58. <h3>Set Up Let&rsquo;s Encrypt Auto Renewal</h3>
  59. <p>&lt;$>[warning]
  60. <strong>Warning:</strong> You can safely complete this guide without worrying about certificate renewal, but you <strong>will</strong> need to address it for any long-lived production environment.
  61. &lt;$></p>
  62. <p>You may have noticed that your Let&rsquo;s Encrypt certificate is due to expire in 90 days. This is a deliberate feature of the Let&rsquo;s Encrypt approach, intended to minimize the amount of time that a compromised certificate can exist in the wild if something goes wrong.</p>
  63. <p>Let&rsquo;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 <a href="https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-14-04">How To Secure Nginx with Let&rsquo;s Encrypt on Ubuntu 14.04</a>, particularly the section on <a href="https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-14-04#step-4-%E2%80%94-set-up-auto-renewal">setting up auto renewal</a>.</p>
  64. <h2>Step 2 – Configure MongoDB for Migration</h2>
  65. <p>Parse provides a migration tool for existing applications. In order to make use of it, we need to open MongoDB to external connections and secure it with a copy of the TLS/SSL certificate from Let&rsquo;s Encrypt. Start by combining <code>fullchain1.pem</code> and <code>privkey1.pem</code> into a new file in <code>/etc/ssl</code>:</p>
  66. <pre><code class="command">sudo cat /etc/letsencrypt/archive/<span style="color: red;">domain_name</span>/{fullchain1.pem,privkey1.pem} | sudo tee /etc/ssl/mongo.pem
  67. </code></pre>
  68. <p>&lt;$>[note]
  69. You will have to repeat the above command after renewing your Let&rsquo;s Encrypt certificate. If you configure auto-renewal of the Let&rsquo;s Encrypt certificate, remember to include this operation.
  70. &lt;$></p>
  71. <p>Make sure <code>mongo.pem</code> is owned by the <strong>mongodb</strong> user, and readable only by its owner:</p>
  72. <pre><code class="command">sudo chown mongodb:mongodb /etc/ssl/mongo.pem
  73. sudo chmod 600 /etc/ssl/mongo.pem
  74. </code></pre>
  75. <p>Now, open <code>/etc/mongod.conf</code> in <code>nano</code> (or your text editor of choice):</p>
  76. <pre><code class="command">sudo nano /etc/mongod.conf
  77. </code></pre>
  78. <p>Here, we&rsquo;ll make several important changes.</p>
  79. <p>First, look for the <code>bindIp</code> line in the <code>net:</code> section, and tell MongoDB to listen on all addresses by changing <code>127.0.0.1</code> to <code>0.0.0.0</code>. Below this, add SSL configuration to the same section:</p>
  80. <pre><code><strong>/etc/mongod.conf</strong><br>
  81. # network interfaces
  82. net:
  83. port: 27017
  84. bindIp: <span style="color: red;">0.0.0.0</span>
  85. <span style="color: red;">ssl:</span>
  86. <span style="color: red;">mode: requireSSL</span>
  87. <span style="color: red;">PEMKeyFile: /etc/ssl/mongo.pem</span>
  88. </code></pre>
  89. <p>Next, under <code># security</code>, enable client authorization:</p>
  90. <pre><code><strong>/etc/mongod.conf</strong><br>
  91. # security
  92. security:
  93. authorization: enabled
  94. </code></pre>
  95. <p>Finally, the migration tool requires us to set the <code>failIndexKeyTooLong</code> parameter to <code>false</code>:</p>
  96. <pre><code><strong>/etc/mongod.conf</strong><br>
  97. setParameter:
  98. failIndexKeyTooLong: false
  99. </code></pre>
  100. <p>&lt;$>[note]
  101. <strong>Note:</strong> Whitespace is significant in MongoDB configuration files, which are based on <a href="http://yaml.org/">YAML</a>. When copying configuration values, make sure that you preserve indentation.
  102. &lt;$></p>
  103. <p>Exit and save the file.</p>
  104. <p>Before restarting the <code>mongod</code> service, we need to add a user with the <code>admin</code> role. Connect to the running MongoDB instance:</p>
  105. <pre><code class="command">mongo --port 27017
  106. </code></pre>
  107. <p>Create an admin user and exit. Be sure to replace &lt;^>sammy&lt;^> with your desired username and &lt;^>password&lt;^> with a strong password.</p>
  108. <pre><code>use admin
  109. db.createUser({
  110. user: "<span style="color: red;">sammy</span>",
  111. pwd: "<span style="color: red;">password</span>",
  112. roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  113. })
  114. exit
  115. </code></pre>
  116. <p>Restart the <code>mongod</code> service:</p>
  117. <pre><code class="command">sudo service mongod restart
  118. </code></pre>
  119. <h2>Step 2 – Migrate Application Data from Parse</h2>
  120. <p>Now that you have a remotely-accessible MongoDB instance, you can use the Parse migration tool to transfer your app&rsquo;s data to your server.</p>
  121. <h3>Configure MongoDB Credentials for Migration Tool</h3>
  122. <p>We&rsquo;ll begin by connecting locally with our new admin user:</p>
  123. <pre><code class="command">mongo --port 27017 --ssl --sslAllowInvalidCertificates --authenticationDatabase admin --username <span style="color: red;">sammy</span> --password
  124. </code></pre>
  125. <p>You should be prompted to enter the password you set earlier.</p>
  126. <p>Once connected, choose a name for the database to store your app&rsquo;s data. For example, if you&rsquo;re migrating an app called Todo, you might use <code>todo</code>. You&rsquo;ll also need to pick another strong password for a user called <strong>parse</strong>.</p>
  127. <p>From the <code>mongo</code> shell, give this user access to <code><span style="color: red;">database_name</span></code>:</p>
  128. <pre><code class="custom_prefix(>)">use <span style="color: red;">database_name</span>
  129. db.createUser({ user: "parse", pwd: "<span style="color: red;">password</span>", roles: [ "readWrite", "dbAdmin" ] })
  130. </code></pre>
  131. <h3>Initiate Data Migration Process</h3>
  132. <p>In a browser window, log in to Parse, and open the settings for your app. Under <strong>General</strong>, locate the <strong>Migrate</strong> button and click it:</p>
  133. <p><img src="https://assets.digitalocean.com/articles/parse_migration/small-000.png" alt="Parse App Settings: General: Migrate" /></p>
  134. <p>You will be prompted for a MongoDB connection string. Use the following format:</p>
  135. <pre><code>mongodb://parse:<span style="color: red;">password</span>@<span style="color: red;">your_domain_name</span>:27017/<span style="color: red;">database_name</span>?ssl=true
  136. </code></pre>
  137. <p>For example, if you are using the domain <code>example.com</code>, with the user <code>parse</code>, the password <code>foo</code>, and a database called <code>todo</code>, your connection string would look like this:</p>
  138. <pre><code>mongodb://parse:foo@example.com:27017/todo?ssl=true
  139. </code></pre>
  140. <p>Don&rsquo;t forget <code>?ssl=true</code> at the end, or the connection will fail. Enter the connection string into the dialog like so:</p>
  141. <p><img src="https://assets.digitalocean.com/articles/parse_migration/small-001.png" alt="Parse App: Migration Dialog" /></p>
  142. <p>Click <strong>Begin the migration</strong>. You should see progress dialogs for copying a snapshot of your Parse hosted database to your server, and then for syncing new data since the snapshot was taken. The duration of this process will depend on the amount of data to be transferred, and may be substantial.</p>
  143. <p><img src="https://assets.digitalocean.com/articles/parse_migration/small-002.png" alt="Parse App: Migration Progress" /></p>
  144. <p><img src="https://assets.digitalocean.com/articles/parse_migration/small-003.png" alt="Parse App: Migration Process" /></p>
  145. <h3>Verify Data Migration</h3>
  146. <p>Once finished, the migration process will enter a verification step. Don&rsquo;t finalize the migration yet. You&rsquo;ll first want to make sure the data has actually transferred, and test a local instance of Parse Server.</p>
  147. <p><img src="https://assets.digitalocean.com/articles/parse_migration/small-004.png" alt="Parse App: Finished Migration, Waiting for Finalization" /></p>
  148. <p>Return to your <code>mongo</code> shell, and examine your local database. Begin by accessing &lt;^>database_name&lt;^> and examining the collections it contains:</p>
  149. <pre><code class="custom_prefix(>)">use <span style="color: red;">database_name</span>
  150. </code></pre>
  151. <pre><code class="custom_prefix(>)">show collections
  152. </code></pre>
  153. <pre><code><span style="color: gray;">Sample Output for Todo App</span><br>
  154. Todo
  155. _Index
  156. _SCHEMA
  157. _Session
  158. _User
  159. _dummy
  160. system.indexes
  161. </code></pre>
  162. <p>You can examine the contents of a specific collection with the <code>.find()</code> method:</p>
  163. <pre><code class="custom_prefix(>)">db.<span style="color: red;">ApplicationName</span>.find()
  164. </code></pre>
  165. <pre><code><span style="color: gray;">Sample Output for Todo App</span><br>
  166. &gt; <span style="color: red;">db.Todo.find()</span>
  167. { "_id" : "hhbrhmBrs0", "order" : NumberLong(1), "_p_user" : "_User$dceklyR50A", "done" : false, "_acl" : { "dceklyR50A" : { "r" : true, "w" : true } }, "_rperm" : [ "dceklyR50A" ], "content" : "Migrate this app to my own server.", "_updated_at" : ISODate("2016-02-08T20:44:26.157Z"), "_wperm" : [ "dceklyR50A" ], "_created_at" : ISODate("2016-02-08T20:44:26.157Z") }
  168. </code></pre>
  169. <p>Your specific output will be different, but you should see data for your app. Once satisfied, exit <code>mongo</code> and return to the shell:</p>
  170. <pre><code class="custom_prefix(>)">exit
  171. </code></pre>
  172. <h2>Step 3 – Install and Configure Parse Server and PM2</h2>
  173. <p>With your app data in MongoDB, we can move on to installing Parse Server itself, and integrating with the rest of the system. We&rsquo;ll give Parse Server a dedicated user, and use a utility called <strong>PM2</strong> to configure it and ensure that it&rsquo;s always running.</p>
  174. <h3>Install Parse Server and PM2 Globally</h3>
  175. <p>Use <code>npm</code> to install the <code>parse-server</code> utility, the <code>pm2</code> process manager, and their dependencies, globally:</p>
  176. <pre><code class="command">sudo npm install -g parse-server pm2
  177. </code></pre>
  178. <h3>Create a Dedicated Parse User and Home Directory</h3>
  179. <p>Instead of running <code>parse-server</code> as <strong>root</strong> or your <code>sudo</code> user, we&rsquo;ll create a system user called <strong>parse</strong>:</p>
  180. <pre><code class="command">sudo useradd --create-home --system parse
  181. </code></pre>
  182. <p>Now set a password for <strong>parse</strong>:</p>
  183. <pre><code class="command">sudo passwd parse
  184. </code></pre>
  185. <p>You&rsquo;ll be prompted to enter a password twice.</p>
  186. <p>Now, use the <code>su</code> command to become the <strong>parse</strong> user:</p>
  187. <pre><code class="command">sudo su parse
  188. </code></pre>
  189. <p>Change to <strong>parse</strong>&rsquo;s home directory:</p>
  190. <pre><code class="custom_prefix(parse\s$)">cd ~
  191. </code></pre>
  192. <h3>Write or Migrate a Cloud Code File</h3>
  193. <p>Create a cloud code directory:</p>
  194. <pre><code class="custom_prefix(parse\s$)">mkdir -p ~/cloud
  195. </code></pre>
  196. <p>Edit <code>/home/parse/cloud/main.js</code>:</p>
  197. <pre><code class="custom_prefix(parse\s$)">nano ~/cloud/main.js
  198. </code></pre>
  199. <p>For testing purposes, you can paste the following:</p>
  200. <pre><code><strong>/home/parse/cloud/main.js</strong><br>
  201. Parse.Cloud.define('hello', function(req, res) {
  202. res.success('Hi');
  203. });
  204. </code></pre>
  205. <p>Alternatively, you can migrate any cloud code defined for your application by copying it from the <strong>Cloud Code</strong> section of your app&rsquo;s settings on the Parse Dashboard.</p>
  206. <p>Exit and save.</p>
  207. <h3>Retrieve Keys and Write /home/parse/ecosystem.json</h3>
  208. <p><a href="http://pm2.keymetrics.io/">PM2</a> is a feature-rich process manager, popular with Node.js developers. We&rsquo;ll use the <code>pm2</code> utility to configure our <code>parse-server</code> instance and keep it running over the long term.</p>
  209. <p>You&rsquo;ll need to retrieve some of the keys for your app. In the Parse dashboard, click on <strong>App Settings</strong> followed by <strong>Security &amp; Keys</strong>:</p>
  210. <p><img src="https://assets.digitalocean.com/articles/parse_migration/small-007.png" alt="Parse Dashboard: App Settings: Security &amp; Keys" /></p>
  211. <p>Of these, only the <strong>Application ID</strong> and <strong>Master Key</strong> are required. Others (client, JavaScript, .NET, and REST API keys) <em>may</em> be necessary to support older client builds, but, if set, will be required in all requests. Unless you have reason to believe otherwise, you should begin by using just the Application ID and Master Key.</p>
  212. <p>With these keys ready to hand, edit a new file called <code>/home/parse/ecosystem.json</code>:</p>
  213. <pre><code class="custom_prefix(parse\s$)">nano ecosystem.json
  214. </code></pre>
  215. <p>Paste the following, changing configuration values to reflect your MongoDB connection string, Application ID, and Master Key:</p>
  216. <pre><code>{
  217. "apps" : [{
  218. "name" : "parse-wrapper",
  219. "script" : "/usr/bin/parse-server",
  220. "watch" : true,
  221. "merge_logs" : true,
  222. "cwd" : "/home/parse",
  223. "env": {
  224. "PARSE_SERVER_CLOUD_CODE_MAIN": "/home/parse/cloud/main.js",
  225. "PARSE_SERVER_DATABASE_URI": "mongodb://<span style="color: red;">parse</span>:<span style="color: red;">password</span>@<span style="color: red;">your_domain_name</span>:27017/<span style="color: red;">database_name</span>?ssl=true",
  226. "PARSE_SERVER_APPLICATION_ID": "<span style="color: red;">your_application_id</span>",
  227. "PARSE_SERVER_MASTER_KEY": "<span style="color: red;">your_master_key</span>",
  228. }
  229. }]
  230. }
  231. </code></pre>
  232. <p>The <code>env</code> object is used to set environment variables. If you need to configure additional keys, <code>parse-server</code> also recognizes the following variables:</p>
  233. <ul>
  234. <li><code>PARSE_SERVER_COLLECTION_PREFIX</code></li>
  235. <li><code>PARSE_SERVER_CLIENT_KEY</code></li>
  236. <li><code>PARSE_SERVER_REST_API_KEY</code></li>
  237. <li><code>PARSE_SERVER_DOTNET_KEY</code></li>
  238. <li><code>PARSE_SERVER_JAVASCRIPT_KEY</code></li>
  239. <li><code>PARSE_SERVER_DOTNET_KEY</code></li>
  240. <li><code>PARSE_SERVER_FILE_KEY</code></li>
  241. <li><code>PARSE_SERVER_FACEBOOK_APP_IDS</code></li>
  242. </ul>
  243. <p>Exit and save <code>ecosystem.json</code>.</p>
  244. <p>Now, run the script with <code>pm2</code>:</p>
  245. <pre><code class="custom_prefix(parse\s$)">pm2 start ecosystem.json
  246. </code></pre>
  247. <pre><code><span style="color: gray;">Sample Output</span><br>
  248. ...
  249. [PM2] Spawning PM2 daemon
  250. [PM2] PM2 Successfully daemonized
  251. [PM2] Process launched
  252. ┌───────────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────────────┬──────────┐
  253. │ App name │ id │ mode │ pid │ status │ restart │ uptime │ memory │ watching │
  254. ├───────────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────────────┼──────────┤
  255. │ parse-wrapper │ 0 │ fork │ 3499 │ online │ 0 │ 0s │ 13.680 MB │ enabled │
  256. └───────────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────────────┴──────────┘
  257. Use `pm2 show &lt;id|name&gt;` to get more details about an app
  258. </code></pre>
  259. <p>Now tell <code>pm2</code> to save this process list:</p>
  260. <pre><code class="custom_prefix(parse\s$)">pm2 save
  261. </code></pre>
  262. <pre><code><span style="color: gray;">Sample Output</span><br>
  263. [PM2] Dumping processes
  264. </code></pre>
  265. <p>The list of processes <code>pm2</code> is running for the <strong>parse</strong> user should now be stored in <code>/home/parse/.pm2</code>.</p>
  266. <p>Now we need to make sure the <code>parse-wrapper</code> process we defined earlier in <code>ecosystem.json</code> is restored each time the server is restarted. Fortunately, <code>pm2</code> can generate and install a script on its own.</p>
  267. <p>Exit to your regular <code>sudo</code> user:</p>
  268. <pre><code class="custom_prefix(parse\s$)">exit
  269. </code></pre>
  270. <p>Tell <code>pm2</code> to install initialization scripts for Ubuntu, to be run as the <strong>parse</strong> user, using <code>/home/parse</code> as its home directory:</p>
  271. <pre><code class="command">sudo pm2 startup ubuntu -u parse --hp /home/parse/
  272. </code></pre>
  273. <pre><code><strong>Output</strong><br>
  274. [PM2] Spawning PM2 daemon
  275. [PM2] PM2 Successfully daemonized
  276. [PM2] Generating system init script in /etc/init.d/pm2-init.sh
  277. [PM2] Making script booting at startup...
  278. [PM2] -ubuntu- Using the command:
  279. su -c "chmod +x /etc/init.d/pm2-init.sh &amp;&amp; update-rc.d pm2-init.sh defaults"
  280. System start/stop links for /etc/init.d/pm2-init.sh already exist.
  281. [PM2] Done.
  282. </code></pre>
  283. <h2>Step 4 – Install and Configure Nginx</h2>
  284. <p>We&rsquo;ll use the Nginx web server to provide a <strong>reverse proxy</strong> to <code>parse-server</code>, so that we can serve the Parse API securely over TLS/SSL.</p>
  285. <p>Install the <code>nginx</code> package:</p>
  286. <pre><code class="command">sudo apt-get install -y nginx
  287. </code></pre>
  288. <p>Open <code>/etc/nginx/sites-enabled/default</code> in <code>nano</code> (or your editor of choice):</p>
  289. <pre><code class="command">sudo nano /etc/nginx/sites-enabled/default
  290. </code></pre>
  291. <p>Replace its contents with the following:</p>
  292. <pre><code><strong>/etc/nginx/sites-enabled/default</strong><br>
  293. # HTTP - redirect all requests to HTTPS
  294. server {
  295. listen 80;
  296. listen [::]:80 default_server ipv6only=on;
  297. return 301 https://$host$request_uri;
  298. }
  299. # HTTPS - serve HTML from /usr/share/nginx/html, proxy requests to /parse/
  300. # through to Parse Server
  301. server {
  302. listen 443;
  303. server_name <span style="color: red;">your_domain_name</span>;
  304. root /usr/share/nginx/html;
  305. index index.html index.htm;
  306. ssl on;
  307. # Use certificate and key provided by Let's Encrypt:
  308. ssl_certificate /etc/letsencrypt/live/<span style="color: red;">your_domain_name</span>/fullchain.pem;
  309. ssl_certificate_key /etc/letsencrypt/live/<span style="color: red;">your_domain_name</span>/privkey.pem;
  310. ssl_session_timeout 5m;
  311. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  312. ssl_prefer_server_ciphers on;
  313. ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
  314. # Pass requests for /parse/ to Parse Server instance at localhost:1337
  315. location /parse/ {
  316. proxy_set_header X-Real-IP $remote_addr;
  317. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  318. proxy_set_header X-NginX-Proxy true;
  319. proxy_pass http://localhost:1337/;
  320. proxy_ssl_session_reuse off;
  321. proxy_set_header Host $http_host;
  322. proxy_redirect off;
  323. }
  324. location / {
  325. try_files $uri $uri/ =404;
  326. }
  327. }
  328. </code></pre>
  329. <p>Exit the editor and save the file. Restart Nginx so that changes take effect:</p>
  330. <pre><code class="command">sudo service nginx restart
  331. </code></pre>
  332. <pre><code><span style="color: gray;">Output</span><br>
  333. * Restarting nginx nginx
  334. ...done.
  335. </code></pre>
  336. <h2>Step 5 – Test Parse Server</h2>
  337. <p>At this stage, you should have the following:</p>
  338. <ul>
  339. <li>A TLS/SSL certificate, provided by Let&rsquo;s Encrypt</li>
  340. <li>MongoDB, secured with the Let&rsquo;s Encrypt certificate</li>
  341. <li><code>parse-server</code> running under the <strong>parse</strong> user on port 1337, configured with the keys expected by your app</li>
  342. <li><code>pm2</code> managing the <code>parse-server</code> process under the <strong>parse</strong> user, and a startup script to restart <code>pm2</code> on boot</li>
  343. <li><code>nginx</code>, secured with the Let&rsquo;s Encrypt certificate, and configured to proxy connections to <code>https://<span style="color: red;">your_domain_name</span>/parse</code> to the <code>parse-server</code> instance</li>
  344. </ul>
  345. <p>It should now be possible to test reads, writes, and cloud code execution using <code>curl</code>.</p>
  346. <p>&lt;$>[note]
  347. <strong>Note:</strong> The <code>curl</code> commands in this section should be harmless when used with a test or development app. Be cautious when writing data to a production app.
  348. &lt;$></p>
  349. <h3>Writing Data with a POST</h3>
  350. <p>You&rsquo;ll need to give <code>curl</code> several important options:</p>
  351. <table>
  352. <thead>
  353. <tr>
  354. <th> Option </th>
  355. <th> Description </th>
  356. </tr>
  357. </thead>
  358. <tbody>
  359. <tr>
  360. <td> <code>-X POST</code> </td>
  361. <td> Sets the request type, which would otherwise default to <code>GET</code> </td>
  362. </tr>
  363. <tr>
  364. <td> <code>-H "X-Parse-Application-Id: <span style="color: red;">your_application_id</span>"</code> </td>
  365. <td> Sends a header which identifies your application to <code>parse-server</code> </td>
  366. </tr>
  367. <tr>
  368. <td> <code>-H "Content-Type: application/json"</code> </td>
  369. <td> Sends a header which lets <code>parse-server</code> know to expect JSON-formatted data </td>
  370. </tr>
  371. <tr>
  372. <td> <code>-d '{<span style="color: red;">json_data</span>}</code> </td>
  373. <td> Sends the data itself </td>
  374. </tr>
  375. </tbody>
  376. </table>
  377. <p>Putting these all together, we get:</p>
  378. <pre><code>curl -X POST \
  379. -H "X-Parse-Application-Id: <span style="color: red;">your_application_id</span>" \
  380. -H "Content-Type: application/json" \
  381. -d '{"score":1337,"playerName":"Sammy","cheatMode":false}' \
  382. https://<span style="color: red;">your_domain_name</span>/parse/classes/GameScore
  383. </code></pre>
  384. <pre><code><strong>Sample Output</strong><br>
  385. {"objectId":"YpxFdzox3u","createdAt":"2016-02-18T18:03:43.188Z"}
  386. </code></pre>
  387. <h3>Reading Data with a GET</h3>
  388. <p>Since <code>curl</code> sends GET requests by default, and we&rsquo;re not supplying any data, you should only need to send the Application ID in order to read some sample data back:</p>
  389. <pre><code class="command">curl -H "X-Parse-Application-Id: <span style="color: red;">your_application_id</span>" https://<span style="color: red;">your_domain_name</span>/parse/classes/GameScore
  390. </code></pre>
  391. <pre><code><strong>Sample Output</strong><br>
  392. {"results":[{"objectId":"BNGLzgF6KB","score":1337,"playerName":"Sammy","cheatMode":false,"updatedAt":"2016-02-17T20:53:59.947Z","createdAt":"2016-02-17T20:53:59.947Z"},{"objectId":"0l1yE3ivB6","score":1337,"playerName":"Sean Plott","cheatMode":false,"updatedAt":"2016-02-18T03:57:00.932Z","createdAt":"2016-02-18T03:57:00.932Z"},{"objectId":"aKgvFqDkXh","score":1337,"playerName":"Sean Plott","cheatMode":false,"updatedAt":"2016-02-18T04:44:01.275Z","createdAt":"2016-02-18T04:44:01.275Z"},{"objectId":"zCKTgKzCRH","score":1337,"playerName":"Sean Plott","cheatMode":false,"updatedAt":"2016-02-18T16:56:51.245Z","createdAt":"2016-02-18T16:56:51.245Z"},{"objectId":"YpxFdzox3u","score":1337,"playerName":"Sean Plott","cheatMode":false,"updatedAt":"2016-02-18T18:03:43.188Z","createdAt":"2016-02-18T18:03:43.188Z"}]}
  393. </code></pre>
  394. <h3>Executing Example Cloud Code</h3>
  395. <p>A simple POST with no real data to <code>https://<span style="color: red;">your_domain_name</span>/parse/functions/hello</code> will run the <code>hello()</code> function defined in <code>/home/parse/cloud/main.js</code>:</p>
  396. <pre><code>curl -X POST \
  397. -H "X-Parse-Application-Id: <span style="color: red;">your_application_id</span>" \
  398. -H "Content-Type: application/json" \
  399. -d '{}' \
  400. https://<span style="color: red;">your_domain_name</span>/parse/functions/hello
  401. </code></pre>
  402. <pre><code><strong>Sample Output</strong><br>
  403. {"result":"Hi"}
  404. </code></pre>
  405. <p>If you have instead migrated your own custom cloud code, you can test with a known function from <code>main.js</code>.</p>
  406. <h2>Step 6 – Configure Your App for Parse Server and Finalize Migration</h2>
  407. <p>Your next step will be to change your client application itself to use the Parse Server API endpoint. Consult the <a href="https://github.com/ParsePlatform/parse-server/wiki/Parse-Server-Guide#using-parse-sdks-with-parse-server">official documentation</a> on using Parse SDKs with Parse Server. You will need the latest version of the SDK for your platform. As with the <code>curl</code>-based tests above, use this string for the server URL:</p>
  408. <pre><code>https://<span style="color: red;">your_domain_name</span>/parse
  409. </code></pre>
  410. <p>Return to the Parse dashboard in your browser and the <strong>Migration</strong> tab:</p>
  411. <p><img src="https://assets.digitalocean.com/articles/parse_migration/small-004.png" alt="Parse App: Migration Process" /></p>
  412. <p>Click the <strong>Finalize</strong> button:</p>
  413. <p><img src="https://assets.digitalocean.com/articles/parse_migration/small-005.png" alt="Parse Migration Finalization Dialog" /></p>
  414. <p>Your app should now be migrated.</p>
  415. <h2>Conclusion and Next Steps</h2>
  416. <p>This guide offers a functional starting point for migrating a Parse-hosted app to a Parse Server install on a single Ubuntu system, such as a DigitalOcean droplet. The configuration we&rsquo;ve described should be adequate for a low-traffic app with a modest userbase. Hosting for a larger app may require multiple systems to provide redundant data storage and load balancing between API endpoints. Even small projects are likely to involve infrastructure considerations that we haven&rsquo;t directly addressed.</p>
  417. <p>In addition to reading the official Parse Server documentation and tracking the <a href="https://github.com/ParsePlatform/parse-server/issues">GitHub issues for the project</a> when troubleshooting, you may wish to explore the following topics:</p>
  418. <ul>
  419. <li><a href="https://www.digitalocean.com/community/tutorial_series/new-ubuntu-14-04-server-checklist">The DigitalOcean New Ubuntu 14.04 Server Checklist</a></li>
  420. <li><a href="https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-14-04">Securing Nginx with Let&rsquo;s Encrypt on Ubuntu 14.04</a></li>
  421. <li><a href="https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-an-ubuntu-14-04-server">Installing Node.js on Ubuntu 14.04</a></li>
  422. <li><a href="https://www.digitalocean.com/community/tutorials/understanding-digitalocean-droplet-backups">DigitalOcean Droplet Backups</a></li>
  423. </ul>
  424. </body>
  425. </html>