7

I am running a web server in OSX Mavericks using the default directory location ($HOME/Sites). How can I add sites from distinct arbitrary directories ?

I have found instructions about how to change the root folder of the apache server, but I do not want to override the default location (I have things there) but to add new folders.

1
  • You will need to modify the /etc/apache2/http.conf file -
    – mmmmmm
    Commented Mar 6, 2014 at 19:24

2 Answers 2

5

This might be overkill, or answering a slightly different question than you asked, but if you have npm, you can

npm install http-server

and then just

http-server

from the directory you want to host. http-server will reply with the path hosted and the port it is hosted on e.g.:

Starting up http-server, serving ./foo on port: 8080 Hit CTRL-C to stop the server

If you launch multiple instances it will increment the port.

3
  • 1
    I had not thought about using something else than Apache (already installed). Maybe I should give it a try to npm. Could you please expand a bit your answer ? How can I access the server at that location ? (I already have the apache server running in port 80). Is it easy to shutdown the server for the directory where I started ?
    – Sergio
    Commented Mar 5, 2014 at 19:13
  • yep. I added a bit. It tells you the port to use and how to shutdown. Just leave that terminal window open and you can quit it whenever you are ready.
    – dwightk
    Commented Mar 5, 2014 at 19:20
  • You can install node (which includes npm) using homebrew, if desired, via "brew install node". Commented Aug 25, 2014 at 4:37
5

The easy way (python):

$ cd some/dir
$ python -m SimpleHTTPServer

The slightly less easy way (apache):

Use apache virtual hosts. Here is a guide http://coolestguidesontheplanet.com/set-virtual-hosts-apache-mac-osx-10-9-mavericks-osx-10-8-mountain-lion/. Basically, it allows you to point apache to multiple directories (they can be located anywhere).

It goes something like this:

$ sudo nano /etc/apache2/httpd.conf

Uncomment this line

Include /private/etc/apache2/extra/httpd-vhosts.conf

Edit the vhosts

$ sudo nano /etc/apache2/extra/httpd-vhosts.conf

There should be an example in the file, you can just edit that to fit your needs. Here is an example.

<VirtualHost *:80>
  ServerAdmin [email protected]
  DocumentRoot "/Users/USER_NAME/Sites/test.com"
  ServerName test.com
  ErrorLog "/Users/USER_NAME/Sites/test.com/error.log"
  CustomLog "/Users/USER_NAME/Sites/test.com/access.log" common
</VirtualHost>

When you want to add another site, copy those lines and append them to the bottom of the file (changing the settings for your new site)

Lastly, edit your /etc/hosts file so you can access your new site.

$ sudo emacs /etc/hosts

Add this line

127.0.0.1 local.test.com

Restart apache for the changes to take effect.

$ sudo apachectl restart

Since you said that you wanted to keep the default site, add an entry into the vhosts file that looks like this

<VirtualHost *:80>
  ServerName localhost
  DocumentRoot /Users/USER_NAME/Sites
</VirtualHost>

Then restart apache.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .