Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move Laravel 5 application in server subfolder

So, I've developed a fully functional Laravel 5 application and deployed it onto my customer's server, running Apache2, PHP5 and MySQL. The app works as expected and URLs are correctly rewritten. I now need to simply clone my app to another server, which runs exactly the same stack. Problem is I need to put my app in a subfolder, so that it can be accessed via URLs matching the pattern domain2.com/movedApp.

In other words: First server responds to:

domain1.com/my/routes

Second server needs to respond to:

domain2/movedApp/my/routes

I tried to reproduce exactly the VirtualHost directive which works on the first server, adding /movedApp where needed and adding RewriteBase to the .htaccess file in the new public folder, but to no avail.

I've noticed that routes are correctly followed from URLs like:

domain2.com/movedApp/public/index.php/my/routes

Still I get 404 error on assets in Apache2 log.

I hereby report my .htaccess file located in the public folder of my working Laravel application.

.htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine on

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ /index.php [L]

</IfModule>

I also report the configuration file used by the web server to serve my app

default.conf

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www/galmaster/public
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/galmaster/public>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
like image 857
ibanjo Avatar asked Apr 21 '16 09:04

ibanjo


People also ask

Can I deploy Laravel on shared hosting?

Q: How to deploy Laravel on shared hosting Do ZIP your Laravel project. Create a database in your cPanel. Import the local exported database into shared hosting database. Upload project ZIP file to public_html folder and extract.


1 Answers

So, I've been fiddling a lot with VirtualHost(s), but the best solution I found does not involve them at all, and here it is.

First thing to do is to configure .htaccess files for both the app's root folder and its public folder. I found a simple working .htaccess file in this answer, which I report here:

<IfModule mod_rewrite.c>
    RewriteEngine On 
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

The default .htaccess works fine, instead, for the public folder.

The last step is to correctly configure your routes in routes.php. The workaround which works for me is quite rough, and maybe one could find some more refined solution, but still here it is.

// Get base (root) route
$base_route = basename(base_path());

Route::get($base_route, function () {
    return view('welcome');
});

Route::get($base_route.'/myRoute', function() {
    return view('myRoute');
});

So basically you need to prepend $base_route to every route in your app. Hope this helps.

like image 71
ibanjo Avatar answered Nov 13 '22 16:11

ibanjo