Step 1: Create the Sites folder in your home directory
Lion and later versions no longer create personal web sites by default. If you already had a Sites folder in Snow Leopard, it should still be there. To create one manually, enter the following command after launching the Terminal app:
$ mkdir ~/Sites
Step 2: Create a basic html page for testing your local web server
$ echo "<html><body><h1>My site works</h1></body></html>" > ~/Sites/index.html.en
Step 3: Enable PHP
PHP is not enabled by default in recent versions of OS X. To enable it, type the following at the terminal:
$ sudo vi /etc/apache2/httpd.conf
Uncomment the following line:
#LoadModule php5_module libexec/apache2/libphp5.so
i.e. it should read as
LoadModule php5_module libexec/apache2/libphp5.so
(if you aren’t familiar with vi, just press ‘x’ over the ‘#’ character to delete it. Then type ‘:wq’ to save and quit.)
You can find this line on the following line numbers depending on your OS.
- 10.7 Lion – line 111
- 10.8 Mountain Lion – line 117
- 10.9 Mavericks – line 118
- 10.10 Yosemite – line 169
- 10.11 El Capitan – line 169
For Yosemite and beyond only, uncomment the following line at line 166:
#LoadModule userdir_module libexec/apache2/mod_userdir.so
to
LoadModule userdir_module libexec/apache2/mod_userdir.so
and do the same at line 493:
#Include /private/etc/apache2/extra/httpd-userdir.conf
to
Include /private/etc/apache2/extra/httpd-userdir.conf
Save and exit (type ESC and :wq and return)
And again, for Yosemite and beyond only, open the file above with:
$ sudo vi /etc/apache2/extra/httpd-userdir.conf
and uncomment the following line at line 16:
#Include /private/etc/apache2/users/*.conf
to
Include /private/etc/apache2/users/*.conf
Save and exit (type ESC and :wq and return)
Step 4: Ensure your user config file exists
Note: For this step and beyond, replace <your short user name> with your short user name. This can be found by typing whoami at the terminal.
Your user config file should exit at the path /etc/apache2/users/.conf.
If that file doesn’t exist, you will need to create it by typing the following at the terminal:
$ sudo vi /etc/apache2/users/.conf
For all OSX versions prior to Yosemite, use the following as the content:
<Directory "/Users/<your short user name>/Sites/">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from localhost
</Directory>
For Yosemite and beyond, use this content:
<Directory "/Users/<your short user name>/Sites/">
AddLanguage en .en
LanguagePriority en fr de
ForceLanguagePriority Fallback
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from localhost
Require all granted
</Directory>
Save and exit (type ESC and :wq and return)
Step 5: Turn on Apache web server
In Lion, do the following:
To turn on Apache, go to System Preferences > Sharing and enable Web Sharing.
In more recent versions of OS X, the Web Sharing checkbox in System Preferences > Sharing is gone. Instead, type the following in terminal:
$ sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
Step 6: Test your Web Server
In Safari, navigate to your web site with the following address:
It should say:
It works!
Now try your user home directory:
http://127.0.0.1/~<your short user name>
It should show the content of the html file we just created:
My site works
Now test PHP. Create a PHP info file by typing the following in terminal:
$ echo "<?php echo phpinfo(); ?>" > ~/Sites/info.php
$ chmod +x info.php
Now test it by entering the following into Safari’s address bar:
http://127.0.0.1/~<your short user name>/info.php
You should see your PHP configuration information.
If you want to make further changes to your Apache system or user config files, you will need to restart the Apache server with:
$ sudo apachectl graceful
If you think that you have a link that adds value to this article please contact us at techie[at]techzog[dot]com for evaluation of inclusion into the article.
Comments left solely for spamming links will be deleted. Thank you for understanding.