Seamlessly Expose Your Localhost Server to the Internet with ngrok

ItsOkindaItsOkinda
4 min read

Access Your Localhost Server Publicly with ngrok: A Simple Guide

If you've ever needed to make your localhost server accessible from the web, you might have encountered the challenge of exposing your local development environment safely. Enter ngrok, a tool designed to create secure tunnels to your localhost, making it easy to share your development work with others or test webhooks. In this blog, we'll walk you through how to set up ngrok to provide a public URL for your local server.

What is ngrok?

ngrok is a tool that creates a secure tunnel to your local server, allowing you to expose a local server to the internet. This can be particularly useful for testing webhooks, demonstrating a web app, or sharing your work with others without deploying it to a live server.

Why Use ngrok?

  • Testing Webhooks: Services like Stripe or Slack require a publicly accessible URL for webhooks. ngrok provides a quick way to test these integrations.

  • Sharing Development Work: Easily share your local development server with colleagues or clients.

  • Debugging: Get access to HTTP request logs to debug issues.

How to Set Up ngrok

1. Install ngrok

To get started, you'll need to install ngrok on your machine.

  • On Windows: Download the ngrok executable from ngrok's website. Unzip the file and place the executable in a directory of your choice.

  • On macOS: You can install ngrok using Homebrew with the command:bashCopy code

  •       brew install ngrok/ngrok/ngrok
    
  • On Linux: Download the ngrok binary from ngrok's website and unzip it. Move the binary to a directory.

    Download the latest ngrok binary for your Linux distribution. You can find the correct binary on our ngrok download page: Select your operating system, select the version, and copy the link that appears in the Download button. Below is an example for x86-64:

  •   wget https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-amd64.tgz
    

    Unzip the downloaded file and move it to a directory in your PATH. Below is an example for /usr/local/bin:

  •   sudo tar xvzf ./ngrok-v3-stable-linux-amd64.tgz -C /usr/local/bin
    

2. Authenticate ngrok

Before you can use ngrok, you'll need to authenticate it with your ngrok account. Sign up for a free account at ngrok.com, and then get your authentication token from the dashboard.

Run the following command to add your auth token:

ngrok authtoken <your_auth_token>

3. Start Your Local Server

Make sure your local server is running. For example, if you're running a development server on port 3000, ensure it's up and listening.

npm run dev

Which starts the nextJs development server on port 3000 or you can use django

python3 manage.py runserver

4. Expose Your Local Server with ngrok

Now, you can expose your local server by running ngrok with the appropriate port number. If your server is running on port 3000, use the following command:

bash

Copy code

ngrok http 3000
ngrok http 8000

5. Access Your Public URL

Once ngrok is running, it will display a public URL that tunnels through to your local server. This URL will look something like https://<random_subdomain>.ngrok.io. You can use this URL to access your local server from anywhere on the web.

Advanced Configuration

For paid subscription

ngrok also offers advanced features like custom subdomains, reserved domains, and more. For example, to reserve a subdomain, use:

ngrok http -subdomain=myapp 3000

This will expose your server with a URL like https://myapp.ngrok.io.

Security Considerations

While ngrok is great for development and testing, remember that exposing your local server to the internet does come with security risks. Ensure your application does not have sensitive information exposed, and consider using ngrok’s authentication features or restricting access if needed.

Conclusion

ngrok is a powerful tool for making your local server accessible over the internet quickly and securely. Whether you're testing integrations, sharing your work, or debugging, ngrok simplifies the process of exposing your localhost. With the steps outlined above, you should be able to set up and use ngrok with ease. Happy coding!

For more detailed information, check out ngrok's documentation.

0
Subscribe to my newsletter

Read articles from ItsOkinda directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ItsOkinda
ItsOkinda