<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Duncan Bain</title>
    <description>The latest articles on DEV Community by Duncan Bain (@duncanbain).</description>
    <link>https://dev.to/duncanbain</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F189063%2F59e3c1d5-d3cc-49e3-a123-51af387ed9ec.jpeg</url>
      <title>DEV Community: Duncan Bain</title>
      <link>https://dev.to/duncanbain</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/duncanbain"/>
    <language>en</language>
    <item>
      <title>Quickly Deploy your GatsbyJS Site onto a DigitalOcean Droplet</title>
      <dc:creator>Duncan Bain</dc:creator>
      <pubDate>Tue, 09 Jun 2020 12:08:31 +0000</pubDate>
      <link>https://dev.to/duncanbain/quickly-deploy-your-gatsbyjs-site-onto-a-digitalocean-droplet-4ha6</link>
      <guid>https://dev.to/duncanbain/quickly-deploy-your-gatsbyjs-site-onto-a-digitalocean-droplet-4ha6</guid>
      <description>&lt;p&gt;I'm a big fan of both DigitalOcean and Gatsby. I've used DigitalOcean for several years for various hosting services, and now that I'm learning web development Gatsby&lt;br&gt;
is what I've used for my first personal website and portfolio.&lt;/p&gt;

&lt;p&gt;I've written this guide to show the steps required in order to deploy your own &lt;a href="https://gatsbyjs.org"&gt;Gatsby&lt;/a&gt; site onto a DigitalOcean droplet.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is GatsbyJS?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://gatsbyjs.org"&gt;Gatsby&lt;/a&gt; is a React based &lt;a href="https://graphql.org/learn/"&gt;GraphQL&lt;/a&gt; powered static site generator. Gatsby can be used to create highly performant sites, with things like service workers,&lt;br&gt;
image optimisation and intelligent data fetching available straight out of the box.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is DigitalOcean?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://digitalocean.com"&gt;DigitalOcean&lt;/a&gt; is an Infrastructure as a service (IaaS) provider who provide several easy to deploy services, such as virtual private servers which are also known as Droplets, Kubernetes clusters and managed databases.&lt;/p&gt;

&lt;p&gt;DigitalOcean Droplets are Linux-based VPS' which can be used as standalone servers, or used as part of a wider cloud based infrastructure. Droplets start at $5 USD per month and you can create and deploy these in less than a minute.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;This tutorial assumes that you have the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Your Gatsby website available on a Git repository.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A DigitalOcean Droplet running Ubuntu Linux 20.04 LTS. You will also need a non-root user account created which has sudo privileges. DigitalOcean provides excellent tutorials on &lt;a href="https://www.digitalocean.com/docs/droplets/how-to/create/"&gt;Droplet creation&lt;/a&gt; and also on &lt;a href="https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-20-04"&gt;setting up Ubuntu 20.04&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A custom domain to utilise LetsEncrypt and provide HTTPS encryption for your Gatsby website.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Deployment
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Installing NodeJS and Gatsby CLI
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Installing NodeJS
&lt;/h4&gt;

&lt;p&gt;Before starting, you should ensure that you have the latest versions of your installed system packages. Run the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt upgrade
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To have a more up to date version of NodeJS than what is generally available in the official Ubuntu repositories, you can add a Personal Package Archive (PPA) which the NodeJS team maintain.&lt;/p&gt;

&lt;p&gt;In this tutorial you will be installing the Long Term Support (LTS) version which is currently v12.x.x. Do the following to add the NodeJS PPA:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ~
curl &lt;span class="nt"&gt;-sL&lt;/span&gt; https://deb.nodesource.com/setup_12.x &lt;span class="nt"&gt;-o&lt;/span&gt; nodesource_setup.sh
&lt;span class="nb"&gt;sudo &lt;/span&gt;language-bash nodesource_setup.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will then add the PPA to your package manager and make the latest NodeJS LTS version available for installation. You can now install NodeJS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;nodejs
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now verify that you have installed the NodeJS package and that it is the correct version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;-v&lt;/span&gt;

