Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Akeno's avatar

.env DB_HOST causes problems (127.0.0.1 vs localhost)

Hi,

if I set "DB_HOST" in .env to "127.0.0.1" I can run php artisan commands from console. But when I load my page in the browser, it refuses the connection.

If I set it to "localhost" it's vice versa. (I get "no such file or directory" error in console) Any ideas?

Thank you :)

0 likes
11 replies
MThomas's avatar

What is your setup? Do you use Laravel Homestead or did you install PHP and the DB yourself?

Akeno's avatar

Oh, sorry, forgot to say that: Im using homestead.

MThomas's avatar

Are you sure that you have ssh'd in the VM when you run the artisan commands?

The default DB_HOST 'localhost' should work for both.

2 likes
Akeno's avatar

Do I have to be inside my vm to run those commands? I'm kind of confused. Which commands need to be ran in the VM and which don't? Because I think some commands work outside the vm (if you're in your project's folder).

But okay, I'll try it out later (I'm not on my computer right now). Thank you :)

bashy's avatar

Try 192.168.10.10 (homestead IP)

1 like
MThomas's avatar

@Akeno all commands that make contact with the DB (or any other things installed on the VM) need to be run in the VM. Otherwise you can add as @bashy states your homestead IP address.

1 like
bashy's avatar

Yeah use the full IP if you want to use DB stuff from both local and VM machines. :)

@MThomas Hi! Been a while.

2 likes
Akeno's avatar

Okay, I tried it, but it didn't work with 192.168.10.10 :(

(Only if I ssh in it works)

MThomas's avatar

@Akeno than you need to get used to SSH into the VM, if you use laravel Elixir it is advisable to run that not on the VM but locally.

@bashy, yeah it has been a wile, started a new job last October and as a result I did not have any time left for some decent coding etc, now I'm starting to find some time to take a look at laracasts once in a while and to work on some of my personal hobby projects :) All well on your side?

1 like
bashy's avatar

@MThomas Ah good stuff! It's hard to get side projects in right... Yes been really good, still going at WithSocial :)

1 like

Please or to participate in this conversation.