Running Apache
In the good old days, Apache could be started and stopped by visiting the Sharing Preferences. Apache is still included in later versions of OSX, but now we need to open the Terminal application, and enter one of the following commands to control it:
sudo apachectl start
sudo apachectl stop
sudo apachectl restart
Enter your admin password when prompted.
After starting Apache, check it's working by browsing to http://localhost
Notes:
- The default "system level" document root is
/Library/WebServer/Documents/
- The Apache configuration file is located at:
/etc/apache2/httpd.conf
- If Apache fails to start, use
apachectl configtest
to find possible reasons why!
To start Apache automatically after a restart, enter the folowing (unload to remove):
sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
Users can not generally write to the "system level" document folder. We can either change permissions, or enable user sites as existed in previous OSX versions.
Restricting access:
We can restrict access in the directory section of httpd.conf
, for example, in the DocumentRoot section:
#Require all granted comment out/change 'all granted' Require local allow localhost access only
There are many other Require
(only from) possibilities:
Require ip 10.1.2.3 IP address Require ip 192.168.1.104 192.168.1.205 IP address's Require ip 10.1 partial IP address Require ip 10.1.0.0/255.255.0.0 network/netmask pair Require ip 10.1.0.0/16 CIDR notation Require ip 2001:db8::a00:20ff:fea7:ccea IPv6 address(s) Require ip 2001:db8:2:1::/64 IPv6, CIDR notation Require host example.org domain-name Require host .net example.edu partial domain-name Require forward-dns bla.example.org forward-dns
If the require rule is not met, visitors are greeted to a 403 Forbidden page.
Enable User Sites:
1 Create a document root folder Sites in your home directory: mkdir ~/Sites
Note the Safari style Icon, which is a throwback to when the folder was standard.
2 Create a user config file: sudo nano /etc/apache2/users/username.conf
Paste in the following text, save and exit (ctrl-o, ctrl-x).
<Directory "/Users/*/Sites/"> Options Indexes MultiViews FollowSymLinks AllowOverride All Require all granted </Directory>
3 Edit the Apache configuration: sudo nano /etc/apache2/httpd.conf
Find, and uncomment the following lines (use ctrl-w to search):
LoadModule userdir_module libexec/apache2/mod_userdir.so LoadModule alias_module libexec/apache2/mod_alias.so LoadModule rewrite_module libexec/apache2/mod_rewrite.so LoadModule php5_module libexec/apache2/libphp5.so : Include /private/etc/apache2/extra/httpd-userdir.conf
Save and exit (ctrl-o, ctrl-x)
4 Edit the user configuration: sudo nano /etc/apache2/extra/httpd-userdir.conf
Uncomment the following line:
Include /private/etc/apache2/users/*.conf
Save and exit (ctrl-o, ctrl-x)
5 Create a PHP test file in the new user level document root (~/Sites) with the following command:
printf "<?php phpinfo(); ?>" > ~/Sites/phpinfo.php
Restart Apache: sudo apachectl restart
6 Open http://localhost/~USERNAME/phpinfo.php
(here, ~mike) in a browser, and behold!