Skip to content

Instantly share code, notes, and snippets.

@bradwestfall
Last active August 25, 2017 16:35
Show Gist options
  • Save bradwestfall/c3e44d5c1e4a74ad852e to your computer and use it in GitHub Desktop.
Save bradwestfall/c3e44d5c1e4a74ad852e to your computer and use it in GitHub Desktop.
OS X (Yosemite) Apache Environment Setup

OS X (Yosemite) Apache Environment Setup

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.

Configure Apache

Who am i?

Run this line of code to see what your username is (and what the name of your home folder is)

whoami

Sites and Hosts

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"

httpd.conf

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.

Port 80

Ensure that Apache is listening to Port 80. The line in the file should look like this:

Listen 80

Enabling PHP (optional)

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.

DocumentRoot

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"

Sites Directory

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.

Security

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>

Include Hosts

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

Starting a new VirtualHost

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).

Hosts file

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.

@vikramvi
Copy link

vikramvi commented Jan 5, 2017

To get rid of above error I have added value of ServerName [projectname].com:80

But still no sucess, when I navigate to that site nothing loads. Please clarify how to debug this issue so that I can find exact root cause in configuration ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment