4

I have an existing server which is working well hosting a number of sites using nginx and ISPconfig. However I have created a new site and wish to use Laravel.

I've installed Laravel successfully via composer and have got as far as seeing the familiar welcome blade displayed when I visit mywebsite.com/public

What I want to do next is make some clean urls. My experience with vhost files is somewhat limited and I'm having a bit of trouble with the config.

My routes file looks like this

Route::get('/', function () {
    return view('welcome');
});
Route::get('/test', function () {
    return view('test');
});

and I'd hoped to see mywebsite.com/test display the contents of test.blade.php

I'm aware I need to do some work with the vhost file before I can expect this to work but my experience with vhosts is limited and I'm at a bit of a loss.

My current file looks like this

server {
    listen *:80;
    server_name mywebsite.com ;

    root   /var/www/mywebsite.com/web;

    index index.html index.htm index.php index.cgi index.pl index.xhtml;

    error_page 400 /error/400.html;
    error_page 401 /error/401.html;
    error_page 403 /error/403.html;
    error_page 404 /error/404.html;
    error_page 405 /error/405.html;
    error_page 500 /error/500.html;
    error_page 502 /error/502.html;
    error_page 503 /error/503.html;
    recursive_error_pages on;
    location = /error/400.html {

        internal;
    }
    location = /error/401.html {

        internal;
    }
    location = /error/403.html {

        internal;
    }
    location = /error/404.html {

        internal;
    }
    location = /error/405.html {

        internal;
    }
    location = /error/500.html {

        internal;
    }
    location = /error/502.html {

        internal;
    }
    location = /error/503.html {

        internal;
    }

    error_log /var/log/ispconfig/httpd/mywebsite.com/error.log;
    access_log /var/log/ispconfig/httpd/mywebsite.com/access.log combined;

    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location /stats/ {

        index index.html index.php;
        auth_basic "Members Only";
        auth_basic_user_file /var/www/clients/client1/web5/web/stats/.htpasswd_stats;
    }

    location ^~ /awstats-icon {
        alias /usr/share/awstats/icon;
    }

    location ~ \.php$ {
        try_files /5e26a1d85cb98f7191261e023385e60d.htm @php;
    }

    location @php {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/lib/php5-fpm/web5.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
    }
}

Now on another server I have this working with this simple directive

server {
    root /var/www/public;
    index index.php index.html index.htm;
    server_name localhost;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
}

But I am limited to what I can do with the vhost on the current server as ISPconfig writes most of it for me and it refuses to write the above config that worked elsewhere. Also I feel editing the file directly will be bad practice, I'd always be on edge that ISPconfig will rewrite the file for me, so I'm not really sure how best to proceed with this.

My options would be to just go ahead and edit the vhost and hope for the best, but if I do that how would I ensure ISPconfig could not overwrite the file without resorting to "hacky" methods?

Alternatively, is there a config I can enter via ISPconfig that will allow rewrites to happen properly in a way that suits Laravel? In this instance, any directive entered would need to take precedence over the ~ .php$ clause as that is written by ISPconfig before any directives entered via the control panel.

cnst
  • 25,870
  • 6
  • 90
  • 122
Darren H
  • 910
  • 8
  • 24
  • 1
    I've created the *ispconfig* tag (this is now the first question under the tag!), and also removed the nginx tag from this question, because as an nginx specialist, I can't see how I can be of any help here. – cnst Aug 10 '15 at 03:37

3 Answers3

4

I just had the same problem recently. Digging the ISPConfig's sources, I understood it can insert/ merge/ delete location blocks of that default vhosts file. So i did the following:

Sites' menu > choose website > Options

Then I inputed the following on the "nginx Directives" field:

# redirect stuff to the public inner folder
location / {
    root {DOCROOT}/public;
    try_files /public/$uri /public/$uri/ /public/index.php?$query_string;
} 

# merged the stuff people suggests for laravel inside the php block
# mind the 'merge' keyword that did the trick
location ~ \.php$ { ##merge##
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-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;
}
  • Interesting, I wasn't aware of the usage of the `##merge##`keyword... also notice that instead of explicitly putting the fastcgi_pass directive, it's always better to use {FASTCGIPASS} instead (that way a configuration using TCP sockets instead of UNIX sockets will work automatically). This works for me with a few glitches — sometimes Laravel will add .../index.php/... on the URLs and nginx will be utterly confused. And I also get a few broken links which come clearly from things not being correctly parsed. But... it sort of works, much better than what I had! – Gwyneth Llewelyn Jan 09 '17 at 18:51
4

There is a slight problem with Danilo's answer. Php ran but assets like js/css/images stopped loading. Adding the following to nginx directives inside ISPConfig works for me:

location / {
    root {DOCROOT}/public;
    try_files $uri public/$uri/ /public/index.php?$query_string;
}
Nitin...
  • 1,274
  • 10
  • 18
1

I am not expert with this but from my past experience if I have two domains I would define server blocks for each in two different files and place them in /etc/nginx/sites-available/site1.com and /etc/nginx/sites-available/site2.com

but it looks like you already have a website that you access using mywesite.com which is located at /var/www/mywebsite.com/web; (see the root value of your configuration file)

Now you install Laravel in test folder in /var/www/mywebsite.com/test location.

To access this you need can try adding following at the end of your ispconfig file.

Note how I used the relative path to laravel's public folder from the root of the server block.

location /../test/public {
    try_files $uri $uri/ /index.php$is_args$args;
}

For more detailed tutorial try Nginx Server Block Setup.

Hope this helps,

K

karmendra
  • 2,206
  • 8
  • 31
  • 49
  • Thanks for the effort, this would work with a typical NGINX setup. The problem I have is that ISPConfig rewrites much of the vhost files for me and I want to avoid having to take control away from ISPConfig – Darren H Aug 14 '15 at 15:02
  • Oh... Sorry I got the question wrong, unfortunately I had never had a chance to work with ispconfig control panel. – karmendra Aug 14 '15 at 16:25