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',
],
Related
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!
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.
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)
My server has the following directory structure
/srv
/www
/site.com
/symfonysite
/Other Drupal hosted site files
when I go to site.com my drupal site opens up and I want to be able to access my symfony site on site.com/symfonysite instead of site.com/symfonysite/web/app.php
I created an .htaccess file inside /symfonysite shown below
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /billing/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
But this only gets rid of the app.php part, i.e. site.com/symfonysite/web opens my symfony site and site.com/symfonysite gives No route found for "GET /symfonysite/" error.
What am I doing wrong?
Its the beauty of symfony that it is highly configurable.
You can achieve what you are looking for by following the simple 4 steps:
Create a directory(say project) and put all the content of symfonysite there. You can put this directory any where you like. For simplicity we are going to create the directory in your symfonysite dir.
Move all content of your symfonysite/web to symfonysite
Edit the app.php and or app_dev.php to modify two line as follow:
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
to
$loader = require_once __DIR__.'/project/app/bootstrap.php.cache';
require_once __DIR__.'/project/app/AppKernel.php';
Edit composer.json file to update the value of "symfony-web-dir": "web" and change it to
"symfony-web-dir": "../",
Your modified directory structure may look like:
/srv
/www
/site.com
/symfonysite
/project
/Other Drupal hosted site files
NB: For security reason symfony suggest to expose only the web directory to a web accessible path. You can put the project directory any where you like. Just need to update the path as per your locations.
To give this set-up a little more security you can put a .htaccess file in the project directory
.htaccess
Deny from all
Enjoy!!
You need to make /srv/www/site.com/symfonysite/web the document root of the project.
This can't be done with .htaccess so you need to change apache virtual host config.
This usually lives in /etc/apache2/sites-enabled/yourserver.com.conf
<VirtualHost *:80>
ServerName yourserver.com
DocumentRoot /srv/www/site.com/symfonysite/web
</VirtualHost>
I'm not sure how rackspace sites work, maybe best to open a support ticket with them if you are unable to change these settings. I head they have Fanatical Support :P
The best approach IMHO is to use symlinks. In the site.com/web/content directory you need to run ln -s symfonysite/web sitesymfony. It will create symlink sitesymfony in your web-root that points to /site.com/web/content/symfonysite/web folder. Then your site.com/sitesymfony will call app.php from site.com/symfonysite/web
I'm trying to run a Yii application placed in a subfolder of the root:
-> public_html
----> yii_app
--------> index.php
--------> .htaccess
--------> protected
etc..
The application runs fine with url: http://www.site.com/yii_app
However, deeper controllers and actions redirect me to http://www.site.com
For example: http://www.site.com/yii_app/controller1/action1 will show the home page of the site.
I tried to put in the root .htaccess file rules that ignore the folder yii_app but didn't solve the problem.
RewriteEngine on
RewriteCond %{REQUEST_URI} "/yii_app/"
RewriteRule (.*) $1 [L]
Any suggestions?
Would really appreciate your help.
Thanks.
You forgot to set basePath in your config:
<?php
return array(
'basePath' => 'yii_app',
....
);
'basePath' => 'yii_app/protected',
Almost universal setting for basePath
'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). Once you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After you move the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details, I describe the approach fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory