July 02, 2015

Apache in Yosemite?

Sounds like a cool trip to those ginormous trees dosen't it? And you may even see a native american while you're there. Well it is some kinda field trip, except you dont get to go to Yosemite. But you get to update your Mac to Yosemite.

So, if you haven't already updated your MacBook Pro to the latest OSX, get on with it lad. The following describes how to setup apache in a freshly baked OS-X 10.10.xx setup.

The apache webserver and php is already installed in Yosemite, it's just not setup to support 'users' sites. The webserver root directory of apache is present at:

# ls /Library/WebServer/Documents/

If you want to know your apache version type: 

# apachectl -v

To start, restart or stop, apache you use the apachectl command, i.e. 

# apachectl start
# apachectl restart
# apachectl stop

But prior to starting the web server you should configure the server for user Sites. We'd like to setup the 'old' Sites folder support, which now no longer can be done from the GUI, unless you view the shell as a GUI (I know I do).

You'll need to create a folder called 'Sites' in your home directory. This folder will serve as the web root for your source code, and allow you to access your files in a browser or eclipse, using the http://localhost/~username/ URL.

# mkdir ~/Sites
# echo "<Directory \"/Users/$USER/Sites\">
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted
</Directory>" | tee sudo /etc/apache2/users/`whoami`.conf
# sudo chown root:wheel /etc/apache2/users/`whoami`.conf
# sudo chmod 644 /etc/apache2/users/`whoami`.conf

The above commands gives you a folder named 'Sites' in our user accounts home directory, and adds the 'users' configuration to the apache server. Next, we'll configure the apache to use the 'users' folder when it runs.

Open the apache configuration file in an editor as superuser (root) and uncomment the following configuration lines. I use emacs, but pico, nano or any other editor is just as good.

# sudo emacs /etc/apache/httpd.conf  

LoadModule authz_user_module libexec/apache2/mod_authz_user.so
LoadModule authz_host_module libexec/apache2/mod_authz_host.so
LoadModule userdir_module libexec/apache2/mod_userdir.so
LoadModule php5_module libexec/apache2/libphp5.so
Include /private/etc/apache2/extra/httpd-userdir.conf

Save the file and exit the editor. Then, open the apaches 'users' configuration file and uncomment the line 

Include /private/etc/apache2/users/*.conf

Save and exit, and your apache server will be ready to rock, with user dir and php support. To verify that the changes work you'll need to restart the apache http deamon.

# sudo apachectl restart
# curl http://localhost

the curl command should echo the contents of the apache servers index.html file, so after the command has run you should see:

<html><body><h1>It works!</h1></body></html>

To verify the userdir configuration you'll need the curl redirect option and an index.html file to check against.

# echo "<html><body><h1>$USER</h1></body></html>" > ~/Sites/index.html
# curl -L http://localhost/~`whoami`

The curl command should dump the file you created above, where $USER has been replaced with your user account name. 

To verify that PHP runs, you can create a simple php file in your ~/Sites folder:

# echo "<?php echo phpversion(); ?>" > ~/Sites/index.php
curl -L http://localhost/~`whoami`/index.php

The output should be the version number, if you prefer you can use the phpinfo() function instead but it will flood your terminal with info.

Finally, you'll need to add appache to the launch deamon to get it started every time you reboot.

# sudo launchctl load -w /System/Library/LaunchDeamons/org.apache.httpd.plist

No comments: