Setting the DocumentRoot in Laravel - php

I am wanting to host multiple laravel projects as subfolders within a domain, so for one project the laravel code base would reside at somedomain.com/project1 and another project at somedomain.com/project2, and so on.
Is there a way to tell laravel that the document root is actually in a subfolder of the top level directory of the domain rather than the top level directory itself?
I had previously setup each project as a 2nd level domain with each having it's own DocumentRoot config in Apache VirtualHost directives (such as project1.somedomain.com & project2.somedomain.com), but I want to switch to using subdirectories and have one top level directory as the Apache DocRoot and the individual projects as subfolders.
What is the best way to do this?

Yes this is possible. There are a few howevers though.
First off, normally you'd put laravel's public directory as your webserver's document root. In this case you'd rename the public directory to be the name of the subfolders.
You also need to ensure that your Laravel code (i.e. not public) is an extra level back from the public folder (so you keep your code away from possible access). You'll probably want to put the two separate apps in their own folders too. Now change all the paths in index.php and the paths.php file to make sure that each application points at the right supporting code.
You'll end up with something like this:
/path/to/docroot-parent/
app1/
app/
bootstrap/
paths.php ('public' => __DIR__.'/../../actualdocroot/app1')
...
app2/
app/
bootstrap/
paths.php ('public' => __DIR__.'/../../actualdocroot/app2')
...
actualdocroot/ ← webserver points here as docroot
app1/
css/
js/
index.php (points to ../../app1/bootstrap/autoload.php and ../../app1/bootstrap/start.php)
app2/
css/
js/
index.php (points to ../../app2/bootstrap/autoload.php and ../../app2/bootstrap/start.php)

Related

PHP folder structure: public/index.php has no access to higher level files

In my own website, I try to refactor the folder structure of my webapps.
All files and folders as shown below are on the same server.
The desired structure is as follows:
my_webapps // Top level directory
apps/ // Subdir that holds the different web apps
app1/
public/
css/
js/
index.php
app/ // app internal src code
app2/
app3/
public/ // $_SERVER["DOCUMENT_ROOT"]
css/
js/
index.php // Entry point
views/
header.php
menu.php
The root index.php file lies in the top level public folder for security reasons.
Looking at this answer that makes sense to me.
So in my "menu.php" (included in root index.php) I have some <a href="[link]"> tags to point to the URL of the different "apps/index.php" files.
But that does not work. The web server does not know the content in the "apps/" folder.
But how does this work with frameworks like Laravel then, like this post says?
Where is the mistake here and does anyone have an idea to solve this while still having the security benefits?
I am using nginx as a web server by the way. .htaccess is not an option.

How to deploy yii2 with a web root other than /web

