Getting Started
First, open the Terminal app and switch to the root user to avoid permission issues while running these commands.
sudo su
Enable Apache on Mac OS X
apachectl start
Verify It works! by accessing http://localhost
Enable PHP for Apache
First, make a backup of the default Apache configuration. This is good practice and serves as a comparison against future versions of Mac OS X.
cd /etc/apache2/
cp httpd.conf httpd.conf.bak
Now edit the Apache configuration.
sudo nano httpd.conf
Uncomment the following line (remove #):
LoadModule php5_module libexec/apache2/libphp5.so
Restart Apache:
apachectl restart
You can verify PHP is enabled by creating a phpinfo() page in your DocumentRoot.
The default DocumentRoot for Mac OS X Yosemite is /Library/WebServer/Documents. You can verify this from your Apache configuration.
grep DocumentRoot httpd.conf
Now create the phpinfo.php page in your DocumentRoot in /Library/WebServer/Documents/phpinfo.php
<?php echo 'phpinfo();’ ?>
Verify PHP by accessing http://localhost/phpinfo.php
Install MySQL on Mac OS X
- Download the MySQL DMG for Mac OS X
- Install MySQL
Once installed, to reduce the bulk of work to create aliases for each mysql command, the environment path needs to be added in .bash_profile file. This file normally resides in
/Users/yourusername/.bash_profile
If you can’t see this file just create one.
nano /Users/yourusername/.bash_profile/
export PATH=/usr/local/mysql/bin:$PATH
Note: After adding environment path you need to logout and login back into your system to take effect.
Additionally, it is a good practice to run mysql_secure_installation.
Connect PHP and MySQL
Now time to ensure PHP and MySQL can communicate with one another. So, do the following:
cd /var
mkdir mysql
cd mysql
ln -s /tmp/mysql.sock mysql.sock
Additional Configuration (optional)
The default configuration for Apache 2.4 on OS X Yosemite seemed pretty lean. For example, common modules like mod_rewrite were disabled. You may consider enabling this now to avoid forgetting they are disabled in the future.
Open Apache Configration:
sudo nano /etc/apache2/httpd.conf
And uncomment the following lines (remove #):
LoadModule deflate_module libexec/apache2/mod_deflate.so
LoadModule expires_module libexec/apache2/mod_expires.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Note: Previous version of Mac OS X ran Apache 2.2. If you upgraded OS X and previously configured Apache, you may want to read more about upgrading to to Apache 2.4 from Apache 2.2.
If you develop multiple projects and would like each to have a unique url, you can configure Apache VirtualHosts for Mac OS X.