mikey
mikey

Reputation: 1460

Laravel in a subfolder on nginx server

I am trying to set up laravel in a subfolder (laravel-project) of a project. I somehow got the home page to work doing this in the config file: rewrite "/project/home.php" /laravel-project/public/index.php$1;

However I cannot figure out how to get the routing work. I would like that any request containing laravel-project would be redirected to laravel-project/public/index.php so that laravel can figure out which controller and which method are to be called. I did this:

location /laravel-project {
  root /home/www/virtual/mysite/laravel-project/public;
  try_files $uri $uri/ /index.php?$query_string;
}

However, when I try to navigate to mysite/laravel-project or mysite/laravel-project/contacts the application never hits public/index.php file in the laravel-project folder.

Hope the information given is enough. Please let me know if any further info is required, and thank you for helping!

Upvotes: 2

Views: 2891

Answers (3)

Prashant Mishra
Prashant Mishra

Reputation: 39

I found a proper configuration for deploy laravel in sub-folder

location /websocket {
    alias /var/www/html/websocket/public;
    try_files $uri $uri/ @websocket;
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
                  
    }  
}

location @websocket {
    rewrite /websocket/(.*)$ /websocket/index.php?/$1 last;
}

Upvotes: 4

Hamid Naghipour
Hamid Naghipour

Reputation: 3635

I want to put some new updated tips, maybe it's helps somebody :

If you want to put your laravel project in a subfolder on a server with ngnix-ubuntu 16-php.7.2, so here is update ngnix config :

1) your nested(subfolder) isn't inside your main folder

/var/www/main:
/var/www/nested:

then your config :

location /nested {

        alias /var/www/nested/public;

        try_files $uri $uri/ @nested;

               location ~ \.php$ {
                        include fastcgi_params;
                        fastcgi_param SCRIPT_FILENAME $request_filename;
                        fastcgi_pass   unix:/run/php/php7.2-fpm.sock;

                                }
   }

location @nested {
        rewrite /nested/(.*)$ /nested/index.php?/$1 last;
}

2) your laravel-test folder (subfolder) inside your main :

/var/www/main:
/var/www/main/nested:

then your config :

location /laravel-test {

    alias /var/www/main/laravel-test/public;

    try_files $uri $uri/ @laravelTest;

           location ~ \.php$ {
                    include fastcgi_params;
                    fastcgi_param SCRIPT_FILENAME $request_filename;
                    fastcgi_pass   unix:/run/php/php7.2-fpm.sock;

                            }


  }

location @laravelTest {
        rewrite /laravel-test/(.*)$ /laravel-test/index.php?/$1 last;
}

Upvotes: 3

Surya
Surya

Reputation: 464

# megadealer
    location ^~ /megadealer {  
        alias /var/www/choppies/megadealer/public;  
        try_files $uri $uri/ @megadealer;  

        location ~ \.php {  
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
            fastcgi_param SCRIPT_FILENAME /var/www/choppies/megadealer/public/index.php;
        }  
    }  

    location @megadealer {
        rewrite /megadealer/(.*)$ /megadealer/index.php?/$1 last;  
    }
    # end megadealer

Upvotes: 1

Related Questions