Errors when adding virtual hosts
Posted by: SimonT (---.lightspeed.stlsmo.sbcglobal.net)
Date: May 30, 2017 03:42AM

I'm running WAMP Server 3.0.4 (32bit) with MySQL 5.7.11, and Apache 2.4.18, PHP 5.6.19 on Windows 7 Home Premium 64bit.

When I try to create a new virtual host I get the following errors:

( ! ) Warning: fopen(C:\\Windows\\system32\\drivers\\etc\\hosts): failed to open stream: Permission denied in C:\wamp32\www\add_vhost.php on line 238
Call Stack
# Time Memory Function Location
1 0.0010 172656 {main}( ) ...\add_vhost.php:0
2 0.0210 280016 fopen ( ) ...\add_vhost.php:238

( ! ) Warning: fwrite() expects parameter 1 to be resource, boolean given in C:\wamp32\www\add_vhost.php on line 239
Call Stack
# Time Memory Function Location
1 0.0010 172656 {main}( ) ...\add_vhost.php:0
2 0.0650 280384 fwrite ( ) ...\add_vhost.php:239

( ! ) Warning: fclose() expects parameter 1 to be resource, boolean given in C:\wamp32\www\add_vhost.php on line 273
Call Stack
# Time Memory Function Location
1 0.0010 172656 {main}( ) ...\add_vhost.php:0
2 0.0690 280072 fclose ( ) ...\add_vhost.php:273


The PHP code at lines 238 and 239 in add_vhost.php is:
$fp1 = fopen($c_apacheVhostConfFile_escape, 'a+');
$fp2 = fopen($c_hostsFile_escape, 'a+');

The two files being accessed are:
c:/wamp32/bin/apache/apache2.4.18/conf/extra/httpd-vhosts.conf
C:\\Windows\\system32\\drivers\\etc\\hosts


The code at line 273 in add_vhost.php is
fclose($fp1);

The file is
c:/wamp32/bin/apache/apache2.4.18/conf/extra/httpd-vhosts.conf


The file c:/wamp32/bin/apache/apache2.4.18/conf/extra/httpd-vhosts.conf does contain an entry for the new virtual host:

<VirtualHost *:80>
ServerName vhostsadd
DocumentRoot c:/wamp32/www/vhostsadd
<Directory "c:/wamp32/www/vhostsadd/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>

The hosts file (which is at c:\windows\system32\drivers\etc\hosts) does NOT contain an entry for the new virtual host.

I can open and edit the hosts file to insert the needed lines, but when I try to save it, I am told "The process cannot access the file because it is being used another process."

I'm using Notepad to edit the hosts file. Even if I run it as the administrator, I cannot save the file.

I have to restart Windows in Safe Mode in order to be able to save the edited hosts file. Then, of course, I have to restart Windows in normal mode to continue working.

Question - I inserted an echo to show me the files being opened at lines 238 and 239 and the file name being used for the host files seems a bit strange:

It is
C:\\Windows\\system32\\drivers\\etc\\hosts

but I would have expected to see
C:/Windows/system32/drivers/etc/hosts

That is, with the other slash (which I call a forward slash - as I, and everyone else, have done since 1973 when I started working with computers) and with only one slash in each position.

Is this part of the problem - the file name being used for the hosts file?

To recap - I get the messages shown above when I add a virtual host. The c:/wamp32/bin/apache/apache2.4.18/conf/extra/httpd-vhosts.conf file has the necessary entry but the hosts file does not.

If I restart Windows in Safe Mode, edit the hosts file to add the needed lines, save the file, and restart Windows, the virtual host works properly.

Note - I was able to get a virtual host correctly created with this WAMP installation a few months ago but now I'm getting these errors.

So - what is wrong here and what should I do to fix it?

Simon

Options: ReplyQuote
Re: Errors when adding virtual hosts
Posted by: RiggsFolly (Moderator)
Date: May 30, 2017 10:19AM

Hi Simon

The HOSTS file is protected by WIndows, for obvious reasons.
Sometimes a firewall/virus checker will also attempt to stop you modifying the HOSTS file.

It sounds to me like your Firewall/Anti Virus app is probably causing this issue.

Normally it is possible to tell said app to stop being over protective of the HOSTS file. Can you try and stop your FW/AV from attempting to protect this file. Most AV's have a stop protecting this file for 30 minutes type option.

It would also be useful in the furture if you start a question with the answers to this
Sticky Post READ (and answer) BEFORE YOU ASK A QUESTION

These posts can also be useful WAMPServer 3 All you need to know

and this one TROUBLESHOOTING TIPS

---------------------------------------------------------------------------------------------
(Windows 10 Pro 64bit) (Wampserver 3.3.4 64bit) Aestan Tray Menu 3.2.5.4
<Apache versions MULTIPE> <PHP versions MULTIPLE> <MySQL Versions MULTIPLE>
<MariaDB versions MULTIPLE> <phpMyAdmin versions MULTIPLE> <MySQL Workbench 8.0.23>

Read The Manuals Apache -- MySQL -- PHP -- phpMyAdmin
Get your Apache/MySQL/mariaDB/PHP ADDONs here from the WAMPServer alternate Repo
-X-X-X- Backup your databases regularly Here is How dont regret it later! Yes even when developing -X-X-X-

Options: ReplyQuote
Re: Errors when adding virtual hosts
Posted by: Otomatic (Moderator)
Date: May 30, 2017 10:32AM

Hi,

For example, for Avira Antivirus Pro it's in:
Perhaps they are not the exact terms because I translated from my French version.
- AntiVirus Pro
- Configuration
- Generalities
- Security
- System protection
- Protecting the Windows Host File from Changes

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons

Options: ReplyQuote


Sorry, only registered users may post in this forum.