Accessing files on external media.
Normally, all files, that your Apache webserver is supposed to serve, are located in some folders of the Apache document directory (a folder called htdocs, located in the Apache installation directory). In fact, without a special configuration, Apache cannot access any files outside the htdocs folder structure. Thus, for example, in order to make Apache access the cgi-bin folder, special settings have to be made in the Apache configuration file httpd.conf. In the case of cgi-bin, this configuration consists of a SciptAlias directive, which maps the cgi-bin folder path to the URL /cgi-bin, and a Directory directive, that defines how the mapped folder may be accessed.
What is true for CGI scripts in cgi-bin is also true for any files located in a directory located outside the htdocs folder. I wrote this tutorial when developing my "Network MP3 Player" web application. To make it possible to play the MP3 files in the browser's HTML5 player, Apache has to access the Windows Music library, that, in my case, not only includes the standard folder C:\Users\<user-name>\Music, but also the folder E:\Music 2 (E: being an external USB disk). To run my application, the following changes have to be made to httpd.conf:
- Create aliases that map the two music folders to some URL; I use:
Alias /media1 "C:/Users/allu/Music"
Alias /media2 "E:/Music 2" - Define the directory permissions for the two music folders:
# MP3 files directory (Windows Music library)
<Directory "C:/Users/allu/Music">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory "E:/Music 2">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
With this configuration done (don't forget to restart Apache!), URLs like /media1/<mymp3file-on-c.mp3> and /media2/<mymp3file-on-e.mp3> are valid and return the corresponding MP3 files from C:\Users\allu\Music resp. E:\Music 2.
Note: Please, notice that in httpd.conf, the folder separator in path names is a "normal" slash (/), and not a back-slash (\), as usual on Windows.
So far, so good. But, what happens if the USB disk is not connected? Does that result in Apache not functioning anymore?
Apache reads the configuration file only when being started or restarted. Thus, when starting the webserver with the USB disk connected, Apache starts up properly. And, afterwords disconnecting the drive will have no effect on the correctly working of Apache. This remains true after a normal shutdown (using Shutdown from the Windows Start menu). I suppose that this has to do with the Windows "fast boot" feature, that, in some sense, restores things as they were before shutting down (for example, a service started manually will still be running after shutdown and re-power-on).
On the other hand, a restart of the computer (for example, using Restart from the Windows Start menu) will execute a restart of the Apache service. Apache will read its configuration file, and if the USB disk is not connected, the corresponding Alias and Directory directives are invalid. This means that Apache will fail to start up. If you try to connect to localhost, you'll get a "We can't connect..." error. If you try to start Apache using Apache Monitor, you'll get a message that the operation failed. Trying to start Apache in Command Prompt, using the command httpd -k start will result in the display of the error message that E:\Music 2 cannot be found.
So, if you use files on a removable media with Apache, either make sure the media is always connected. Or, with Apache properly running, do not restart your computer (just shut it down, and re-power-on instead).
If you find this text helpful, please, support me and this website by signing my guestbook.