OSX 10.8+ | Apache | PHP | Dropbox | VirtualHosts | symlinks | php
NOTE: there are a number of things that will stuff up your machine if you do not know what you are doing, so please be careful. I do not tell you how to take a backup of the files you are changing, so please take a copy backup of the file before you start hacking with the below, and make sure you know how to go back if things go wrong.
I have been needing to do this on a number of OSX machines, and I need to look it up every time. Most sites do part of what I need, however I thought that I would put it all in one spot.
Step 1. Create virtual website folder inside of Dropbox
In my configuration, I want to synchronise my websites across machines so that I can work the same on multiple machines. So my website folder is in ~/DropBox/Development
cd ~/DropBox/Development mkdir newsite (I called mine stg)
Step 2. Setup a folder to manage apache and logs across machines
I use Dropbox to keep my work sync'd across machines, so I want to keep my logs all in one place across machines.
In my Dropbox, directory, I set up a folder called apache2conf and then set up a folder per machine under it. So on the machine I am using is named "Little iMac", so I have a folder:
mkdir ~/Dropbox/apache2conf mkdir ~/Dropbox/apache2conf/littleimac
Inside this folder, I then create the following:
mkdir ~/Dropbox/apache2conf/littleimac/extra mkdir ~/Dropbox/apache2conf/littleimac/logs
The extra folder contains our vhosts file and our logs directory will hold the log files for each virtual site on this machine
Step 3. Creating the vhosts file
The vhosts.conf file is where you set up the specifics of your virtual sites. It is a subservient file (or an include) to Apache's main configuration file called httpd.conf. We will create the vhosts file that we want and then update httpd.conf to include it (and start using it)
So instead of starting from scratch on the vhosts.conf file, I will copy the standard one on my OSX machine.
cd ~/Dropbox/apache2conf/littleimac/extra cp /private/etc/apache2/extra/httpd-vhosts.conf .
You should now have a httpd-vhosts.conf file in your extra directory, and we can edit it using vi or nano if you find it easier.
sudo vi httpd-vhosts.confpassword: <your user password>
#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
#
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
# Setup permissions for all folders under the base folder
Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
AllowOverride All
Order allow,deny
Allow from all
# RETAIN LOCALHOST
# For http://localhost point it at your document root, however put logs inside the custom folder we setup
DocumentRoot /Users/Douglas/DropBox/Development
ServerName localhost
CustomLog "/Users/Douglas/Dropbox/apache2conf/littleimac/logs/default-access_log" combined
ErrorLog "/Users/Douglas/Dropbox/apache2conf/littleimac/logs/default-error_log"
# VHOST local.stg
DocumentRoot "/Users/Douglas/DropBox/Development/local.stg"
ServerName local.stg
ErrorLog "/Users/Douglas/Dropbox/apache2conf/littleimac/logs/local.stg-error_log"
CustomLog "/Users/Douglas/Dropbox/apache2conf/littleimac/logs/local.stg-access_log" common
Notice a few things above:
- The log directories are the apache2conf folder, and named appropriately for the site.
- The permissions will work no matter what the username you are using on the current machine
- You will need to update this file depending on the user and machine. This is why this file will be save in the apache2conf folder under a machine name folder. i.e. on littleimac my user is Douglas
Step 3. Updating httpd.conf to include and activate the vhosts file
sudo vi /private/etc/apache2/httpd.conf Inside vim editor search for vhosts by typing /vhosts and hitting enter
You should be at a line that looks like this:
# Virtual hosts #Include /private/etc/apache2/extra/httpd-vhosts.conf
Copy the #Include line below this line. Then remove the # in front of the Include statement so that the vhosts file that you set up already is enabled. So it should look like the following:
# Virtual hosts#Include /private/etc/apache2/extra/httpd-vhosts.conf
Include /Users/Douglas/Dropbox/apache2conf/littleimac/extra/httpd-vhosts.confSo now your apache knows to include (and activate) the vhosts file that you setup above.
Step 4. Updating host file
You now need to edit the host file so that your computer will know that your new site exists locally on your machine rather than on the web. You will need to use sudo, as it is a protected file.
sudo vi /private/etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
You need to add a record at the bottom that points to your local machine when local.stg is requested
127.0.0.1 local.stg
Step 5. Updating permissions in the Dropbox folders so that apache can access them.
Permission issues are the biggest problem from my experience with Apache and Dropbox and symlinks.
First of all we want to use a group that will have access to sites on your local machine, however not necessarily give the apache user root access.
One way of doing this is to add the apache user (_www) to the staff group that is setup for all normal users. This will give _www elevated access to areas where group users are allowed. Not great, and there will definitely be better solutions out there, however I just want to get this going, and it is my development machine (not a production box).
The second thing we need to realise is that as far as I can tell (trial and error), apache needs access all the way up the tree to the root of the directory. I am not sure if this is correct, however this is what I needed to do in order for it to work.
Change the permissions from the /Users/ folder up to your site folder.
So as a memory hit 7 is rwx, 5 is r_x and 4 is r__. So we start by making sure that the group staff has access down from /Users
sudo chmod 740 /Users sudo chmod 740 /Users/Douglas chmod 740 /Users/Douglas/Dropbox chmod -R 750 /Users/Douglas/Dropbox/Development
The recursive -R makes the staff group rwx from Development down to the end of the folder branch.
Now next you need to add _www user to the staff group, so that it will pick up this access.
sudo dseditgroup -o edit -a _www -t user staff
So now the permissions should be set appropriately, we need to restart apache.
Step 6. Turning on php
There is one more step to turn on php if you need it.
sudo vi /private/etc/apache2/httpd.conf Inside vim editor search for php5 by typing /php5 and hitting enter
Remove the # in front of LoadModule in this line.
#LoadModule php5_module libexec/apache2/libphp5.soStep 7. Restarting apache and cross fingers
sudoThat should be it. Good luck.
Comments
Post a Comment