Installing Apache, PHP, And MySQL on Mac OS X

Embed Size (px)

DESCRIPTION

Step by step Installing Apache, PHP, And MySQL on Mac OS X

Citation preview

Installing Apache, PHP, and MySQL on Mac OS XOctober 9, 2012Update:I added steps for existing installs whichupgradedto Mac OS X Mavericks. For new installs of Apache, PHP, and MySQL on Mac OS X Mavericks, continue reading.I have installed Apache, PHP, and MySQL on Mac OS X since Leopard. Each time doing so by hand. Each version of Mac OS X having some minor difference. This post serves as much for my own record as to outline how to install Apache, MySQL, and PHP for a local development environment on Mac OS XMavericks.I am aware of the several packages available, notablyMAMP. These packages help get you started quickly. But they forego the learning experience and, as most developers report, eventually break. Personally, the choice to do it myself has proven invaluable.It is important to remember Mac OS X runs atop UNIX. So all of these technologies install easily on Mac OS X. Furthermore, Apache and PHP are included by default. In the end, you onlyinstallMySQL then simply turn everything on.First, openTerminaland switch torootto avoid permission issues while running these commands.sudo su - Enable Apache on Mac OS Xapachectl start Note: Prior to Mountain Lion this was an option forWeb SharinginSystem Prefrences Sharing.VerifyIt works!by accessinghttp://localhostEnable PHP for ApacheMac OS X Mavericks Update: You will need to rerun the steps in this section after upgrading an existing install to Mac OS X Mavericks.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. Feel free to useTextEditif you are not familiar withvi.vi httpd.conf Uncomment the following line (remove#):LoadModule php5_module libexec/apache2/libphp5.so Restart Apache:apachectl restart Install MySQL1. Downloadthe MySQL DMG for Mac OS X2. InstallMySQL3. InstallPreference Pane4. OpenSystem Preferences MySQL5. Ensure the MySQL Server is running6. Optionally, you can enable MySQL to start automatically. I do.TheREADMEalso suggests creating aliases formysqlandmysqladmin. However there are other commands that are helpful such asmysqldump. Instead, Iupdated my pathto include/usr/local/mysql/bin.export PATH=/usr/local/mysql/bin:$PATH Note: You will need to open a newTerminalwindow or run the command above for your path to update.I also runmysql_secure_installation. While this isnt necessary, its good practice.Connect PHP and MySQLYou need to ensure PHP and MySQL can communicate with one another. There areseveral optionsto do so. I do the following:cd /var mkdir mysql cd mysql ln -s /tmp/mysql.sock mysql.sock CreatingVirtualHostsYou could stop here. PHP, MySQL, and Apache are all running. However, all of your sites would have URLs likehttp://localhost/somesite/pointing to/Library/WebServer/Documents/somesite. Not ideal for a local development environment.Mac OS X Mavericks Update: You will need to rerun the steps below to uncomment thevhostIncludeafter upgrading an existing install to Mac OS X Mavericks.To run sites individually you need to enableVirtualHosts. To do so, well edit the Apache Configuration again.vi /etc/apache2/httpd.conf Uncomment the following line:Include /private/etc/apache2/extra/httpd-vhosts.conf Now Apache will loadhttpd-vhosts.conf. Lets edit this file.vi /etc/apache2/extra/httpd-vhosts.conf Here is an example ofVirtualHostsIve created. DocumentRoot "/Library/WebServer/Documents" DocumentRoot "/Users/Jason/Documents/workspace/dev" ServerName jason.local ErrorLog "/private/var/log/apache2/jason.local-error_log" CustomLog "/private/var/log/apache2/jason.local-access_log" common AllowOverride All Order allow,deny Allow from all The firstVirtualHostpoints to/Library/WebServer/Documents. The firstVirtualHostis important as it behaves like the default Apache configuration and used when no others match.The secondVirtualHostpoints to mydevworkspace and I can access it directly fromhttp://jason.local. For ease of development, I also configured some custom logs.Note: I use the extensionlocal. This avoids conflicts with anyrealextensions and serves as a reminder Im in mylocalenvironment.Restart Apache:apachectl restart In order to accesshttp://jason.local, you need to edit yourhostsfile.vi /etc/hosts Add the following line to the bottom:127.0.0.1 jason.local I run the following to clear the local DNS cache:dscacheutil -flushcache Now you can accesshttp://jason.local.Note:You will need to create a newVirtualHostand edit yourhostsfile each time you make a new local site.A note about permissionsYou may receive403 Forbiddenwhen you visit your local site. This is likely a permissions issue. Simply put, the Apache user (_www) needs to have access to read, and sometimes write, your web directory.If you are not familiar with permissions,read more. For now though, the easiest thing to do is ensure your web directory has permissions of755. You can change permissions with the command:chmod 755 some_directory/ In my case, all my files were under my local~/Documentsdirectory. Which by default is only readable by me. So I had to change permissions for my web directory all the way up to~/Documentsto resolve the403 Forbiddenissue.Note: There are many ways to solve permission issues. I have provided this as theeasiestsolution, not thebest.Install PHPMyAdminUnless you want to administer MySQL from the command line, I recommend installingPHPMyAdmin. I wont go into the details. Read the installation guide for more information. I install utility applications in the default directory. That way I can access them under, in this case,http://localhost/phpmyadmin.cd /Library/WebServer/Documents/ tar -xvf ~/Downloads/phpMyAdmin-3.5.2.2-english.tar.gz mv phpMyAdmin-3.5.2.2-english/ phpmyadmin cd phpmyadmin mv config.sample.inc.php config.inc.php ClosingA local development environment is a mandatory part of theSoftware Development Process. Given the ease at which you can install Apache, PHP, and MySQL on Mac OS X there really no excuse.- See more at: http://jason.pureconcepts.net/2012/10/install-apache-php-mysql-mac-os-x/#sthash.VmgMKie1.dpuf

Reference: http://jason.pureconcepts.net/2012/10/install-apache-php-mysql-mac-os-x/