How to start the yii application on subdomain?


(Jamess) #1

Hi,
I am using yii2 basic application template. I have built the software in yii2 with Admin LTE theme. Now I have a VPS server on host gator for hosting the software.
Now for example I have a domain example.com . I want that if the user enters url appplication.example.com then the application login page should be displayed. Basically I need to create a subdomain. I have transfered the basic folder in public_html of VPS. If i enter the URL as example.com/basic/web/index.php?r=site/login then the login page is displayed. I dont want the users to type the whole above URL. Only they need the enter application.example.com and then login page should be displayed.

How should I achieve this?


CORS policy: No 'Access-Control-Allow-Origin' error in yii2
(Steve) #2

Maybe you should switch to the advanced template. I have my admin backend site running on a subdomain. You need to set the subdomain up so the root folder is pointing to, for example, /httpdocs/backend/web, which is installed and running on your primary domain. Your subdomain only needs to have an .htacess file.

See https://stackoverflow.com/questions/30583757/yii2-remove-index-php-from-url for instructions on cleaning up your url.

Getting frontend and backend to share authentication is another matter. By default the template is configured to have separate logins.


(Jamess) #3

I am using basic application template only. So can you tell me the detail step by step instructions for what I need?


(Steve) #4

What exactly are you trying to do? Are you wanting to install the basic template on a subdomain as a standalone application? Or are you wanting to divide your application between primary and subdomain, for example for front-end users and backend admins? If the former, then you would just install it on the subdomain like you have on your primary domain. If the latter, then I think you need to switch to the advance template.


(Jamess) #5

The former one you mentioned, installing basic template on subdomain


(Steve) #6

Follow the installation guide here: https://www.yiiframework.com/doc/guide/2.0/en/start-installation. If you run into trouble or have specific questions, post it here.


(Jamess) #7

Thanks for reference


(Jamess) #8

Hi, I have a VPS server with host gator. I have a basic application template. I created a sub-domain. The document root is /public_html/
Now when I see my public_html then I can see the folder named with the subdomain. Now I open that subdomain folder and installed the yii basic template in it using SSH and putty. The basic template has been installed. Now for example my subdomain is application.
Please assume my website is soft.com
Now when I enter application.soft.com then I see the folders basic/ and cgi-bin/.

Now when I click on basic/ I see all the folders that got installed while installing yii. There is a web/ folder and when I click it, I see the Congratulations page meaning that yii basic application template has been successfully installed.

Now every time I need to enter application.soft.com, then click on basic then click on web and then I would see the Congratulations page. I dont want this to happen.

I want that when I simply enter application.soft.com in the URL then the Congratulations page should be displayed.

How to achieve this?


(Steve) #9

