FWagner's avatar

Lighttpd Setup for Lararvel 5

Hello all, I need to get Laravel 5 to run on a lighttpd webserver, changing it is not an option atm. I found this http://laravel-tricks.com/tricks/lighttpd-server-configuration-files :

$HTTP["host"] =~ "example.com$" {
        server.document-root = "/path/to/our/public/"
        accesslog.filename = "/path/to/access.log"

        alias.url = ()
        url.redirect = ()
        url.rewrite-once = (
                "^/(css|img|js|fonts)/.*\.(jpg|jpeg|gif|png|swf|avi|mpg|mpeg|mp3|flv|ico|css|js|woff|ttf)$" => "$0",
                "^/(favicon\.ico|robots\.txt|sitemap\.xml)$" => "$0",
                "^/[^\?]*(\?.*)?$" => "index.php/$1"
        )
} 

...but when the rewrite rule sets in Laravel does not see the parent directory in index.php and therefore fails loading! I get the same behaviour when I set the general server.document-root to the parent folder and rewrite incoming requests to my domain inside the $HTTP["host"] section to public/index.php So my question is how to properly setup Laravel 5 with lighttpd and mod_rewrite and mod_alias available? Thanks in advance !

0 likes
5 replies
Tangente's avatar

Did you get it to run correctly? I am trying and getting same issues! url rewrite doesn't work at all!

FWagner's avatar

Actually yes, it is running here! My poblem was that we are generating the lighttpd.conf out of another file and certain chars had to be escaped. As the original post and my post are incomplete (just showing a part of the lighttpd.conf ) and I post you my complete configuration:

server.document-root = "/general/wwwroot"
server.username = "www"
server.groupname = "www"

server.modules =  (
        "mod_access",
        "mod_accesslog",
        "mod_fastcgi",
        "mod_rewrite",
        "mod_redirect",
        "mod_setenv"
)
$HTTP["url"] =~ "\.html" { setenv.add-response-header = ( "Cache-Control" => "no-cache" ) }
$HTTP["host"] =~ "myDomain.test.com+$" {
  server.document-root = "/path/to/laravel/public"
  accesslog.filename = "/var/log/lighttpd-access.log"
  alias.url = ()
  url.redirect = ()
  url.rewrite-if-not-file = (
          "^/(css|img|js)/.*\.(jpg|jpeg|gif|png|swf|avi|mpg|mpeg|mp3|flv|ico|css|js)$" => "$0",
          "^/(favicon\.ico|robots\.txt|sitemap\.xml)$" => "$0",
          "^/[^\?]*(\?.*)?$" => "index.php/$1"
  )
}
fastcgi.server = ( "/fastcgi/monitor.fcgi" => ((
 "bin-path" => "/usr/local/bin/monitor_cgi",
 "max-procs" => 1,
 "socket" => "/tmp/monitor_fcgi.socket",
 "check-local" => "disable"
)))
fastcgi.server +=( ".php" => ( "localhost" => (
 "bin-path" => "/usr/bin/php-cgi",
 "socket" => "/tmp/php.socket",
)))
fastcgi.server +=( ".fcgi" => ( "localhost" => (
 "bin-path" => "/usr/local/wwwbin/uigateway.fcgi",
 "min-procs" => 4,
 "max-procs" => 16,
 "socket" => "/tmp/light_fcgi.socket",
 "check-local" => "disable",
 "kill-signal" => 9,
 "idle-timeout" => 30,
)))
fastcgi.server += ( ".sh" => ( "localhost" => (
 "bin-path" => "/usr/local/wwwbin/bash.fcgi",
 "min-procs" => 4,
 "max-procs" => 16,
 "socket" => "/tmp/light_sh.socket",
 "check-local" => "disable",
 "kill-signal" => 9,
 "idle-timeout" => 30
)))
server.error-handler-404 = "/404.php"
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
fastcgi.debug = 1
server.errorlog = "/var/log/lighttpd/error.log"
accesslog.filename = "/var/log/lighttpd/access.log"
url.access-deny = ( "~", ".inc" )
index-file.names =  ( "index.php", "index.html",
                         "index.htm", "default.htm" )
