Tag Archives: apache

Virtual Web Hosting on OSX Leopard

Recently I brought a new iMac for my home development, music, movies, games and general play things. For work I develop Web Applications and with my ISP I have a static IP address, so I decided to set up a small testing webserver. Since Apple has built in PHP and Apache2, this was pretty much a couple simple steps.

I used my post about Installing PHP5 and mySQL on Mac OSX Leopard to recompile PHP and mySQL, then I started setting up my Virtual Hosts.

Heres what I did from a fresh Install of Leopard;

Step One: Enable Apache Web Sharing

Open your System Preferences, then Select the Sharing option.

Enable the Web Sharing checkbox.

Enable Apache Web Sharing

Enable Apache Web Sharing

Step 2: Install PHP and mySQL

Download and install PHP 5 and mySQL 5 and install using my guide @ Installing PHP5 and mySQL on Mac OSX Leopard.

Step 3: Enable Virtual Hosts

Next you have to tell apache that we are going to use ‘Virtual Hosts’ and not just the default document server.

I am going to explain how to edit this in TextMate, but if you don’t use TextMate ( and why not? ) in a later post I can explain how to use Terminal and PICO to do the changes.

Open TextMate, and go to teh file menu, and select Open…

Check the Show Hidden Files checkbox and navigate to

/private/etc/apache2/

and select the httpd.conf file.

Open your Apache httpd.fonfig file

Open your Apache httpd.config file

Now if you press the Apple ( Command ) + L kets which will bring up the Go to Line dialog box. Go to line 465.

TextMate 'Go to Line'

TextMate 'Go to Line'

You will see the Virtual Hosts Include line.

Remove the # so that the file will now be included when Apache runs.

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

Save and close the file. You will be asked for your password.

Step 4: Create your Virtual Hosts

Still in TextMate go to the File Menu and Select Open…. This time you want to navigate to the Virtual Hosts file.

/private/etc/apache2/extra/httpd-vhosts.conf
Open your Virtual Hosts Config File

Open your Virtual Hosts Config File

The first line to edit in this file is to remove the :80 on the NameVirtualHost *:80.

NameVirtualHost *:80

becomes

NameVirtualHost *

For each site you you are going to be serving, you need to add a VirtualHost block.



    ServerAdmin your@email.com
    DocumentRoot "/Library/WebServer/Documents/mysite.com"
    ServerName mysite.com
    ServerAlias www.mysite.com
    ErrorLog "/Library/WebServer/logs/mysite.com_error"
    CustomLog "/Library/WebServer/logs/mysite.com_access" common




    ServerAdmin your@email.com
    DocumentRoot "/Library/WebServer/Documents/mysite2.com"
    ServerName mysite2.com
    ServerAlias www.mysite2.com
    ErrorLog "/Library/WebServer/logs/mysite2.com_error"
    CustomLog "/Library/WebServer/logs/mysite2.com_access" common




    ServerAdmin your@email.com
    DocumentRoot "/Library/WebServer/Documents/mysite3.com"
    ServerName mysite3.com
    ServerAlias www.mysite3.com
    ErrorLog "/Library/WebServer/logs/mysite3.com_error"
    CustomLog "/Library/WebServer/logs/mysite3.com_access" common

Save this file, using your Admin Password.

Step 5: Restart Apache

Now we need to restart the Apache Web Server, you can do this the same as Step 1, disable Web Sharing then re-enable it.

Virtual Hosting Summary

Now you should be able to host more than one domain successfully on your Mac. Things to note are you must be pointing the A Record for your domain DNS Records to your IP Address, and you must point port 80 of your Router or internet gateway to your Mac Internal IP address, I would recommend. using a static Internal IP Address for this machine.

Preventing Image theft with Apache's .htaccess

Do you have an image or images that people are using on their site, Not giving you the credit for your artwork and using your bandwidth?

With a simple .htaccess on your Apache server you can prevent Image HotLinking.

In your Images Directory, or in your mail web root if you have multiple image areas create a .htaccess file. If you already have one, we can just append the following code in to that.

Insert this in to your file.

RewriteEngine On
# REPLACE lendrum\.co.nz WITH YOUR DOMAIN NAME
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?lendrum\.co\.nz/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]

This will send a 403- Forbidden message to all external sites calling your images.

You can allow multiple domains to access the images if you have more pointing to your site, or want certain sites to use your images.

RewriteCond %{HTTP_REFERER} !^http://(.+\.)?lendrum\.co\.nz/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?inboxdesign\.co\.nz/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?apple\.co\.nz/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?apple\.com/ [NC]

If you want to send a different image, rather than a 403 error you can change the last line to this: But this is almost defeating the purpose, but it will let the visitors on the other sites know that the webmaster is an image thief

RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/DoNotSteelMyImages.jpg [L]

Simple :)

Note: This will only work on Apache Servers.