How to correctly configure VirtualHost with Yii 2 Advanced App

Since I am total newbie to the Apache’s idea of VirtualHosts, in the Yii 2 Guide, section Preparing application, point “4. Set document roots of your web server:” I am totally missing the information where to put this example configuration file that defines an new virtual host?

Should I put it in:

  • Apache’s conf\httpd.conf global configuration file (which section)?
  • The apache\conf\extra\httpd-vhosts.conf file?
  • The htdocs\.htaccess file?
  • The path\to\yii-application\frontend\web\.htaccess file?

Or maybe somewhere else?

My experience of virtual host is only “experimental”.

In Ubuntu:
Create a file (somename.conf) for each vhost in /etc/apache2/sites_available
Use a2ensite to enable
Will be included in /etc/apache2/apache.conf

IncludeOptional sites-enabled/*.conf

In Opensuse:
Create the file(s) in /etc/apache2/vhosts.d
Will be included in /etc/apache2/httpd.conf

IncludeOptional /etc/apache2/vhosts.d/*.conf

Something like that, I may have forgotten some step(s).

Thank you for sharing this info.

I believe that under XAMPP for Windows paths will be similar. Can’t find any similarity to /etc/apache2/sites_available, but /etc/apache2/vhosts.d seems somewhat similar to C:\xampp\apache\conf\extra\httpd-vhosts.conf.

Three questions, if you experience allows answering them:

  1. I understand that using vhost configuration file is a must and I cannot have virtual hosts configuration stored in .htaccess file in some folder, right?

  2. Do you know, if using virtual hosts I can define domain-only solution or I can include also some (none existing) path to folder? I.e. Can I have frontend.test/platform or am I limited to frontend.test only?

  3. Can I / should I use virtual hosts to define existing domains and subdomains or is this solution limited to non-existing (virtual?) domains only, i.e. those that also requires addition to hosts file on client machine, as the guide says.

I.e. I have a domain that points to my Apache installation. Can I use virtual hosts to point this domain locally to some folder? I.e. projects\test\lab\frontend\web\? Or I should achieve this in some other way, as virtual hosts are not meant to be used this way? Thanks!

I only have experience with *Nix systems, zero on Windows. But basically this is what you do:

  1. Define *.conf file in sites-available folder
  2. Make your subdomain pointing to specific subfolder in your www folder
  3. Enable site a2ensite
  4. Reload/Restart your Apache server

If you need specific help in any of the stages let me know

1 Like