Laravel Homestead with custom Nginx configurations

Markus Schober
3 min readJan 17, 2017

--

If you are using Laravel Homestead as your development environment, there are chances, that you have custom Nginx configurations for some of your sites.
You probably know the following situation:

You are running a provision to add a new site and all your Nginx-configurations got overwritten. You have to manually update them again. Aaargh…

Add custom configurations

Since Homestead v3 (I guess 😄) it’s very easy to add a custom configuration. Lets assume Laravel Homestead is installed like described in the official documentation.

In your home directory on your mac is a folder called Homestead. This is the cloned Github-Repository of Laravel Homestead.
There’s a folder called scripts, where all configuration scripts (and other files) life, for example serve-laravel.sh, which is the default nginx configuration of Laravel Homestead.

In this example, I’ll add a configuration for Kirby, which is a great CMS by the way. Kirby needs some additional rewrite rules (if you copy/paste this rules, you need to mask the variables in it — see Code example below).

Make a copy of serve-laravel.sh and call it serve-kirby.sh. Make your modifications inside of the block called server (here in serve-laravel.sh), in our case the additional rewrite rules and save the file.

serve-kirby.sh looks like this:

#!/usr/bin/env bashmkdir /etc/nginx/ssl 2>/dev/nullPATH_SSL=”/etc/nginx/ssl”
PATH_KEY=”${PATH_SSL}/${1}.key”
PATH_CSR=”${PATH_SSL}/${1}.csr”
PATH_CRT=”${PATH_SSL}/${1}.crt”
if [ ! -f $PATH_KEY ] || [ ! -f $PATH_CSR ] || [ ! -f $PATH_CRT ]
then
openssl genrsa -out “$PATH_KEY” 2048 2>/dev/null
openssl req -new -key “$PATH_KEY” -out “$PATH_CSR” -subj “/CN=$1/O=Vagrant/C=UK” 2>/dev/null
openssl x509 -req -days 365 -in “$PATH_CSR” -signkey “$PATH_KEY” -out “$PATH_CRT” 2>/dev/null
fi
block=”server {
listen ${3:-80};
listen ${4:-443} ssl http2;
server_name $1;
root \”$2\”;
index index.html index.htm index.php;charset utf-8;# block content
location ~ ^/content/(.*).(txt|md|mdown)$ {
rewrite ^/content/(.*).(txt|md|mdown)$ /error redirect;
}
# block all files in the site folder from being accessed directly
location ~ ^/site/(.*)$ {
rewrite ^/site/(.*)$ /error redirect;
}
# block all files in the kirby folder
location ~ ^/kirby/(.*)$ {
rewrite ^/kirby/(.*)$ /error redirect;
}
# site links
location / {
try_files \$uri \$uri/ /index.php?\$uri&\$args;
}
# panel links
location /panel {
try_files \$uri \$uri/ /panel/index.php?\$uri&\$args;
}
# deny access to .htaccess files
location ~ /\.ht {
deny all;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/$1-error.log error;
sendfile off;client_max_body_size 100m;location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
ssl_certificate /etc/nginx/ssl/$1.crt;
ssl_certificate_key /etc/nginx/ssl/$1.key;
}
echo “$block” > “/etc/nginx/sites-available/$1”
ln -fs “/etc/nginx/sites-available/$1” “/etc/nginx/sites-enabled/$1”

To use this configuration for a specific site, simple add a type property to your site configuration in your Homestead.yaml file.

sites:
— map: kirby.dev
to: /home/vagrant/Sites/kirby
type: kirby

The value of the type-property is the second part of your configuration file name.

  • type: kirbyserve-kirby.sh
  • type: wordpressserve-wordpress.sh
  • etc.

This type -property defaults to laravel — as seen here.

If everything works fine, adding new sites is a little bit simpler and faster. Hopefully!

If this is useful for you, a 💚 would be nice 🙂. Thank you!

Disclaimer

If you find mistakes in my English (I guess, there are a lot 😄), feel free to drop me a line. I’m not a native english speaker, but I try to improve my english this year, so I’m forcing myself to write this post in english 🙈.

--

--

Markus Schober

Making stuff at RME Digital Productions (https://www.rme.digital). Developer. Cyclist. Father. Music Lover. Twitter: @maxxscho