How to deploy a Laravel app to a sub-directory - php

I'm new to Laravel framework so this question might be a little stupid but here's the problem:
I have a small project on my local machine and now I need to deploy my project to a live environment ( shared hosting ).
The problem is, my project needs to be stored in a sub-directory ( sub-folder ) of the main domain ( eg. my domain will be main.com, and the project will be at main.com/my-project ).
I've created a sub folder inside the public_html of the main domain, then I uploaded my project to that sub-directory. At first, when I went to the url main.com/my-project, I see a directory tree ( like localhost ), then I needed to click to public folder to go to my app. So I looked for the solutions on the internet and I tried the .htaccess way, here's my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/project/public/
RewriteRule ^(.*)$ /project/public/$1 [L,QSA]
</IfModule>
But when I reload the url main.com/my-project, I kept receiving the message URL request not found.
I've been struggling with this issue for a while and still haven't figured it out. Any idea how I can solve this problem? Please help me out, thanks.

Thank you guys for helping, well actually I figured it out somehow. Here's my solutions for anybody who might run into the same problem like I did:
Inside /public_html, I created a folder and named it my-project. Inside my-project folder, I placed all files & assets folders ( css, js ) and .htaccess file here.
Then I went back to the parent level ( same level as /public_html ), I created a folder and name it app-core. I placed all the other Laravel's folders & files here.
The structure is gonna be like this :
public_html
=app-core
--- All others files & folders except **public**
=my-project
--- index.php
--- css
--- js
--- .htaccess
Finally, I went back to edit the index.php like this :
require __DIR__ . '/../app-core/vendor/autoload.php';
$app = require_once __DIR__ . '/../app-core/bootstrap/app.php';
And saved it, then everything was ready to go!

Related

uploading yii2 basic on shared hosting but not working

