Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...)
like image 412
TMorgan Avatar asked Oct 21 '15 14:10

TMorgan


People also ask

How do I install Yii2 on Windows?

Installing from an Archive File Download the archive file from yiiframework.com. Unpack the downloaded file to a Web-accessible folder. Modify the config/web. php file by entering a secret key for the cookieValidationKey configuration item (this is done automatically if you are installing Yii using Composer):

What is composer in Yii2?

Composer is a tool for dependency management in PHP. Yii2 uses it to install itself and other vendors' modules (for example, bootstrap). It is also possible to install Yii2 in the old way, by downloading the complete package and transferring it to the host, local or remote, where the framework will be installed.


1 Answers

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.

like image 200
Mihai P. Avatar answered Sep 21 '22 19:09

Mihai P.