Deploy: Laravel on Heroku in 2 minutes
Laravel + Heroku = Love ❤️ Get up and running on heroku in minutes.
Create a Procfile in the root of your application, the location of this file is important.
Procfile
web: vendor/bin/heroku-php-nginx -C bin/nginx_app.conf public/
This will tell heroku that we want a nginx server, with a custom configuration file, and where our public folder is.
Nginx configuration
Now this can be tweaked and modified to your needs, but it will serve nicely as base template.
Create a nginx_app.conf and put in into a /bin folder, you can put it wherever you like, I just like to keep all my 'non-code/devops' config in a seperate folder. Just remember to change the path to the file in your Procfile.
/bin/nginx_app.conf
# Recursively process X-Forwarded-For header
real_ip_recursive on;
real_ip_header X-Forwarded-For;
# Allow for internal Heroku router - 10.x.x.x
set_real_ip_from 10.0.0.0/8;
# Additional headers
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
# Enable Gzip compressed.
gzip on;
# Enable compression both for HTTP/1.0 and HTTP/1.1.
gzip_http_version 1.1;
# Compression level (1-9).
# 5 is a perfect compromise between size and cpu usage, offering about
# 75% reduction for most ascii files (almost identical to level 9).
gzip_comp_level 5;
# Don't compress anything that's already small and unlikely to shrink much
# if at all (the default is 20 bytes, which is bad as that usually leads to
# larger files after gzipping).
gzip_min_length 256;
# Compress data even for clients that are connecting to us via proxies,
# identified by the "Via" header (required for CloudFront).
gzip_proxied any;
# Tell proxies to cache both the gzipped and regular version of a resource
# whenever the client's Accept-Encoding capabilities header varies;
# Avoids the issue where a non-gzip capable client (which is extremely rare
# today) would display gibberish if their proxy gave them the gzipped version.
gzip_vary on;
gzip_types text/plain text/css application/json text/xml application/xml application/xml+rss text/javascript application/javascript;
# make sure gzip does not lose large gzipped js or css files
# see http://blog.leetsoft.com/2007/7/25/nginx-gzip-ssl
gzip_buffers 16 8k;
# Disable gzip for certain browsers.
gzip_disable “MSIE [1-6].(?!.*SV1)”;
# Enables larger file uploads
client_max_body_size 10M;
location / {
# try to serve file directly, fallback to rewrite
try_files $uri @rewriteapp;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location @rewriteapp {
# rewrite all to app.php
rewrite ^(.*)$ /index.php/$1 last;
}
# browser cache control
location ~* \.(ico|css|js|gif|jpeg|jpg|png|woff|ttf|otf|svg|woff2|eot)$ {
expires 365d;
access_log off;
add_header Pragma public;
add_header Cache-Control "public";
}
location ~ ^/(app|config)\.php(/|$) {
try_files @heroku-fcgi @heroku-fcgi;
internal;
}
All done!
Now all that is needed is to add whatever .env variables you need to your application settings, and you are of to the races.
You can find all the example code on my github; Tchilly/laravel_heroku