Laravel Valet 502 Bad Gate Way nginx/1.15.7
Asked Answered
E

16

36

I am getting a 502 Bad Gateway on my Laravel projects running Laravel valet.

I have tried many of the solutions online and with no success. i.e. https://gist.github.com/adamwathan/6ea40e90a804ea2b3f9f24146d86ad7f

At the moment the error I see is 502 bad gateway and an error in my command line when running valet install is an error when it gets to the updating PHP configuration. It gives the following error:

Warning: file_get_contents(/usr/local/etc/php/7.3/php-fpm.d/www.conf): failed to open stream: No such file or directory in /Users/username/.composer/vendor/laravel/valet/cli/Valet/Filesystem.php on line 112

Warning: file_get_contents(/usr/local/etc/php/7.3/php-fpm.d/www.conf): failed to open stream: No such file or directory in /Users/username/.composer/vendor/laravel/valet/cli/Valet/Filesystem.php on line 125

Has anybody had similar issues?

Thanks

Etymon answered 18/12, 2018 at 16:50 Comment(0)
F
18

Had the same symptoms after updating to php 7.3 and then installing a new Laravel project.

It appears that brew install php73 doesn't install php-fpm

Solution is to uninstall php

brew uninstall php73 
brew uninstall php72
brew uninstall php71 ... whatever versions you have
brew uninstall --force php

Now reinstall php

brew install php --build-from-source

I encountered permission errors mkdir: /usr/local/etc/php/7.3/php-fpm.d: Permission denied so sudo chown -R: <yourusercode> /usr/local/etc/php fixed that and then brew install php --build-from-source again. Once it builds php 7.3 successfully reinstall valet:

valet install
Ftlb answered 19/12, 2018 at 6:49 Comment(2)
This is correct, however, we just installed [email protected] instead and it worked.Febrifacient
If you don't want to go to an older version, 7.4 also does the trick!Quantifier
V
87

If you're anyone like me who're seeing 502 Bad Gateway while using Laravel Valet after updating it composer global update to the latest version, you most probably forgot to run valet install command. Laravel Valet requires (in most cases) to run valet install command after updating to the latest version.

Volauvent answered 27/2, 2019 at 8:5 Comment(2)
This is the actual answer to the user's question above and is a much better first step in diagnosing this problem than the accepted answer, which works because of the last step in the process.Ogilvy
And just to note if you get a 404 after this, instead of the original 502 - re-run valet link tooGrummet
C
46

in most cases running valet install will solve the issue.

Cnossus answered 20/11, 2021 at 11:45 Comment(0)
F
18

Had the same symptoms after updating to php 7.3 and then installing a new Laravel project.

It appears that brew install php73 doesn't install php-fpm

Solution is to uninstall php

brew uninstall php73 
brew uninstall php72
brew uninstall php71 ... whatever versions you have
brew uninstall --force php

Now reinstall php

brew install php --build-from-source

I encountered permission errors mkdir: /usr/local/etc/php/7.3/php-fpm.d: Permission denied so sudo chown -R: <yourusercode> /usr/local/etc/php fixed that and then brew install php --build-from-source again. Once it builds php 7.3 successfully reinstall valet:

valet install
Ftlb answered 19/12, 2018 at 6:49 Comment(2)
This is correct, however, we just installed [email protected] instead and it worked.Febrifacient
If you don't want to go to an older version, 7.4 also does the trick!Quantifier
F
11

None of the above answers worked for me, but found the solution here: https://janostlund.com/2019-06-20/502-bad-gateway-laravel-valet

~/.config/valet/Log/nginx-error.log shows:

[error] 17423#0: *1 upstream sent too big header while reading response header from upstream [...]

Solved by adding two lines to http in /usr/local/etc/nginx/nginx.conf

http {
  fastcgi_buffers 16 16k;
  fastcgi_buffer_size 32k;
  
  //...
}

and then running valet restart

Flivver answered 19/7, 2020 at 11:25 Comment(0)
B
9

I solved this by doing:

php -v

PHP 8.0.1 (cli) (built: Jan 8 2021 09:07:02) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.1, Copyright (c) Zend Technologies with Zend OPcache v8.0.1, Copyright (c), by Zend Technologies

followed by:

valet use [email protected] --force

Unlinking current version: php Linking new version: [email protected] Updating PHP configuration... Restarting php... Restarting nginx... Valet is now using [email protected].

Valet seemed to be confused over which PHP it was using.

Biography answered 10/8, 2021 at 18:14 Comment(1)
This was my issue. I have two PHP versions installed and I had to tell it to use 8.2Wiring
A
2

Well, normally "valet install" solves the issue but for me it was different. For my case, I was using valet isolate for a different project with different PHP versions. So, I have to do the binding process again.

I did,

  1. valet install
  2. valet isolate [email protected] (Here you have to use the selected version)

this solves the issue for me.

Aitken answered 17/9, 2022 at 17:40 Comment(1)
Thanks you SOOOOOOOOOOOOO much. I would never have found this by trial and error. You're a lifesaver.Tiebold
D
2

In my case I reinstalled different version of php. I just run

valet install

and it worked fine.

Damson answered 3/12, 2022 at 1:51 Comment(1)
God forbid you do this on a MacBook with tons of sites, I have to fingerprint like 154 https projects 😅Exosmosis
H
1

I ran into the same problem with Laravel 8. Both Valet and Expose seemed to work, but the webpage always gave a 502 response.

The solution I found when I updated composer and tried to reinstall Valet was that Valet didn't know which version of php to use.

To fix this, use the following command to tell valet which version of php to use.

valet use [email protected]
Hasson answered 20/12, 2020 at 14:17 Comment(0)
H
1

Try this

brew services start php

If it didn’t work, try to reinstall php from source

brew uninstall php
brew install php --build-from-source
valet install

Source: laravel/valet github issues

Henkel answered 24/5, 2022 at 20:5 Comment(0)
A
1

If the valet restart or valet install didn't work, try to change the PHP version.

valet use [email protected]

Or

valet isolate [email protected]
Auric answered 2/1, 2023 at 8:22 Comment(0)
C
1

This is the definite solution:

  • Run valet status to check the status of all Valet services. Normally they should all have status "Yes", except this one Is linked PHP (php@<version>) running as root?

  • Run valet use php@<version>, make sure you have that php version already installed.

  • Happy coding.

Charil answered 15/12, 2023 at 23:36 Comment(0)
F
1
  1. Run composer global update
  2. Run valet install
  3. Run valet restart

The last step isn’t always necessary but I’ve found there are times when I’ve had to use it.

Ferri answered 6/2 at 15:46 Comment(0)
G
0

I had the same problem. I solved it by upgrading mariadb. brew upgrade mariadb

Grubbs answered 2/5, 2019 at 8:54 Comment(0)
P
0

Following the config above, but put it in the file.

~/.valet/Nginx/all.conf

  fastcgi_buffers 16 16k;
  fastcgi_buffer_size 32k;

This did catch on all the sites "Im use Valet plus"

Pancake answered 19/9, 2020 at 13:52 Comment(0)
R
0

My nginx-error.log file was full of messages like this

*3 connect() to unix:/home/oakbox/.valet/valet.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1

It took me too long to figure out that php-fpm was running, but that Nginx could not 'see' that socket. Look in your Nginx configuration file for the line that starts with

        fastcgi_pass unix:/home/oakbox/.valet/valet.sock;

And then look to see if that socket actually exists. In my setup, an update of my system moved my default PHP installation from version 8.1 to 8.2. The actual socket file was renamed from valet.sock to valet82.sock.

I edited my Nginx configuration files to point to the real location of the socket

        fastcgi_pass unix:/home/oakbox/.valet/valet82.sock;

A restart of Nginx and my 502 Bad Gateway went away.

Revisory answered 22/2, 2023 at 7:58 Comment(0)
H
0

In 2023, I found a fix from this GitHub issue. Apparently, my Valet services got messed up when I ran valet use command. 502 Bad Gateway was everywhere!

Things I tried:

  • valet uninstall
  • valet install
  • Unlinked and re-linked the site folder.

Here's what worked:

sudo chmod 755 /etc/init.d/php7.2-fpm

According to the GitHub issue, valet use messes up the file rights of the services and you need to re-assign the right permissions again. Hopefully I saved anyone looking at this thread some hours of reconfiguring Valet from scratch. Try it!

Hagridden answered 8/9, 2023 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.