Laravel Sail how to change local dev domain
Asked Answered
P

2

14

I have recently decided to try out Laravel Sail instead of my usual setup with Vagrant/Homestead. Everything seems to be beautifully and easily laid out but I cannot seem to find a workaround for changing domain names in the local environment.

I tried serving the application on say port 89 with the APP_PORT=89 sail up command which works fine using localhost:89 however it seems cumbersome to try and remember what port was which project before starting it up.

I am looking for a way to change the default port so that I don't have to specify what port to serve every time I want to sail up. Then I can use an alias like laravel.test for localhost:89 so I don't have to remember ports anymore I can just type the project names.

I tried changing the etc/hosts file but found out it doesn't actually help with different ports

I essentially am trying to access my project by simply typing 'laravel.test' on the browser for example.

Also open for any other suggestions to achieve this. Thanks

**Update ** After all this search I actually decided to change my workflow to only have one app running at a time so now I am just using localhost and my CPU and RAM loves it, so if you are here moving from homestead to docker, ask yourself do you really need to run all these apps at the same time. If answer is yes research on, if not just use localhost, there is nothing wrong with it

Puggree answered 12/2, 2021 at 11:35 Comment(0)
C
43

To change the local name in Sail from the default 'laravel.test' and the port, add the following to your .env file:
APP_SERVICE="yourProject.local"
APP_PORT=89
This will take effect when you build (or rebuild using sail build --no-cache) your Sail container.

And to be able to type in 'yourProject.local' in your web browser and have it load your web page, ensure you have your hosts file updated by adding
127.0.0.1 yourProject.local
to your hosts file. This file is located:

  • Windows 10 – “C:\Windows\System32\drivers\etc\hosts”
  • Linux – “/etc/hosts”
  • Mac OS X – “/private/etc/hosts”

You'll need to close all browser instances and reopen after making chnages to the hosts file. With this, try entering the alias both with and without the port number to see which works. Since you already set the port via .env you may not need to include it in your alias.

If this doesn't work, change the .env APP_URL=http://yourProject.local:89

Ok another option, in /routes/web.php I assume you have a route set up that may either return a view or call a controller method. You could test to see if you can have this ‘return redirect('http://yourProject.local:89');’ This may involve a little playing around with the routes/controller, but this may be worth looking into.

Ceria answered 12/2, 2021 at 11:56 Comment(6)
So this has solved my first issue of changing the default port, thank you. Now I have access to the environment via localhost:89 however using the alias thing still doesnt work. I essentially want to be able to access locahost:89 by typing "yourProject.local" to my browser. Do you know how to do this?Puggree
Thanks for updating your answer, however this is the method I mentioned I tried already. It replaces name of the localhost and also still requires me to remember the port number not exactly ideal unfortunately. On the other hand I found a post on reddit about this, it seems a lot more complex than i thought it would be. reddit.com/r/laravel/comments/kfk2yp/…Puggree
It didn't work for me. I use ubuntu 20.04.Vincentvincenta
This worked for me. But once I added APP_SERVICE, APP_PORT, and changed the /etc/hosts, all I needed to do is run "vendor/bin/sail up".Blowfish
APP_PORT worked but APP_SERVICE didn't. I already run sail up -d --force-recreate. I am using Ubuntu 22 LTS WSL2.Cabinetwork
Worked for me! ubuntu 22.04, thank you.Luker
B
2

Not New To Hosts but Am New to Sail/Docker I'm not new to using custom domains/urls for local dev/work in /etc/hosts and in the app's/VM's config files and have had as many as 20+ apps so configured simultaneously but I am new to using it with Sail/Docker and I couldn't get any of the above options to work but found this very simple solution to work very well:

sudo vim /etc/hosts

127.0.0.1 localhost laravel.test

where laravel.test is your custom domain/url.

Works great for me and is so simple ( don't have to rebuild after this change as editing hosts doesn't affect the build.

Balbinder answered 4/4, 2023 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.