Local Magento

We recently switched our live website at work to the new Django backend I had created. With that our old Magento instance can go away! Well, sort of. I still may need to reference it in the future so I needed to replicate the Magento install locally. Our Magento version was old (1.3.2.4) and I work on a Mac so I needed a way to duplicate the server environment. Enter Vagrant. I already had a working VirtualBox installation so install was pretty simple. Then I found this awesome recipe for Magento that happened to be just what I needed.

I simply cloned it to my Vagrant directory and then used rsync to copy my entire Magento directory from the server to the www directory. Once everything was synced I deleted everything in the www/var/ folder because I know how Magento loves to screw things up with all it’s cache stuff. Make sure and add the entry to your /etc/hosts file on your map as explained on the Vagrant recipe page linked to earlier.

Next I needed a database. So, after running vagrant up I logged into the shell using vagrant ssh

Once logged in you need to connect to mysql by running

mysql -u root -p

Again use the docs to see the usernames and passwords that Vagrant is setup with. Once you’re in mysql you can create your database.

create database yourdbname;

Once that is created we need to move the database from the live server down to our local environment. I tried a few things and they are were a pita, but then I found Navicat which has an awesome data transfer tool. So go download that and after installing add a connection for your remote server (set ssh and then your db info is connecting to localhost on the general tab). But now we need to connect to our local Vagrant database as well. To do that you simply set the ssh settings to a hostname of 127.0.0.1 and a port of 2222 with the user vagrant and password vagrant. Then in the general tab it’s just hostname of localhost and port of 3306.

Great! Now we have a DB. Simply launch the tools / data transfer utility in navicat and sync the tables. Sweeeet!!! That was easy!

Then we need to edit our Magento config to connect to our new database. Do this in www/app/etc/local.xml

Once that’s done we need to restart apache.

sudo service apache2 restart

And then we should be good to go! Auuhh no. When I tried to load the site I got a 500 error and looking in the logs it said:

[alert] [client 10.0.2.2] /vagrant/www/.htaccess: Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration

Turns out this is because a module for Apache is not installed. To install it we:

sudo a2enmod headers

And then restart again. And…. the Magento white screen of death! #GoshDarnItAll

First off I need to see the stupid error! That would be nice. Following lots of directions for uncommenting lines in index.php to display errors did nothing. Finally, by adding the following to index.php I could actually see the error.

ini_set('error_reporting', E_ERROR);
    register_shutdown_function("fatal_handler");
    function fatal_handler() {
    $error = error_get_last();
    echo("<pre>");
    print_r($error);
}

I then was able to fix my error by following the directions here.

And now I have a full working backup locally of the old site that I can reference whenever I need to! You gotta love Vagrant!