Yii 2 application on shared hosting server

I did some testing at my host provider and I guess I was wrong.
/web will be “non-existent” even from the subdomain.
It probably should work for virtual hosts.
Something like this should work (it seems so in my case):

<If "%{HTTP_HOST} != 'sub-domain.my-domain.in'">
Redirect 404 /web
</If>

Ok thank you

Hi, when I log in to the control panel then I see public_html folder in the file manager.

Now I create the subdomain under public_html. Now I can see the subdomain folder appearing in the public_html.

Now should I move the contents of the web folder inside the subdomain folder which is under public_html?

Should I create a folder at the public_html level with the name app and move the application files inside it.

So the directory structure would look like something.

app (all application files)
public_html
    - subdomain
        - index.php (contents of the web folder)

Yes, that looks good.

Then change the paths:

require __DIR__ . '/../../app/vendor/autoload.php';
require __DIR__ . '/../../app/vendor/yiisoft/yii2/Yii.php';

$config = require __DIR__ . '/../../app/config/web.php';

Relative path did work in my local server (one level up from DocumentRoot)
but it may not work on a shared server (I had to use the actual path).

Ok thank you.

Also the database which we create on the shared hosting server, we give the database name, username and the password while creating.

So in the below line of code of db.php which is inside config folder, what should we set at the place of host, database name, username, password

Do we need to include the prefix as well

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=;dbname=',
    'username' => '',
    'password' => '',
    'charset' => 'utf8',
];

Log in, go to the control panel or customer service and search that info.

Ok, thanks. Do we need to transfer the composer.json and composer.lock files?

Do we also need to transfer the runtime and tests folder?

The composer files are of no use unless you have command line access.

No. You can empty the runtime and the assets folder under “web” (NOT the one under “app”).
Keep the directories, at least for assets you will get “directory not found” if it’s not present.

Ok thank you

Are we supposed to transefer the following files to the server

.bowerrc
.gitignore
.codeception.yml
.docker-compose.yml
.LICENCE.md
reuirement.php
Vagrantfile
yii
yii.bat

As far as I know (and without command line access)
only LICENCE.md

(perhaps requirement.php temporarily)

I transfered all the necessary files and folders on the server app folder which is at the public_html level.

public_html folder contains all the contents of the web folder.

Now I tried to access the subdomin.my-domin.in, then the login page is displayed but not in a correct structure and proper alignment. Most propbably the css, js, and plugins files are not getting linked correctly.

Where should I make the changes now?

Is there any thing needed to change in the config/web.php or the assets file?

Also when I enter correct username and password and click on the Login button, then still the login page is displayed. I have imported the database and also set the host, database name , username, password in the config/db.php

As the structure is

 app  (This folder contains all the application files and folders except web folder)
 public_html
     subdomain  (subdomain folder contains all the things inside the web folder as shown)
         assets
         css
         js
         plugins
         index.php
         .htaccess  

I’m don’t know what’s in plugins thus don’t know what to do with it.

For js and css, how are they used? Are they called directly or are they specified in an asset bundle e.g. under app/assets? By using an asset bundle they can be copied to web/assets automatically.
https://www.yiiframework.com/doc/guide/2.0/en/structure-assets#asset-bundles

Yes , the assets are located under app/assets

Should I move or copy the assets file from app/assets to web/assets?

Do we need to change the basePath and baseUrl?

Below is the AppAsset file located under app/assets

<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace app\assets;

use yii\web\AssetBundle;
use faryshta\disableSubmitButtons\Asset as DisableSubmitButtonsAsset;