v12.17.0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To upgrade npm (NodeJS's package manager) to the latest version run the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; npm@latest
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Sometimes some npm packages need compiling from source, so you will need to install the build-essential package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;build-essential
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Installing Gatsby CLI
&lt;/h4&gt;

&lt;p&gt;The next step is to now install the Gatsby CLI package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-g&lt;/span&gt; gatsby-cli
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And now you can check that the Gatsby CLI package was installed correctly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;gatsby &lt;span class="nt"&gt;-v&lt;/span&gt;

╔════════════════════════════════════════════════════════════════════════╗
║                                                                        ║
║   Gatsby collects anonymous usage analytics                            ║
║   to &lt;span class="nb"&gt;help &lt;/span&gt;improve Gatsby &lt;span class="k"&gt;for &lt;/span&gt;all users.                                ║
║                                                                        ║
║   If you&lt;span class="s1"&gt;'d like to opt-out, you can use `gatsby telemetry --disable`   ║
║   To learn more, checkout https://gatsby.dev/telemetry                 ║
║                                                                        ║
╚════════════════════════════════════════════════════════════════════════╝
Gatsby CLI version: 2.12.40
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  Cloning and Building Your Gatsby Site
&lt;/h4&gt;

&lt;p&gt;To build your Gatsby site and make it ready for deploying you need to clone your Gatsby site repository to your Droplet. Run the following, where &lt;code&gt;&amp;lt;your-git-url&amp;gt;&lt;/code&gt; is your Gatsby site's Git repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone &amp;lt;your-git-url&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The directory where you cloned your Gatsby site will be referred to as &lt;code&gt;&amp;lt;your-gatsby-site&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can now enter your Gatsby site directory and install the sites' dependencies. Do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; &amp;lt;your-gatsby-site&amp;gt;
npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can now then build your Gatsby site:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;gatsby build
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Installing Nginx and Configuring LetsEncrypt
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Installing Nginx
&lt;/h4&gt;

&lt;p&gt;In order to serve your Gatsby website you need a webserver. One of the most widely used web servers for serving static content is &lt;a href="https://www.nginx.com/"&gt;Nginx&lt;/a&gt;. Nginx is a lightweight high performance web server, perfectly suited for serving static sites such as Gatsby.&lt;/p&gt;

&lt;p&gt;Install Nginx:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you have set up &lt;a href="https://help.ubuntu.com/community/UFW"&gt;UFW&lt;/a&gt; then you will need to allow access to both HTTP and HTTPS from the outside world. Do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;ufw allow &lt;span class="s1"&gt;'Nginx HTTP'&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;ufw allow &lt;span class="s1"&gt;'Nginx HTTPS'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should now be able to access your server by visiting &lt;code&gt;http://&amp;lt;your-server-ip&amp;gt;/&lt;/code&gt; using your browser, you should see a Nginx placeholder page if everything is working correctly.&lt;/p&gt;

&lt;p&gt;Now you need to let Nginx know where your Gatsby site files are. Nginx is capable of hosting more than one site at a time, this is done by creating '&lt;a href="https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/"&gt;server blocks&lt;/a&gt;' for each site you wish to host with each server block containing its own configuration.&lt;/p&gt;

&lt;p&gt;Although you're only setting up one site at the moment configuring things this way will make it easier to add sites at a later date if you wish.&lt;/p&gt;

&lt;p&gt;In order to create a server for your custom domain, copy the default provided by Nginx:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo cp&lt;/span&gt; /etc/nginx/sites-available/default /etc/nginx/sites-available/&amp;lt;your-custom-domain&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you can edit the server block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/nginx/sites-available/&amp;lt;your-custom-domain&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;First you need to look at the listen directives. You can have &lt;strong&gt;only one server block with the default_server option enabled&lt;/strong&gt;. This option specifies which site to serve if the requested site does not match any of the server blocks.&lt;/p&gt;

&lt;p&gt;For this example you can leave it as the default site, so you should remove this option from your newly created server block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[...]
        listen 80 default_server; // highlight-line
        listen [::]:80 default_server; // highlight-line
[...]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Your listen directives should now look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[...]
        listen 80; // highlight-line
        listen [::]:80; // highlight-line
[...]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And then update the site &lt;code&gt;root&lt;/code&gt; with the directory where your Gatsby site is, and &lt;code&gt;server_name&lt;/code&gt; to your domain name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[...]
        #e.g root /home/gatsby_user/gatsby_site/public
        root &amp;lt;your-gatsby-site&amp;gt;/public; // highlight-line

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        #e.g server_name www.example.com example.com
        server_name www.&amp;lt;your-custom-domain&amp;gt; &amp;lt;your-custom-domain&amp;gt;; // highlight-line
[...]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now enable the site by creating a symlink in the &lt;code&gt;sites-enabled&lt;/code&gt; directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo ln&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; /etc/nginx/sites-available/&amp;lt;your-custom-domain&amp;gt; /etc/nginx/sites-enabled/&amp;lt;your-custom-domain&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you can reload the Nginx configuration files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl reload nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now visit &lt;code&gt;http://&amp;lt;your-custom-domain&amp;gt;&lt;/code&gt; and your Gatsby site should be live!&lt;/p&gt;

&lt;h4&gt;
  
  
  Configuring LetsEncrypt
&lt;/h4&gt;

&lt;p&gt;Although you can now access your site over HTTP, you should always secure your websites using HTTPS, even if they don't handle sensitive communications. HTTPS is also a requirement for many new browser features such as progressive web apps (PWAs) or service workers.&lt;/p&gt;

&lt;p&gt;With services like &lt;a href="https://letsencrypt.org/"&gt;LetsEncrypt&lt;/a&gt;, HTTPS can now be quickly be added to your website. LetsEncrypt is a free, automated and open certification authority (CA) service provided by the &lt;a href="https://www.abetterinternet.org/"&gt;Internet Security Research Group&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;LetsEncrypt recommend using &lt;a href="https://certbot.eff.org/"&gt;Certbot&lt;/a&gt; to manage your HTTPS certificates. You should now install Certbot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;certbot python3-certbot-nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once installed, run Certbot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;certbot &lt;span class="nt"&gt;--nginx&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When prompted for what domains you would like to enable HTTPS for, you can leave the prompt blank to select all domains and continue by pressing enter.&lt;/p&gt;

&lt;p&gt;When prompted for whether you want to redirect all HTTP traffic to HTTPS, you should select redirect all HTTP traffic to HTTPS.&lt;/p&gt;

&lt;p&gt;Certbot will then automatically update your Nginx server blocks, request a certificate from LetsEncrypt and place this in the correct location and finally it will setup automatically renewing your certificate every 60 days.&lt;/p&gt;

&lt;p&gt;Your site will now be available to access on &lt;code&gt;https://&amp;lt;your-custom-domain&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Keeping Your Site Up To Date
&lt;/h3&gt;

&lt;p&gt;You can manually keep your site afterup to date, after changes are made to the site's Git repo, by SSH'ing into your DigitalOcean Droplet, entering the directory of your Gatsby site and then running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;git pull
gatsby build
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This pulls your updated Gatsby site from your Git repository, and then builds the latest version of your Gatsby site.&lt;/p&gt;

</description>
      <category>gatsby</category>
      <category>react</category>
      <category>tutorial</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
