What is a virtual host? Virtual hosting is a method for hosting multiple domain names on a computer using a single IP address. Shared hosting uses this same method for all of the sites they host. Aren’t those URLs you use in Zend Server getting pretty ugly? Would you like to change http://localhost/my/awesome/website into http://www.super-awesome.local You can!
Last time we talked about general Zend Server setup. This time, we will be going through setting up virtual hosts on your local machine. This again will involve Terminal.
This assumes you have Zend Server set up and running. If it is not, go here.
-
Open Terminal. Terminal can be found at /Applications/Utilities/Terminal.app
You can also open it from Spotlight by typing “Terminal”
-
We are going to edit our httpd.conf file. In Terminal type:
sudo vi /usr/local/zend/apache2/conf/httpd.conf
Enter your password if prompted
-
Use the up, down, left, right arrow keys to scroll down to where you see:
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf
-
In Terminal press “i”. This will take you into edit mode to edit the file.
-
Remove the “#” before “Include”. It should look like this now:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
-
Press the escape key to exit out of edit mode. Press “:x” to save your changes.
-
Now we are going to edit our httpd-vhosts.conf file. In Terminal type:
sudo vi /usr/local/zend/apache2/conf/extra/httpd-vhosts.conf
Enter your password when prompted
-
In Terminal press “i” to go into edit mode.
-
Use the up, down, left, right arrow keys to scroll down to where you see:
NameVirtualHost *:10088
In the previous tutorial we change the port to 80. So now we have to change this line to read:
NameVirtualHost *:80
-
Let’s say you want to have a local virtual host for http://www.super-awesome.local that needs to be pointed at the /Users/YOUR_USERNAME/Sites/super-awesome directory. We need to add the following to the httpd-vhosts.conf file:
<VirtualHost *:80>
DocumentRoot "/Users/YOUR_USERNAME/Sites/super-awesome"
ServerName www.super-awesome.local
ServerAlias super-awesome.local
</VirtualHost>
* Note: .local is just what I picked. You can change that to .dev or .blah or whatever you like. Try to stay away from common domain extensions like .com or .net. And, yes, you can end it with more than one extension like .foo.bar
-
Press the escape key to exit out of edit mode. Press “:x” to save your changes.
-
It’s probably a good idea to make sure we didn’t mess up syntax or spelling. In Terminal type:
sudo apachectl configtest
sudo apachectl graceful
Enter your password if prompted
If there are any errors they will be displayed to you. Repeat steps 10 and 11 as many times as needed.
-
Let’s go ahead and restart Zend Server so it can get the changes. In Terminal type:
sudo /usr/local/zend/bin/zendctl.sh restart
Enter your password if prompted
-
Browse to either http://www.super-awesome.local or http://super-awesome.local and your site should display
You can repeat step 10 as many times to add more and more virtual host in your system.
Very helpful – I think you also need to add the lines
127.0.0.1 super-awesome.local
127.0.0.1 http://www.super-awesome.local
to the hosts file
http://www.super-awesome.local/ didn’t work for me, but http://super-awesome.local/ did. Also, just a reminder, you need to add the folder super-awesome to /Users/YOUR_USERNAME/Sites directory with an index.html in order to see something.
I’ve started using http://clickontyler.com/virtualhostx which does this in a lovely GUI