mimetype.assign =  (
  ".pdf"          =>      "application/pdf",
  ".sig"          =>      "application/pgp-signature",
  ".spl"          =>      "application/futuresplash",
  ".class"        =>      "application/octet-stream",
  ".ps"           =>      "application/postscript",
  ".torrent"      =>      "application/x-bittorrent",
  ".dvi"          =>      "application/x-dvi",
  ".gz"           =>      "application/x-gzip",
  ".pac"          =>      "application/x-ns-proxy-autoconfig",
  ".swf"          =>      "application/x-shockwave-flash",
  ".tar.gz"       =>      "application/x-tgz",
  ".tgz"          =>      "application/x-tgz",
  ".tar"          =>      "application/x-tar",
  ".zip"          =>      "application/zip",
  ".mp3"          =>      "audio/mpeg",
  ".m3u"          =>      "audio/x-mpegurl",
  ".wma"          =>      "audio/x-ms-wma",
  ".wax"          =>      "audio/x-ms-wax",
  ".ogg"          =>      "application/ogg",
  ".wav"          =>      "audio/x-wav",
  ".gif"          =>      "image/gif",
  ".jar"          =>      "application/x-java-archive",
  ".jpg"          =>      "image/jpeg",
  ".jpeg"         =>      "image/jpeg",
  ".png"          =>      "image/png",
  ".xbm"          =>      "image/x-xbitmap",
  ".xpm"          =>      "image/x-xpixmap",
  ".xwd"          =>      "image/x-xwindowdump",
  ".css"          =>      "text/css",
  ".html"         =>      "text/html",
  ".htm"          =>      "text/html",
  ".js"           =>      "text/javascript",
  ".asc"          =>      "text/plain",
  ".c"            =>      "text/plain",
  ".cpp"          =>      "text/plain",
  ".log"          =>      "text/plain",
  ".conf"         =>      "text/plain",
  ".text"         =>      "text/plain",
  ".txt"          =>      "text/plain",
  ".dtd"          =>      "text/xml",
  ".xml"          =>      "text/xml",
  ".mpeg"         =>      "video/mpeg",
  ".mpg"          =>      "video/mpeg",
  ".mov"          =>      "video/quicktime",
  ".qt"           =>      "video/quicktime",
  ".avi"          =>      "video/x-msvideo",
  ".asf"          =>      "video/x-ms-asf",
  ".asx"          =>      "video/x-ms-asf",
  ".wmv"          =>      "video/x-ms-wmv",
  ".bz2"          =>      "application/x-bzip",
  ".tbz"          =>      "application/x-bzip-compressed-tar",
  ".tar.bz2"      =>      "application/x-bzip-compressed-tar",
  # default mime type
  ""              =>      "application/octet-stream"
)

I hope it helps you. Cheers

FWagner's avatar

It works fine here with the above configuration as I said in the fomer post, yes! :)

halfSpinDoctor's avatar

Hi,

Reviving an old thread. This was very helpful, but I had to make one tweak to make this work. Most of these configs have the trailing slash in the server.document-root, but for me, it only works if the trailing slash goes before index.php/$1. I think the issue is that lighttpd requires that a rewritten URL start with a slash.

Raspberry Pi, Linux 4.19.75-v7+ armv7l, Raspbian GNU/Linux 10 (buster), lighttpd/1.4.53 (ssl)

Here is my config:

$HTTP["host"] =~ "(^|\.)photos\.mysite\.net$" {
    server.document-root = "/var/www/photos/public"
    ## rewrite settings for Laravel
    alias.url = ()
    url.redirect = ()
    url.rewrite-if-not-file = (
      "^/(css|img|js)/.*\.(jpg|jpeg|gif|png|swf|avi|mpg|mpeg|mp3|flv|ico|css|js)$" => "LARACASTS_SNIPPET_PLACEHOLDER",
      "^/(favicon\.ico|robots\.txt|sitemap\.xml)$" => "LARACASTS_SNIPPET_PLACEHOLDER",
      "^/[^\?]*(\?.*)?$" => "/index.php/"
    )
}

Please or to participate in this conversation.