{"id":1998,"date":"2020-03-09T20:00:18","date_gmt":"2020-03-09T12:00:18","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1998"},"modified":"2021-04-24T23:40:03","modified_gmt":"2021-04-24T15:40:03","slug":"how-to-put-your-raspberry-pi-server-on-the-internet-with-ngrok","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-put-your-raspberry-pi-server-on-the-internet-with-ngrok\/","title":{"rendered":"How to put your Raspberry Pi server on the internet with ngrok"},"content":{"rendered":"<p>As I had mentioned in <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-host-multiple-websites-from-home\/\" rel=\"noopener\" target=\"_blank\">how to host multiple websites from home<\/a>, a typical network architecture looks like the following:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/home-network-architecture.gif\" alt=\"Home network architecture\" class=\"aligncenter\" \/> In such a case, we will need our router to hold a public IP address and perform port forwarding for our home servers.<\/p>\n<p>However, there can be cases where we do not have the environment to put our server on the internet. <\/p>\n<p>In such a situation, you will find <a href=\"https:\/\/ngrok.com\/\" rel=\"noopener\" target=\"_blank\">ngrok<\/a> useful. Since ngrok accepts traffic on a public address and relays that traffic through to the ngrok process running on your machine, you can put your server on the internet easily.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/ngrok-demo-diagram.gif\" alt=\"ngrok demo diagram\" class=\"aligncenter\" \/><\/p>\n<p>Given that, let's look at how you can put your <a href=\"https:\/\/www.techcoil.com\/raspberry-pi-server-project-ideas\/\" rel=\"noopener\" target=\"_blank\">Raspberry Pi server<\/a> on the internet with ngrok.<\/p>\n<h2>Create an account with ngrok<\/h2>\n<p>First of all, you will need an account with ngrok so that your Raspberry Pi is able to identify itself with ngrok. Therefore, proceed to <a href=\"https:\/\/dashboard.ngrok.com\/signup\" rel=\"noopener\" target=\"_blank\">create an account with ngrok<\/a>.<\/p>\n<h2>Setup your Raspberry Pi server<\/h2>\n<p>Once you have an account with ngrok, you are ready to use ngrok to put your Raspberry Pi server on the internet. <\/p>\n<p>If you have not done so, then you may want to setup your Raspbery Pi server. For example, you can setup <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-setup-raspbian-buster-lite-for-raspberry-pi-server-projects\/\" rel=\"noopener\" target=\"_blank\">Raspbian Buster Lite on your Raspberry Pi<\/a> as the operating system to run your server applications.<\/p>\n<p>For the purpose of this tutorial, let's assume that we want to <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-host-a-static-website-on-your-raspberry-pi-with-nginx-running-on-raspbian-buster-lite\/\" rel=\"noopener\" target=\"_blank\">host a static website on our Raspberry Pi<\/a> and make it available on the Internet with ngrok. <\/p>\n<p>In order to keep things simple, we will make use of the <a href=\"https:\/\/www.techcoil.com\/blog\/understanding-the-default-nginx-virtual-host-or-server-configuration\/\" rel=\"noopener\" target=\"_blank\">default Nginx configurations<\/a> to serve some static content from the <code>\/var\/www\/html\/<\/code> directory.<\/p>\n<p>For example, if your Raspberry Pi IP is <strong>192.168.1.114<\/strong>, you should be able to retrieve \/var\/www\/html\/index.html via <strong>http:\/\/192.168.1.114\/<\/strong> on your web browser.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Chrome-accessing-sample-HTML-resume-hosted-as-static-website-on-Nginx-running-on-Raspberry-Pi.gif\" alt=\"Chrome accessing sample HTML resume hosted as static website on Nginx running on Raspberry Pi\" class=\"aligncenter\"><\/p>\n<h2>Downloading the ngrok client program on your Raspberry Pi<\/h2>\n<p>When you have a <a href=\"https:\/\/www.techcoil.com\/glossary\/http-server\/\" rel=\"noopener\" target=\"_blank\">HTTP server<\/a> listening for incoming <a href=\"https:\/\/www.techcoil.com\/glossary\/http-request\/\" rel=\"noopener\" target=\"_blank\">HTTP requests<\/a> on your Raspberry Pi, you can install the ngrok client.<\/p>\n<p>In order to do so, run the following command on a terminal program on your Raspberry Pi:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncd ~\r\nwget https:\/\/bin.equinox.io\/c\/4VmDzA7iaHb\/ngrok-stable-linux-arm.tgz\r\ntar -xvzf ngrok-stable-linux-arm.tgz\r\n<\/pre>\n<p>When the above command completes, you can verify your download with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n.\/ngrok help\r\n<\/pre>\n<p>If ngrok client had downloaded successfully, then you should find instructions on how to use the ngrok client in the output.<\/p>\n<h2>Authenticate your Raspberry Pi with ngrok<\/h2>\n<p>At this point in time, you are ready to authenticate your Raspberry Pi with ngrok.<\/p>\n<p>In order to do so, <a href=\"https:\/\/dashboard.ngrok.com\/\" rel=\"noopener\" target=\"_blank\">login to the ngrok dashboard<\/a>. When you have logged in successfully, <a href=\"https:\/\/dashboard.ngrok.com\/auth\" rel=\"noopener\" target=\"_blank\">navigate to the Auth page<\/a> to get your Tunnel Authtoken.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/ngrok-auth-page-in-dashboard.gif\" alt=\"ngrok auth page in dashboard\" class=\"aligncenter\" \/><\/p>\n<p>As shown above, the Auth page will show the command to authenticate your Raspberry Pi with ngrok:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n.\/ngrok authtoken 1YkSVonpCzVJrPyIBZTAF4DGSsj_4UD755hEaVcFJh66tnLkh\r\n<\/pre>\n<p>When the command completes, you should see an output similar to the following:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nAuthtoken saved to configuration file: \/home\/pi\/.ngrok2\/ngrok.yml\r\n<\/pre>\n<h2>Running the ngrok client program to put your Raspberry Pi server on the internet<\/h2>\n<p>At this point in time, you will be able to put your Raspberry Pi server on the internet by running the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n.\/ngrok https -bind-tls=true 80\r\n<\/pre>\n<p>When you do so, the terminal will produce an output similar to the following:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nngrok by @inconshreveable                                       (Ctrl+C to quit)\r\n                                                                                \r\nSession Status                online                                            \r\nAccount                       youremailaddress@gmail.com (Plan: Free)                    \r\nVersion                       2.3.35                                            \r\nRegion                        United States (us)                                \r\nWeb Interface                 http:\/\/127.0.0.1:4040                             \r\nForwarding                    https:\/\/5114a8e6.ngrok.io -&gt; http:\/\/localhost:80  \r\n                                                                                \r\nConnections                   ttl     opn     rt1     rt5     p50     p90       \r\n                              0       0       0.00    0.00    0.00    0.00  \r\n<\/pre>\n<h2>Accessing your Raspberry Pi server application with the allocated ngrok domain name<\/h2>\n<p>Once you see the output, you can then access your Raspberry Pi server with the ngrok URL stated. For example, when I access <strong>https:\/\/5114a8e6.ngrok.io<\/strong>, I was able to see the web page that was served by my Raspberry Pi:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Accessing-web-page-hosted-on-Raspberry-Pi-Nginx-server-via-ngrok-url.gif\" alt=\"Accessing web page hosted on Raspberry Pi Nginx server via ngrok url\" class=\"aligncenter\" \/><\/p>\n<p>In addition to the command line output, you can also find the URL in the <a href=\"https:\/\/dashboard.ngrok.com\/status\" rel=\"noopener\" target=\"_blank\">status page of your ngrok dashboard<\/a>.<\/p>\n<h2>Running ngrok client as a daemon<\/h2>\n<p>If you want ngrok to put your Raspberry Pi server on the internet on boot, then you will need to run it as a daemon. <\/p>\n<p>In order to do so, we need to:<\/p>\n<ol>\n<li>edit our <code>ngrok.yml<\/code> file<\/li>\n<li>create a system unit file for our ngrok client.<\/li>\n<li>register the system unit file with <code>systemd<\/code> and run the ngrok client as a daemon.<\/li>\n<\/ol>\n<h3>Editing <code>ngrok.yml<\/code><\/h3>\n<p>First, use <code>nano<\/code> to edit the ngrok.yml file that our ngrok client had created for us earlier:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo nano ~\/.ngrok2\/ngrok.yml\r\n<\/pre>\n<p>When <code>nano<\/code> starts, add the following content <strong>after the authtoken row<\/strong>:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ntunnels:\r\n  nginx-server:\r\n    proto: http\r\n    addr: 80\r\n    bind-tls: true\r\n    console_ui: false\r\n<\/pre>\n<p>After you had made the changes, press <strong>Ctrl-X<\/strong>, <strong>Y<\/strong> and <strong>Enter<\/strong>6 to save the file.<\/p>\n<h3>Creating a system unit file for our ngrok client<\/h3>\n<p>Next, use <code>nano<\/code> to create a system unit file for our ngrok client:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo nano \/etc\/systemd\/system\/ngrok-client.service\r\n<\/pre>\n<p>When the <code>nano<\/code> starts, copy the following directives into the editor:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;Unit]\r\nDescription=ngrok client\r\nAfter=network.target\r\n\r\n&#x5B;Service]\r\nExecStart=\/home\/pi\/ngrok start --all -config \/home\/pi\/.ngrok2\/ngrok.yml\r\nRestart=on-abort\r\n\r\n&#x5B;Install]\r\nWantedBy=multi-user.target\r\n<\/pre>\n<p>After you had copied the directives to the editor, press <strong>Ctrl-X<\/strong>, <strong>Y<\/strong> and <strong>Enter<\/strong> to save the changes.<\/p>\n<h3>Registering the system unit file with <code>systemd<\/code> and running the ngrok client as a daemon<\/h3>\n<p>Once you had created the system unit file, run the following commands to run the ngrok client as a daemon:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo systemctl daemon-reload\r\nsudo systemctl enable ngrok-client\r\nsudo systemctl start ngrok-client\r\n<\/pre>\n<p>After you have registered <code>ngrok<\/code> client to run as a daemon, you can check if it is running successfully with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsystemctl status ngrok-client.service\r\n<\/pre>\n<p>If your <code>ngrok<\/code> client is able to run successfully, then you should find output similar to the following:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\u25cf ngrok-client.service - ngrok client\r\n   Loaded: loaded (\/etc\/systemd\/system\/ngrok-client.service; enabled; vendor preset: enabled)\r\n   Active: active (running) since Mon 2020-03-09 17:43:55 +08; 8min ago\r\n Main PID: 8888 (ngrok)\r\n    Tasks: 13 (limit: 4915)\r\n   Memory: 5.9M\r\n   CGroup: \/system.slice\/ngrok-client.service\r\n           \u2514\u25008888 \/home\/pi\/ngrok start --all -config \/home\/pi\/.ngrok2\/ngrok.yml\r\n\r\nMar 09 19:43:55 raspberrypi systemd&#x5B;1]: Started ngrok client.\r\n<\/pre>\n<p>Given that, you can then head on to your <a href=\"https:\/\/dashboard.ngrok.com\/status\" rel=\"noopener\" target=\"_blank\">ngrok status page<\/a> to find the URL to access your Raspberry Pi server.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/How-to-put-your-Raspberry-Pi-server-on-the-internet-with-ngrok.gif\" alt=\"How to put your Raspberry Pi server on the internet with ngrok\" class=\"aligncenter\" \/><\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-we\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-we&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-we&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1998&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>As I had mentioned in <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-host-multiple-websites-from-home\/\" rel=\"noopener\" target=\"_blank\">how to host multiple websites from home<\/a>, a typical network architecture looks like the following:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/home-network-architecture.gif\" alt=\"Home network architecture\" class=\"aligncenter \" \/> In such a case, we will need our router to hold a public IP address and perform port forwarding for our home servers.<\/p>\n<p>However, there can be cases where we do not have the environment to put our server on the internet. <\/p>\n<p>In such a situation, you will find <a href=\"https:\/\/ngrok.com\/\" rel=\"noopener\" target=\"_blank\">ngrok<\/a> useful. Since ngrok accepts traffic on a public address and relays that traffic through to the ngrok process running on your machine, you can put your server on the internet easily.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/ngrok-demo-diagram.gif\" alt=\"ngrok demo diagram\" class=\"aligncenter \" \/><\/p>\n<p>Given that, let&#8217;s look at how you can put your <a href=\"https:\/\/www.techcoil.com\/raspberry-pi-server-project-ideas\/\" rel=\"noopener\" target=\"_blank\">Raspberry Pi server<\/a> on the internet with ngrok.<\/p>\n","protected":false},"author":1,"featured_media":2000,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[484,225,727,240,507,453],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/ngrok-demo-diagram.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-we","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1998"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1998"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1998\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/2000"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}