NeuroAgent

How to Run Yii2 Advanced on Apache in Windows 11

Step-by-step guide to running Yii2 Advanced template on Apache in Windows 11. Learn how to configure virtual hosts, edit the hosts file, and fix application display issues.

Question

How to run Yii2 Advanced template on Apache in Windows 11?

I’m trying to run the Yii2 Advanced template on an Apache server (Windows 11) following the official instructions. However, when opening the frontend.test and backend.test URLs, only the default “It works!” message is displayed instead of the expected application content. What needs to be done to properly configure and run Yii2 Advanced on Apache?

NeuroAgent

Running Yii2 Advanced Template on Apache in Windows 11

To run the Yii2 Advanced template on Apache in Windows 11, you need to properly configure virtual hosts for frontend and backend, edit the hosts file, and ensure that Apache has access rights to the project directories. The main problem usually lies in incorrect virtual host configuration or missing entries in the hosts file for accessing frontend.test and backend.test.

Contents

Yii2 Advanced Template Structure

The Yii2 Advanced template has a three-tier architecture with separation between frontend and backend:

yii2-app-advanced/
├── backend/
│   └── web/         # Root directory for backend.test
├── frontend/
│   └── web/         # Root directory for frontend.test
└── common/          # Common components

Each component (frontend and backend) has its own web directory, which should be set as the DocumentRoot in the Apache configuration. This structure requires separate virtual host configuration to work correctly.


Apache Virtual Host Configuration

Apache 2.4 Configuration

For Apache 2.4 (installed via XAMPP or WAMP), you need to edit the virtual host configuration file:

apache
<VirtualHost *:80>
    ServerName frontend.test
    DocumentRoot "C:/path/to/yii2-app-advanced/frontend/web"
    <Directory "C:/path/to/yii2-app-advanced/frontend/web">
        Require all granted
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . index.php
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName backend.test
    DocumentRoot "C:/path/to/yii2-app-advanced/backend/web"
    <Directory "C:/path/to/yii2-app-advanced/backend/web">
        Require all granted
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . index.php
    </Directory>
</VirtualHost>

Apache 2.2 Configuration

For older Apache 2.2 versions, use the following configuration:

apache
<VirtualHost *:80>
    ServerName frontend.test
    DocumentRoot "C:/path/to/yii2-app-advanced/frontend/web"
    <Directory "C:/path/to/yii2-app-advanced/frontend/web">
        Order allow,deny
        Allow from all
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . index.php
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName backend.test
    DocumentRoot "C:/path/to/yii2-app-advanced/backend/web"
    <Directory "C:/path/to/yii2-app-advanced/backend/web">
        Order allow,deny
        Allow from all
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . index.php
    </Directory>
</VirtualHost>

Important: This configuration should be added to the httpd-vhosts.conf file, which is typically located at:

  • C:/xampp/apache/conf/extra/httpd-vhosts.conf (for XAMPP)
  • C:/MAMP/bin/apache/conf/extra/httpd-vhosts.conf (for MAMP)

Editing the hosts file in Windows 11

To resolve domain names frontend.test and backend.test to the local IP address, you need to edit the system hosts file:

  1. Open Notepad as administrator
  2. Open the file C:/Windows/System32/drivers/etc/hosts
  3. Add the following lines:
127.0.0.1    frontend.test
127.0.0.1    backend.test
  1. Save the file (the hosts file has no extension)

Tip: If you can’t save the hosts file, try saving it with a .txt extension, then rename it back to hosts without an extension.


Permissions and Configuration

Setting Access Rights

Ensure that Apache has read permissions for files in the frontend/web and backend/web directories. This can be configured through folder properties or by changing file ownership.

Checking mod_rewrite Module

Ensure that the mod_rewrite module is enabled in Apache. Check the httpd.conf file for the following line:

apache
LoadModule rewrite_module modules/mod_rewrite.so

If the line is commented out (#LoadModule...), uncomment it and restart Apache.

.htaccess Configuration

The frontend/web and backend/web directories should contain .htaccess files with proper configuration for handling clean URLs:

apache
RewriteEngine on

# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward the request to index.php
RewriteRule . index.php

Testing and Verification

Steps to Follow

  1. Restart Apache after making changes to the configuration
  2. Open a browser and navigate to http://frontend.test
  3. Check the application functionality and clean URLs
  4. Repeat the test for http://backend.test

Expected Results

  • frontend.test should display the frontend part of the application
  • backend.test should display the backend part of the application
  • Clean URLs should work correctly (e.g., /site/login instead of ?r=site/login)

Troubleshooting Common Issues

Problem: “It works!” instead of the application

Cause: DocumentRoot points to the default Apache directory instead of the project directory.

Solution: Check the virtual host configuration and ensure that DocumentRoot points to the correct frontend/web and backend/web directories.

Problem: 404 Not Found

Cause: mod_rewrite is disabled or .htaccess configuration is incorrect.

Solution: Enable mod_rewrite and check the contents of .htaccess files in the web directories.

Problem: Access Denied

Cause: Incorrect access rights to project directories.

Solution: Configure access rights for the user running Apache (usually this is apache or www-data).

Problem: Domain names don’t resolve

Cause: Missing entries in the hosts file.

Solution: Add entries for frontend.test and backend.test to the hosts file and restart the browser.

Sources

  1. Official Yii 2 Advanced Project Template Documentation - Installation
  2. Yii2-app-advanced on single domain (Apache, Nginx) - Wiki
  3. How To Configure Apache For Yii2-Advance - Yii Forum
  4. GitHub - yii2-advanced-one-domain-config
  5. How to add a custom virtual host on Windows 11 - DEV Community
  6. How to setup Apache virtual host on Windows using XAMPP

Conclusion

Running the Yii2 Advanced template on Apache in Windows 11 requires proper configuration of virtual hosts, editing the hosts file, and ensuring access rights to project directories. The main steps include:

  1. Setting up separate virtual hosts for frontend.test and backend.test
  2. Adding entries to the hosts file to resolve domain names
  3. Enabling the mod_rewrite module for handling clean URLs
  4. Checking access rights to the web directories
  5. Testing application functionality after restarting Apache

If you follow these instructions, you will be able to successfully set up and configure the Yii2 Advanced template on a local Apache server in Windows 11.