Tutorial details | |
---|---|
Difficulty | Advanced (rss) |
Root privileges | Yes |
Requirements | OS X 10.8.x+/Terminal |
Time | 15m |
Configuration files
- Apache configuration file – /etc/apache2/httpd.conf
- PHP 5 configuration file – /etc/php.ini
Finding default Apache DocumentRoot
Open the Terminal and type the following command:
grep --color DocumentRoot /etc/apache2/httpd.conf
OR
grep --color '^DocumentRoot' /etc/apache2/httpd.conf
Sample outputs:
DocumentRoot "/Library/WebServer/Documents"
You need to store or create php/html/images/js/css app files at DocumentRoot i.e. /Library/WebServer/Documents directory.
How do I start / stop / restart Apache server?
Type the following command to start / stop / restart apache web-server in OS X:
sudo apachectl start sudo apachectl stop sudo apachectl restart sudo apachectl graceful
Test your setup
Fire a browser and type url:
http://127.0.0.1/
OR
http://localhost/
OR
http://ip-address-of-your-mac-os-x/
Sample outputs:
How do I change the default index.html?
Type the following command to create or edit the index.html file:
sudo vi /Library/WebServer/Documents/index.html
Sample code:
<html> <head> <title>My first html page</title> </head> <body> <h1>Hi, User</h2> <p>This is a test web-page. It was created on OS X 10.8 Mountain Lion using the following:</p> <ol> <li>Vim text editor</li> <li>Apache 2.x</li> <li>PHP 5.x</li> <li>Gimp</li> </ol> <hr> <center> <small>© 2013 nixCraft.</small> </center> </body> </html>
Save and close the file. Fire a browser and type url:
http://127.0.0.1/
Sample outputs:
Configure and Turn on PHP 5 support for Apache
Edit the file /etc/apache2/httpd.conf
sudo vi /etc/apache2/httpd.conf
OR better try vi/vim syntax to open file and go to line called php5_module:
sudo vi +/php5_module /etc/apache2/httpd.conf
Find and make sure the following line is uncommented:
LoadModule php5_module libexec/apache2/libphp5.so
Sample outputs:
sudo apachectl -k restart
OR
sudo apachectl restart
Create a test php script as follows:
sudo vi /Library/WebServer/Documents/phpinfo.php
Add the following code:
<?php //Display the configuration info, line, php.ini location, build date, Web Server, System, Version and more. phpinfo(INFO_GENERAL); ?>
Type the following url:
http://localhost/phpinfo.php
Sample outputs:
How do I start/reload Apache on boot?
Make sure Apache+PHP5 get reloaded when you reboot the system:
sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
And, there you have it, the Apache and PHP 5 installed, configured and working correctly on the latest version of Mac OS X 10.8.
🐧 7 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
The better option is to install a virtualization product and run your development code there. I would check out virtualbox and vagrant.
Thank You very much for sharing this Info …. Works perfectly ….
Hi, I have been struggling for a while trying to configure my MAMP, I’ve work with Linux before and I manage to configure my LAMP pretty well. But MAC is ginving me a hard time. What I’ve could do so far is starting the apache serve although i dont know if the apache server I started is the one that i am seeing on my browser, that because when i go to localhost i see the It Works! message, but if i change the index.html in /Library/WebSever/Documents/ and reload the browser or open another browser and search the address localhost, I am still getting the same It Works message and not the one i’d change. I have try different browsers, clearing cache, incognito windows. I getting crazy here. I would appreciate some help
Followed your tutorial exactly as written.
However, when I run the url http://localhost/phpinfo.php all that displays is:
Seems for some reason either Apache and/or my localhost are not executing the php code but just displaying the contents????
Is a chmod required??
Have thrashed a bit on this with no luck.
Any thoughts???
I am running Mac OS X 10.8.5 on a MacBook Pro.
I am using Google Chrome, Safari, and Firefox as browsers for testing.
Thanks
Michael
Ok got it to work. So no reply is necessary.
Hey there,
This worked well. There are a bunch of instructions out there for using PHP + Apache on Mac OS X with their default install libraries (or XCode or whatever installs it), and they all seem dated.
This one worked perfectly. Very easy.
Thanks!
Perfect tutorial!