Storing multiple PHP apps on one domain - php

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

Related

PHP - How to import assets with multiple web applications

everyone. How are you all?
I'm a beginner at PHP, and I was hoping you could help me with something.
I have two web applications that used to be independent from one another. Now, however, there's the need for them to run in the same server, share the same database and so on.
I thought the best (and more organized) way to do this would be to use a new index.php that require the index.php from each of these applications, but I'm having trouble importing their CSS and JAVASCRIPT correctly.
So, here's the thing:
I had two web applications with the following structure:
bower_components/
templates/
images/
scripts/
index.php
(It's actually larger than this, but the idea is the same).
I tried this, then:
webapp1/
webapp2/
index.php
(webapp1 and webapp2 both have the structure presented above).
My index.php is something like this:
<?php
require_once 'webapp1/index.php';
require_once 'webapp2/index.php';
?>
That... Somewhat works. But then the applications can't correctly import the files from bower_components, for instance. These files are imported in a header.html file located in templates folder. The code is something like this:
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="../bower_components/owl.carousel/dist/owl.carousel.min.js"></script>
<script src="../bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
Used to work just fine when they were, in fact, independent. Now, not so much.
It would work if I changed these to:
<script src="../webapp1/bower_components/jquery/dist/jquery.min.js"></script>
BUT!! I don't want to do that. It's a lot of changes to be made, across a lot of files, there has to be a simpler way.
Anyway, so sorry for the long question, hope it was clear enough. So, is there a way to do this without changing the html files?
Thank you in advance.
Additional notes: I'm running them in the simple PHP built in server, but have Apache available (just didn't want to use it). I'm using SLIM framework in one of them.
You would need to configure your web server.
Apache example configuration:
# Ensure that Apache listens on port 80
Listen 80
<VirtualHost *:80>
DocumentRoot "/www/example1"
ServerName www.example.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/www/example2"
ServerName www.example.org
# Other directives here
</VirtualHost>
And than you can have them as 2 separate applications one in folder
/www/example2 another /www/example1 with own set of libraries each.
For Nginx see tutorial
It is actually not a good idea to share code components from one app to another directly. They can use same library and same version but I would still have 2 copies of them, disk space is cheap!

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.

Routing to a directory outside of Laravel 4

I'm sorry if this is a very vague question, however I have found no other solutions on stackoverflow or the Laravel 4 docs.
I have the following directory structure
main / laravel
forums / forum installation
localhost points to main / laravel / public
I want to access the contents of /forums in my browser like localhost/forums however since a /forums does not exist in main / laravel / public it returns 404.
Now, I simply can just move the contents of /forums into laravel's public directory however I want to try to get laravel separate from other non-laravel directories like forum softwares and wiki softwares.
So to recap, localhost returns 404 when accessing /forums because localhost points to main / laravel / public and /forums is located outside of the laravel installation. I would like to have localhost/forums access the contents of ~forums and not search in ~main / laravel / public. If it's more logical putting ~forums in laravel's public folder, then please do explain to me because I've only been using laravel for around two months on one project.
You can use an Alias in Apache to route a specific URL to a specific directory on the filesystem. For example:
server-wide (will apply to all VirtualHosts served by Apache)
Alias /forums /path/to/forums/install
or, for a specific VirtualHost:
<VirtualHost *:80>
DocumentRoot /path/to/laravel/install
ServerName your.domain.com
CustomLog /var/log/apache2/access.log combined
Alias /forums /path/to/forums/install
</VirtualHost>
Then anything incoming to localhost/forums will be served from that directory.
Your web server's public directory has nothing to do with Laravel, it's not Laravel's public directory!
So IMO there is no problem to put other application's files in the public directory.
You can also create a separate directory for Laravel's public stuff and put that in your main public directory in order to keep things cleaner. You can simply change Laravel's public directory in bootstrap/paths.php file.
For example from
'public' => __DIR__.'/../public',
to
'public' => __DIR__.'/../public/laravel',

Setting the DocumentRoot in Laravel

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)

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

Categories