Virtual hosts and multiple Solar projects
September 27th, 2008 by Ray in Cookbook, Solar | Tags: Cookbook, Solar | No CommentsIn the first part of the series, Big Bang with Solar, I showed you how to download and install Solar for a single project. What I didn’t show you is a way to set up Solar for multiple virtual hosts under Apache, which is what I will be showing you today in our example.
For our example we will use the fictional web sites polygon-ex.com and square-ex.com. If these sites actually exist I apologize in advance for using the names without permission…please forgive me
Configuring Apache
Let’s start by creating two new configuration files for Apache. These configuration files will hold information pertaining to our virtual hosts.
$ cd /etc/httpd/conf.d/ $ touch polygon-ex.conf $ touch square-ex.conf
Before we edit these files, let’s create the directories for each web site under /var/www.
$ cd /var/www $ mkdir polygon-ex $ mkdir square-ex
Now we can edit our configuration files. Let’s start with polygon-ex.com.
$ nano /etc/httpd/conf.d/polygon-ex.conf
To define the virtual host you will want something like the following.
<virtualHost *:80>
ServerName polygon-ex.com
DocumentRoot /var/www/polygon-ex/docroot
<directory />
AllowOverride All
</directory>
</virtualHost>
Note that you need to have “NameVirtualHost *:80″ in your main httpd.conf file, most likely located at /etc/httpd/conf/httpd.conf, in order for virtual hosts to work. See Using Name-based Virtual Hosts in Apache’s documentation for more information regarding this.
Note that I have opted for name-based virtual hosting instead of IP-based.
Note the line AllowOverride All. I have set very loose access here for example purposes only. Please refer to Apache’s wonderful documentation on the AllowOverride Directive so you are not compromising the security of your server.
Now do the same for square-ex.conf but change the ServerName and DocumentRoot information to coincide with square-ex.com.
Last but not least, restart Apache.
$ /etc/init.d/httpd restart
Obtaining and extracting Solar
If you haven’t downloaded solar-system-1.0.0alpha2.tgz already, go do so now.
Change your working directory to where you saved solar-system-1.0.0alpha2.tgz and do the following.
$ tar -zxvf solar-system-1.0.0alpha2.tgz -C /var/www/polygon-ex $ tar -zxvf solar-system-1.0.0alpha2.tgz -C /var/www/square-ex
Now if you change your working directory to /var/www/polygon-ex or /var/www/square-ex you will see a folder called “solar” under each. We do not want to keep the directory set up like this, since we want Solar to be installed at the root of our virtual host.
To correct this we just move some files around.
$ cd /var/www/polygon-ex/ $ mv ./solar/* ./ $ rm -Rf ./solar/
And for square-ex.
$ cd /var/www/square-ex/ $ mv ./solar/* ./ $ rm -Rf ./solar/
What we just did here, was move the contents of the “solar” directory up a level. We also removed the now empty “solar” directory that is no longer needed. Now our Apache configuration file will be able to find the directory “docroot.”
One last thing before we move on. We need to change the permissions of our /tmp folder within our project directories so that our pre-configured Solar project can read/write PHP sessions to our sessions folder (located at /var/www/polygon-ex/tmp/session and /var/www/square-ex/tmp/session).
$ cd /var/www/polygon-ex $ chmod -R 777 ./tmp/ $ cd /var/www/square-ex $ chmod -R 777 ./tmp/
Testing it all out
Now how do we know this all works? Well, let’s test it! First and foremost, since polygon-ex.com and square-ex.com are not real domains, we must enter them into our “hosts” file so we can resolve the domain name. Let’s do that now.
$ nano /etc/hosts
Your hosts file may look like the following.
127.0.0.1 localhost.localdomain localhost
We want to change it to look like this.
127.0.0.1 localhost.localdomain localhost polygon-ex.com square-ex.com
Go ahead and open up your web browser and point it to http://polygon-ex.com or http://square-ex.com. You should see “Hello World!” on each page.
If you are not convinced that each project is associated with its own domain then let’s step it up a bit and edit one of the Hello World examples.
$ nano /var/www/polygon-ex/source/solar/Solar/App/Hello/View/index.php
Change the text to read.
Hello World! - Welcome to polygon-ex.com!
Save and close that file and refresh http://polygon-ex.com in your web browser. You should now see the text we just entered. Now browse on over to http://square-ex.com and you will just see “Hello World!”
Ta da! You just setup two virtual hosts that use Solar. Now go explore Solar by visiting SolarPHP.com and don’t forget to join us on the mailing list and/or in the IRC channel.
Leave a Reply