Saturday, June 6, 2015

How to enable Apache & PHP on Mac OS X Yosemite

Apache and PHP are packaged with OS X. So we will just have to enable them in order to create a local web server.

First open terminal and switch to the root user to avoid permission problems

sudo su

Starting Apache server


Run following command from the terminal

apachectl start

This will start the apache server and you can verify it by invoking following url from the browser

http://localhost

You will see a page showing 'It works!'

Enable PHP


Backup the default apache configuration at /etc/apache2 in any case you might need it :)

cd /etc/apache2/
cp httpd.conf httpd.conf.bak

Now we have to edit the configuration file

vi httpd.conf

To enable PHP we have to uncomment the following line in httpd.conf by removing #

LoadModule php5_module libexec/apache2/libphp5.so

Restart Apache server

apachectl restart

Now we can verify if PHP is enabled. In order to do that let's create a page that calls phpinfo() in the DocumentRoot.

The default DocumentRoot for Mac OS X is /Library/WebServer/Documents
You can see that configuration in httpd.conf

grep DocumentRoot httpd.conf

Now create the phpinfo() page

echo '<? php phpinfo();' > /Library/WebServer/Documents/phpinfo.php

Now we can verify PHP by invoking http://localhost/phpinfo.php

No comments:

Post a Comment