I am installing Yii2 on a shared hosting environment,Apache, (Godaddy),
here is what I did as per the docs:
Renamed web folder to www
copied all the folders, now the directory structure looks like this:
public_html\
assets
commands
config
controllers
model
modules
views
www\index.php
.htaccess (this is both in public_html and www)
but when I access my domain, I get the following error:
You don't have permission to access / on this server. Additionally, a
404 Not Found error was encountered while trying to use an
ErrorDocument to handle the request.
Here are the contents of my .htaccess which I have copied in both public_html and www.
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
is the htaccess creating problem? Is index.php not being run from the right folder? What else should I look for? any help is much appreciated.
Update:
The actual problem with the above error was that It was a Permissions issue.
Set the permissions from 644 to 755, and now the system is accessible.
BUT
the to access index.php I still need to navigate to it manually by typing in the url : www.example.com/www/index.php
UPDATE
As I don't have prettyurl's enabled, just to make it work, I deleted all the contents of the .htaccess, then I copied the contents of basic folder in the home folder (for future visitors it should be like home/your_user _name
CAVEAT EMPTOR
I know almost nothing about .htaccess. Also my this project is just for learning, this solution may not be useful in production settings.
Question is still open for expert advice on best practices in such scenario.
You need to move your domain pointer to www instead of htdocs, its probably somewhere under domain -> root folder. Your .htaccess looks fine.

Why should we keep index.php in the public folder instead of in the root?

I still don't quite understand why we must keep index.php in a public directory instead of in the root directory.
root/of/project
public/
index.php
.htacess
(html, image, css, etc)
Then, write the following in our virtual host file:
DocumentRoot /path/to/myapp/app/public
<Directory "/path/to/myapp/app/public">
# other setting here
</Directory>
The .htaccess file then redirects all non-existing URLs to index.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
I notice that most frameworks do so, such as Symfony and Zend, just as this tutorial. What is the actual benefits really by having the trouble of modifying the virtual host file?
Why shouldn't we do have this below instead without modifying the virtual host file? Is it a security risk or something?
root/of/project
index.php
.htacess
public/
(html, image, css, etc)
If keeping index.php and modifying the virtual host file is better, how can we modify the virtual host file in the live server then? Let's say I have this domain name, http://my-website.com/ and I access it on my browser, what I see first is not the web page but the directories below untill I click the public directory then I see the home page,
root/of/project
public/
index.php
.htacess
(html, image, css, etc)
I assume that this is the same for Zend or Symfony project as well without being able to modify the virtual host file in the live server. I am no good at these frameworks, I will see this below if I upload my Zend project to the live server,
So, how do you deploy your Zend or Symfony project to your live server to see your web page right away?
It is a very good idea to keep index.php outside of your project root directory because of one simple reason:
You don't want your user to access any files other that one in public folder (index.php, css, js etc). When you will put index.php in root folder you will be also able to access composer.json file for example which is a security risk - a potential hacker will know what packages are you using, in which versions so it's easier for him to perform attack.
When it comes to your hosting - you should have some public_html folder on your root directory which is meant to be public folder (web) of your Symfony app and you should also be able to keep files outside of public folder. If you don't - you really need to think about changing hosting partner
EDIT:
Answering your comment. Let's assume you have public_html folder on your hosting and you want to deploy Symfony app which should be accessible directly on http://your-domain.com. Then you should put whole Symfony project anywhere (but outside of public_html folder) and make a public_html folder a symbolic link to web folder of your Symfony project. This action is equivalent of editing virtual host and changing DocumentRoot which, I assume, you are not able to do.
You can also check my answer on another question to get more clarification

How to deploy laravel 4.2 on shared hosting?

I developed an application with laravel 4.2.8 and now I am having trouble deploying it. I followed this answer https://stackoverflow.com/a/16683938/3153380 but its not working. I am getting a white screen and the headers are returning a 500 Internal Server Error status.
I read around that laravel 4.2 is a bit tricky to set up on shared hosting is this true? I can seem to find a working solution so those that have deployed 4.2 before please help. My folder structure is like below
root/
laravel_base/
app/
...
public_html/
siteroot/
assets/
packages/
uploads/
index.php
...
Any pointers?
First make sure that your shared host runs php >= v5.4. Second try to follow this steps:
Create a folder outside your public_html/ or www/. Ex: project/
Copy every folder and file (except the public folder) from your laravel app into that project/ folder
Copy the content of public/ folder into your public_html/ or www/ (the .htaccess must be there too)
Inside public/ locate the index.php file and change the following paths:
a. Path to autoload.php
require __DIR__.'/../bootstrap/autoload.php';
into
require __DIR__.'/../project/bootstrap/autoload.php';
b. Path to start.php
$app = require_once __DIR__.'/../bootstrap/start.php';
into
$app = require_once __DIR__.'/../project/bootstrap/start.php';`
After all that it should be working.
I did something similar to #Helder Lucas - I also had to edit the bootstrap/paths.php file:
# change: 'public' => __DIR__.'/../public',
'public' => __DIR__.'/../../public_html',
I created a script to setup laravel projects for shared hosting environments. Here is the gist: https://gist.github.com/meganlkm/74dba6c4350ed58bf7bb
Try to play with .htaccess
In your public_html, add a .htaccess to forward the request
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^yoursite.com/public
RewriteRule ^(.*)$ yoursite.com/public/$1 [L]
</IfModule>
i.e.
public_html
├── yoursite.com <-------- laravel project
├── .htaccess <-------- this file
It was pretty easy to get Laravel 4/5 running on OpenShift Online. For anyone else running into hosting issues, check out: https://hub.openshift.com/quickstarts/115-laravel-5-0
I just add a new folder in the / folder on my shared hosting
www.domain.com
Where i add a sub folder called
public/
So that the path looks like this
/www.domain.com/public/
And from the CPANEL i link the domain name root folder to the
www.domain.com/public/
And then i just upload the laravel application in
/www.domain.com
It works for laravel 4.2 and 5.
And there is no security breaches because the root domain folder is in the public folder of the laravel app, no need for updating the .htaccess file.
It just works.

How to route request to codeigniter project outside document root

I have the following shared hosting file structure using a codeigniter project:
myTLD.com/sites/mysite
mysite contains: application, system , index.php ... ( standard CI2 setup )
myTLD.com/public_html - contains : index.php
I have symlinked myTLD.com/public_html/index.php to myTLD.com/sites/mysite/index.php
Unfortunately I am getting:
Your system folder path does not appear to be set correctly. Please open the following file and correct this: index.php
I have set it up this way to avoid placing the actual site in the document root for security purposes . I don't want to change mysite/index.php because I want to keep the entire project in its mysite directory where it can easily be revised etc.
The application and mysite/ folder are set to 755 so I don't think this is a permission problem .
My myTLD.com/public_html/.htaccess folder directs all requests to index.php:
RewriteEngine On
RewriteRule ^(.*)$ index.php
Can someone advise me on an approach to sending requests through to the codeigniter index file without causing this error?
Thank you
You can try following way
1) Remove the symlink
2) Use this in htaccess at myTLD.com/public_html/.htaccess
RewriteEngine on
RewriteRule ^(.*)$ myTLD.com/sites/mysite/index.php?/$1 [L]
Use absolute system path if you are aware of it.

Laravel home page gives 404 but index.php works

I'm creating a custom cms in a subfolder of my site. I installed a fresh copy of laravel at www.mysite.com/cms/ <-- application, bundles, public, etc are there.
I can not make a virtual host due to shared hosting but I managed to remove the need for the /public to be in the url by putting
RewriteCond %{REQUEST_URI} !^/cms/public/.*$
RewriteRule ^(.*)$ /cms/public/$1 [L]
in the .htaccess file in my cms folder.
The thing is, clean urls works www.mysite.com/cms/public/ shows the Laravel starter page. But www.mysite.com/cms/ gives me a 404 not found error and www.mysite.com/cms/index.php shows the Laravel starter page.
I have set the url array element in application.php to
'url' => 'http://www.mysite.com/cms/',
and the index element to
'index' => '',
Has anyone come across this issue?
To use laravel on shared hosting, I would recommend to try this.
By the way, in your .htaccess, you are just redirecting to all non-exist request query to index.php in public directory. Because of the fact that, you should access with url.com/public.
Perhaps if you were to upload the contents of your public folder into /cms, and the other folders into /cms_system, you could change the paths.php file to reflect this?
So, you'd have this:
/cms
(contents of the public folder, with .htaccess)
/cms_system
/application
/bundles
/laravel
/storage
(etc)
Your paths would then look like this:
$paths['sys'] = 'system';
$paths['bundle'] = 'bundles';
$paths['app'] = 'application';
$paths['storage'] = 'storage';
$paths['public'] = '../cms';
I haven't tested this yet, but it should work. Please let me know if there are any problems with it, and will see what can be changed accordingly.

Categories