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?
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
- Apache Virtual Host Configuration
- Editing the hosts file in Windows 11
- Permissions and Configuration
- Testing and Verification
- Troubleshooting Common Issues
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:
<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:
<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:
- Open Notepad as administrator
- Open the file
C:/Windows/System32/drivers/etc/hosts - Add the following lines:
127.0.0.1 frontend.test
127.0.0.1 backend.test
- Save the file (the hosts file has no extension)
Tip: If you can’t save the hosts file, try saving it with a
.txtextension, then rename it back tohostswithout 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:
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:
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
- Restart Apache after making changes to the configuration
- Open a browser and navigate to
http://frontend.test - Check the application functionality and clean URLs
- Repeat the test for
http://backend.test
Expected Results
frontend.testshould display the frontend part of the applicationbackend.testshould display the backend part of the application- Clean URLs should work correctly (e.g.,
/site/logininstead 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
- Official Yii 2 Advanced Project Template Documentation - Installation
- Yii2-app-advanced on single domain (Apache, Nginx) - Wiki
- How To Configure Apache For Yii2-Advance - Yii Forum
- GitHub - yii2-advanced-one-domain-config
- How to add a custom virtual host on Windows 11 - DEV Community
- 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:
- Setting up separate virtual hosts for frontend.test and backend.test
- Adding entries to the hosts file to resolve domain names
- Enabling the mod_rewrite module for handling clean URLs
- Checking access rights to the web directories
- 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.