<?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: Mark</title>
    <description>The latest articles on DEV Community by Mark (@markhelmsglitch).</description>
    <link>https://dev.to/markhelmsglitch</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%2F633112%2F343b3593-9b50-48e0-b528-355f2fca8a78.png</url>
      <title>DEV Community: Mark</title>
      <link>https://dev.to/markhelmsglitch</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/markhelmsglitch"/>
    <language>en</language>
    <item>
      <title>Self hosting with Pi</title>
      <dc:creator>Mark</dc:creator>
      <pubDate>Tue, 18 May 2021 20:09:33 +0000</pubDate>
      <link>https://dev.to/markhelmsglitch/self-hosting-with-a-pi-1dpi</link>
      <guid>https://dev.to/markhelmsglitch/self-hosting-with-a-pi-1dpi</guid>
      <description>&lt;p&gt;Self Hosting! &lt;/p&gt;

&lt;h6&gt;
  
  
  (cough cough, this might take a while...    it did for me)
&lt;/h6&gt;

&lt;p&gt;If your looking for the end all be all self hosting guide this is not it, but it will at least give you a good idea of how to do it for your own situation you got going on. Through this guide you'll probably have a better understanding of how hosting works.&lt;br&gt;
Also, I used a Raspberry PI 4 model B for this project.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Here is the short version.&lt;/p&gt;

&lt;p&gt;TLDR:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Install Linux&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enabling Wifi&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install Nginx and Node.js&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configure UFW&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configure the router&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configuring Nginx config files to listen in on specified ports and folders&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Register your site through google domains&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Point google domains at your device&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Installing Certbot for SSL&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Serve to the WEB!&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Most of this tutorial is done from the Linux terminal and is under the assumption of using VS Code with SSH enabled or have access to an SSH client. So if your not already familiar with the terminal or SSH this will be a good place to start.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;
  
  
  1. Installing Linux
&lt;/h3&gt;

&lt;p&gt;Install Linux Server /Ubuntu 20.04 UTS using Raspberry Pi Imager.&lt;/p&gt;

&lt;p&gt;You will have to download the Ubuntu 20.04 UTS image separately.&lt;br&gt;
For this I recommend using at least a 32 GB SD card.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdo27v35uf4lhp2k0zvok.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdo27v35uf4lhp2k0zvok.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Once you successfully boot into Linux, its time to get your PI connected to the internet.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;
  
  
  2. Enabling wifi
&lt;/h3&gt;

&lt;p&gt;How to Connect to WiFi from the Terminal in Ubuntu Linux&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Identify your wireless network interface name:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are several ways to identify your network interface name. You can use the ip command, the deprecated ipconfig command or check this file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ls /sys/class/net
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should give you all the available networking interface (Ethernet, wifi and loopback). The wireless network interface name starts with ‘w’ and it is usually named similar to wlan0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:~$ ls /sys/class/net
eth0 lo wlan0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Then Edit the Netplan configuration file with the wifi interface details&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo nano /etc/netplan/50-cloud-init.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;YAML files are very sensitive about spaces, indention and alignment. DONT USE TAB KEY FOR SPACING, use 4 (or 2, whichever is already used in the YAML file) spaces instead where you see an indention.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;network:

    ethernets:

        eth0:

            dhcp4: true

            optional: true

    version: 2

    wifis:

        wlan0:

            dhcp4: true

            optional: true

            access-points:

                "SSID_name":

password: "WiFi_password"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generate the configuration using this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo netplan generate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now to apply it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo netplan apply
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should have network connected. &lt;/p&gt;

&lt;p&gt;If not, restarting Linux should do the trick.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  3. Installing Nginx and node
&lt;/h3&gt;

&lt;p&gt;just run the commands below one after the other&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get update
sudo apt-get install nginx
sudo apt-get install nodejs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  4. Configuring UFW
&lt;/h3&gt;

&lt;p&gt;This enables UFW to run and allows the ports that we need to communicate &lt;/p&gt;

&lt;p&gt;Once you have port 22 enable you'll be able to SSH into your PI remotely from any application of your choosing, I recommend VS code and setting up SSH keys as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ufw enable

sudo ufw allow 443

sudo ufw allow ssh

sudo ufw allow from 0.0.0.0/24 to any port 22 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  5. Configuring the router
&lt;/h3&gt;

&lt;p&gt;Log into your router and enable the IP address to communicate only on port 443 for https access and SSL certs later on.&lt;/p&gt;

&lt;p&gt;Typically you will look for the IP of your PI and assign the port to 443, this will only allow https traffic from outside the network while still allowing you to use port 22 from within the network.&lt;/p&gt;

