Instructions for setting up a basic Apache environment on OS X Yosemite Only. For Mavericks, view this guide
For this guide, square brackets are used to denote places where you need to plugin your own stuff. In none of these cases are you supposed to use actual square brackets.
Note that /private/etc
and /etc
go to the same place on Yosemite. This is good to know for later.
If you've already configured Apache correctly and you just want to know the part about setting up a new Virtual Host, then you can skip to that section. If not, then you'll need to configure Apache.
Run this line of code to see what your username is (and what the name of your home folder is)
whoami
Make a Sites
folder in your home directory. In older versions of OsX, this folder may have been created through other means. If you upgraded to Yosemite, this folder may exist and be filled for whatever reason. If you're coming from the Ubuntu world, this folder is equivalent to Ubuntu's /var/www
. Also make a folder called Hosts
. You'll learn more about this folder later in this gist.
mkdir ~/Sites
mkdir ~/Hosts
Note that the tilde
~
means "My Home Directory"
Open Apache's main config file to edit it. Note that you must use sudo
or you won't be able to save your changes.
sudo nano /etc/apache2/httpd.conf
The file is long. You will need to scroll through it to find the parts that need to be changed.
Ensure that Apache is listening to Port 80. The line in the file should look like this:
Listen 80
Look for the section with many instances of "LoadModule". Where Mavericks had most of these modules loaded by default, Yosemite's default has many of these disabled. You will need to enable some that might be disabled. These three files are located toward the bottom of the list:
mod_vhost_alias.so
libphp5.so
mod_rewrite.so
At the very least you will need to enable the mod_vhost_alias.so
file. If you are going to be using PHP, then enable the libphp5.so
file. If you are going to do Mod Rewrite (if you don't know what that is, assume you will need it. It's a module that many websites need).
Enable the modules by removing the #
sign.
The next section you need to possibly change will start with a tag called <Directory />
. Change yours to look exactly like this if it doesn't already
<Directory />
AllowOverride none
Require all denied
</Directory>
This sets up how Apache deals with your entire computer. We don't want to turn your whole computer into a server, only the "Sites" folder you created. Notice the single
/
forward slash after the word Directory? That's the folder path that these rules apply to. A single forward slash means your computer's root. Therefore these rules are being applied to the whole computer as a default.
Change the DocumentRoot
to be what you see below. Note that you'll put your real username in (what you got from the whoami
command).
DocumentRoot "/Users/[username]/Sites"
The next <directory ... >
section is where we setup the Sites folder for Apache to use. If you have the defaults still, it might look like this: <directory "/Library/WebServer/Documents">
Make yours look like this:
<Directory "/Users/[username]/Sites">
Options Indexes FollowSymLinks MultiViews
MultiviewsMatch Any
AllowOverride All
Require all granted
</Directory>
Note that there might be other settings in this Directory section. Leave those alone. The above example only shows things you need to change.
When you setup Apache locally, your computer is a server for your browser, and for every computer on the local network that you're on. Someone can type your IP address into their browser and potentially see your sites on your computer. To lock your Apache down to only listen to requests from your computer, add these Deny from all
and Allow from 127.0.0.1 localhost
lines above the Require all granted
in the previous steps
<Directory "/Users/[username]/Sites">
Deny from all
Allow from 127.0.0.1 localhost
Require all granted
</Directory>
Now you'll need to scroll way down to the line of code that looks like this:
Include /private/etc/apache2/extra/httpd-vhosts.conf
It may or may not be commented out.
Change it to look like this:
#Include /private/etc/apache2/extra/httpd-vhosts.conf
Include /Users/[username]/Hosts/*.conf
Yes, we want to comment out the Include for the httpd-vhosts.conf
file. Most guides will tell you to uncomment that and they won't have the lines for the NameVirtualHost
and the next include. In those cases, they will have you make all of your VirualHost records in that httpd-vhosts.conf
file. But that sucks! I don't want to navigate to /private/etc/apache2/extra/httpd-vhosts.conf
every time I want to make a Virtual Host. The extra two lines I have will allow us to make VirtualHost records in a place that's much more convenient; the Hosts
folder we made in earlier steps
That's it for this file. Exit and save. Note that you are required to do an Apache restart when editing this file, but we still have some other changes to make first that also require a reset. So we'll do that reset at the end.
Restart Apache every time you make Virtual Host changes or general Apache.conf changes
sudo apachectl restart
Make Project Folder in your sites folder. You can do this with Finder, or in command-line:
mkdir ~/Sites/[projectname]
Make an index file in your new project (optional)
echo 'hello world' > ~/Sites/[projectname]/index.html
Make a VirtualHosts record. If you followed the instructions from above, you may remember that our VirtualHost records are going to be placed in the ~/Hosts
folder. Also, you may have noticed that the way we included the Hosts folder allows us to have separate files for each VirtualHost. How convenient :)
If you don't have any VirtualHost files yet, you'll need to make your first one. Otherwise it might be easier to just copy an existing on and edit it.
The VirtualHost file should look like this:
<VirtualHost *:80>
DocumentRoot "/Users/[username]/Sites/[projectname]"
ServerName "[projectname].com"
</VirtualHost>
The ServerName
is what we'll be typing in the browser to pull up this project. You can type any domain name you want. Typically we use a sub domain to indicate it's a local project like "dev". So imagine you're working on a project "www.foo.com". For your local development you would put "dev.foo.com". In the next steps we'll show you how to edit your "hosts" file to tell your computer that "dev.foo.com" (or whatever you choose) needs to look at our local Apache and not on the real web.
Restart Apache every time you make Virtual Host changes or general Apache.conf changes
sudo apachectl restart
If you're coming from the Ubuntu world, you might now that we would need to "enable" the VirtualHost at this point (using a2ensite
). OsX's version of Apache has no such concept. However, you do have to restart Apache any time you make changes to any VirtualHost file (or to the Apache configuration file as we did in the setup).
Edit our local computer's "hosts file".
sudo nano /etc/hosts
The file should already have some stuff in it. Don't change that stuff. At the bottom of the file add this:
127.0.0.1 [projectname].com
127.0.0.1
is your computer's local IP address (it's the same for everyone). After the IP type a tab
then type the same value that you have in your VirtualHost's ServerName.
Note that editing the hosts
file does not require an Apache restart.
What's the file name of VirtualHost in the Hosts folder? I named it httpd-vhosts.conf.
The curious thing is that I followed your tutorial, but it didn't work.
The apache error log output:
AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace