Laravel Valet usage with special sites

Update: 2019-10-24 - listen 80; has been updated to listen 127.0.0.1:80; to ensure that custom sites are properly recognized with the current Valet Nginx configuration. 

In a nut shell, Laravel Valet is:

… a Laravel development environment for Mac minimalists. No Vagrant, no /etc/hosts file. You can even share your sites publicly using local tunnels. Yeah, we like it too.

'I like it too', means that Laravel Valet is the Irish Spring of development environments. Valet makes spinning up almost any type of php site on your local Mac a breeze. There are some edge cases where you might not want to run a site within Valet. On macOS, it is really easy to run other local sites using a custom Nginx config files. This is a basic Nginx config file that you will put in ~/.config/valet/Nginx/customsite.conf

server {
    listen 127.0.0.1:80;
    server_name customsite.test;
    charset utf-8;
    root /path/to/your/custom/site;
    index index.php index.html;
    access_log /usr/local/var/log/nginx/access.log;
    error_log /usr/local/var/log/nginx/error.log;
    location ~ ^(.+\.php)(.*)$ {
         fastcgi_split_path_info ^(.+\.php)(.*)$;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_param SCRIPT_NAME $fastcgi_script_name;
         fastcgi_pass unix:/Users/yourusername/.config/valet/valet.sock;
         fastcgi_index index.php;
         include fastcgi_params;
     }
}

You need to modify server_name, root and fastcgi_pass:

  • server_name: change to the url you wish to use to access this site. Suggest using .test to ensure DNS doesn't go online looking for the site. In the example above, hitting http://customsite.text/ would load your site.
  • root: Change this to where on your computer you are putting your site files. Typically this might be /Users/username/Sites/customsite/public/
  • fastcgi_pass: Be sure to change username to whatever user you have on your computer. This will tell Nginx to use the valet.sock.

Next up, restart Nginx and ensure that your configuration files are error free:

# sudo brew services restart nginx
Stopping `nginx`... (might take a while)
==> Successfully stopped `nginx` (label: homebrew.mxcl.nginx)
==> Successfully started `nginx` (label: homebrew.mxcl.nginx)
# sudo nginx -t
Password: <enter your password>
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

If you did everything correctly, you will now be able to load your site in your browser. Do you have a way to improve on this, or having troubles getting it working. Let me know in the comments below.

Read more…

Comments