Hosting & Domaining Forum

Hosting Discussion => VPS Hosting => Topic started by: charli19 on Dec 20, 2022, 12:55 AM

Title: Configure Yii2 for virtual webhosting
Post by: charli19 on Dec 20, 2022, 12:55 AM
Hi everyone,

I am currently facing an issue with uploading a website on Yii2 to virtual web hosting. While everything works perfectly fine on the locale, the hosting gives out the page "Apache Test Page powered by CentOS". Upon researching, I found that this is due to the lack of an index file.

I tried the solution mentioned on various forums, which advised registering Options + Indexes in the root of the website's .htaccess file - but that didn't help either. I checked the error log and found an entry saying "Directory index forbidden by Options directive". Unfortunately, I don't have access to the httpd.conf config.

So, I decided to return the .htaccess file back to the root in a specific format. Here is how it looked like:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ web/css/$1 [L]
RewriteRule ^js/(.*)$ web/js/$1 [L]
RewriteRule ^images/(.*)$ web/images/$1 [L]
RewriteRule ^fonts/(.*)$ web/fonts/$1 [L]
RewriteRule ^sitemap.xml web/sitemap.xml [L]
RewriteRule ^robots.txt web/robots.txt [L]
RewriteRule (.*) /web/$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php

I hope this helps anyone facing a similar issue.
Title: Re: Configure Yii2 for virtual hosting
Post by: sanwalahmed on Dec 20, 2022, 01:26 AM
Hello,

If you encounter a missing index page, it can result in either a 403 error or yii2 may end up terminating with an error. In such cases, the issue is most probably with the .htaccess settings and urlManager.

To tackle this problem, you can try the following steps:

1. Create a .htaccess file with the content mentioned below in the root directory of your project:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine on
</IfModule>

<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_URI} ^/.*
  RewriteRule ^(.*)$ web/$1 [L]

  RewriteCond %{REQUEST_URI} !^/web/
  RewriteCond %{REQUEST_FILENAME} !-f [OR]
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^.*$ web/index.php

</IfModule>

2. Create another .htaccess file in the web directory with the content mentioned below:

RewriteEngine on

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

RewriteRule . index.php

After completing these two steps, check whether the site is working or not. Once you complete these manipulations, the web directory should leave the address bar. You don't need to write additional parameters for connecting resources in .htaccess; add all resources to the web directory and configure AssetBundle.

Lastly, if your project is located in a subdirectory, specify the parameter mentioned below in the configuration file:

'request' => [
  'baseUrl' => '/name_folder', // there should be a slash before the name
],

I hope this explanation helps you resolve the issue.
Title: Re: Configure Yii2 for virtual hosting
Post by: sujitbikash on Feb 22, 2023, 02:42 AM
It is possible to move files from "backend/web" to "frontend/web/admin" directly and then adjust the paths to "frontend/web/admin/index.php". Another option is to separate the engine and webroot into different folders - for instance, index.php can be placed in "mysite/public_html/" while the engine can be moved to "mysite-engine/". In this case, it is only necessary to adjust the paths in the index files.

This method also enables running multiple sites on the same hosting. However, sessions must be stored in a database; otherwise, if not, login will work on all sites at once, including mysite itself (if it is on Yii). With the database storing, login on one site will overwrite the login on the other.

I have stumbled upon an issue where Apache complains about the absence of the index.php file if a symbolic link to the web is created. I didn't entirely understand why but resolved the problem by adding a slash in .htaccess: "RewriteRule . /index.php".
Title: Re: Configure Yii2 for virtual webhosting
Post by: Atcomaart on Jan 17, 2025, 01:56 AM
The "Directory index forbidden by Options directive" error message is typically caused by the Options directive being set to -Indexes in the Apache configuration. This directive prevents the server from listing the contents of a directory when a directory is requested.

To resolve this, you can try adding the following lines to your.htaccess file: Options +Indexes and DirectoryIndex index.php. This should allow the server to display the index.php file when a directory is requested. Alternatively, you can try setting the Indexes option to +Indexes in the Apache configuration file, if you have access to it.