I often use Apache virtual hosts for development and testing, but each time I need to configure a virtual host, I habitually copy and paste. This time, after reinstalling the system, I needed to configure a new PHP development environment virtual host, so I’m summarizing the methods and steps for configuring Apache httpd-vhosts virtual hosts for easy reference and lookup.
Development environment: WAMP
Website: http://www.wampserver.com/en/
Example 1: Steps for Configuring an Apache localhost Virtual Host
1. Open the httpd.conf file in the Apache directory with Notepad (e.g., D:/wamp/bin/apache/apache2.2.8/httpd.conf) and find the following modules:
- #Virtual hosts
- #Include conf/extra/httpd-vhosts.conf
Remove the # at the beginning to enable the httpd-vhosts virtual host file. At this point, restarting the WAMP environment will fail to open localhost; you need to configure it in httpd-vhosts.conf first.
2. Open the httpd-vhosts file with Notepad, configure the localhost virtual host, and modify it as follows based on the examples in the httpd-vhosts file:
- <VirtualHost *:80>
- ServerAdmin [email protected]
- DocumentRoot "D:/wamp/www"
- ServerName localhost
- ServerAlias localhost
- ErrorLog "logs/dummy-host.localhost-error.log"
- CustomLog "logs/dummy-host.localhost-access.log" common
- </VirtualHost>
Modify the configuration as follows:
DocumentRoot: change to the www directory of your local WAMP environment (e.g., D:/wamp/www)
ServerName: change to localhost
3. Restart Apache, and you will find localhost opens normally. Configuring localhost is relatively simple.
Example 2: Steps for Configuring an Apache test.biuuu.com Virtual Host
1. Same method as above; copy and modify the configuration code as follows:
- <VirtualHost *:80>
- ServerAdmin [email protected]
- DocumentRoot E:/WebRoot/biuuu
- ServerName test.biuuu.com
- ErrorLog "logs/dummy-host2.localhost-error.log"
- CustomLog "logs/dummy-host2.localhost-access.log" common
- </VirtualHost>
2. Open the hosts file (C:/WINDOWS/system32/drivers/etc/hosts) and add the following line:
- 127.0.0.1 test.biuuu.com
3. When opening test.biuuu.com in the browser, the following error appears: 403 Forbidden
Forbidden: You don’t have permission to access / on this server.
Analysis: This is mainly because directory access permissions are not configured; you need to set access permissions for the directory!
4. Open the httpd file and find the following statements:
- <Directory />
- Options FollowSymLinks
- AllowOverride None
- Order deny,allow
- Deny from all
- </Directory>
Copy the above code, modify the directory by replacing / with E:/WebRoot/biuuu, and update the VirtualHost code as follows:
- <VirtualHost *:80>
- ServerAdmin [email protected]
- DocumentRoot E:/WebRoot/biuuu
- ServerName test.biuuu.com
- ErrorLog "logs/dummy-host2.localhost-error.log"
- CustomLog "logs/dummy-host2.localhost-access.log" common
- <Directory E:/WebRoot/biuuu>
- Options FollowSymLinks
- AllowOverride None
- Order deny,allow
- Deny from all
- </Directory>
- </VirtualHost>
After testing in the browser, it still cannot be opened, showing the same 403 Forbidden error as above. Change Deny from all to allow from all.
5. Restart Apache, and the virtual host is configured successfully!
Important Notes
1. Directory path, e.g., E:/WebRoot/biuuu
2. Access permissions: change Deny from all to allow from all as shown above
3. Hosts file: configure the virtual domain name host mapping
4. httpd file: enable the Include conf/extra/httpd-vhosts.conf module
5. httpd-vhosts file: configure the virtual host
6. It may also be caused by the Linux SELinux firewall; be sure to remember this!
Configuring Apache httpd-vhosts virtual hosts is relatively simple for developers, but it is very important. For reference only!
References:
Original article. If republished, please credit: Republished from Biuuu Blog http://www.biuuu.com/
Gu Yinxin note: If you encounter the “Fatal error: Allowed memory size of 8388608 bytes exhausted” error
Modify php.ini to set memory_limit = 12M (default is 8M)
Or simply add the following at the beginning of your script: ini_set(“memory_limit”,”12M”);