&lt;p&gt;Here is a link for that if you need help as there are many types of routers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.netspotapp.com/how-to-log-into-router.html" rel="noopener noreferrer"&gt;https://www.netspotapp.com/how-to-log-into-router.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  6. Configuring Nginx config files to listen in on specified ports and folders
&lt;/h3&gt;

&lt;p&gt;Useful commands for managing Nginx and apply updates&lt;/p&gt;

&lt;p&gt;To restart and apply changes made&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl restart nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tests if the current Nginx configuration is viable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nginx -T 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to apply changes as you go.&lt;/p&gt;

&lt;p&gt;Now go to file location:    /etc/nginx/sites-available/default &lt;/p&gt;

&lt;p&gt;and change the contents to the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;server {

    root /usr/share/nginx/html;

    listen [::]:443 ssl ipv6only=on;
    listen 443 ssl;

    location / {
            proxy_pass http://Your IP Address:port number;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }   

}

server {

     if ($host = Yoursite.com) {
        return 301 https://$host$request_uri;
    } 

    listen 80 ;
    listen [::]:80 ;
    server_name Yoursite.com;
    return 404; 

    location / {  
            satisfy any;  
            allow 127.0.0.1;  
            deny all;  
            auth_basic "closed site";  
            auth_basic_user_file conf/htpasswd;  
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next go to file location /etc/nginx/nginx.conf&lt;/p&gt;

&lt;p&gt;and change that file as well to look like below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    server {
    listen 80;
    }

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip off;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  7. Registering your site through google domains
&lt;/h3&gt;

&lt;p&gt;Log into/ register &lt;a href="http://domains.google.com/" rel="noopener noreferrer"&gt;domains.google.com&lt;/a&gt; and select Get a new domain, and continue through the process of obtaining your domain name. &lt;/p&gt;

&lt;p&gt;Once you have obtained your domain name:&lt;/p&gt;

&lt;p&gt;Go to google domains and log in → select your domain of choice → select DNS on the left side bar→ select Dynamic DNS under Synthetic records → select add → gather your Username and Password that google domains generates for your Dynamic DNS synthetic record&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiz6v1pyua0lxunq0zcus.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiz6v1pyua0lxunq0zcus.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  8. Point google domains at your device
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;a.&lt;/strong&gt;  Have google domains point to and update the reported IP every  30 min. for those of you with dynamic IP addresses&lt;/p&gt;

&lt;p&gt;run this script from any directory of your choosing, it will run properly no matter where its stored&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nano ~/dns_update_script.sh
    wget https://username:password@domains.google.com/nic/update?hostname=yourdomain.com -qO dns_update_results.txt
    wget https://username:password@domains.google.com/nic/update?hostname=www.yourdomain.com -qO- &amp;gt;&amp;gt; dns_update_results.txt
    echo " Last run: `date`" &amp;gt;&amp;gt; dns_update_results.txt
chmod +x ~/dns_update_script.sh

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The script downloads the web pages to the text file (the first wget creates the text file, the second wget appends to the text file) and then I also append the current date and time to the text file.&lt;/p&gt;

&lt;p&gt;Remember to replace username:password with YOUR username and password that google domains provide when generating your DNS.&lt;/p&gt;

&lt;p&gt;Also do not forget to change &lt;a href="http://yourdomain.com" rel="noopener noreferrer"&gt;yourdomain.com&lt;/a&gt; &amp;amp; &lt;a href="http://www.yourdomain.com" rel="noopener noreferrer"&gt;www.yourdomain.com&lt;/a&gt; to the domain name of your site.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;b.&lt;/strong&gt; Setup a cron job to run the script at the start of every hour:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;crontab -e
    0 * * * * ~/dns_update_script.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  9. Installing Certbot for SSL
&lt;/h3&gt;

&lt;p&gt;Follow the instructions that Certbot provides on there site&lt;/p&gt;

&lt;p&gt;&lt;a href="https://certbot.eff.org/instructions" rel="noopener noreferrer"&gt;https://certbot.eff.org/instructions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  10. Serving to the WEB!
&lt;/h3&gt;

&lt;p&gt;serving your web app to the web with node.js and express or just a static site&lt;/p&gt;

&lt;p&gt;The default location to store your site should be: &lt;/p&gt;

&lt;p&gt;/usr/share/nginx/html&lt;/p&gt;

&lt;p&gt;From that directory you will add your static site or applications that run from your self hosted site&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  CONGRATULATIONS!!!
&lt;/h2&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;At this point you should be able to go to your browser an type in "yourdomain.com"&lt;/p&gt;

&lt;p&gt;and see your new website with HTTPS enabled and verified.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>nginx</category>
      <category>raspberrypi</category>
    </item>
  </channel>
</rss>
