Yii 2 application on shared hosting server

We have a Yii 2 web application. We are planning to host it on the shared hosting server.

Do we need to install the Yii 2 on the shared hosting server or can we directly transfer the files to the public_html folder and it will work?

Will .htaccess file work on the Lite Speed Web Server?

1 Like

It looks like it will. The first example from docs: https://docs.litespeedtech.com/lsws/configuration/#uniqueid

[…] This directive may be set at the server level, the virtual-host level or in the document root’s .htaccess file.

Ok, do we need to install Yii 2 and composer on the shared hosting server or if we transfer the files to public_html then it will work

1 Like

I think you don’t. If you copy exact whole project with vendor folder it should work. However it will be much easier to manage it with composer in the future.

Ok thankyou.

Aw Jeez!.. Might I suggest, uploading your web application in a folder not within the “pubic_html” folder…

BUT separate from the “pubic_html” folder entirely. The “web” folder from your web application will become the “pubic_html” folder by copying that data into the “pubic_html” and redirecting the index.php to wherever you put the web application, for example “yii2-app” folder.

So the directory to your web application would be:
‘/shared_host/yii2_app/’

and the pubic_html would be something like…

‘/shared_host/your_user_name/public_html/index.php’ file exmaple…

<?php
declare(strict_types=1);

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

/**
* require '/shared_host/your_user_name/yii2_app/vendor/autoload.php';
* require '/shared_host/your_user_name/yii2_app/vendor/yiisoft/yii2/Yii.php';

* $config = require '/shared_host/your_user_name/yii2_app/config/web.php';
* 
* '/shared_host/your_user_name/public_html'
*/
require dirname(__DIR__, 1) . '/yii2_app/vendor/autoload.php';
require dirname(__DIR__, 1) . '/yii2_app/vendor/yiisoft/yii2/Yii.php';

$config = require dirname(__DIR__, 1) . '/yii2_app/config/web.php';

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

Meaning we should not put into the public_html?

On localhost we have htdocs/folder1/basic

So on the server, we need to transfer the basic folder in the public_html folder

You should never expose the app root folder in public, eg. as public_html. It’s extremely dangerous.

So on the server, we need to transfer the basic folder in the public_html folder

No. If your app is based on basic Yii template, your public folder should be basic/web and it should be the one and only visible from the outside.
Analyze @ricksmort example config, it is a good way to go.

Ok,

  1. So all the contents from web folder (which is inside the basic folder of localserver) should be transferred to the public_html or only index.php?

  2. When we login to the cpanel of any other hosting panel and click on the File Manager, then we see only public_html. So should I create a new folder say yii2-app and put all the files and folder other than web inside it?

  3. If we transefer all the contents of the web folder inside the public_html, then will it cause any issue for the js files, css which we use in the application?

  4. Can you tell why it is not good to tranfer all the files of the basic folder into the public_html?

  5. If we transefer the basic folder into the public_html and if we use the .htaccess file inside the web folder and the basic folder with the following code, will it still cause security issues?

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php
Options -Indexes

This is my file manager… My yii2 web application isn’t in the public_html folder, expect for the “web” folder… The “web” folder is the “public_html” folder.

I use the code for index.php that I posted up top and this for my .htaccess file that is in the “public_html” folder.

##################################################
## Shared webhosting
# @url https://www.yiiframework.com/doc/guide/2.0/en/tutorial-shared-hosting#add-extras-for-webserver
#

<IfModule mod_rewrite.c>
Options +FollowSymLinks
IndexIgnore */*

RewriteEngine on

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
RewriteRule . index.php

</IfModule>

Hope that helps. Wubba Lubba Dub Dub!

Ok, thanks

What if I want to host the application on the subdomain?

As web folder contains js and css folders with their files, which is getting accessed in the application. So if we move the web folder only in the public_html and rest all other outside the public_html, all the js files, css files and plugins files which are being accessed in the application will work correctly or whether I need to make some more changes or settings in the application configuration or any other code files?

Point DocumentRoot to the web folder.
Keep all files out of public_html (if possible).
Published assets will be “visible” in the web folder.

Edit: Point DocumentRoot to the web folder (if possible).

We will be using Hostinger.in shared hosting server. So when we create a subdomain what are the necessary steps that need to make. The subdomain folder will get created in the public_html?

I don’t know hostinger.in but it has pretty nice documentation I see.
Google it and you will have starting point in seconds.

https://www.google.com/search?q=hostinger+subdomain+config

Dig it a little, try some ways, show us what is wrong then it will be much easier to help you. That’s the way the forum works the best.

Ok, thanks

Suppose we have a domain www.my-domain.in

We will create a subdomain like sub-domain.my-domain.in

So whenever user tries to visit www.my-domain.in then they should see Access fobidden messag and whenever the user tries to visit sub-domain.my-domain.in then our yii2 application should be started.

Check using ftp if you can create a subdir under my-domain (one level above public_html)
You can have a web directory and a parallel directory for the protected files

Then try creating the sub-domain (try using “Custom folder for subdomain” )

To create a subdomain, open Websites → Manage, search for Subdomains on the sidebar and click on it:

You may need to store the content of “web” in “sub-domain”
Modify paths in index.php accordingly

(I would start with simple html scripts to try it out)

Ok thank you. So the sub-domain folder will get created in the public_html folder.

So i need to move the web folder contents in the public_html/sub-domain/

Do I need to create a folder outside the public_html with the name sub-domain and move all the application files except web folder inside it?

Now I need to change the path in the index.php of the web folder inside the public_html/sub-domain/ to point to the sub-domain folder outside the public_html

Question

  1. Will it affect the js, css files and any other files in the application?
    because in the assets folder, I have the file Asset.php which links the js, css, plugins files which are in the web folder.

Will I need to change that as well?

<?php


namespace app\assets;

use yii\web\AssetBundle;

class DashboardAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
       public $css = [
	
			
		'css/bootstrap.min.css',
		
    

    ];
    public $js = [

			'plugins/raphael/raphael.min.js',
		
			'js/popupmodal.js',
		
	
				'js/result.js',

		    ];
}

Also do i need to change the paths in the config/web.php

(Perhaps there is a file manager you can use from the control panel)

I think most questions are already answered.
If it’s not possible to do what I quoted above, all the files will be placed under public_html.

On the other hand, if it’s possible, point the subdomain to …/…/my-domain/web and change the paths in index.php to …/…/my-domain/somefolder (e.g. …/somefolder)
You can see online in the control panel what …/…/ will be.

(And of course you will access this as http://sub-domain.my-domain.in )

If it’s only possible to have subdomain under public_html move the “web” directory there and point the subdomain to it, keep the rest of the files one level above (…/…/somefolder)

Then add “RedirectMatch 404 /web” to .htaccess in public_html
I think you can also create “somefolder” under public_html and add a RedirectMatch.
Files should still be accessable from php.
I don’t know if this solution guarantee security in all situations.

Thank you for the information.