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

I am working on a yii2 project and would like to deploy it on an Apache server, where it is expected that the entire website will exist in the / public _html directory, and / public_html is the web root node for this host. The problem is that yii2 expects the web root to be / web, and expects that most of the site hierarchy will exist outside of the / web directory.

I would prefer not to change the website in / public _html / web, and for future projects this may not be an option. So, is there a way to put the entire site hierarchy in / public _html and still work correctly with the site?

I tried to create a file / public _html / .htaccess with the following contents:

    RewriteEngine on
    RewriteCond %{REQUEST_URI} !web/
    RewriteRule (.*) /web/$1 [L]

And that seemed to work right at the beginning. Nothing outside / public _html / web was accessible and the files in the / web subdirectories, such as /web/js/signup.js, which could be accessed via the url http: //localhost/js/signup.js , but this led to my routing rules (which were configured on the local system where I could have a web root pointing to / web) no longer being recognized.

So, I would be pleased either with an alternative to the .htaccess path, or with a suggestion on how to fix the routing rules to work with the .htaccess route.

Edit to clarify

-, - ~/public_html. Yii , , www public_html , /www, ~, dotfiles , Linux ~. , , ( webroot).

, :

~/
 |--- (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...)
+4
2

, Yii2.

, .

+2

.

- webroot (, ~/mysite.com - ):

rm -rf ~/public_html
mv ~/mysite.com/web ~/public_html

, -, :

mv -prf ~/mysite.com ~/mysite.example-external

- ~/public_html, ~/mysite.example-external. ~/public_html/index.php. , yii, . :

<?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();

~/mysite.example-external/config/web.php, yii . web.php, , $config , , :

$config = [
    'aliases'   => [
        'webroot/assets' => '/Users/XXXXXX/mysite.example-external/web/assets',
        'root' => '/Users/XXXXXX/mysite.example-external/web',
    ],
+2

Source: https://habr.com/ru/post/1612600/


All Articles