Posts Tagged: Apache


13
Mar 10

How to run multiple sites on OS X using Apache

Update 1: An automated way doing all the stuff in this post is using VirtualHostX – you might want to try that out. It’s free for up to three vhosts – after that it costs money.

Update 2: Or even better: You can use the all free hostess. It’s pure command line and works great.

Today I wanted to test some PHP and MySQL using Apache in OS X but wasn’t able to find any guide on how to do this if I wanted multiple sites. So I might as well create my own:

cd /etc/apache2
sudo mkdir mysites
# or whatever (I used my name, 'lasse', for the name)

cd mysites
sudo nano phptest.conf
# or vi or whatever

Put the following in phptest.conf:

<VirtualHost *:80>
  ServerName phptest.local
  DocumentRoot "/Users/yourname/dev/web/phptest"
  <Directory "/Users/yourname/dev/web/phptest">
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

Remember to replace yourname for your username and to create the ~/dev/web/phptest folder.

Back into the bash:

cd /etc/apache2
sudo nano httpd.conf

First find the lines that say:

#LoadModule php5_module        libexec/apache2/libphp5.so
#LoadModule fastcgi_module     libexec/apache2/mod_fastcgi.so

And uncomment these two lines likes this:

LoadModule php5_module        libexec/apache2/libphp5.so
LoadModule fastcgi_module     libexec/apache2/mod_fastcgi.so

This enables PHP.
Next, go to the bottom of the same file and add the following line:

Include /private/etc/apache2/mysites/*.conf

This tells Apache to also load configuration files from the mysites folder.
Next, edit your hosts file to enable the phptest.local name:

sudo nano /etc/hosts

Add the following line at the bottom of the file:

127.0.0.1 phptest.local

This tells your DNS resolver to look for the site locally at your computer (127.0.0.1) when you type phptest.local in your browser.

It’s necessary to restart Apache to enable the new settings (you’ll need to do this every time you add a site).

sudo apachectl -k restart

Finally we create a test file to make sure it all works:

cd ~/dev/web/phptest
nano test.php

Enter the following:

<?php echo "Hello World!" ?>

Now you should be able to go to http://phptest.local/test.php in your browser and it should say Hello World!. If not, then something is wrong.

Adding a new site

Next time you want to add a site, just complete the following steps:
1. Create a site root folder in ~/dev/web/xx or wherever you keep your sites.
2. Create a configuration file similar to phptest.conf in /etc/apache2/mysites/ where you replace the paths and ServerName for the new ones.
3. Create an entry in /etc/hosts with the same name as you used as ServerName.
4. Restart Apache using apachectl -k restart.

Is there an easier way to do this? Please let me know in the comments.


24
Sep 09

Three months on the Mac stack

It’s actually four months now – since May 1st – but I’ve been wanting to write this article for a month. Hence the – somewhat misleading – title.

I bought my MacBook on May 1th after viewing a screencast where a guy sets up a blog in 15 minutes using Ruby on Rails. I immediately said to my stepdad: “I’m going to buy a Mac and learn Ruby on Rails!”

And so I did – the day after, I bought a Mac.

I’ve always been a dedicated user of Microsoft’s products. First C64, then Amiga, then Mac, then PC. I liked the way everything was tested and came from one place, unlike open source. I’ve always been saying stuff like “I like to pay for my software because then I’m be sure about the quality.” – but in reality, everyone who uses Windows and other Microsoft products know that this isn’t always the case.

So I bought the Mac, and the first thing that surprised me, was how much of day-to-day work was done in the Terminal, or command line. When I installed Ruby on Rails, it was via command line; when I installed plugins, it was via command line. Evererything command line. :-)

Over the next few days I began to get a hang of it. I bought a couple of domain names and signed up for a slice at Slicehost – because I had read some job description that said that you should’ve at least tried to use a whole day of setting a slice.

Coming from Windows, Linux is a whole other deal to setup. I used a lot of the Slicehost articles that guides you through the whole process from setup and security to getting your slice to run as a webserver.

In the beginning I was a little nervous about all the command lines, if they would really work and so one. But the more you try it, the more calm you get. It just works! And lucky me there was a lot of helpful articles about Unix and Linux commands out there (just search for the command on Google).

Since starting out on the Mac, I’ve learned a multitude of things:

In short I’ve learned so much about the open source world that just wasn’t that easy on the Microsoft platform.

I still use Microsoft Windows and other products, but now it’s through VMware Fusion on the Mac.

I’m happy about the Mac because OS X is very unobtrusive, fast operating system and what it does, it does very good. But at the same time, I also want a netbook that’s easier to carry, so I may end up running both systems for different purposes (unless I’m just installing Ubuntu on the netbook too ;-)).

Hope you found this post interesting – I wrote it to tell about a beautiful (yak, I know) progress from Windows to Mac and Linux. Thumbs up if this has made you want to try it too. And please tell me in the comments. :-)


Fork me on GitHub