You’ll need to change your document root directory to public_html/basic/web. Do you have linux server? If so, then you need to update httpd.conf. (https://stackoverflow.com/questions/5891802/how-do-i-change-the-root-directory-of-an-apache-server).

If you use Plesk, go Websites and Domains --> Hosting Settings and there you’ll be able to specify your document root relative to public_html.


(Jamess) #10

I am using VPS server on Host gator. I use C panel and I have linux server. I have a subdomain application and the Document Root is /public_html/
When I create the subdomain the document root is /public_html/application
Now in public_html directory, the folder named application is created. In application folder I installed the basic application template and basic folder is created.

Do I need to install one more time the basic template in public_html?

How to achieve this? Can you please tell in detail steps by steps?


(Steve) #11

I don’t use cPanel, but I see on their forums that you can use it to change the document root of a subdomain (but not a primary domain).

Install Yii in /public_html/application using the guide I linked to above. Just make sure you’re in the application directory when you run

composer create-project --prefer-dist yiisoft/yii2-app-basic

Then change your document root to /public_html/application/basic/web. If you have trouble with this step, you may want to call host gator, and they should be able to walk you through it.

Hopefully that will get you going.


(Jamess) #12

Hi, I have successfully created subdomain and transfered my software files into application directory. Now everything is working fine, but I have a dashboard when Admin logs In, and on dashboard I have displayed Chart using chart js to display some database data as graphical representation.
So in my views/dashboard/ I have,

  1. index.php
  2. data.php
  3. app.js

1 . Here, index.php is the dashboard page and on this page I have a code as below:

<div class="box box-success">
            <div class="box-body">
              <div class="chart">
                  
					<canvas id="barChart" width="50" height="20"></canvas>
			  </div>
		    </div>
		</div>
  1. In data.php I have written My SQL query which returns total no of groups each FieldOfficer has.
<?php
header('Content-Type: application/json');
$servername = "";
$username = "";
$password = "";
$dbname = "";

$conn = new mysqli($servername, $username, $password,$dbname);

if ($conn->connect_error) {
   
} 

$sql = "SELECT CONCAT(employee.FirstName,' ',employee.LastName) as FullName, count(*) as TotalGroups from groupdetails, employee WHERE groupdetails.EmpId=employee.EmpId group by groupdetails.EmpId";

$result = $conn->query($sql);

if ($result!=null) {
  
   $data = array();
	foreach ($result as $row) {
	$data[] = $row;
	}
} 
$conn->close();

print json_encode($data);

In above data.php I have mentioned all my database credentials properly. It works

  1. Now in app.js I have foll code,
$(document).ready(function(){
	$.ajax({
		url: "data.php",
		method: "GET",
			dataType: "json",
		success: function(data) {
			console.log(data);
			var emp = [];
			var groups = [];

			for(var i in data) {
				emp.push(" " +data[i].FirstName);
				groups.push(data[i].TotalGroups);
			}

			var chartdata = {
				labels: emp,
				datasets : [
					{
						label: 'SHG Groups',
						backgroundColor: 'rgba(200, 200, 200, 200)',
						borderColor: 'rgba(200, 200, 200, 0.75)',
						hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
						hoverBorderColor: 'rgba(200, 200, 200, 1)',
						data: groups
					}
				]
			};

			var ctx = document.getElementById("barChart");

			var barGraph = new Chart(ctx, {
				type: 'bar',
				data: chartdata
			});
		},
		error: function(data) {
			console.log(data);
		}
	});
});

Everything works very fine on localhost. Graph is displayed properly on localhost. But on the subdomain it does not shows the Graph. When I inspect the code, The error is as below.

Access to XMLHttpRequest been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

How to resolve this?


(Steve) #13

I’m not sure why you’re getting that error since you’re calling a js file in the same directory.

I know this doesn’t directly solve your problem, but you might want to try out this widget:
https://packagist.org/packages/2amigos/yii2-chartjs-widget


(Jamess) #14

Can you tell me where should I find httpd.conf file? I am using VPS server with Host Gator and Cpanel andd Apache is Easy Apache 4


(Steve) #15

I’m on Ubuntu running apache. Each httpd.conf file is located in var/www/vhosts/system/mydomain/conf/httpd.conf.

You can try:

tree -f  / | grep httpd.conf

(Jamess) #16

On entering above command, I get

-bash: tree: command not found


(Jamess) #17

Hi,
I am using yii2 basic application template. I have a VPS server with hostgator and I have created subdomain ifsoft under my primary domain. I have transfered my basic folder through File Zilla into public_html/ifsoft. I have also changed the document root of this subdomain to public_html/ifsoft/basic/web
Everything works fine. I can accessmy admin panel, but on dashboard/index, the graph is not displayed at all whereas on localhost and on primary domain it works very fine. When I inspect the code the error is as below:

Access to XMLHttpRequest been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

So in my views/dashboard/ I have,

  1. index.php
  2. data.php
  3. app.js

1 . Here, index.php is the dashboard page and on this page I have a code as below:

<div class="box box-success">
            <div class="box-body">
              <div class="chart">
                  
					<canvas id="barChart" width="50" height="20"></canvas>
			  </div>
		    </div>
		</div>
  1. In data.php I have written My SQL query which returns total no of groups each FieldOfficer has.
<?php
header('Content-Type: application/json');
$servername = "";
$username = "";
$password = "";
$dbname = "";

$conn = new mysqli($servername, $username, $password,$dbname);

if ($conn->connect_error) {
   
} 

$sql = "SELECT CONCAT(employee.FirstName,' ',employee.LastName) as FullName, count(*) as TotalGroups from groupdetails, employee WHERE groupdetails.EmpId=employee.EmpId group by groupdetails.EmpId";

$result = $conn->query($sql);

if ($result!=null) {
  
   $data = array();
	foreach ($result as $row) {
	$data[] = $row;
	}
} 
$conn->close();

print json_encode($data);

In above data.php I have mentioned all my database credentials properly. It works

  1. Now in app.js I have foll code,
$(document).ready(function(){
	$.ajax({
		url: "data.php",
		method: "GET",
			dataType: "json",
		success: function(data) {
			console.log(data);
			var emp = [];
			var groups = [];

			for(var i in data) {
				emp.push(" " +data[i].FirstName);
				groups.push(data[i].TotalGroups);
			}

			var chartdata = {
				labels: emp,
				datasets : [
					{
						label: 'SHG Groups',
						backgroundColor: 'rgba(200, 200, 200, 200)',
						borderColor: 'rgba(200, 200, 200, 0.75)',
						hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
						hoverBorderColor: 'rgba(200, 200, 200, 1)',
						data: groups
					}
				]
			};

			var ctx = document.getElementById("barChart");

			var barGraph = new Chart(ctx, {
				type: 'bar',
				data: chartdata
			});
		},
		error: function(data) {
			console.log(data);
		}
	});
});

How to resolve this?


(Softark) #18

@SoftwareAccount, the post above (#17) has been merged from another topic that you have started. Please do not start multiple topics regarding the same issue.

IMO, you have to read the guide provided by HostGator. You can not expect us to guide you how to configure the VPS of it.


(Softark) #19

I would rather recommend you that you upload the vanilla basic template (without your specific codes) to your server and confirm that it really works with no problem. You look trying too many things at once.


(Steve) #20

sudo apt-get install tree