Install Apache MySQL, & PHP on Mac OS X Yosemite
LAMP is an acronym for Linux, Apache HTTP Server, MySQL relational database management system, and PHP server-side programming language. This web service solution stack is a model for building dynamic web applications and web services. Web developers can now implement this model on their Mac OS X Yosemite computers to support the development of web-based applications and web services.
Please use caution when installing new software on computer systems. A Best Practice includes creating system backups before changes.
How to Enable Apache Web Server on Mac OS X
The Apache HTTP Server Project is an effort to develop and maintain an open-source HTTP server for modern operating systems.
Apache Web Server is already included with Mac OS X Yosemmite. This server can be started with the following steps.
Open a terminal window and log in as the root user:
$ sudo su –
(enter root password at the prompt)
Start Apache with the following command:
$ apachectl start
Verify the Apache Web Server is running on your Mac OS X by opening a web browser and navigating to http://localhost
How to Enable PHP for Apache
PHP is a popular server-side scripting language designed for web development.
Enabling PHP for the web server on Mac OS X requires an edit of the Apache configuration file. A copy of the current version of this configuration file can be saved before the edit by running the following command in the terminal.
$ cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.bak
The Apache configuration file can be edited by running the following command in the terminal.
$ vi /etc/apache2/httpd.conf
This will open the configuration file in the terminal window. Edit this file by pressing “i” key. Enable PHP for Apache by uncommenting (remove #) the following line.
LoadModule php5_module libexec/apache2/libphp5.so
Enable Virtual Hosting by uncommenting the following line.
Enable Mod Rewrite by uncommenting the following line.
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Save and close this configuration file by pressing the “escape” key, then entering the following command.
How to Create Virtual Hosts in Apache
The term Virtual Host refers to the practice of running more than one web site on a single machine. We enabled virtual hosting by allowing Apache to load the httpd-vhosts.conf file in the previous step of editing the httpd.conf file by uncommenting the following line. Include /private/etc/apache2/extra/httpd-vhosts.conf
How to Define a Virtual Host in Apache
The site we are creating in the example is “mobile” with “dev” as the top-level domain (TLD). This will give us http://mobile.dev as the URL. I use “dev” as the TLD to help me remember the site is running on my local development environment, but you can use any value for the TLD. You can also use any value for the site. The next few steps will also include defining a local project directory for your new web project. You can change these values to match your needs, just make sure your values match the values you define in the Virtual Host entry in Apache.
Create a folder on your local file system for the DocumentRoot of your new site.
Create a backup of the httpd-vhosts.conf file by running the following command.
$ cp /etc/apache2/extra/httpd-vhosts.conf /etc/apache2/extra/httpd-vhosts.conf.bak
Edit the httpd-vhosts.conf file with the following command.
$ vi /etc/apache2/extra/httpd-vhosts.conf
Add the following markup to the httpd-vhosts.conf file.
CustomLog "/private/var/log/apache2/mobile.dev-access_log" common
Options Indexes MultiViews FollowSymLinks
Require all granted
Note how the <Directory> definition is slightly different for Apache 2.4. This Apache 2.4 configuration will prevent the Forbidden error that is accompanied by the Apache error message, “You don’t have permission to access / on this server.”
This next step assumes the owner defined in Apache config is _www. Replace “_www” with the name of Apache Server owner. Open a Terminal Window and navigate to the parent folder of the new site directory, then enter the following commands:
# Move into the new site directory
$ cd /1/projects/mobile/www/
# Change ownership of all files to the Apache Server process owner
$ sudo chown -R :_www DocumentRoot
# Change permissions of all site files
$ sudo chmod -R g+w DocumentRoot
How to Define Virtual DNS
In order to access your new site by name, you will need to edit your system’s hosts file.
# Open /etc/hosts file
$ vi /etc/hosts
# Add the following line towards the bottom of the /etc/hosts file.
# Save and close file, then flush the DNS with the following command:
$ discoveryutil udnsflushcaches
# Restart Apache
$ apachectl restart
How to Create a PHP Test Page
The PHP test page for your new site can be created with the following commands.
# Create file
$ vi /1/projects/mobile/www/DocumentRoot/php.php
# Press the “i” key to edit this new file and enter
<?php phpinfo(); ?>
# Press the escape key to exit editing mode, then save file
# Open a web browser and go to the following URL
How to Install MySQL on Mac
MySQL is the world’s most popular open source database.
- Download the MySQL DMG for Mac OS X here.
- Install MySQL
- Install Preference Pane
- Start MySQL from Mac System Preferences Panel
Add MySQL to System PATH with the following commands.
# Modify/Create the .bash_profile file
$ touch ~/.bash_profile
$ vi ~/.bash_profile
# Add the following line to the bottom of ~/.bash_profile
# Activate ~/.bash_profile update
$ source ~/.bash_profile
$ echo “/usr/local/mysql/bin” >> /etc/paths.d/mysql.path
Connect PHP and MySQL
$ cd /var
$ mkdir mysql
$ cd mysql
$ ln -s /tmp/mysql.sock mysql.sock
How to Install phpMyAdmin
phpMyAdmin is a free software tool written in PHP, intended to handle the administration of MySQL over the Web. Download phpMyAdmin here.
Download phpMyAdmin to the system’s default web directory, “/Library/WebServer/Documents/”; then run the following commands from the Terminal Window.
# Move into default web directory
$ cd /Library/WebServer/Documents/
# Unpack phpMyAdmin download
# NOTE: Use actual phpMyAdmin download file name
$ tar -xvf ~/Downloads/phpMyAdmin-188.8.131.52-english.tar.gz
# Rename unpacked phpMyAdmin folder
# NOTE: Use actual phpMyAdmin download folder name
$ mv phpMyAdmin-184.108.40.206-english/ phpmyadmin
# Move into the new phpmyadmin directory
$ cd phpmyadmin
# Create the phpmyadmin config file
$ mv config.sample.inc.php config.inc.php
Launch the phpmyadmin dashboard by opening a web browser and navigating to the following URL: http://localhost/phpmyadmin
How to Start the Virtual Environment
This Mac OS X Apache MySQL PHP development environment can be started with the following steps.
# Make sure the MySQL database is running
Open MySQL Panel from Mac OS X System Preferences
# Open Terminal and switch to root user
$ sudo su –
# Use Terminal Command to start Apache
$ apachectl restart
# Use a Web Browser and Navigate to Website
How to Create Additional Virtual Environments
Additional virtual environments can be easily be created by just repeating the steps outlined in “How to Define a Virtual Host in Apache” section of this tutorial.
Following these steps provides the web developer with a development environment for creating web applications and web services using open-source technology.