A few months ago, I wrote about how to install MySQL on Windows with IIS. Now I want to show you how to create an AMP web programming stack (Apache, MySQL & PHP) on Mac OS X. As with Windows, you could just go with a ready-made development environment like MAMP but, if you’re going to be doing real database and web design work with MySQL, it’s best to know how everything works.
For this demonstration, I’m going to be using Mac OS X El Capitan (v.10.11), the latest version as of this writing. My installation environment is a hosted Mac server with 1 GB of RAM and 40 GB of disk space. I will be demonstrating the process using the Terminal commands.
Unlike Windows, OS X already includes the Apache web server and PHP language pre-installed so it’s a much simpler process to ensure that everything is working together. You will need root access in order to install and configure the components.
Establishing Root Access
Open the Terminal environment from the Utilities (Go >> Utilities from top menu and then select Terminal).
In OS X, it is possible to get root access by activating the root user account with dsenableroot. Improper use of this command can open up your system to security issues, however. It’s safer to use sudo (superuser do) on the individual commands or temporarily switch users with sudo su –.
For more information on working with the root user, see https://support.apple.com/en-us/HT204012.
You can activate Apache simply by issuing either of the the following commands in the Terminal.
sudo apachectl start
sudo apachectl restart
The restart command will start Apache even if it’s stopped. There won’t be any notification on the command line as to the change in status so you will need to verify that Apache is working by opening a web browser window and navigating to the following address:
If everything is working, you should get a simple white screen with the heading “It works!” indicating that the web server is up and running.
Configuring PHP and Apache
The Apache configuration file (httpd.conf) must be changed to load the necessary PHP module so that PHP pages can be served up. This is easily done from the Terminal. First, you will need to change to the Apache directory and you should make a backup of the file.
cd /etc/apache2/ cp httpd.conf httpd.conf.bak
To edit the file, you can use the command line editor vi or the Mac TextEdit program, either of which can be opened from the Terminal.
open -a TextEdit httpd.conf
If you’re not familiar with vi (pronounced “vee-eye”), it’s a powerful command-line editor common to UNIX-based systems and you can read more about using it on the University of Washington’s site at https://www.washington.edu/computing/unix/vi.html. If you choose the TextEdit command from above, the TextEdit program will open outside the Terminal and will load the configuration file for you to work with.
In either editor, you’ll need to find the following line:
#LoadModule php5_module libexec/apache2/libphp5.so
Remove the hashtag (#) from the beginning of the line to un-comment it and then save the file. You will then need to restart Apache with the following command.
sudo apachectl restart
To verify that PHP is working with Apache, you can create a phpinfo.php file in the web document root folder where the localhost files reside. On OS X, this folder is generally \Library\WebServer\Documents. Create a simple text file in that folder with the following text on the first line:
<?php phpinfo(); ?>
Save the file with the name phpinfo.php and then load the file in your web browser.
If everything is working, you should then get the PHP settings screen.
(Article continues below)
Learn more about designing databases with MySQL!
MySQL Explained offers a complete guide to the installation and use of MySQL and is written for people without a background in I.T. or database design. MySQL Explained takes the reader from the very basics of database theory through the installation options for MySQL and the design of custom databases based on everyday real-world examples. Find out more and get your copy on LeanPub.com and Amazon.com in paperback and a variety of electronic formats!
The latest versions of MySQL Community Server are available in TAR and DMG format from http://dev.mysql.com/downloads/mysql. I recommend the DMG format which is a disk image that can be mounted as a drive accessible from your Desktop.
The image file contains a single PKG file which you can double-click to run the installation wizard. As of this writing, the latest version, 5.7.11 is labeled for OS X 10.10 but I was able to install it with no problems on El Capitan (10.11). During the installation, you will be asked to create a password for the root MySQL user – make sure to write this password down somewhere safe!
After the installation is finished, it’s a good idea to add the MySQL program directory to your PATH statement so you can run it from any directory. Use the following line in the Terminal to do this.
Then, try logging in to your new MySQL server as the root user.
mysql -u root -p
It’s also a good idea to run
mysql_secure_installation which does the following to secure your database server:
- Enables you to change the root password that was chosen during installation.
- Provides the option to remove the anonymous MySQL user.
- Provides the option to disable remote logins.
- Allows you to remove the test database and access to it.
- Reload the privilege tables
Now that MySQL is working, it needs to be able to work with PHP. This is done by creating a link to the MySQL socket file which will allow PHP to communicate with the server. Entering the following commands in the Terminal will create a new MySQL directory in the system Var directory and a symbolic link in that directory to the socket file.
cd /var mkdir mysql cd mysql ln -s /tmp/mysql.sock mysql.sock
When the MySQL installation is finished, you should be able to Access the MySQL preference pane shown in Figure 2.11 from the System Preferences panel. This pane enables you to start and stop your MySQL server and set it to start when the OS starts up.
Installing the Design Tools
You probably won’t want to manage all of your databases from the Terminal and OS X supports some of the same graphical tools as Linux and Windows.
MySQL Workbench is available from http://dev.mysql.com/downloads/workbench as a DMG image file. Installing Workbench is as simple as opening the mounted DMG image and dragging the MySQL Workbench icon to the Applications folder when prompted. You will then be able to run the program from your Applications folder.
As a PHP application, PHPMyAdmin can also be installed within your new AMP environment, regardless of the operating system.
- Download the latest version from PHPMyAdmin.net. The package is available in ZIP format.
- UnZIP the files to a PHPMyAdmin subdirectory under your Localhost folder, usually \Library\WebServer\Documents.
- Copy the config.sample.inc.php file to config.inc.php and open the new file for editing.
- Look for the line that starts with $cfg[‘blowfish_secret’] = and add an encryption value of your choice. This value can be a series of random letters and numbers if you like.
If you would like to have a default user and password for phpMyAdmin, you can add the following lines to the
$cfg['Servers'] section of the file, substituting your own values for the user name and password.
$cfg['Servers'][$i]['user'] = '<username>'; $cfg['Servers'][$i]['password'] = '<password>';
Once this is done, you should be able to run PHPMyAdmin simply by navigating to http://localhost/PHPMyAdmin.
For more information on installing PHPMyAdmin, see the documentation at https://phpmyadmin.readthedocs.org/en/latest/setup.html.