/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class AppAsset extends AssetBundle
{
    public  $basePath = '@webroot';
    public $baseUrl = '@web';
     public $css = [
		'https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css',
		'https://use.fontawesome.com/releases/v5.9.0/css/all.css',
		'https://use.fontawesome.com/releases/v5.9.0/css/v4-shims.css',
	
		'css/skins/_all-skins.css',
		'plugins/iCheck/flat/blue.css',
		'plugins/morris/morris.css',
	
		'css/bootstrap.min.css',
        'css/site.css',

    ];
     public $js = [
			'plugins/raphael/raphael.min.js',
		    'plugins/jquery-ui/jquery-ui.min.js',

		    ];

     public $depends = [
		DisableSubmitButtonsAsset::class,
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

Definitely not.

I think you can find out the actual paths by adding this to the end of index.php

echo "@app: " . Yii::getAlias('@app') . "<br>";
echo "@webroot: " . Yii::getAlias('@webroot') . "<br>";
echo "@web: " . Yii::getAlias('@web') . "<br>";

Check what is being published under web/assets. You can remove everything, update and check again.
The web/css is already web accessible (basePath set, should not be copied) .
Check the page source in your browser for the actual paths being used.
Check the browser log, for assets not found?

It outputs the below

@app: /home/username/domains/domain-name/app
@webroot: /home/username/domains/domain-name/public_html/subdomain
@web:

I have checked the borwser console. There are many css and js files for which 403 error is shown.

Below is the login page code which finds the css, js and plugins files. May be there should I need to change the paths

<?php

/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model app\models\LoginForm */

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

$this->title = 'Login';
$this->params['breadcrumbs'][] = $this->title;


?>
<html>
<head>

  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.6 -->
  <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="dist/css/AdminLTE.min.css">
  <!-- AdminLTE Skins. Choose a skin from the css/skins
       folder instead of downloading all of them to reduce the load. -->
  <link rel="stylesheet" href="dist/css/skins/_all-skins.min.css">
  <!-- iCheck -->
  <link rel="stylesheet" href="plugins/iCheck/flat/blue.css">
  <!-- Morris chart -->
  <link rel="stylesheet" href="plugins/morris/morris.css">
  <!-- jvectormap -->
  <link rel="stylesheet" href="plugins/jvectormap/jquery-jvectormap-1.2.2.css">
  <!-- Date Picker -->
  <link rel="stylesheet" href="plugins/datepicker/datepicker3.css">
  <!-- Daterange picker -->
  <link rel="stylesheet" href="plugins/daterangepicker/daterangepicker.css">
  <!-- bootstrap wysihtml5 - text editor -->
  <link rel="stylesheet" href="plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.min.css">

  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->
</head>
<div class="site-login">
    <!--<h1><?= Html::encode($this->title) ?></h1>!-->

<body class="hold-transition login-page">
<div class="login-box">
  <h2><?= Html::encode($this->title) ?></h2>
  <!-- /.login-logo -->
  <div class="login-box-body">
    <p class="login-box-msg">Please fill out the following fields to login:</p>

	<?php $form = ActiveForm::begin(['id' => 'login-form', 'layout' => 'horizontal','options' => ['class' => 'disable-submit-buttons','enctype'=>'multipart/form-data'],
    'fieldConfig' => [
          //'template' => "{label}\n<div class=\"col-sm-5\">{input}</div>\n<div class=\"col-sm-7\">{error}</div>",
        'horizontalCssClasses' => [
           'label' => 'col-sm-4',
            //'offset' => 'col-sm-offset-2',
            //'wrapper' => 'col-sm-7',
           // 'error' => '',
            //'hint' => '',

        ],
    ],]);?>


	<div class="form-group has-feedback">
		<?= $form->field($model, 'username')->textInput(['autofocus' => false]) ?>
	</div>

	<div class="form-group has-feedback">
		<?= $form->field($model, 'password')->passwordInput() ?>
	</div>

	<div class="row">




	</div>

	<div class="row">
      <div class="col-lg-offset-7 col-lg-4">
          <?= Html::submitButton('Login', ['class' => 'btn btn-primary btn-block btn-flat', 'name' => 'login-button']) ?>
        </div>
    </div>

    <?php ActiveForm::end(); ?>

    <!-- /.social-auth-links -->


  </div>
  <!-- /.login-box-body -->
</div>
<!-- /.login-box -->

<!-- jQuery 2.2.3 -->
<script src="../../plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- Bootstrap 3.3.6 -->
<script src="../../bootstrap/js/bootstrap.min.js"></script>
<!-- iCheck -->
<script src="../../plugins/iCheck/icheck.min.js"></script>
<script>
  $(function () {
    $('input').iCheck({
      checkboxClass: 'icheckbox_square-blue',
      radioClass: 'iradio_square-blue',
      increaseArea: '20%' // optional
    });
  });
</script>
</body>
</html>

There are many files which are not loaded and when clicked on them from browser console 403 error is shown.

For example there is .ico file stored in public_html/subdomain/ which is needed on the title bar to be displayed, but not displayed and if I click on the that file link then 403 error is displayed.

Use this as a test case. Try to put the url in the browsers adress field (sub-domain.domain.ico-file). Still 403? If not check the page (raw) source in the browser for the actual path used.

Ok thank you

Hello,

So far I have hosted the Yii 2 application on the hostinger’s shared hosting server.

Now I have another application same as the previous one. I need to host that as well.

I will create another sub domain. For example the subdomain with the name lwa.
So it would look like

public_html
      lwa

I will transfer all the web/ contents inside the lwa folder. Now outside the public_html folder I will transfer all the contents in a folder say app2 other than web folder.

The question I need to ask here is that whether should I transfer the vendor folder as well in app2, or should I use the vendor folder of the previous yii2 application, so that the index.php of the second yii2 application would like as below

<?php

declare(strict_types=1);

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'prod');


require __DIR__ . '/../../app1/vendor/autoload.php';      // link app1 vendor
require __DIR__ . '/../../app1/vendor/yiisoft/yii2/Yii.php';     // link app1 vendor

$config = require __DIR__ . '/../../app2/config/web.php';

(new yii\web\Application($config))->run();

Why I do not want to transfer the vendor folder, because it is the same as the previous yii2 application hosted. It will unnecessary consume the disk space.

In Yii1 it was easy to reuse the framework folder.
In Yii2 I think it’s not recommended to reuse the vendor folder.

Even if it’s doable (I don’t know) there may become differences over time e.g. added components and more, so it’s better to keep them separate e.g. so they can be maintaned locally by composer and uploaded.