How can I set a web server in a specific directory in OSX Mavericks?
Solution 1:
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.
Solution 2:
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.