Location, Location, Location: Tips for Storing Web Site Files
by Patrick Crowley05/16/2003
If you're like me, you've figured out how to install and configure your very own web development environment on Mac OS X. You've got Apache. You've got PHP. You've got MySQL (and phpMyAdmin, too). And, like a proud father, you've already cranked out a bunch of whiz-bang, dynamic web sites -- all on a Mac.
But, as your kids grow older, they're bound to go through some growing pains. And one of the first things you'll need to figure out is where to store your web site's files. Should they be in the Sites folder? The Documents folder? What are your options?
Here are three common approaches -- which address the needs of basic, intermediate, and advanced web developers.
Option 1: Use Your Sites Folder
Pros: No work requiredCons: Files must be stored in your Sites folder
|
Related Reading
Mac OS X in a Nutshell |
If you're just beginning to experiment with web development on Mac OS X, storing files in your Sites folder (AKA ~/Sites/) is definitely your best option. (Note: if you're already a Mac web-dev whiz, you can definitely skip this part.)
If you haven't already, make sure you enable Web Sharing (via System Preferences->Sharing), so that your copy of the Apache web server is fired up and ready to serve.
Once you've started web serving, to access any file or directory in your ~/Sites/ folder, just type 127.0.0.1 (also referred to as "localhost") into your browser, followed by your user name, and then the file or directory name you wish to access, like so:
~/Sites/ --------> http://127.0.0.1/~your_user_name/
~/Sites/site/ -----> http://127.0.0.1/~your_user_name/site
~/Sites/page.html ---> http://127.0.0.1/~your_user_name/page.html
Each time you start working on a new web site, all you need to do is create a directory for the new site, drag it into your Sites folder, and, voila, that site is now being served!
Option 2: Use Aliases
Pros: Serve files from anywhere on your MacCons: Need to know under-the-hood OS X stuff
So let's step things up a notch. Let's say you already have an existing file structure for your projects, and you're none too pleased about having to move everything over to the Sites folder. What do you do?
Easy! Just add a couple of aliases to your Apache config file (/etc/httpd/httpd.conf) and you'll be able to serve your web sites from wherever they happen to be located -- not just from the Sites folder.
There's one catch, though. Like many of OS X's UNIX underpinnings, your Apache config file is hidden from view. So the first thing you need to do is get a tool that will let you open the config file. To do this, you should use a text editor capable of opening and editing hidden files (like BBEdit) or, alternately, use a utility like TinkerTool that can enable the Finder to show all hidden files. (Note: it's always a good idea to back the config file up before making any changes.)
OK, so once you've got that sucker open, scroll down to the Aliases section and take a look. Here's how it should look:
Alias /icons/ "/usr/share/httpd/icons/"
<Directory "/usr/share/httpd/icons">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
As you can see, each alias contains two parts: an Alias directive, which is the actual name used to access your site from the browser (as in http://127.0.0.1/alias), and then a <Directory> tag, which points to the actual location of the files you wish to serve.
Let's say you want to serve up some files from a project that's stored deep down in your Document directory, such as /Users/your_name/Documents/Clients/Client 4/site/. Just add a new set of Alias and Directive commands to your config file, like so:
Alias /icons/ "/usr/share/httpd/icons/"
Alias /client4/ "/Users/your_name/Documents/Clients/Client 4/site/"
<Directory "/usr/share/httpd/icons">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Directory "/Users/yourname/Documents/Clients/Client 4/site/">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
Once you've made these changes, save your httpd.conf file and restart Apache (by turning Web Sharing off and then back on again in the Sharing pane in System Preferences).
Now, you should be able to access your new project by just entering http://127.0.0.1/client4/ straight into your browser! (And, of course, for each additional site you wish to serve from outside your Sites folder, just add a new Alias/Directory combo, restart your server again, and you're good to go.)
...
But wait, there's a catch -- and, if you're using PHP, it's an important one.
By default, PHP is configured to work properly with any files in your Sites directory. But should you serve files from a different location, you'll need to tweak your PHP configuration file, as well.
Now -- don't get scared on me -- the truth is Mac OS X, by default, doesn't have a php.ini file. So you'll actually need to make your own! But don't worry. It's easy.
- Add a lib directory to /usr/local/.
- Download a sample php.ini file from the PHP mothership.
- Rename the file you just downloaded to php.ini.
- Move the file to /usr/local/lib/.
Now that you're all set, open up your new php.ini file and scroll down to the Paths and Directories section, which should look like this:
;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;
; UNIX: "/path1:/path2"
;include_path = ".:/php/includes"
First, remove the semi-colon in front of include_path, so that we can enable includes.
include_path = ".:/php/includes"
I know what you're thinking -- "Why do we need to tweak our include path, Patrick?" Well, dear Mac web developer, it turns out that PHP apps often rely on something called the include() command, which, you guessed it, allows PHP to include (or load) external files. Being a talented Mac OS X web developer, you'll probably wish to enable this feature at some point, either for use in other people's code or your own.
So, next, as you can probably guess, you need to add the directory from our earlier example to the include_path variable, so that PHP will work in this directory the same way it works in the Sites folder. (Use this trick each time you add a new alias to Apache's config file that might use PHP, separating each new include path with a colon, as in the example below.)
include_path = ".:/php/includes:/Users/your_name/Documents/Clients/
Client 4/site/"
Ok, that's it! You're now fully set up to serve both HTML and PHP files using aliases!
Option 3: Enable Virtual Hosts
Pros: Use customized URLs (no need for 127.0.0.1)Use absolute file paths
No php.ini editing necessary
Cons: Easy to screw up
But wait! There's more! This next option is the trickiest of the three, but it offers the greatest ability to make your local development server truly function like a production server.
And this gets to why I wrote this article in the first place. I had to figure out how to enable virtual hosts over the weekend -- and a thousand Google searches later, I realized that while there may be a cornucopia of documentation on Apache, MySQL, PHP, and the like, virtually none of it deals with Mac OS X! (Come on, people! Start documenting your experiences with this stuff, will ya?)
OK, with my rant out of the way, let's get busy with this virtual host stuff. Basically, with virtual hosts, a single Apache web server can serve multiple web sites from the same IP address. And not only that, you can actually use different, customized domain names for each web site.
In other words, instead of accessing files through 127.0.0.1, you can use a real (or simulated) domain name. So, to the untrained eye, your local web site will look like it's being served straight off the web — without any 127.0.0.1 gobbledygook.
Not only are there a lot of technical benefits to doing things this way (support for absolute file paths, no need to mess with PHP includes, etc.), but this stuff is perfect for client demos! You can run presentations straight off of your PowerBook and no one ever needs to know you're not on the Web.
Since this is exactly the sort of thing that many web developers on OS X really need, let's set up some virtual hosts!
To get started, scroll down to the very bottom of your Apache config file (/etc/httpd/httpd.conf) and find the NameVirtualHost section.
# Use name-based virtual hosting.
#
#NameVirtualHost
First, let's enable name-based virtual hosts, which we'll do by removing the # comment prefix from the last line.
# Use name-based virtual hosting.
#
NameVirtualHost
Next, let's specify the IP address to which our name-based virtual host names will resolve: which, of course, is our old friend 127.0.0.1.
# Use name-based virtual hosting.
#
NameVirtualHost 127.0.0.1
Now, we need to set up our VirtualHost directives. Using the example provided in the Apache config itself, we'll first add a virtual host for our existing Sites folder. We do this to ensure that we'll still be able to access web sites stored in the Sites folder using our existing 127.0.0.1-based addresses once we've enabled virtual hosts.
As you can see, we place 127.0.0.1 in the ServerName field, and then, with DocumentRoot, we specify where the files for our virtual host are stored -- the Sites folder.
<VirtualHost 127.0.0.1>
ServerName 127.0.0.1
DocumentRoot /Users/your_name/Sites
</VirtualHost>
With that behind us, we'll now add a VirtualHost directive using the same logic, but this time, we'll do it using my web site iCalShare as an example.
Since we still need to access the real iCalShare web site, we can't use the icalshare.com domain for our virtual host. If we did use that domain, whenever we entered icalshare.com into our browser, it wouldn't know whether to load the local site, or the external one.
Since this is a test server, we'll append a "test" subdomain to our primary domain name, making our complete server name test.icalshare.com. That name won't conflict with the existing iCalShare domain name, and it also tells us a little about the purpose of this server.
So let's place our new name in both the VirtualHost and ServerName tags. And, then, once again, we'll use DocumentRoot to point to the location of the files for this site, which are stored a few levels down in our Documents folder.
<VirtualHost test.icalshare.com>
DocumentRoot /Users/patrick/Documents/Projects/iCalShare/wwwroot
ServerName test.icalshare.com
</VirtualHost>
Now I bet you're asking, "How does my browser know that test.icalshare.com is a local request, and that icalshare.com is a remote one?" Good question! And the answer is ... because we're going to tell it!
The Web as we know it is actually strung together using IP addresses, not domain names. Each time you enter a web site domain name into your browser, it's actually translating that name into an IP address, which it does by checking with a DNS server. Once the DNS server finds an IP address match for the domain name you entered, you're magically transported to the web site you're seeking.
As it turns out, with Mac OS X, when you enter a domain name, your browser first checks with /etc/hosts to see if there's a matching IP address. If no match is found, the browser will forward the domain name on to a DNS server. So, by editing /etc/hosts, you can actually change where your browser goes when you enter various domain names. And this is exactly how we'll get your machine to recognize the virtual hosts we set up earlier.
So, open up your /etc/hosts file, and you should be looking at something like this:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
To enable the virtual host we set up earlier, just add a new entry to the file, as we've done below for our iCalShare example.
127.0.0.1 localhost
127.0.0.1 test.icalshare.com
255.255.255.255 broadcasthost
::1 localhost
For each virtual host you need, just create a new line that begins with 127.0.0.1, followed by a space or tab, and then the actual domain name.
So that's it! Save your /etc/httpd/httpd.conf and /etc/host files, restart Apache, and your vanity domain should now work like magic!
(Note: I've only touched the surface of all of the nifty things you can do with virtual hosts. Further reading on this topic is recommended.)
Final Thoughts
And, now patient readers, in the tradition of the great Jerry Springer, I leave with you a final thought ... get out there and go crazy with this stuff! And if you discover something cool in your web-development-on-OS-X travels, don't forget to share it!
Patrick Crowley is the creator of iCalShare, the iCal calendar directory, which was recently featured in Steve Jobs' keynote speech at Macworld San Francisco 2003.
Return to MacDevCenter.com
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 56 of 56.
-
localhost and 127.0.0.1 seem to require a network connection
2004-04-20 16:52:49 patrick_ [Reply | View]
On my computer running os x 10.3. Localhost and 127.0.0.1 work great with a network connection. If I try to work from home where I don't have a network connection the local loop back doesn't work. Is there a way to use localhost without any net connection?
-
ln -s
2003-12-15 11:15:03 anonymous2 [Reply | View]
Hi, for me, the easy way to to that, is to work in my normal folders (no inside the ~/Documents) and then create an unix alias to the /Library/Webserver/Documents with'ln -s' command
Works like a norma mac alias, so ou can rename, etc, but also work like an unix alias, so you can put all your php, etc files, and test in your local machine, without having to make any modifications to your ini files, or config files...
[]s
fergussom
-
Virtual Domains on 10.3 server
2003-12-07 08:14:44 eprice [Reply | View]
This is the closest article I could find that might shed light on my current problem. I am a student so excuse the probable newbie question. I am running 10.3 server and my primary website has been live on the net for weeks with no problems. I registered a second domain and set the DNS using hn.org which seems to be working great.
When I enter the URL for either website they both bring up the index.html from the new site. Using the server admin application I created the second site and selected the web folder (document root) and the Default index file. Do I need to manually set virtual host settings for every site I create on OSX Server? I thought this would only be necessary if I were using client. TIA
http://www.pauleprice.com
http://www.ericswoodshed.com
-
Deleting ~username from url
2003-10-27 04:25:58 anonymous2 [Reply | View]
Greetings all, love the articles...have been a blessing.
I installed a BBS, Apache, MySQL etc and all works well. Also use dyndns to point to my iBox. Have located BBS in User/Sites but notice a ~username in the responding URL. Was wondering the best way to eliminate this a have a cleaner URL like www.mydyndns.net/mysite/mypix etc. Should I alias the BBS or move it back to root Webserver and reconfig the httpd.conf?
Regards, Scott G, KiwiLand
-
Name-based Virtual Hosts
2003-09-14 22:55:36 empass [Reply | View]
I found an easier way to do number 3 (above)
According to the Apache manual you can do the following; I did not modify the "hosts" file. Also see the documentation about mass virtual hosting. (see apache.org and search for terms)
# example
NameVirtualHost 111.22.33.44
<VirtualHost 111.22.33.44>
ServerName www.mysite1.com
ServerPath /web
DocumentRoot /web/mysite1
</VirtualHost>
<VirtualHost 111.22.33.44>
ServerName www.mysite2.com
ServerPath /web
DocumentRoot /web/mysite2
</VirtualHost>
<VirtualHost 111.22.33.44>
ServerName www.mysite3.com
ServerPath /web
DocumentRoot /web/mysite3
</VirtualHost>
# end example
It DOES work with the same IP for all three.
I also used a DNS Service (free for 5) to register the name servers @ http://www.zoneedit.com
Hope this helps.
-
Contextual menu makes symbolic links inside Finder
2003-07-25 09:03:34 lixlpixel [Reply | View]
"SymbolicLinker.plugin"
this little contextualMenu Item lets you create "symbolic links", which work like Alias directives, right in the finder.
works perfect (at least for me) with PHP and looks like a "real" subdirectory .(http://YOUR_IP/SYMBOLIC_LINK/)
the symblic link can even point to files or folders on external disks, apache will treat it like the original.
(just don't move the target file - symbolic links don't update themselves like the "alias" in finder)
i found it @
http://www.versiontracker.com/dyn/moreinfo/mac/17655
-
Great Article!!
2003-07-22 09:16:36 anonymous2 [Reply | View]
Just wanted to say thanks! This is a great article! I have been impressed with O'rielly's coverage of OS X Apache, PHP, MYSQL, etc.
The authors have done a fantastic job at covering the material. A local development environment has proven to be very effective for me! and It's been cool to see how it all works. This article addressing the VHosts was a great progression in the series. Keep up the good work!!!
- cb
-
more on the 403 error.
2003-07-18 06:04:35 anonymous2 [Reply | View]
Great article! I tried to do this and it works if I put my website in the Sites folder:
<VirtualHost test.koen.com>
DocumentRoot /Users/koen/Sites/MyWeb
ServerName test.koen.com
</VirtualHost>
If I put it somewhere else, which is I guess the purpose of using virtual hosts, I get an 403 error:
<VirtualHost test.koen.com>
DocumentRoot /Users/koen/Documents/HTML/MyWeb
ServerName test.koen.com
</VirtualHost>
any ideas what might be the problem?
thanks,
- Koen.
-
more on the 403 error.
2003-07-28 13:01:27 anonymous2 [Reply | View]
You have to remember that the web server is probably operating under it's own user account (like www or whatever it is -- try 'ps -ajx|grep http' to see). So, you need to let the webserver read your webpages. The Sites folder is set up for public access, but your Documents folder is not. Heck, you don't want a billion poeple looking at your docs. But if you do, you'll need to set the permissions correctly. Try 'chmod 755 MyWeb' from your HTML dir to give read access to the world (including the web server). Basically, you got a permissions problem. About 95% of Unix problems are permission problems.
-
What if site is hosted on my computer and I have a router?
2003-06-12 07:04:50 anonymous2 [Reply | View]
Does any of this change if the site is hosted on my computer?
I have an additional problem that I can't use http://mydomain.com because of the Netgear Router. http://mydomain.com brings up the Netgear set up. So to see my site, I have to use mycomputer.local, but some things are difficult to deal with. Particularly confusing when I'm trying out things like WebCalendar and the config.php file needs to be different if I access the site from within or outside.
Any thoughts?
-
HUH?
2003-06-05 14:27:22 anonymous2 [Reply | View]
Why do you have to go through all this just to serve a website from your Mac?
Seems to me it would be a lot easier and more secure, on OS 9....
Help me out here.
-
php.ini
2003-06-04 03:58:37 anonymous2 [Reply | View]
Thanks for a great article!
I would like to know how one changes the expected location of the php.ini file. Most docs and refs (including this article) say it should be placed in usr/local/lib but my PHP installation looks for the config file in /usr/lib. (I found this by checking php(info) after having become just a little frustrated when 'Includes' wouldn't work for any new paths I specified in php.ini)
Is this default location for php.ini something that is set at compile time? Can I change it to be usr/local/lib now? If so, how?
-
alias (itunes music library)
2003-06-02 18:21:33 anonymous2 [Reply | View]
I am trying to give access to my itunes music library /user/user_name/music/itunes/itunes music/ on my webpage. Apache Webserver is looking for the files here here /documents/ located in my webserver folder under my library. How can I get it to let me give access to it. -
alias (itunes music library)
2003-06-03 10:43:40 anonymous2 [Reply | View]
I have figured out how to do this now...but when I try to pull it up it says "Forbidden - You don't have permission to access /musicarchive/ on this server." Why do I not have permission now. -
alias (itunes music library)
2003-06-05 19:41:50 maarky [Reply | View]
Remember that you have to add the Directory directive after you create the alias. For example:
Alias /musicarchive/ "/user/user_name/music/itunes/itunes music/"
<Directory "user/user_name/music/itunes/itunes music/">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
Also, the path in the Alias and Directory is case sensitive, so you would use "/Users", not "users". -
alias (itunes music library)
2003-06-10 16:24:43 anonymous2 [Reply | View]
i've done all of that, but this is still what i get:
Forbidden
You don't have permission to access /musicarchive/ on this server.
---------------------------------------------
Apache/1.3.27 Server at themcnetwork.local Port 80
any ideas? -
alias (itunes music library)
2003-10-24 02:46:14 anonymous2 [Reply | View]
you need to change the permissions of your folders to enable apache to acces them. Set every one (You, group, and others) to read. You may have additional write privelages for yourself.
I have done this myself, and that's all there was to it for me (as well as adding the folder in Terminal)
-
Virtual hosts
2003-05-28 03:18:19 elektron [Reply | View]
an empty "NameVirtualHost" is unnecessary. All you need is "NameVirtualHost *". Or, in Sodium's case,
NameVirtualHost *:80
NameVirtualHost *:8080
because some ISPs suck. Port 80 is still used just because it feels proper.
<VirtualHost test.icalshare.com> is equivalent to <VirtualHost 127.0.0.1> because Apache only looks at the IP in <VirtualHost>. The "Host" header (or server portion of the request line) is compared to ServerName. And when virtualhosting to the rest-of-world, unless you have a static ip or a nameserver that tells you who you are when you get connected, it's best to use <VirtualHost *>.
If you do, however, use <VirtualHost *>, all requests that don't match a VirtualHost will match the first wildcard VirtualHost. To stop this from happening, the first virtualhost should be
<VirtualHost *>
</VirtualHost>
to set the default DocumentRoot and stuff to everything you painstakingly configured in the rest of httpd.conf.
And thirdly, you just need one line for each ip address in the hosts file, e.g. "127.0.0.1 localhost me myself i test.icalshare.com". lookupd doesn't consult /etc/hosts by default in 10.1.5, though, so it's a lot of playing with netinfo (i don't remember if you could click a box to tell it to consult the flat files).
I don't worry about Apple overwriting my httpd.conf, mainly because I set apache2 to run instead.
-
add rendezvous names
2003-05-20 12:16:16 anonymous2 [Reply | View]
if you develop on a local network, it can be really handy to add rendezvous names for virtual hosts or even directories.
For example if you add:
RegisterResource "icalshare" /Users/patrick/Documents/Projects/iCalShare/wwwroot
at the end of your httpd.conf file (providing you are running 10.2.5+) the site will show up in Safari or Camino's Rendezvous menu as "icalshare" -
add rendezvous names
2003-05-20 17:18:56 maarky [Reply | View]
This isn't working for me. I get the link in my Safari rendezvous menu, but I then get a not found error that says:
The requested URL /path/to/virtualdomain's/DocumentRoot was not found on this server.
I made sure that the path in RegisterResource matches the virtualhost's DocumentRoot. It basically ads that path to the rendezvous url so I get:
http://my-computer.local./path/to/DocumentRoot
I tried adding a "/" at the end but that did nothing.
What it looks like it's doing is looking for /Library/WebServer/Documents/path/to/document/root which does not work since the path to document root is located at /path/to/document/root, not inside your /Library/WebServer/Documents directory. But if you actually got this to work I would love some additional info. -
add rendezvous names
2003-05-22 19:12:11 maarky [Reply | View]
I got a workaround. Look at this article:
http://www.macdevcenter.com/pub/a/mac/2003/04/08/mod_rendezvous.html
and read my comment:
http://www.oreillynet.com/cs/user/view/cs_msg/19109
I do something slightly different on my computer than what is in my comment above. Since I am never connected to a rendezvous network I can skip a lot of the steps in that article. I just want my sites to show up in the rendezvous list in my browser because I think it's neat. So if you're like me you can follow the steps in this "Location, Location, Location..." article (without having to do everything in the above article). If you follow the link above to read my comment simply make these changes to what I say there:
I use this in my httpd.conf rendezvous directive:
RegisterResource "PHP Docs" "/virtualRedirect.php?domain=php.mac"
(adapt this for your own virtual domains)
And I create a file in the specified location with this php code:
<?php
header('Location: http://'.$_GET['domain']);
?>
Give it a try. It works for me.
-
Virtual Hosts & Content Negotiations
2003-05-20 03:46:19 senjaz [Reply | View]
Has anyone else had problems with content negotiation in conjunction with virtual hosts?
It only appears to work with the root site. I've tried putting the directives inside the virtual host tags too.
-
Great article
2003-05-20 01:57:28 anonymous2 [Reply | View]
I tried to do something similar and got it working, but couldn't repeat it. The clarity of this article is really helpful... Covering both aliases and virtual hosts (including the /etc/hosts file - which I had missed) - GREAT!
-
another VirtualHosts article
2003-05-19 08:23:19 anonymous2 [Reply | View]
There's another article about VirtualHosts for OS-X (the author must have missed it) on evolt.org - it's been there for a while too (nearly two years).
Here
It's been helpful to me!
-
DynDNS has a good tutorial
2003-05-19 02:28:31 anonymous2 [Reply | View]
I actually followed the small tutorial on DynDNS and it seems it is working according to my friends except for one thing. http://myvirtualhost.com/ and http://myipnumber/ both point to my first virtual host in the list, myvirtualhost.com. but the other http://myothervirtualhost.com/ point to the account it is supposed to.
I had some initial problems with apache restarting but I found if I turned off Web Sharing first then did the edit it came on no problem. It also seems to be a simplified version of this. check it out.
https://www.dyndns.org/support/kb/apachevhosts.html
-
Forbidden 403 Error on Virtual Hosts?
2003-05-18 23:39:54 anonymous2 [Reply | View]
I have been through the directions 3 times and can't figure what I messed up. I have three separate virtual domains setup in both etc/hosts and httpd.conf. All provide the same error message.
Do I need to add "Allow from all" to the master <Directory> section? This already appears under the root document directory and in
"Include /private/etc/httpd/users" which is the last line of httpd.conf
Suggestions please. -
Forbidden 403 Error on Virtual Hosts?
2003-05-19 14:20:17 anonymous2 [Reply | View]
Check and make sure every directory from / down to the directory your serving files from has execute permission set for all users. You need to make sure that *every* directory in the path has this set. The lack of this can produce a 403 error. -
Same problem with me!
2003-05-19 09:41:40 anonymous2 [Reply | View]
I have a 403 Error, too. I did it exactly like the tutorial sais, so I would be glad if someone could help...
Thank you. -
Try this...
2003-05-19 14:00:48 icalshare [Reply | View]
Hmmm... not sure what's causing that.
It may help to look at the complete Apache config file I used for this article. (This conf comes straight from my main dev machine -- and it's working fine.)
http://iCalShare.com/sample.conf
Best,
Patrick -
Try this...
2003-05-19 16:43:46 anonymous2 [Reply | View]
Okay. One thing was different and that was the document root. Mine still said "/Library/WebServer/Douments" while yours was set to "/Users/patrick/Sites"
I changes this accordingly and followed the permission advice of the other poster. Now my error is a simple File Not Found.
I am attempting to set the document root for the Virtual hosts outside of the root. Do I need to set an alias somewhere or mess with NetInfo?
-
Visiting your virtual sites from another computer
2003-05-18 19:17:44 wkolean [Reply | View]
Good article! Sometimes, however, you need to check your site on another computer, like a PC, and it doesn't seem to work if you only use 127.0.0.1. If you have a fixed IP address, you can add your IP address to NameVirtualHost:
NameVirtualHost 127.0.0.1 192.168.1.100
and then add your IP address to each VirtualHost:
<VirtualHost test.icalshare.com 192.168.1.100>
...
</VirtualHost>
Finally, if you want to test your site on a PC, edit your HOSTS file, which for Win2000 is in C:\WINNT\SYSTEM32\DRIVERS\ETC
and add:
192.168.1.100 test.icalshare.com -
Visiting your virtual sites from another computer
2004-02-17 21:49:47 rmcgonegal [Reply | View]
Getting Apache working on just my Mac wasn't too hard. But trying to make test domains resolve from other computers does not work so far for me.
I don't know what version you are using but if I try to add
NameVirtualHost 127.0.0.1 192.168.1.100
to httpd.conf, then loading Apache errors out with the message
NameVirtualHost takes one argument, A numeric IP address:port, or the name of a host
It will load if I take out the localhost IP so it reads
NameVirtualHost 192.168.1.100
But then using your recommendation with multiple development sites such as
<VirtualHost test.icalshare.com 192.168.1.100>
...
</VirtualHost>
<VirtualHost test.icalshare2.com 192.168.1.100>
...
</VirtualHost>
produces the error
[warn] VirtualHost 192.168.1.100:80 overlaps with VirtualHost 192.168.1.100:80, the first has precedence, perhaps you need a NameVirtualHost directive
If I eliminate the IP so it is just name based VirtualHost,
<VirtualHost test.iCalshare.com>
then it works fine on my Mac. But not from PCs on the LAN. It's great to run cgi's and Php, but being able to test all this on a local PC would be a huge help.
Do I need to run a DNS server? Bind is on the Mac but I don't know if it really needs a dedicated machine. And it sounds like overkill (I've only got 2 Macs and 2 PCs).
Does anyone know of a solution? A tutorial on intranets would be great. -
I can't get this to work!!!
2004-04-10 22:34:14 bcamp [Reply | View]
This is EXACTLY what i'm looking for right now. Want to be able to test my sites via my VirtualPC install as well as local mac browsers. This tip doesn't work for me tho. When i tried to stop/restart apache it hangs. Giong back and removing the above edits allows it to start normally. Anyone have any other hints??? -
Visiting your virtual sites from another computer
2004-01-02 20:51:31 anonymous2 [Reply | View]
thank you, thank you, thank you! I was going crazy trying to figure out how to test my pages local on different computers. It's great being able to develop web sites on my Mac with Virtual Hosts and now I can preview the site on my VirtualPC for testing. This is a great tip!
Brigitte -
Dynamic DNS also works
2003-05-19 14:25:10 icalshare [Reply | View]
If you're on cable and you don't have a static ip, you can also access your site over the web using one of these options.
(1) Use your temporary IP address -- while cable IPs aren't technically static, they tend not to change very often
(2) Register with a Dynamic DNS service -- which will create a static domain that always points to your current IP address, no matter how often your ISP changes it.
http://www.dyndns.org/services/dyndns/
Best,
Patrick
-
Use NetInfo Manager instead of /etc/hosts and other comments
2003-05-17 15:34:05 maarky [Reply | View]
For me it's easier to open NetInfo Manager rather that looking for a hidden file. So instead of directly editing your /etc/hosts file you can go to /Applications/Utilities/NetInfo Manager
Once you launch NetInfo Manager you will click on "machines" and then "localhost". Now you want to click on the lock in the bottom left corner so that you can edit the info (must be an admin user). Now duplicate "localhost" and rename the duplicate whatever you want your virtual host to be named. So for instance, you change "localhost copy" to "test.icalshare.com".
Now deselect your new machine by clicking on another one. It will ask you if you really want to make the change. Say yes. There you go.
Another thing. I don't know the wisdom of this but I got it from the apache documentation ( http://httpd.apache.org/docs/vhosts/name-based.html ) and I have had no issues with it. You do not need to specify 127.0.0.1 in your virtual host directives. All you need is "*". For example, the following should work just fine:
NameVirtualHost *
<VirtualHost *>
DocumentRoot /Users/patrick/Documents/Projects/iCalShare/wwwroot
ServerName test.icalshare.com
</VirtualHost>
You can also add other directives inside your virtual host directive. For example, do you want each of your virtual hosts to have its own php include path? Do you want to add a domain specific alias? If so do this:
<VirtualHost *>
DocumentRoot /Users/patrick/Documents/Projects/iCalShare/wwwroot
ServerName test.icalshare.com
<IfModule mod_php4.c>
php_value include_path "Users/patrick/Documents/Projects/iCalShare/"
</IfModule>
Alias /manual/ "/Library/WebServer/Documents/manual/"
<Directory "/Library/WebServer/Documents/manual">
Options FollowSymlinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
There's plenty of other stuff you can put in there too.
I have also noticed that with these virtual hosts they are case sensitive. Taking the above example, "http://test.icalshare.com" will work, but "http://test.iCalShare.com" will not. Also, if you name you virtual host "php.mac" then "http://php.mac" will work, but "http://www.php.mac" will not. But if you name your virtual host "www.php.mac" then the opposite is true.
As a side note I have gone kinda crazy with virtual hosts since getting os x. I downloaded the PHP documentation Web pages and made a virtual host for that and the same for mysql documentation, phpmyadmin and so on. The convention I have used for tld's is .mac There is no .mac tld so I don't have to worry about it conflicting with real domains. So, if I want my php documentation I go to "http://php.mac" -
Re: Use NetInfo Manager instead of /etc/hosts and other comments
2003-05-19 14:10:10 icalshare [Reply | View]
Good idea on using NetInfo Manager -- that sounds much easier, especially for people who prefer a GUI option.
...
As for using 'NameVirtualHost *', yes, this works fine if you only intend to use one virtual host.
But, if you wish to use several hosts, and more importantly, wish to use other projects in your Sites folder *without* using Alias or VirtualHost, this will not work.
We need to explicitly name 127.0.0.1, or otherwise Apache sends us straight to the virtual host.
Best,
Patrick
-
Re: Use NetInfo Manager instead of /etc/hosts and other comments
2003-05-20 16:28:39 maarky [Reply | View]
Quote "As for using 'NameVirtualHost *', yes, this works fine if you only intend to use one virtual host."
I use 'NameVirtualHost *' and yet I have no problems despite having set up 12 virtual hosts. From my experiences explicitly naming 127.0.0.1 is not necessary. I could be overlooking something, but it does not seem to be affecting any aliases, though the only ones I have are the standard icons and manual aliases which are there by default and they work fine in all of my virtual domains.
You mentioned there would be problems using "other projects in your Sites folder *without* using Alias or VirtualHost". What do you mean by this? I don't have any problems with the sites folders in /Library/WebServer/Documents or /Users/userName/Sites by going to localhost or localhost/~username. If we don't specify 127.0.0.1 which virtual host are we sent to? Am I missing something because everything at least seems to be working just fine for me.
Thanks -
Re: Use NetInfo Manager instead of /etc/hosts and other comments
2003-05-23 12:39:31 anonymous2 [Reply | View]
I'm also using "NameVirtualHost *" without problems; I'm hosting a couple of real web sites on my Cube, connecting via an Airport base to internet.
One additional thing: Since Apple updates often overwrites http.conf, add
Include /private/etc/httpd/virtual.conf
at the bottom of httd.conf and add all virtual host settings in this file like this:
NameVirtualHost *
<VirtualHost *>
ServerName "www.wigzell.net"
DocumentRoot "/Library/WebServer/Documents"
</VirtualHost>
<VirtualHost *>
ServerName "www.wigzell.se"
DocumentRoot "/Library/WebServer/wigzell.se"
</VirtualHost>
Using this, you only have to re-add the first line after a system update
-
Don't forget to NameVirtualHost
2003-05-17 13:38:02 coyote4til7 [Reply | View]
Apache likes to be not just told how to handle test.icalshare.com, but asked to handle it. If you don't ask it to handle it before telling it how, it'll complain on restart and might even mis-handle some sites.
First, scroll just above the VirtualHost directives and add a NameVirtualHost directive:
NameVirtualHost test.icalshare.com
Then restart Apache: sudo apachectl restart
You can also restart apache via System Preferences, but by doing it at the command line, you'll get to immeadiately see any errors that stopped Apache from starting.
-
Easier way to view hidden files
2003-05-17 09:57:11 kenahoo [Reply | View]
In the Finder, you can navigate to any hidden directory by using the "Go to Folder..." menu command in the "Go" menu. For instance, to look at /tmp/ (which I do frequently), just type /tmp/ in the "Go to Folder" pop-up sheet. You'll see its contents in a regular Finder window.
-Ken
-
Option 1 (~/Sites folder) BADLY wrong???
2003-05-17 09:56:52 anonymous2 [Reply | View]
The article says:
for ~/Sites/ --> http://127.0.0.1/
But that will serve the files in /Library/WebServer/Documents (the webserver's DocumentRoot), not those in ~/Sites/. For access to a user's Sites folder, you need:
http://127.0.0.1/~username/
with subdirectories and filenames appended to that base. Otherwise, on a multiuser machine how would apache know whose Sites to show?
If I am right (99% sure) please correct the article!!!
-
Correction
2003-05-17 11:07:58 icalshare [Reply | View]
I will add a clarification on this issue ASAP. In the meantime, here are two options:
(1) If you're running OS X in single-user mode (which I suspect is the case for most of you) and want to keep things simple, just enable the DocumentRoot option in your Apache config file (/etc/httpd/httpd.conf/).
DocumentRoot "/Users/your_user_name/Sites/"
Doing this will allow you to serve files from your Sites folder without appending your user name to the localhost address (as specified in my first example).
(2) If you need to run in multi-user mode, just append the relevant user name to the localhost examples in the first section.
So, 'http://127.0.0.1' would become 'http://127.0.0.1/~ your_user_name/', and so on.
Cheers,
Patrick -
Correction <-- comments on
2003-05-17 14:10:20 anonymous2 [Reply | View]
Two nit-picky notes:
Be careful with the phrase "single-user mode"--I know you mean that only one person ever uses the machine; it auto-logs-them-in; they never think of "logging out", only shutting down; and so on. But in technical terms, "single-user mode" on OS X means holding down apple-s at startup, which boots you straight to a root shell (no password) instead of the gui. It's intended to save a broken machine by giving you full command-line access. Also, you may confuse people because there is no "setting" for single-user mode vs multi-user mode--it's just the existence of other gui accounts or not. So maybe say something about being the "sole user of the machine", or it being a "shared machine with multiple accounts".
DocumentRoot is already "enabled" (set to /Library/WebServer/Documents). You'd want to *change* its value, which is a perfectly good idea. But then you have some confusion about what .conf file controls what--you can edit /etc/httpd/users/username.conf and ignore the main directives in httpd.conf, or you could change the main <Directory> block in httpd.conf and comment out your username.conf file (so it won't override your earlier settings).
Sorry to be pushy, but I spend a good bit of time in forums answering similar questions and there is no point in confusing people unnecessarily with published material... -
Correction <-- comments on
2005-02-24 15:17:54 CTth [Reply | View]
1. I am so glad to have read this article. Great service.
2.. Before I read the article, I spent several hours tinkering my way through adding aliases and virtual hosts in an effort to change the root folder while maintaining a "clean" URL. Finally it dawned on me that all I really had to do (for my ends) was edit the DocumentRoot reference in the httpd.conf file to point at my folder of choice. This answered all my needs, and I suspect it's all most of the people reading this article are really looking for.
3. As an easily confused person, I concurr with Mr. "Two nit-picky notes" that the wording could have been more presise in your message spelling out the DocumentRoot solution. But no biggee.
4. Even though I figured out that I could edit DocumentRoot, it wasn't until I read your message that I realized it was totaly "okay" to do this. Call me a worrywart. I was concerned, because no one spelled this out in the face of so many forums and articles concentrating on aliases and virtual hosts. So, I'm glad it came to light.
5. Last thing: I do not like how Apache automatically indexes directories and publishes thier contents to anonymous web users. Seems like this should be turned off by default. I know this is not within the direct scope of your article, but turning this OFF was the very next thing I found myself tinkering my way through. I ended up commenting out the two lines that load mod_autoindex. That said, I think this might be a heavy handed approach and I am now wondering if there's a more granular way to turn this off (so that I might be able to turn it on for some directories)?
Thanks again for the article.
-
Name-based virtualhosting made easy
2003-05-16 18:48:19 opatrickly [Reply | View]
I have written a script that I use to add name-based virtualhosts to my Mac OS X machine. It uses NetInfoManager to add the name into the system, and Apache's NameVirtualHost directive. All the virtualhosts the scripts add are strictly local, so you don't have to worry about others snooping about your work. If you care to try it out, you can read more about it here: http://patrickg.com/virtualhost
Patrick -
Name-based virtualhosting made easy
2003-11-15 13:18:54 anonymous2 [Reply | View]
i spent days messing with "httpd.conf" and "hosts" trying to get virtualhosts to work. i read this article, the apache manual and other tutorials. everyone recommended different stuff; nothing worked.
patrick's solution to use netinfo manager and save other directives in "/etc/httpd/virtualhosts/" is simple and elegant. using his shell script makes it even easier.
check out his site.
thanks, patrick. -
Name-based virtualhosting made easy
2004-02-17 21:27:14 rmcgonegal [Reply | View]
Actually I found using BBEdit to be much easier. I've used it thousands of times whereas I've only used Netinfo Manager twice.
In BBEdit you use the "open hidden files" command. That makes all of the files visible in the open dialogue box. Set it to columns view and it is easy to navigate to /etc/httpd. And when you go to save changes on a file such as httpd.conf, BBEdit is smart enough to temporaily elevate your rights to sudo level by asking for the password.
In my documents folder, I made a backup copy of httpd.conf and hosts. Then I created aliases to the originals. -
Name-based virtualhosting made easy
2005-08-18 09:04:57 clussman [Reply | View]
A little late, but I just wanted to add an alternative here that seems simpler to me:
I always keep a Terminal window open. At the command line you can type "bbedit /etc/httpd/httpd.conf" or "bbedit /etc/hosts".
Of course this assumes you're using BBEdit. :)






What can I do to set them in Mac OS X default system?
Is there any free utilities to make it easier?
Thank you in advance.