I am working on a yii2 project, and would like to deploy it to an apache server where the entire website is expected to exist in the /public_html directory, and /public_html is the web root for that host. The issue is that yii2 expects the web root to be /web, and it expects most of the site hierarchy to exist outside of the /web directory.
I would prefer not to have to change the webroot to /public_html/web, and for future projects, it may not be an option. So, is there a way to place the entire site hierarchy in /public_html , and still have the site work correctly?
I have tried creating a /public_html/.htaccess file with the following contents:
RewriteEngine on
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
And this seemed to work correctly, at first. Nothing outside of /public_html/web was accessible, and files within subdirectories of /web, such as /web/js/signup.js which could be accessed via the url http://localhost/js/signup.js, but this caused my routing rules (which were set up on a local system where I could have the web root pointing at /web) to no longer be recognized.
So, I would be happy with either an alternate to the .htaccess route, or a suggestion to how to fix the routing rules so that the .htaccess route would work.
Edit for Clarification
I have a hosting provider where the web root is ~/public_html . Yii has documentation explaining that you can rename www to public_html, and have everything that would normally be outside of /www located in ~, along with the various dotfiles and user-documents that Linux normally places in ~. I would prefer to have all site-related directories together (under the webroot).
As an alternative, a two-directory structure, such as the following would work:
~/
|--- (Various dotfiles and subdirectories unrelated to the web site)
|--- yii - Contains all subfolders that should be outside of the web root
|--- commands
|--- config
|--- controllers
|--- (etc...)
|--- public_html - Contains everything that can be within the web root.
|--- (etc...)
This has been asked so many times that they made a page for it in the Yii2 docs.
I personally answered this too many times to remember.
This can be done via the use of aliases.
Move your web directory to the new webroot (assuming ~/mysite.com is your existing site):
rm -rf ~/public_html
mv ~/mysite.com/web ~/public_html
Then, you move everything that was outside of your webroot to the new directory:
mv -prf ~/mysite.com ~/mysite.example-external
At this point, you should have a web root at ~/public_html , and you should have everything else in ~/mysite.example-external. You then edit your ~/public_html/index.php file. Please note that this is not considered part of yii core, so it's ok to modify it. The new index should have some variation on this:
<?php
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
$thepath = __DIR__ . '/../mysite.example-external';
require( $thepath . '/vendor/autoload.php');
require( $thepath . '/vendor/yiisoft/yii2/Yii.php');
$config = require( $thepath . '/config/web.php');
(new yii\web\Application($config))->run();
From here, you also have to add aliases to your ~/mysite.example-external/config/web.php file, so yii can find its core files. In the web.php, at the point where $config is assigned a value, modify it to look something like the following:
$config = [
'aliases' => [
'webroot/assets' => '/Users/XXXXXX/mysite.example-external/web/assets',
'root' => '/Users/XXXXXX/mysite.example-external/web',
],

using laravel in a subdirectory

It seems to me that the idea behind laravel is that the public/ directory is where your DocumentRoot points to and that the app/, vendor/, bootstrap/ and build/ directories all live outside the web root. But what happens if you want the laravel project to live in a subdirectory.
ie. http://www.domain.tld/ might be static HTML, http://www.domain.tld/phpbb/ is phpBB and you wanted your laravel app to live at http://www.domain.tld/app/.
Should you just put the entire laravel folder in http://www.domain.tld/app/ and redirect, via .htaccess, all requests to http://www.domain.tld/app/public/? That seems inelegant.
Maybe you could put laravel in http://www.domain.tld/ and rename the /public/ directory to /app/ but that seems even more inelegant.
Any ideas?
The question is why you need it in subdirectory. So far I never need to put custom project into subdirectory. I use domain or subdomain if needed.
You could try with:
renaming your public directory to the directory name you want to use
put other directories into your root domain directory
add .htaccess for those directories with deny from all (to block access from your root domain)
edit paths in bootstrap/paths.php
I haven't tested it and I wouldn't probably even try to do that. Many frameworks are rather created to use them on separate domains or subdomains and not in directories so I would recommend you to rethink it.
Use virtual hosts in your web engine (eg. apache, nginx, IIS, etc.) and point the document root for your alias to your laravel app.

codeigniter folder structure on webserver

I want to upload code igniter folder to remote server. Should I upload codeigniter root folder to my public_html folder so I got following structure
public_html/
codeigniter/
application/
system/
....
or should I upload application system ... directly to my public_html,
and if I upload under codeigniter folder can I point somewhere in config to my codeigniter library in a way that my links remains without /codeigniter/
it's better to do like:
application/
system/
public_html/
index.php
for security reason, put your application and system folder outside your public_html
Form the user guide
For the best security, both the system and any application folders
should be placed above web root so that they are not directly
accessible via a browser.
So my setup is to have a custom public directory in the codeigniter codebase and have the index.php inside it.
And copy the .htaccess and index.html from either system or application folder to the codebase to forbid access to the base.
It will depend on the apache configuration on your server.
From my POV, it is better to have a structure like :
public_html/
codeigniter/
application/
system/
[...]/
Then, make your apache configuration point to this folder with something like :
<VirtualHost *:80>
DocumentRoot "path_to_the_folder/codeigniter"
ServerName yourDomain.loc
ServerAlias www.yourDomain.loc
<Directory path_to_the_folder/codeigniter/ >
#Your options
</Directory>
</VirtualHost>
and your /etc/hosts file look like :
127.0.0.1 yourDomain.loc
127.0.0.1 www.yourDomain.loc
You would typically upload everything under /codeigniter. So you would have a directory structure like:
- public_html
- images
- js
- system
- application
- other codeigniter folders

Storing multiple PHP apps on one domain

I have got myself a server, and on it I'd like to host my portfolio. My portfolio will be built using a tiny PHP MVC framework I wrote myself. This will be accessed by going to domain.com (for instance).
Now, on my portfolio, I want to host all of the other pieces of work I do. These will also be PHP MVCs. So, for example, domain.com/app1, domain.com/app2 and so on. However, as you probably know, the nature of a PHP MVC means that some files are not meant not be in the DocumentRoot (talking about Apache here). So, if you wanted to store all of your work like this, how would you go about achieving it?
An example directory structure of one of my apps:
/application
/views
/models
/controllers
/public
/js
/css
Therefore domain.com/css should point to that apps css folder which is in the public folder, as should domain.com/app1/css point to its css folder in its public folder.
I'm just trying to work out a good way of organising my work. I would really appreciate anyones thoughts!
I would suggest that you use an apache alias for each new application :
http://httpd.apache.org/docs/2.2/mod/mod_alias.html#alias
You will keep only one Virtual Host :
<VirtualHost *:80>
ServerName domain.com
Alias /app1/ /var/www/app1/public/
Alias /app2/ /var/www/app2/public/
</VirtualHost>
Well
I would have my folders something like this:
/lib (main portfolio lib)
/projects
/project1 (main lib for project 1)
/public_html (main portfolio public root)
/projects
/project1 (main public root for project 1)
The only thing here is that for project 1, you'd have to set the lib folder to
../../../lib/projects/project1
as opposed to it's standalone
../lib
I hope that helps

Categories