Featured image of post How to Easily Share Local Laravel Projects to the Public Internet

How to Easily Share Local Laravel Projects to the Public Internet

“Sometimes we need to share local projects to the public internet, for example for demo purposes or sharing projects with clients. Here's an easy way to share a local Laravel project to the public internet.”

Laravel is one of the most popular PHP frameworks today. With Laravel, we can create web applications quickly and easily. However, sometimes we need to share local projects to the public internet without deploying/hosting first, for example for demo purposes or sharing projects with clients. Here is an easy way to share a local Laravel project to the public internet.

Using ngrok

Ngrok is a tool that allows us to create a tunnel to localhost. With ngrok, we can make our local projects accessible from the public internet. Make sure you have a ngrok account at https://ngrok.com/.

  1. Login to ngrok dashboard
  2. Download ngrok
  3. Run the laravel project
    1
    
    php artisan serve
    
  4. Run the build command if using vite or webpack
    1
    
    npm run build
    
  5. Run ngrok
    1
    
    ./ngrok http 8000
    
  6. Done, the laravel project can now be accessed from the public internet with the URL generated by ngrok

Using expose.dev

expose.dev is a service that allows us to make local projects accessible from the public internet. This service is one of the best alternatives to ngrok, besides that expose.dev is made with PHP, so it is more compatible even though it is not too influential. Make sure you have an expose.dev account at https://expose.dev/.

  1. Login to expose.dev dashboard
  2. Install expose.dev
    1
    
    curl -sSL https://expose.dev/install | bash
    
  3. Run the laravel project
    1
    
    php artisan serve
    
  4. Run the build command if using vite or webpack
    1
    
    npm run build
    
  5. Run expose.dev
    1
    
    expose share http://localhost:8000
    
  6. Done, the laravel project can now be accessed from the public internet with the URL generated by expose.dev

Configure proxy in Laravel

In order for laravel to know that the project is being accessed via tunnel or proxy, we need to add a proxy configuration. Thus the url generation functions in laravel such as asset, url, route, @vite and others will run properly.

For configuring the proxy in Laravel, we can add the following code to the bootstrap\app.php file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
        $middleware->trustProxies(at: '*'); // add the following line
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

If using Laravel 10 and below, the proxy configuration can be done by adding the following code to the app\Http\Middleware\TrustProxies.php file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php

namespace App\Http\Middleware;

use Illuminate\Http\Middleware\TrustProxies as Middleware;
use Illuminate\Http\Request;

class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array<int, string>|string|null
     */
    protected $proxies = '*'; // add the following line
    

    /**
     * The headers that should be used to detect proxies.
     *
     * @var int
     */
    protected $headers =
        Request::HEADER_X_FORWARDED_FOR |
        Request::HEADER_X_FORWARDED_HOST |
        Request::HEADER_X_FORWARDED_PORT |
        Request::HEADER_X_FORWARDED_PROTO |
        Request::HEADER_X_FORWARDED_AWS_ELB;
}

Conclusion

Sharing local Laravel projects to the public internet is very easy. We can use ngrok or expose.dev to create a tunnel to localhost. In addition, we also need to configure the proxy in Laravel so that the project can run properly when accessed via tunnel or proxy. Happy coding!

Built with Hugo
Theme Stack designed by Jimmy