Ramblings on technology with a dash of social commentary
RSS icon Email icon Home icon
  • How to install PHP 5.4 or PHP 5.3 on Mac OS X Yosemite

    Posted on December 14th, 2014 PHP Guru No comments

    Yosemite comes with Apache 2.4 & PHP 5.5 preinstalled. You don’t need MAMP, only MySQL or MariaDB.

    Regardless, you can use PHP 5.5, PHP 5.4 or PHP 5.3 as needed. Try the following…

    sudo mv /usr/local/bin/php /usr/local/bin/php55
     sudo mv /usr/bin/php /usr/bin/php55

    Install PHP 5.4

    brew install php54 php54-mcrypt php54-mysql

    Install PHP 5.3

    brew unlink php54
     brew install php53 php53-mcrypt php53-mysql

    Switch to PHP 5.4

    brew unlink php53
     brew link php54

    Switch to PHP 5.5

    brew unlink php54
     brew unlink php53
     sudo ln -s /usr/local/bin/php55 /usr/local/bin/php
     sudo ln -s /usr/bin/php55 /usr/bin/php

    https://gist.github.com/irazasyed/5987693

  • How to start MySQL on Mac OS X

    Posted on December 14th, 2014 PHP Guru No comments

    Observe:

    macpro:~ me$ mysql.server start
    Starting MySQL
    . ERROR!

    macpro:~ me$ sudo mysql.server start
    Password:
    Starting MySQL
    . SUCCESS!

  • Extending Apple Apache & PHP with Homebrew

    Posted on November 18th, 2014 PHP Guru No comments

    Quick start

    I recently got a new Macbook Pro (Mavericks) and decided to use the bundled Apache 2.2.6 and PHP 5.4.30.

    All you have to do is uncomment the PHP extension:

    LoadModule php5_module libexec/apache2/libphp5.so

    in /private/etc/apache2/httpd.conf and start Apache:

    sudo apachectl start

    This had me up and running in no time, but after a few days I realized I needed some additional extensions, including MySQL, Mcrypt, Mongo and Redis.

    Brew to the rescue

    There’s no better package manager for Mac OS X than Homebrew (except possibly Macports). So after installing Homebrew:

    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    I set off to install some additional software and PHP-native extensions:

    brew install redis
    ...
    redis-server start
    brew install php54-redis
    ==> Installing php54-redis from homebrew/homebrew-php
    ==> Downloading https://github.com/nicolasff/phpredis/archive/2.2.5.tar.gz
    ######################################################################## 100.0%
    ==> PHP_AUTOCONF="/usr/local/opt/autoconf/bin/autoconf" PHP_AUTOHEADER="/usr/local/opt/autoconf/bin/autoheader" /usr/local/Cellar/php54
    ==> ./configure --prefix=/usr/local/Cellar/php54-redis/2.2.5 --with-php-config=/usr/local/Cellar/php54/5.4.33/bin/php-config
    ==> make
    ==> Caveats
    To finish installing redis for PHP 5.4:
    * /usr/local/etc/php/5.4/conf.d/ext-redis.ini was created,
    do not forget to remove it upon extension removal.
    * Validate installation via one of the following methods:
    *
    * Using PHP from a webserver:
    * - Restart your webserver.
    * - Write a PHP page that calls "phpinfo();"
    * - Load it in a browser and look for the info on the redis module.
    * - If you see it, you have been successful!
    *
    * Using PHP from the command line:
    * - Run "php -i" (command-line "phpinfo()")
    * - Look for the info on the redis module.
    * - If you see it, you have been successful!
    ==> Summary
    (beer) /usr/local/Cellar/php54-redis/2.2.5: 3 files, 216K, built in 9 seconds

    Okay, now I should be able to use Redis commands (PHPRedis) in PHP, right?

    Wrong! Whoops:

    sudo apachectl restart

    How about now?

    Nope!

    php -i | grep redis

    Nope, phpinfo() doesn’t show it.

    php -m | grep redis

    Nada; no Redis extension. Rat’s why didn’t that work?

    Solution

    Let’s look back at the output from the brew php54-redis installer. This part in particular:

    * /usr/local/etc/php/5.4/conf.d/ext-redis.ini was created,
    

    So, what gives? That seems pretty legit. At least for an Apache & PHP config installed by Homebrew.

    Oh, right, we didn’t do that.

    Here’s the problem: That’s not where Apple’s default PHP build is configured to load extra parsed ini files from.

    That path, by default, is:

    /Library/Server/Web/Config/php/

    To make the situation even more confusing, that path doesn’t even exist! And, it doesn’t even exist in any config file! It’s actually supplied as an argument (by someone at Apple who worked on the Mac OS X Mavericks Rom supplied to the Macbook Pro factory) when building the PHP binary:

    Configure Command => ... '--with-config-file-scan-dir=/Library/Server/Web/Config/php' ...

    which means we can’t even alter it without re-compiling PHP. Not that we couldn’t, but we don’t need to. Here’s why…

    Symlinks for fun and profit

    All we need to do is get the ini files created by homebrew installers to be loaded by the built-in Apache/PHP that ships with Apple and we are good to go.

    Create this lame, un-changeable non-existent directory tree (-p is recursive; go figure):

    sudo mkdir -p /Library/Server/Web/Config/php

    Next, symlink in there the redis.ini

    sudo ln -s /usr/local/etc/php/5.4/conf.d/ext-redis.ini /Library/Server/Web/Config/php/redis.ini

    Finally, restart Apache

    sudo apachectl restart

    Did that work?

    php -m | grep redis
    redis

    Yep, I take that as a good sign.

    All future installs of PHP extensions can be sudo symlinked into this same config-file-scan-dir and we’re good to go.

  • How to fix: The connection has timed out in Firefox 29

    Posted on June 6th, 2014 phpguru No comments

    If your web page dies in 5 minutes, it may not be a server-side issue

    I recently ran into a situation where a long-running webpage would timeout after 5 minutes in Firefox. Every time, with no server logs generated.

    I tried increasing the max_execution_time PHP configuration value with ini_set. That didn’t work. I tried set_time_limit() too, but to no avail.

    After a whole lot of debugging PHP on the server side, I discovered the problem was not related to any timeout on the server configuration. The timeout problem results from a new invisible configuration value that was implemented in Firefox itself at version 29. If you’re running Firefox 28 or before, then there was no such default.

    I tried it in Chrome and things worked fine.

    If you use Firefox, please check your version. If you’re on version 29 then Firefox itself can be the culprit generating a timeout error. You might see, “Sorry, the page could not be displayed.” Try again in a few minutes, check your proxy settings, and so on.

    To fix it:

    1. Quit and restart Firefox so only one tab is open.
    2. Go to about:config (type that into the address bar) and then click OK when you are warned that you are about to void your warranty.
    3. Navigate to the following config key: Network -> http -> request -> timeout
    4. and double click on 300
    5. Change it to a larger value, such as 3600 (1 hour), 7200 (2 hours), or 86400 (24 hours) – the value that works best for your application.

    As you can see, the default timeout in Firefox 29 is 300 seconds, or 5 minutes.

    See the accepted answer on support.mozilla.org/en-US/questions/998088 for the gory details.

  • How to split a file into 2 chunks in bash

    Posted on May 9th, 2014 phpguru No comments

    I have a task to take a file with millions of lines, and to split it in exactly two files, with a controlled top `head` portion part A file, and the remainder of the lines in part B file.

    It actually turns out to be somewhat non-trival to split a file in 2 chunks, with a small defined top portion and a large arbitrary bottom portion.

    The split [OPTION]... [INPUT [PREFIX]] function is really designed to make N chunks out of your file, or M chunks each containing N lines; I debated using it and merging all but the first file back together, but decided to look up sed examples instead.

    I ended up with the below function. There’s almost certainly a faster/better way to achieve it but this seems to work.

    Sample usage

    remainderof bigfile.txt 1000
    When that runs you will have this result

    1000_bigfile.txt // this is the top 1000 lines
    1000R_bigfile.txt // this is the remainder of the file after splitting off the top 1000 lines
    bigfile.txt // the original untouched file

    Using optional 3rd argument

    remainderof bigfile.txt 1000 true
    When that runs you will have this result

    1000_bigfile.txt // this is the top 1000 lines
    bigfile.txt // this is the remainder of the file after splitting off the top 1000 lines
    // the original file no longer exists

    Here’s the function

    alias ll='ls -larth'
    
    function remainderof() {
    
        thefile=$1
        batchsize=$2
            if [ ! -z $3 ]
                    then
            : # $1 was given
            replaceoriginal=$3
            else
            : # $1 was not given
            replaceoriginal=false
            fi
    
        if [ $# -lt 2 ]
        then
             echo "Usage: $0 filename_to_split rows_to_chop [opt_bool_process_in_place_destructively]"
             exit 1
        fi
    
        extension="${thefile##*.}"
        filename="${thefile%.*}"
        length=$( wc -l < $thefile )
        buffer=$(($length-$batchsize))
        echo Splitting $batchsize lines off the top of $filename $extension leaving $buffer from $length  ...
        #  The top chunk filename
        topfile=${filename}_${batchsize}.${extension}
    
        # The bottom chunk = remainder of file
        startofnext=$(($batchsize+1))
        remainder=${filename}_${startofnext}-${length}.${extension}
        echo "Writing $topfile and $remainder"
    
        # Split off the first N lines of the file
        head -n $batchsize $thefile > $topfile
    
        # split off the bottom LENGTH - N lines of the file
        sed "1,${batchsize}d" $thefile > $remainder
    
        # whether to leave a copy
        if [ "$replaceoriginal" = true ] ; then
            rm -rf $thefile
            mv $remainder $thefile
        fi
    
        echo `wc -l < $topfile` lines in $topfile
        echo `wc -l < $remainder` lines in $remainder
    
    
        echo Done
    }
    

    If you have improvements, leave a comment!

  • How to prevent iPhoto launching when connecting iPhone

    Posted on August 25th, 2013 phpguru No comments

    The solution is on the Apple forums:

    1. Plug in your iPhone.
    2. Launch Image Capture from your Applications folder
    3. Select your iPhone once it shows up under devices
    4. Look at the lower bottom pane where you’ll see “Connecting this iPhone opens”
    5. Set this preference to “No application”

     

    See also: How to prevent iTunes from launching when connecting your iPhone or iPad

  • How to prevent iTunes launching when connecting iPhone

    Posted on August 25th, 2013 phpguru No comments

    The solution is on the Apple forums:

    1. From the iTunes menu at the top go to iTunes>Preferences>Devices>Prevent iPods, iPhones and iPads from syncing automatically.
    2. Check the box at the bottom of that window.
    3. Click on [Your Phone's Button] to access the device. This is on the right side of the iTunes main window toward the top, just to the left of the store button. If you don’t see your phone here, unplug it, plug it in again.
    4. Scroll down and uncheck Automatically Sync when this iPhone is connected
    5. Click Done
    6. Quit iTunes.
    7. Unplug and re-plug in your phone. iTunes should not launch.

    If that doesn’t work, also try:

    1. See if iTunes Helper is in System Preferences > Users & Groups > Login Items
    2. If so, delete it (click it then press the minus button)

     

    See also: How to prevent iPhoto from launching when you connect your iPhone

     

  • What happened to the j.mp sidebar?

    Posted on June 2nd, 2012 phpguru No comments

    A few years back, I found the iPhone app Runmeter. It keeps getting better, too, and is still one of the best $5 I ever spent. Very cool app. When you finish a run, skate or walk, it sends you a j.mp short link to a Google map of your exercise with all the stats it captured via GPS.

    As a web developer interested in link shortening services, I instantly signed up to use j.mp to shorten my links.

    One of the best things about the j.mp link shortening service was the j.mp sidebar, a Javascript bookmarklet that creates a side panel over whatever site you’re on that allows you to use the j.mp shortening service without leaving the page you’re on. It was elegant, easy and just perfect for my needs.

    Even though around September of 2009 bit.ly encouraged their users to switch to j.mp to make their URLs even shorter, they have now appeared to have tabled the j.mp brand along with the excellent j.mp sidebar. This wouldn’t be a big deal if the new bit.ly services were the same or better, but they’re far from it. The new bit marker is annoying and takes five times longer to shorten a link, tries to integrate with sharing sites for you and lots of other annoyances.

    So they deprecated the j.mp sidebar, which as of the time of this writing is still working, but you cannot find it anywhere on the web! So frustrating.

    To combat this problem, here are some instructions below — just sign up for bit.ly if you don’t already have an account, and follow the steps below.

    bit.ly j.mp sidebar bookmarklet

    Drag this link to your browser’s bookmark toolbar
    j.mp sidebar

    bit.ly j.mp sidebar manual creation method

    1. Create a new blank bookmark in your bookmark toolbar folder and name it j.mp sidebar (or bit.ly sidebar if you prefer)

    2. Paste in the following for the URL

    Enjoy!

  • How to Fix no acceptable C compiler found in $PATH on Mac OS X Lion

    Posted on May 16th, 2012 phpguru 2 comments

    If you have a need to compile Memcache or wget on Mac OS X Lion and are wondering why you are getting the error

    no acceptable C compiler found in $PATH

    on Mac OS X Lion, you’re not alone.

    Thanks to this post, I was able to fix my problem. Here are the steps.

    1. Run App Store
    2. Search for Xcode – it’s a free install from Apple
    3. Wait for awhile. Took 30min to download for me on a 20mbps connection
    4. Authenticate and let Xcode install. Once Xcode is installed you may be thinking you’re done. You would be wrong!
    5. Launch Xcode and run the mobile toolkit update (you can’t skip it, deal with it)
    6. Go to Xcode Preferences or press ⌘, (Command-comma)
    7. Click the Downloads tab -> Components list as shown below
      How To Fix C compiler error on Mac OS X Lion
    8. On the last row of the available downloads are the Command Line tools. Install them.

    You should be good to go after that!

  • How to use Setup Assistant to migrate to a new Mac

    Posted on May 15th, 2012 phpguru No comments

    I told you in a recent rant on this blog that I bought myself a new MacBook Pro.

    It truly is a glorious computer; perhaps the best laptop on the planet. It’s blazing fast, ultra quiet, and it’s built like a Sherman tank, but is as sexy as a Ferrari. With Unix under the hood, Apache, PHP and MySQL preinstalled, it’s a web developer’s dream machine.

    I always look forward to setting up a new computer for the first time, but instead of starting from scratch as is my normal approach, this time, I decided to use Apple’s built-in Migration Assistant.

    How to use Migration Assistant to transfer files from another Mac is the name of KB article HT4413 at Apple.com. If you’ve already created your account on your Mac for the first time, but want to migrate a profile (your user account, applications and files) from a different machine or Time Machine backup, using Migration Assistant is the way to go.

    But if you already created your login on your new Mac, read carefully:

    Important info not obvious on Apple’s KB article

    Using Migration Assistant, your old files will be copied to a new, alternate, secondary profile on your new Mac. In other words, if you logged in as ghoffman on your old Mac, and you already created a fresh, new profile ghoffman on your new Mac, you can’t use Migration assistant to get old ghoffman copied into new ghoffman. You can use Migration Assistant to restore ghoffman (old) to ghoffman2 or ghoffmanNEW or any other alternate named profile, just not the one you probably want.

    If you have not yet created your user account on your new computer, or if you are willing to format your Mac and restore it to factory default settings, there is a little-known startup configuration mode when you first boot Mac OS X. It’s called Setup Assistant.

    The first time you start up a new Mac, on one of the very first screens, even before entering your name for creating your account, you have the option of using Setup Assistant, which may be better named First-run Migration Assistant.

    Using Setup Assistant at first-run is by far the fastest way to get going on a new Mac. I was extremely impressed at how simple and complete it was. Just like Migration Assistant, you have a several options as far as the source and transfer method, including using your other computer (as a disk in target mode) or from a Time Machine backup. I chose to use my Time Machine backup over FireWire 800. Other transfer options include USB, Ethernet and Wi-fi.

    I was able to restore my Mac OS X Snow Leopard account on a 250GB MacBook Pro 17″ (Aug 2008) onto a new i7 processor 750GB MacBook Pro 17″ running Lion, in about 2 hours. Over 200 GB of data were restored, including my login username and password, all my keychain files, every document, every application – even Adobe Creative Suite, Microsoft Office, iTunes, iPhoto – everything! Even my ~/Sites folder for web development, with all my local MySQL databases, were perfectly restored.

    I’ve really got to hand it to you, Apple. Not only is this laptop amazing hardware, but your software is extremely good, too.

    Now I just need a new iPhone. And an iPad.