Deploying Yii 2 app to document root subdirectory
I want to deploy a Yii 2 application to a site subdirectory:
My application directory structure looks like this:
|-- LICENSE.md
|-- README.md
|-- assets
|-- commands
|-- components
|-- composer.json
|-- composer.lock
|-- config
|-- controllers
|-- mail
|-- models
|-- requirements.php
|-- runtime
|-- test.php
|-- test.txt
|-- tests
|-- vendor
|-- views
|-- web
|-- yii
`-- yii.bat
According to Yii 2 documentation , for deployment, you have to rename the folder web
to your server root, for example public_html
, wwww
etc. Then you copy all files to the parent directory of the document root. But it doesn't say anything about how to deploy the application to a subdirectory of the document root.
The host is Apache and I am wondering if using an alias is the easiest way. I'm not sure about mod_alias documentation which directive to use or where it should go.
What do you recommend?
source to share
It turned out to be easier than I expected.
- Move directory
web
in the document root directory (for examplepublic_html
,www
etc.). - Rename the directory
web
as you want to appear after the domain name. For example, if you want your application to be inhttp://example.com/myapp
, renameweb
tomyapp
. - Move your application directory (which should now be minus a subdirectory
web
) one directory above your document root. - In the directory,
web
change the paths of the three functionsrequire
toindex.php
.
My modified is index.php
similar to the one below. (If you copy and paste, remember to replace your_app_name
with the name of your app directory. Also note that I have multiple sites in the root of my document, so I need to get the parent of the parent of the parent web
to do this from the root of the document. Whew. )
<?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');
require(__DIR__ . '/../../../your_app_name/vendor/autoload.php');
require(__DIR__ . '/../../../your_app_name/vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../../../your_app_name/config/web.php');
(new yii\web\Application($config))->run();
source to share