Posts Tagged ‘ mamp ’

Tutorial: Use DropBox to Share a Database (or any file)

Friday, January 14th, 2011

Admittedly, this is a niche problem, but it’s one I run into quite frequently. I have several development machines, and it used to be a huge pain in the but to keep my MySQL databases synced between them all so that I’m always working with the same data.

Enter DropBox and symbolic linking on the Mac (sorry Windows users).

In your DropBox folder, create a folder that will hold your shared databases. In recent versions of DropBox, you’ll want to ensure that this folder is not excluded from syncing on your target machines.

Quick steps:

  1. Back up your db files
  2. Move the db folder you want to share from your MySQL db directory to its DropBox counterpart
  3. Create a symbolic link from the DropBox version to your MySQL directory

Detailed steps:

On your primary development machine (or the one with the latest version of the database you wish to share), navigate to the folder containing your MySQL database files (For MAMP installations, this is typically /Applications/MAMP/db/mysql).

WARNING: BACK UP YOUR DATA BEFORE PROCEEDING!

Now move the folder containing the database you wish to share to your newly created counterpart in your DropBox folder. Move it, don’t copy it.

Fire up your terminal window. You’re going replace the folder you just moved with a symbolic link (not an alias!) from your DropBox version. DropBox folders can live anywhere you choose, so replace this example with your own paths:

ln -s /Users/Steve/Dropbox/Databases/example_db /Applications/MAMP/db/mysql/

Now you should see the DropBox version of your db folder there in the finder. Because it’s a symbolic link, MySQL is tricked into seeing the shared directory even though it’s no longer in your database directory.

Repeat this on every machine on which you wish to share this database.

Some caveats: this is a development solution only! Obviously, you will not use this technique on your live database! Also, this technique is susceptible to version control errors. Hence, it’s a good solution when you’re the only developer hitting the database files. My own use-case, for example, is syncing between my primary MacPro and my travel laptop. Your mileage may vary.

But wait! There’s more! This trick works with anything and any app, not just database files and MySQL. Give it a try. It’s yet another reason DropBox is one of the greatest services available on the Net today.

Tutorial: Launching MAMP Silently on Startup

Monday, August 25th, 2008

MAMP Web ServerMAMP is a great tool for running and managing a development server when you require more than the basic PHP configuration included with Leopard. However, getting the web server to run typically involves launching MAMP, entering your root password, and quitting MAMP.

This tutorial walks through the simple steps needed to launch MAMP silently on startup so that you do not have to enter your root password every time.

The issue with MAMP is that launching Apache must be done as root, so there’s no way to simply add MAMP to your startup items via the system’s Accounts Preferences in a way that will launch the app silently. But you don’t need to. Here’s the deal: MAMP’s launcher is just a pretty UI that opens a shell script that, in turn, launches MAMP’s Apache and MySQL servers.

You can set up launch daemons that do the exact same thing without the need to launch MAMP or enter a password.

Step 1: Create the Start-up Items

Open up your favorite text editor and paste the following into an empty document:

Create another for the MySQL start-up item:

Be sure to replace “YOUR_USERNAME” with the username for your account.

Step 2: Save the Files

Save this file as (or move the file to) /Library/LaunchDaemons/info.mamp.start.apache.plist and /Library/LaunchDaemons/info.mamp.start.mysql.plist, respectively.

Step 3: Set Permissions

If you try to launch the daemons at the moment, you’ll get a “dubious permissions” error. To correct this problem, you’ll need to change your permissions.

In your terminal, type:

You’re done! Reboot your computer and test that your development server is running as expected by opening a served page in your browser.

Setting up a Web Server on Leopard (OS X 10.5) and Snow Leopard (10.6) Using MAMP – A Step by Step Guide (Revised)

Wednesday, November 7th, 2007

MAMP Web Server

Recently, I wrote a Step-by-Step guide for installing a basic development server on Mac OS X Leopard (10.5) using the built-in packages. It turns out that the version of PHP that is packaged with Leopard is missing many commonly used components such as the GD Library, MCrypt, and many others. Since recompiling PHP on Leopard is no small feat even for experienced developers, you may be looking for an alternative method to get your web server working.

UPDATE: Based on preliminary testing, these instructions remain up to date for Snow Leopard (10.6).

This is where MAMP comes in handy. This tutorial will walk you through the steps necessary to get MAMP set up on your site, migrate your existing data, and set up multiple virtual hosts.

Revision Notes 02/03/08: Several readers have kindly pointed out some errors in the guide that follows. I’ve updated this article to correct some (all?) of these mistakes/omissions.

MAMP

MAMP (Macintosh, Apache, MySQL, PHP) is a tool released by a German company called living-e. MAMP comes in two flavors: free and Pro. The pro version is not free (appx. $70 at the time of this writing), but offers a bunch of convenient power tools to help you configure virtual hosts, your Apache server, and your startup items.

However, with a few simple tweaks to the httpd.conf, php.ini, and hosts file, the free version of MAMP should be more than adequate for most developers.

Overview

This tutorial can be used whether you are installing a web server for the first time or are migrating from Tiger (10.4), or have set up a default installation on Leopard. Steps aimed at migrating your data and settings are labeled with an "optional" tag.

Step 1: Enable your root password

  1. Open the Directory Utility: In the Finder, navigate to the Utilities folder (tip: click on the desktop, hit Cmd+Shift+U).
  2. Click on the padlock to allow edits.
  3. Go Edit > Enable Root Password
  4. Enter and re-enter your password.

Now, you are set to access protected areas of the system via the terminal.

Step 2: Export your existing data (optional)

If you were running a web server previously, you’ll want to transfer your data and settings. The best way to get your MySQL data is to use the MySQL Administrator tool to backup all of your databases.

  1. Download and install the MySQL GUI Tools. You’ll only need the Administrator tool, but the others are good to have as well.
  2. After installing the package, open MySQL Administrator.
  3. Connect to your existing database(s).
  4. Click the "Backup" tab and run a backup routine to save your database as sql dump file on your local drive.

Alternatively, you can use phpMyAdmin to export your data if your database is under 2mb (typically). I found this method to be unusable for large databases, however.

You can also use the mysql dump command line tool. This effectively creates the same dump file as the Administrator Tool, but is not nearly as easy and foolproof to use.

Regardless of the method you choose, you should have one or more sql files ready to be imported.

Why not just copy the data files? After many, many attempts to do this, I was unable to end up with a running database, even after trying to preserve permissions.

Step 3: Save your existing settings (optional)

If you have previously set up your own server, you are likely to have settings you want to preserve. You’ll need backup copies (or access to) the following files:

  • httpd.conf
  • httpd-vhosts.conf (Apache 2)
  • httpd-userdir.conf (Apache 2)
  • php.ini
  • hosts (Leopard, Windows, or *nix; Tiger & previous OS Xes used Net Info and don’t use a hosts file)

Step 4: Shut down built-in servers

If you have installed and configured Web Sharing and a MySQL database on your machine, we’ll need to ensure these are shut down before proceeding. If you have not, then skip to the next step.

  1. In your System Preferences > Sharing control panel, uncheck Web Sharing
  2. If you have MySQL installed, in a terminal window, type the following to shut down the existing MySQL server:

    sudo /usr/local/mysql/support-files/mysql.server stop

  3. In a terminal window, type the following to remove any previous MySQL start-up items:

    sudo rm -R /Library/StartupItems/MySQLCOM

Step 5: Download and install MAMP

  1. Go to the MAMP web site and download the MAMP package.
  2. Extract and mount the dmg package. Drag MAMP (not MAMP Pro) to your Applications folder.
  3. Go to /Applications/MAMP and launch the MAMP Control Panel.
  4. Start the servers by clicking the "Start Servers" button.
  5. Do not change any of the settings at this time. Just run defaults for the moment.

Step 6: Test your installation

Click on the "Open Start Page" button. This should launch the MAMP start page.

Congratulations. You have a perfectly fine web server running on your machine. This may be fine for some people‚ in which case, you’re done‚ but web developers are going to need to tweak a few settings.

You’ll notice from your URLs that you are running on an unusual port number (8888). This is fine for the time being, but you’ll probably want to switch to the default ports to avoid having to tack on ":8888" to every local domain.

To make changes, let’s shut down the server by clicking the "Stop Servers" button.

Step 7: Switch to HTML & MySQL default ports (optional)

  1. With the MAMP Control Panel still open, click the "Preferences…" button.
  2. Click the "Ports" tab
  3. Click the "Set to default Apache and MySQL ports" button to change the Apache port to "80" and the MySQL port to "3306".
  4. Click Ok.

Step 8: Transfer your existing settings (optional)

MAMP keeps its config files in the Applications/MAMP/conf/ directory.

There are a million ways to get your settings over from your old server, including simply tweaking the settings by hand. I find the safest way to do this is to use a file comparison tool like the FileMerge tool included with XCode (included on your Leopard disc) to transfer settings surgically from your old server to your new one.

Regardless, this is the time to move your settings over if you want to customize your installation. Be careful here. This is the most common way to screw things up.

Step 9: Import your existing data (optional)

If you exported your data in Step 2, you’ll want to import them to your new MySQL server. The best way to get your MySQL data is, again, to use the MySQL Administrator tool to backup all of your databases.

  1. Connect to your existing database(s).
  2. Click the "Restore" tab. Select the file(s) saved in Step 2 to reconstruct your database(s).

Alternatively, you can use phpMyAdmin’s import tool.

