Deploy Laravel to domain subfolder - php

Intro
I am struggling to deploy a Laravel project to a domain subfolder... Here's the complete "story":
My teacher has control of the machine at: http://ilp.fe.up.pt/
He created a user for me called mitocondrias so I could use it via ssh like:
ssh mitocondrias#ilp.fe.up.pt
He also set the usual public_html folder to be called html only.
So, to sum up, if I create a simple hello world index.html at /home/mitocondrias/html/ I can successfully see it at http://ilp.fe.up.pt/mitocondrias/
What I did
Now, regarding the Laravel deploy, here is what I did:
I cloned my Laravel project called CoExpr to my user home: /home/mitocondrias/coexpr
I deleted the html folder and created a symlink to /home/mitocondrias/coexpr/public called html with the following command:
mitocondrias#ilp:~$ ln -s ~/coexpr/public/ html
So, here is the final tree structure:
ilp.fe.up.pt/ (the root of my teacher web hosting)
|
|-- mitocondrias/ (my user home, aka /home/mitocondrias)
| |
| |-- coexpr/ (the Laravel project)
| | |
| | |-- [...]
| | |-- public/
| | |-- [...]
| |
| |-- html/ (this is actually not a folder,
| | but a symlink to /home/mitocondrias/coexpr/public/)
I also ran the following commands to set the correct Laravel permissions:
chmod -R 755 coexpr/
chmod -R o+w storage
The problem
After all this, I can successfully see the landing page at: http://ilp.fe.up.pt/mitocondrias/ (the equivalent of localhost:8000 if I were serving the app with php artisan serve)
But when I try to use other routes, like: http://ilp.fe.up.pt/mitocondrias/explorer (the equivalent to localhost:8000/explorer) I get:
404 Not Found
The requested URL /home/mitocondrias/html/index.php was not found on this server.
What did I do wrong..?
Extra info
In case this is relevant, the .htaccess in /home/mitocondrias/coexpr/public/ is as it was generated upon the Laravel project creation with composer - I did not touch it:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
And because of the symlink, this is also the .htaccess at /home/mitocondrias/html/.
End
So, that's it... Aff, this was a long post... Thanks in advance!

This related question provides a debugging hint: Check if the route works with /index.php/, to see if the problem is related to .htaccess.
Loading http://ilp.fe.up.pt/mitocondrias/index.php/explorer works!
So for some reason your .htaccess is not working.
Maybe simply enabling it will fix your problem, as stated in this answer to another related question.

Related

Laravel 5.8 Fresh Deployment Not Working In Hostinger Shared Hosting

I tried to deploy a fresh laravel application. I installed a laravel 5.8 and upload to github repository.
The repository or project folder is working since I tried to run php artisan serve and it successfully shows the laravel home screen page.
I tried to connect in my shared hosting using ssh.
Here's the steps that I've done:
Access thru ssh
create a project folder mkdir my-assignment-calculator then cd my-assignment-calculator
Clone project repository. git clone **repo_url**
Navigate the project folder, cd my-assignment-calculator
Copy .env file, cp .env-example .env
Install composer packages. composer install
Then copy all files in Public folder to public_html folder
Go to public_html then update index.php, nano index.php
In index.php, I update these two lines, the vendor/autoload.php and the bootstrap/app.php
Here's the sample code for #9:
require __DIR__.'/../my-assignment-calculator/my-assignment-calculator/vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once
__DIR__.'/../my-assignment-calculator/my-assignment-calculator/bootstrap/app.php';
Here's the structure of my project folder
-public_html
-domains
--my_assignment_calculator
--my_assignment_calculator
---laravel files
The current output is only white screen. I tried to check the developers tools or devtools, there's no error. It's just a white background. It's the same as before.
I tried to check .htaccess file and I didn't touch this file. Here is the .htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
So what is the expected output
Should display laravel welcome or home screen page
laravel folder is in root then all your public folder files transfer it into public_html then go to your index file in your public_html configure by adding laravel directory like this:
require __DIR__.'/../laravel/vendor/autoload.php';
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';

Laravel 5 shared hosting deployment issues

I am trying to deploy laravel 5.2 on shared hosting.
I installed the laravel core app folder a level above the public_html folder (/../public_html or /home/username ).
I extracted the files in the laravel core public folder (public) and pointed the index.php file to the laravel core app folder a root above (/../laravel-app/bootstrap/autoload.php and /../laravel-app/bootstrap/app.php).
The website homepage is working but pages links are returning a 404 and it isnt pulling in any css or js.
Any ideas on how I can fix this?
Points to note:
I am on php ver 5.6
I changed the file permission in laravel-app/storage to a 777
I deleted the .htaccess fil in the public_html folder
Server is apache
Buddy just follow this tutorial, I tried it and it's working just fine!
https://medium.com/laravel-news/the-simple-guide-to-deploy-laravel-5-application-on-shared-hosting-1a8d0aee923e#.5lywcjmnn
Btw, you don't even need the last part after this sentence
If you don’t have composer installed already on your server, you can easily grab it to the project directory then.
In my case I did the following:
placing laravel project outside of public_html as lara folder
then I had this htaccess in it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
and inside public_html I had .htacess with:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^awesome
RewriteRule ^(.*)$ awesome/$1 [L]
Where awesome is the directory that contains htacess (listed down) and index.php, favicon and robots.txt:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
final change is in index.php:
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylorotwell#gmail.com>
*/
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels nice to relax.
|
*/
/* die(__DIR__); /home/{YOURHOST}/public_html/awesome */
require __DIR__.'/../../lara/bootstrap/autoload.php';
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/../../lara/bootstrap/app.php';
/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);

Laravel as an addon to an existing site

