Setting up Virtual Hosts on my Mac 10.7

Introduction

I’ve setup Apache virtual hosts on my Lubuntu laptop. I want my Mac to also have virtual hosts so that my development environment will be the same on both computers.

Relevant Info from My Laptop Setup

Here I will write about things I did for my laptop. This will help me in that I’ll be able to quickly glance over to see this type of information as I follow directions for the new setup and configuration.

It should be noted that Apache configuration directives get applied just the same no matter which configuration file they exist in—as long as that file gets included in one of the other configuration files which get read when Apache starts. Sequence matters—since directives which are the same overwrite each other.

Fundamentals

On Lubuntu the Apache master configuration file is:

/etc/apache2/apache2.conf

The FQDNs I used:

ci-practice.local
static-html-site.local

I’ll return the DocumentRoot for the default VH on my Mac to the standard one—like /var/www. This will make it so there is only one way to access my static-html-site.local VH. Additionally, this will match the set up I have in Lubuntu.

ServerAdmin gets set for each VH.

ServerName gets set for each VH—except the default VH.

Create a ServerAlias for each non-default VH. For example one that starts with www.

Put an explanation in the page which loads for the default VH informing the user about where to find content—namely using the FQDNs for the VHs. I’m thinking this page is in the original/default DocumentRoot for a new Apache installation.

DocumentRoot gets set for each VH.

I’ll have to modify the permissions on the Dropbox directory if I decide to set the DocumentRoots directly to within the Sites directory inside Dropbox.

chmod 755 /home/sameh/Dropbox

is the command I used for my laptop.

The <Directory> stanza gets modified for each VH to reflect the specified DocumentRoot.

/etc/hosts has to be configured for the new FQDNs to point to 127.0.0.1

AllowOveride All gets set for All the VHs to allow .htaccess to work.

Restart Apache after making the changes:

service apache2 restart

On Mac you may have to do something different.

Find the existing DirectoryIndex directive and add default.htm to its path. In Lubuntu this directive was found in the file:

/etc/apache2/mods-available/dir.conf

Remove Indexes option for each VH. See the Options directive in the <Directory> stanza.

/etc/hosts

127.0.0.1	localhost ci-practice.local static-html-site.local
127.0.1.1	sameh-Panasonic-TB

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

/etc/apache2/mods-available/dir.conf

<IfModule mod_dir.c>

          DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm default.htm

</IfModule>

The default VH

<VirtualHost *:80>
	ServerAdmin gxsam1968@yahoo.com

	DocumentRoot /var/www
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /var/www/>
		Options FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel debug

	CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

static-html-site.local

<VirtualHost *:80>
	ServerAdmin gxsam1968@yahoo.com
	ServerName static-html-site.local
	ServerAlias www.static-html-site.local

	DocumentRoot /home/sameh/Dropbox/Sites
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /home/sameh/Dropbox/Sites/>
		Options FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/static-html-site.local-error_log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel debug

	CustomLog ${APACHE_LOG_DIR}/static-html-site.local-access_log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

ci-practice.local

<VirtualHost *:80>
	ServerAdmin gxsam1968@yahoo.com
	ServerName ci-practice.local
	ServerAlias www.ci-practice.local

	DocumentRoot /home/sameh/Dropbox/Sites/CodeIgniter_2.1.0
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory  /home/sameh/Dropbox/Sites/CodeIgniter_2.1.0/>
		Options FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/ci-practice.local-error_log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel debug

	CustomLog ${APACHE_LOG_DIR}/ci-practice.local-access_log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

Actions Taken to Configure VHs on my Mac

Generally, I’ll be following steps outlined in the post called Virtual Hosts in 10.7.3 .

hosts

/private/etc/hosts

127.0.0.1	localhost ci-practice.local static-html-site.local sameh-labibs-imac.local

Enabled Virtual Host in /private/etc/apache2/httpd.conf

This step was not necessary for configuring Apache on my Lubuntu laptop; Maybe it’s because Lubuntu’s Apache has virtual hosts hard coded; Or, maybe it’s because this directive is just not neccessary—since, its functionality is served by other directives. I’m talking specifically about the NameVirtualHost directive. The NameVirtualHost directive tells Apache that you will use VirtualHost containers on a specific IP address. This command is needed only once per IP address.

Further search showed me that the use of <VirtualHost *:80> makes it unnecessary (and potentially harmful) to use NameVirtualHost *:80. However, I’ll use it just to be compliant with the tutorial.

Important — Once virtual hosting is active Apache will NOT use the DocumentRoot directive in the main configuration file. However, it will use the Directory stanzas—and, those will cause the “forbidden” access problem if they’re not removed.