Step 10: Install PEAR (optional)

PEAR is a companion to PHP that is typically installed by default along with PHP. PEAR is a set of applications, modules and pre-packaged classes that provide a wealth of functionality to your apps with minimal effort. One example covered on this blog is how to implement an elegant caching mechanism with just a few lines of code. Using PEAR is highly recommended.

If you do not already have PEAR installed, type the following in a terminal window:

curl http://pear.php.net/go-pear > go-pear.php
sudo php -q go-pear.php

This will auto-install and pre-configure PEAR for you. Accepting the defaults during the installation should be fine for most users.

Now, we need to tell MAMP’s version of PHP to look for the PEAR files. To do this, we’ll need to modify the php.ini config file installed by MAMP. First, back up the config file by typing the following in a terminal window:

sudo cd /Applications/MAMP/conf/php5/
sudo cp -p php.ini php.ini.bak

Now, edit the php.ini file, open "/Applications/MAMP/conf/php5/php.ini" in your favorite text editor.

Scroll down to the include_path directive and change the following [↵ shows line wraps -Ed.]:

FROM:

include_path = ".:/Applications/MAMP/bin/php5/lib/php"

TO:

include_path = ↵
".:/Applications/MAMP/bin/php5/lib/php:/usr/share/pear"

Step 11: Set up your first virtual host (optional)

Virtual hosts are Apache’s way of letting you serve up multiple sites on a single server. Name-based virtual hosting is a convenient way to do this. Not everyone needs to set up a virtual host. For instance, if you are just tinkering on a single site, then you can skip this step. However, if you are developing and testing multiple sites locally, you will need to do this step.
For this example, we’re going to set up a test site called "site1". With a name-based virtual host, all we’d type in is "http://site1/".

In order to make this work, we’ll need to edit the hosts file on your machine. (Note: In Tiger and previous versions of OS X, you accomplished this through the NetInfo dialog. Since the NetInfo dialog was killed in Leopard, we do this the same way you do this for *nix and Windows by editing the hosts file directly).

To edit your hosts file, type in the terminal:

sudo nano /etc/hosts

Below the default entries, you’ll add the following:

# My sites
127.0.0.1 site1

The following is a screenshot of my hosts file. Yours will be similar but different:

Screenshot of my hosts file

Save your changes.

Now, we’ll need to add a corresponding virtual host. But first, Apache wants us to add our existing default directory as the very first virtual host. This is critical, so do not skip this step.

To edit your virtual hosts file, open "/Applications/MAMP/conf/apache/httpd.conf in a text editor.

Uncomment the virtual host directive:

NameVirtualHost *

Replace the two example virtual hosts with the following:

<VirtualHost *>
DocumentRoot &quot;/Applications/MAMP/Library&quot;
ServerName localhost
</VirtualHost>

Next, add your first virtual host similar to the following:

<VirtualHost *>
ServerName site1
DocumentRoot /path/to/site1
</VirtualHost>

This is a screenshot of the vhosts section of my http.conf file. Yours will be similar but different:

Screenshot of vhosts file

Who do we have to add our default directory first? According to the Apache Docs on Virtual Hosts, they recommend the following:

If you are adding virtual hosts to an existing web server, you must also create a <VirtualHost> block for the existing host. The ServerName and DocumentRoot included in this virtual host should be the same as the global ServerName and DocumentRoot. List this virtual host first in the configuration file so that it will act as the default host.

Note: Failing to add my default directory as the very first virtual host tripped me up for days. If your virtual hosts are defaulting to an unexpected directory, this is likely to be the culprit.

Step 12: Restart the servers

Back in the MAMP Control Panel, click the &quot;Start Servers&quot; button to restart Apache and MySQL with your new settings. Fingers crossed, everything went well, and you can now access your local test sites.

If something went wrong, retrace your steps. The most likely cause is a mis-configuration in your Apache config file. To check the syntax of your conf file, type the following into a terminal window:

cd /Applications/MAMP/bin/apache2/bin
sudo ./apachectl -t

Repair your config file as necessary.

Step 13: …

There is no step 13. I just didn’t want to end on a bad number.

Step 14: Create a startup item

Now that you have everything running, you’ll probably want to launch the web server on start up. One of the nice things about MAMP Pro is its built in ability to start itself up on login. The free version of MAMP does not have this luxury, but this is easily solved by creating a launch daemon.

Follow this tutorial to get MAMP to launch silently on startup.

Conclusion

By this point, you should have a fully functional development server that you can extend and expand as you take on new projects.

Please feel free to add comments below if you find errors or problems with the guide above.

Good luck.

Leopard Upgrade Hell: Web Developers Hold Off (Updated)

Friday, November 2nd, 2007

Leopard[Update 11-05] I’ve written a step-by-step guide that should help you set up your web server on Leopard. Click here to read it.