I have my site already built up in say: http://example.com
My Directory structure is:
root/
|
-- htdocs/ ---> This is the document root. All the front end scripts are located here.
| |
| - css/
| - ...
|
-- cms/ --> This is the backend. This site is like a cms Driven Site.
Now I wanted to install laravel in the back end. I want to build my CMS using laravel. But I don't want the URL to be like: http://example.com/cms/public/
I want it just to be: http://example.com/cms/
I know, I can just place all the folders outside in the document root and rename the public folder and change some settings to achieve what I want. But I don't want my document root to have all those files & folders and files mixed with my front end related files & folders. I want this whole thing to be separate and easy to use in other web site back end.
In order to achieve this, I have used 2 .htaccess files.
In the document root, I have:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^cms/$ cms/public/ [L]
RewriteRule ^cms/(.*)$ cms/public/$1 [L]
</IfModule>
And inside the cms folder I have:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
I am not very good at writing .htaccess or doing mod_rewrite.
Can anybody help me
You could achieve what you want in this way:
Create a directory junction for the public folder from laravel to your backend target folder:
mklink /J C:\www\htdocs\app-frontend\cms C:\www\htdocs\app-backend\public
The first path is for the alias and the second path is for the real folder. Here app-backend would be the folder where laravel is installed to.
This is how I deal with Laravel in shared hosting.
If you were to opt for the mod_rewrite approach, the .env file will be exposed.

Simulating MVC/CoC folder structure in php with mod_rewrite

My project directory structure:
project_root/
|- src/
| |- model/
| |- view/
| |- controller/
| -- ...others
-- resources/
|- css/
|- js/
-- images/
Ok, somewhere in src there is a file responsible for redirecting controllers, views and so on, let us say it is the src/url_mapping.php, it is prepared to solve any kind of url, such as /{controller}/{action}/{id}?{query}, there is no problem here, the big deal is I do not want the user realize this folder structure exists, I want to hide it from them and let the flow as simples as RoR and other based systems.
I want allow only http://host/{css,js,images}/* from resources without allowing http://host/resources/* itself, and http://host/{controller}/{action}/{id}?{query} to the src/url_mapping.php file denying direct access to http://host/src.
For now, the closest I managed is the following .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/resources/$1 !-f
RewriteCond %{DOCUMENT_ROOT}/resources/$1 !-d
RewriteCond %{DOCUMENT_ROOT}/resources/$1 !-l
RewriteRule (?!^resources/|^src/url_mapping.php/)^(.*)$ src/url_mapping.php/$1 [L,PT]
RewriteCond %{DOCUMENT_ROOT}/resources/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/resources/$1 -d [OR]
RewriteCond %{DOCUMENT_ROOT}/resources/$1 -l
RewriteRule (?!^resources/)^(.*)$ /resources/$1 [L,PT]
</IfModule>
In that case my first intention was: If users tries to get /resources or /src folder it will be filtered by src/url_mapping.php as well, although I am trying several combinations with no success over and over again.
I noted mod_rewrite are in loop such a way I can not block resources directory as well as src/url_mapping.php, I am limited to .htaccess file with my hands tied, but if there is another way to do that I will accept the solution.
The last choice is do only one redirect and treating even files in resources by src/url_mapping.php, but it will kill the cache, I am avoiding reinvent the wheel.
Well, I realised apache is not so smart, so an acceptable solution I found was changing the directory tree to hide the non-resources content such a way apache cannot access.
Imagine the DocumentRoot is /home/user/www, so if you browse to http://hostname/somefile will point to /home/user/www/somefile, this way files in /home/user/somedir/somefile is not accessible by apache, but could be accessible by your PHP script if it has ownership/permission over that directory.
So, everything in resources I put on /home/user/www and the src was on /home/user/private_html, only my url_mapping.php stayed on /home/user/www/index.php, this way I can fool the user if I throw 404 Not Found when direct accessing it.
The final tree:
project_root
|- private_html
| |- model
| |- view
| |- controller
| -- ...others
-- public_html
|- css
|- js
|- images
-- index.php
Where my .htaccess file is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase / # Change it if there are a subdirectory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
And my index.php is:
<?php
if (!array_key_exists('REDIRECT_URL', $_SERVER) && substr($_SERVER['REQUEST_URI'], 0, strlen($_SERVER['SCRIPT_NAME'])) == $_SERVER['SCRIPT_NAME']) {
header('HTTP/1.0 404 Not Found');
exit;
}
define('VALID_KERNEL', true);
require '../private_html/core/core.php';
$server = new Server();
$server->request(array_key_exists('PATH_INFO', $_SERVER) ? $_SERVER['PATH_INFO'] : '/', $_REQUEST);
?>
This way it permits to access index.php only if it is the DirectoryIndex, I mean, http://hostname/ is allowed but http://hostname/index.php is 404 Not Found because this file should not exist, as well as http://hostname/index.php/anything and http://hostname/index.php?anything, although http://hostname/anything will redirect to http://hostname/index.php/anything but with REDIRECT_URL so it is allowed.

htaccess rewrite rule accesing folder without redirect

I have an root folder like this:
ROOT
|- Service
| |- Admin
|
|- Service 2
I'm interesting in accessing the admin folder from service folder
To access the admin path via url i use mydomain.com/Service/Admin
Is there any way to make it access like this mydomain.com/Admin without moving folders?
I got the answer from one guy and is working, but is not working if i don't add / at the end of Admin
RewriteEngine On
RewriteRule ^(Admin/.*)$ Service/$1 [L,NC]
mydomain.com/Admin/ - working
mydomain.com/Admin - not working (404 error)
I want both to work
I made is like this and is working
RewriteEngine On
RewriteRule ^Admin$ http://mydomain.com/Admin/ [L,NC]
RewriteRule ^(Admin/.*)$ Service/$1 [L,NC]

Categories