Additionally, the first virtual host defined is the one which becomes the default—according to the documentation in the httpd-vhosts.conf file below.

I un-commented the line in the main Apache httpd.conf file which does an Include on the following file:

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

Important File — this file will be used to hold ALL VH directives.

Since now httpd-vhosts.conf is being read by Apache, the following directive will get executed.

NameVirtualHost *:80

which tells Apache that name-based virtual hosting will occur for the IP/ports on this machine satisfying the wildcard expression *:80.

Started Configuring — following steps like for laptop

I made sure httpd-vhosts.conf has boxes for the following VHs:

  • default
  • ci-practice.local
  • static-html-site.local

ServerAdmin gets set to gxsam1968@yahoo.com for each VH.

ServerName gets set for each VH—except the default VH.

Create a ServerAlias for each non-default VH. For example one that starts with www.

At this point I set some of the values in the VH blocks. However, I won’t list them here—but I’ll just say: I set the DocumentRoot for the default VH to /Library/WebServer/Documents

Put an explanation in the webpage which loads for the default VH informing the user about where to find content—namely using the FQDNs for the VHs. This page is in /Library/WebServer/Documents

Problem: When I use one of the FQDNs to access the default VH I get a forbidden error from Apache—Forbidden You don’t have permission to access / on this server.

Update! This problem has been solved. Ignore the workaround; and, see solution at bottom of this post. I don’t have to set DocumentRoot to be under my Sites directory any more!

I set up the following DocumentRoot for ci-practice.local

/Users/samehlabib/Sites/Sites/CodeIgniter_2.1.0

Likewise, for static-html-site.local I set up the following DocumentRoot

/Users/samehlabib/Sites/Sites

Amazingly, I don’t get the forbidden error as I do for the default VH. I think it is because the DocumentRoot for each of my specific VHs falls inside my user Sites folder. I think (it is a Mac quirk) that every DocumentRoot has to point to a directory or symlink within the user’s Sites folder. Therefore, I’ll try that for the default VH.

I set up a symlink /Users/samehlabib/Sites/Documents which points to /Library/WebServer/Documents . Then I changed the DocumentRoot for the default VH to /Users/samehlabib/Sites/Documents

Hooray! It worked. Just make sure you create the symlink by using the command line.

The <Directory> stanza gets modified for each VH to reflect the specified DocumentRoot. Basically I copied the directory stanzas for the VHs from the laptop; and, modified the directory paths as needed.

Find the existing DirectoryIndex directive and add default.htm to its path. By testing I could tell this has already been configured. I had already configured it in /private/etc/apache2/httpd.conf

Now, I’ll try to put some directives into each VH configuration for specifying things related to logs. I’ll be merging the configurations from the laptop with what I’ve already got there. Here are my notes on this matter:

  • I didn’t use environment variables as part of the log file paths
  • All logs are in /private/var/log/apache2 in files named after their FQDN
  • I made sure those log files existed and were empty. Apparently, doing this was unnecessary since the system is able to create, name, change ownership and assign permissions to these files all on its own.
  • error_log and access_log were the names I stuck with for the default VH—to match the names given in the main .htaccess file.
  • Contrary to what’s happened so far I see Apache takes some log file directives from the main configuration—as opposed to from the VH configuration.

Continuing based on Instructions

Generally, I’ll be following steps outlined in the post called Virtual Hosts in 10.7.3 .

This command-line command tests Apache:

apachectl -S

I set ServerName sameh-labibs-imac.local in the main Apache configuration file. This took care of the error log entry that keeps showing up when I restart Apache.

I’m looking for an answer to why I lost permission (forbidden!) to directly access the old default Apache DocumentRoot. I think the answer has something to do with a lack of a Directory stanza for its directory.

To cut out some clutter I commented out the unnecessary DocumentRoot directive in the main config file. And Apache tested good!

I took out the two Directory stanzas in the main config. The one for / and the one for /Users/samehlabib/Sites/Sites/ . Doing so should eliminate the restrictions which result in me having to access all directories through the Sites folder. And, this should remove the ambiguity with the corresponding VH configuration. And Apache didn’t break after I did this!

Now, I’ll test to see if this has eliminated the problem of “forbidden” when I specify a DocumentRoot outside of my Sites folder.

Hooray again! It worked.

Advertisements

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in Configure My Systems. Bookmark the permalink.

2 Responses to Setting up Virtual Hosts on my Mac 10.7

  1. Pingback: Lubuntu cont’d — Actual Apache2 Configuration | Sam's PHP

  2. Pingback: Notes about Setting up my Mac LAMP | Sam's PHP

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s