Ramblings on technology with a dash of social commentary
RSS icon Email icon Home icon
  • Redis 101 at Desert Code Camp 2011

    Posted on October 27th, 2011 phpguru No comments

    I’m proud to announce that I’ll be teaching Redis 101 at Desert Code Camp on November 5, 2011.

    Redis is a powerful memory-resident data store.

    In order to give you a good background on what Redis is, let’s take a trip down memory lane, back to our first lesson on how computers work. Computers have a hard disk drive, which enables data and files to be stored permanently (or, at least, for very long periods of time). They also have memory, or RAM, which is volatile, but very, very fast, comparatively.

    Why do we need both hard-drives and memory? Economics, mainly. Cheap, huge hard drives can store millions of files permanently, even if the power goes out. The tradeoff to all that massive amount of permanent storage is that hard drives are slow. RAM, on the other hand, is volatile storage, meaning that whatever is in RAM is lost when you power down your computer. Data in RAM is blazing fast, though. The trade-offs for all that blazing speed are high cost and impermanence.

    The latest Solid State Disks (SSD’s) are breaking some of these rules by being decent-sized, permanent storage that’s faster and quieter than a regular hard drive, but not as expensive as RAM. SSDs are more like hard drives or memory sticks with no moving parts. What’s Cool About SSDs would make a good blog post in the near future.

    Back to caching. In general terms, a cache is simply a faster place to retrieve data from.

    Your computer can access data stored in RAM much, much faster than it can access files on disk. When you launch a program, your computer is reading the application data from disk (slow) and loading it into memory (fast) so you can work on it.

    Several forms of caching are used to speed up surfing the web.

    You’re probably most familiar with client-side caching—your browser cache, or Temporary Internet Files for you Windows users. Web browsers use a local cache (just a special hidden folder) on your hard drive to store the stuff you’ve downloaded before. The theory here is, if you’ve already downloaded the home page of a site, chances are most of the files, scripts and stylesheets are reused across other pages on the stite, so by keeping local copy in the cache, your browser doesn’t have to re-fetch assets from the server again.

    Server-side caching, on the other hand, is a technique implemented by web application architects, to help speed up web applications. Some of the things that can be cached on the server-side include frequently-used files, query results, or processing PHP templates into their rendered HTML. With server-side caching, the idea is to alleviate some or most of the work your web server has to do when processing a particular request.

    Going back to our lesson on how computers work, we know hard drives are slow and memory is very fast. So when talking about maximum performance for web servers, we had better be looking at storing data in memory.

    Two of the most popular memory-caching platforms for PHP are Memcache and APC, the Alternative PHP Cache. These PHP extensions have been around a very long time, and are able to utilize RAM, instead of files on disk, to make data available to your PHP scripts almost instantly.

    Memcache and APC are known as key-value stores. Memcache is only a key-value store, meaning that’s basically the only feature it offers. APC is a key-value store, and also an opcode cache. An opcode cache actually compiles PHP scripts into machine-executable code, very much like compiling C# into a .DLL or Java code into a .war file. Compiled code runs faster than code that has to be parsed first.

    With any key-value store, you hand it a key and some data, and then later, you can lookup the data again almost instantly by using the original key.

    So why Redis? Like Memcache, Redis is also a key-value store, but it also offers several unique, blazing-fast data structures, including hashes, sets, lists and more. In addition, Redis includes a terrific write-to-disk feature as well as master-slave replication, giving you an extremely flexible and powerful set of tools, that fits in perfectly with any database-driven web application. And all for the amazing low-low price of only $0.00! By combining APC’s opcode cache capabilities, with the flexibility of Redis, you have everything you need to make your web applications really scream.

    Whether you’ve never used a caching platform before or you’re already fluent in Memcache or APC for high-performance website scalability, come and learn why Redis is quite possibly the best thing since sliced bread.

  • Using XDebug with Command-line PHP and NetBeans

    Posted on October 25th, 2011 phpguru No comments

    Magic sauce, so easy to misplace:

    export $XDEBUG_CONFIG="idekey=netbeans-xdebug"
    echo $XDEBUG_CONFIG
    idekey=netbeans-xdebug

    The string, `netbeans-xdebug` is configurable inside NetBeans preferences.

  • Enabling Command-line PHP Curl on Mac OS X Lion

    Posted on October 25th, 2011 phpguru No comments

    If you’re doing web development on the Mac with the Apache 2 and PHP 5.3.6 that ships from Apple, you might one day run into a use for some command-line scripts. I happen to be doing just that at the moment. Well it seems that Apple ships PHP 5 with everything you need to use the curl libraries, except the extension.

    I was surprised with an error warning like this:

    PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/extensions/no-debug-non-zts-20090626/php_curl.dll' - dlopen(/usr/lib/php/extensions/no-debug-non-zts-20090626/php_curl.dll, 9): image not found in Unknown on line 0
    Huh? I need a Windows DLL on Lion? I guess so…

    I’ve been using CURL scripts from PHP running under Apache since day one, but what I’m realizing is that the command line version of PHP, aka, the php-cli, is installed a little differently.

    First, you may want to check your php.ini that the extension is uncommented (remove the preceding semicolon):
    extension=php_curl.dll This is a pretty obvious step to try, even though Lion is clearly not Windows, and the section in php.ini clearly says it’s for Windows Extensions.

    So you might try installing the latest version of cURL.
    cd ~
    mkdir src
    cd src
    curl -V
    curl 7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5

    At this point, you start wondering what’s going on, because cURL is installed at the Factory by Apple. Strange…

    So you can
    sudo find / -name php_curl -print just to see if the DLL is in the wrong place or something, maybe a quick symlink would fix it.
    Nope.

    But this is interesting… PHP 5.3.6 from Apple ships with the curl extension:

    cd /Users/%me%/Documents/src/php-5.3.6/ext/curl
    phpize
    ./configure
    make
    sudo make install

    Now edit your php.ini and add
    extension=curl.so
    and
    sudo apachectl restart
    Well that didn’t do much. Now I get:
    PHP Warning: Module 'curl' already loaded in Unknown on line 0

    Hmmm, stranger still.

    Here’s the punchline: All you need to do is comment out (add the semicolon at the front)
    ;extension=php_curl.dll
    and restart Apache.

    The warning was exactly correct – it couldn’t find a Windows DLL. I wonder why that’s uncommented.

    I don’t remember uncommenting it, but maybe I did.

  • How to install PHP APC extension on Snow Leopard

    Posted on August 11th, 2011 phpguru No comments

    I followed several posts on the Apple Suppor forum to install APC on Snow Leopard (not Server, but this forum seems much more appropriate as I am using Snow Leopard on my local MBP 17 for serious web development, trying to match my Ubuntu Server config as closely as possible.)

    Most notably, these:

    discussions.apple.com/thread/2448001
    discussions.apple.com/message/12923918
    discussions.apple.com/message/11696363
    discussions.apple.com/message/10454666

    as well as

    www.kevinworthington.com/nginx-mac-os-snow-leopard-2-minutes/
    serverfault.com/questions/206633/failed-to-instal-apc-via-pecl-install-apc

    I had to instal PCRE 8.12 before APC 3.1.9 install would work. Wincent has perfect instructions for that.

    Now back in my APC source directory, doing phpize and ./configure resulted in test failures; I realized that the recommended way of installing APC is via pecl channel-discover pecl.php.net
    pecl install APC

    This succeeded: Build process completed successfully
    Installing '/usr/include/php/ext/apc/apc_serializer.h'
    Installing '/usr/lib/php/extensions/no-debug-non-zts-20090626/apc.so'
    install ok: channel://pecl.php.net/APC-3.1.9
    configuration option "php_ini" is not set to php.ini location
    You should add "extension=apc.so" to php.ini

    Okay, so I add extension=apc.so to php.ini and restart apache. sh-3.2# php -i | grep apc
    apc
    apc.cache_by_default => On => On
    apc.canonicalize => On => On
    apc.coredump_unmap => Off => Off
    apc.enable_cli => Off => Off
    apc.enabled => On => On
    apc.file_md5 => Off => Off
    apc.file_update_protection => 2 => 2
    apc.filters => no value => no value
    apc.gc_ttl => 3600 => 3600
    apc.include_once_override => Off => Off
    apc.lazy_classes => Off => Off
    apc.lazy_functions => Off => Off
    apc.max_file_size => 1M => 1M
    apc.mmap_file_mask => no value => no value
    apc.num_files_hint => 1000 => 1000
    apc.preload_path => no value => no value
    apc.report_autofilter => Off => Off
    apc.rfc1867 => Off => Off
    apc.rfc1867_freq => 0 => 0
    apc.rfc1867_name => APC_UPLOAD_PROGRESS => APC_UPLOAD_PROGRESS
    apc.rfc1867_prefix => upload_ => upload_
    apc.rfc1867_ttl => 3600 => 3600
    apc.serializer => default => default
    apc.shm_segments => 1 => 1
    apc.shm_size => 32M => 32M
    apc.slam_defense => On => On
    apc.stat => On => On
    apc.stat_ctime => Off => Off
    apc.ttl => 0 => 0
    apc.use_request_time => On => On
    apc.user_entries_hint => 4096 => 4096
    apc.user_ttl => 0 => 0
    apc.write_lock => On => On

    So now we know, from the command line anyway, PHP CLI is reporting it has APC installed.

    Now here’s the interesting part… from Apache (e.g. trying to run any local PHP file http://localhost/info.php for example) I just get a white page of death.

    If I comment that extension out and restart Apache, PHP files work again.

    Hmm, lets look at the error.tail -f error_log
    shows: Fatal error: Unknown: apc_fcntl_unlock failed: in Unknown on line 0

    I google that and find this bug report, where it is stated that a patch is available for apc_lock.h.

    I go back to the PECL APC source site and browse the sources in trunk, find the apc_lock.h file, download it, put it into my APC-3.1.9 source directory and run phpize
    ./configure
    make clean
    make install
    apachectl restart

    Note that `make clean` is the magic bit there since we already installed APC from these sources, we need to make a new fresh install from these same sources.

    Now everything is working happily again.

    Good luck with yours!

  • Cool 404 Pages

    Posted on June 23rd, 2011 phpguru No comments
    Google 404 Page

    Google 404 Page

    GitHub 404 Page

    GitHub 404 Page

  • Kohana 3.x in 10 Minutes

    Posted on June 11th, 2011 phpguru No comments

    Prerequisites:

    This guide assumes you have already completed:

    So you have a LAMP/MAMP/XAMPP stack? Great!

    Now we’re ready to create our first Kohana project.

    1. Download the source from www.KohanaFramework.org
    2. Extract the zip file and move the directory it to your  web directory
      1. On Mac OS X: ~/Sites/* (subsequent references will use Mac notation)
      2. On Windows (wamp): C:/wamp/www/*
      3. On Linux (varies): /var/www/*
    3. Rename the directory to kohanasite.dev
      1. Remember the full path to this now
      2. something like /Users/yourname/Sites/kohanasite.dev
    4. Create an htdocs directory inside this directory
      1. Your hdtocs will be at the same level as Kohana folders application, modules and system.
      2. Remember the full path to this now
      3. something like /Users/yourname/Sites/kohanasite.dev/htdocs
    5. Drag index.php into the htdocs directory
    6. Edit 3 lines of index.php
      1. where it says $application = ‘application’; change it to $application = ‘../application’;
      2. where it says $modules = ‘modules’; change it to $modules = ‘../modules’;
      3. where it says $system = ‘system’; change it to $system = ‘../system’;
      4. save and close
    7. chmod or chown application/cache and application/logs to be writable by Apache
      1. open terminal, and cd to your application directory
        cd /Users/your-name/Sites/kohanasite.dev/htdocs
      2. chmod the two directories
        chmod 0777 cache && chmod 0777 cache
      3. or chown it to apache, wheel, www-data or whoever owns httpd process (ps aux | grep httpd)
    8. Create a VirtualHost entry in your Apache config.
      1. Depending on your install, this might be in httpd.conf or…
      2. on my Mac it’s at /private/etc/apache2/extra/httpd-vhosts.conf
      3. E.g.
        sudo vi /private/etc/apache2/extra/httpd-vhosts.conf
        NameVirtualHost *:80
        <VirtualHost *:80>
        ServerName kohanasite.dev
        DocumentRoot "/Users/your-username-here/Sites/kohanasite.dev/htdocs"
        </VirtualHost>
      4. save and quit (type :wq and hit enter)
    9. Edit your hosts file to point to this site locally
      1. E.g.
        sudo vi /etc/hosts
        127.0.0.1   kohanasite.dev
      2. save and quit (:wq)
    10. Restart Apache
      1. sudo apachectl restart
    11. Move example.htaccess to htdocs and rename it to .htaccess
      1. Note, because it’s a “dot file” it will disappear on a Mac, so use terminal:
        mv ../example.htaccess .htaccess
    12. Now test your site at http://kohanasite.dev
      1. It should say Hello World!

    Congratulations! You now have Kohana 3.1 running locally.

    Finishing up…

    Let’s enable some of the modules that come with Kohana 3.x

    1. Open up application/bootstrap.php
    2. Scroll down to the modules section
    3. Enable the following modules by removing the double slash in front of the module name:
      1. auth
      2. cache
      3. codebench
      4. database
      5. orm
      6. userguide
    4. Save the bootstrap file and refresh your browser to make sure you still get hello world

     

    Next up… How to build a simple CMS using Kohana.

  • Apache, PHP and MySQL in 10 Minutes

    Posted on June 11th, 2011 phpguru No comments

    Whether you want to start developing web applications for the first time, you’re wanting to make better use of your local computer or a virtual machine to streamline development, create a development or staging server, or just checking back as a reference for a new platform, welcome to…

    Apache, PHP and MySQL in 10 Minutes

    In this guide you will find the resources needed to build a web development environment on the platform of your choice

    1. Choose Your Platform – Mac, Windows or Linux.
      1. Mac OS X Instructions
        1. Easy - Using MAMP
        2. Intermediate - Using built-in Apple Apache
      2. Windows Instructions
        1. Install WampServer
        2. Install XAMPP
      3. Linux Instructions
        1. Install LAMP stack on CentOS/RHEL
        2. Install LAMP stack on Debian/Ubuntu
        3. Install LAMP stack on Fedora
        4. Install LAMP stack on OpenSUSE
        5. Install LAMP stack on ArchLinux
      4. CPanel
        1. CPanel comes with Apache & PHP already installed, so here’s how to customize your install
        2. Note – make sure your config comes with WHM (Web Host Manager)
        3. Many ISPs call this a Reseller account
        4. How to use the EasyApache script
      5. Plesk
        1. Plesk comes with Apache & PHP already installed, so here’s how to customize your configuration
        2. Parallels Plesk Apache Configuration Guide
    2. Install any PHP Extensions you might need
      1. The one PHP extension that Kohana requires you that doesn’t come by default (See Kohana 3.1 in 5 minutes) is mcrypt:
      1. Pluging mcrypt into OS X
      2. Enabling mcrypt under WampServer
      3. Install mcrypt for php under Linux
      4. Install mcrypt under CPanel
    3. How to create a VirtualHost
      1. Find your Apache Config file
      2. Find your Apache VirtualHost config file
      3. Sample VirtualHost container
      4. <VirtualHost *:80>
        ServerName website.dev
        DocumentRoot "/Users/username/Sites/website.com"
        </VirtualHost>
      5. More from the Apache VirtualHost documentation
    4. How to edit your hosts file
      1. hosts file information
    5. Start Apache
      1. Start Apache
        1. Mac OS X:
          1. Terminal
            1. sudo apachectl start
          2. or start Web Sharing in System Preferences
        2. Windows
          1. WampServer – start all services using the taskbar tray icon
        3. Linux
          1. Debian/Ubuntu
            1. sudo /etc/init.d/apache2 [start | stop | restart ]
          2. Fedora
            1. service httpd [start | stop | restart]
          3. ArchLinux
            1. /etc/rc.d/httpd restart
          4. OpenSUSE
            1. apachectl [start | stop | restart]
    6. Test your install
      1. Go to http://localhost in your browser
      2. Are you seeing the Apache start page?
      3. If so, you are done. Congratulations! You just installed a web server.
    7. Install MySQL
      1. Mac OS X
        1. Installing MySQL on OS X
      2. Windows
        1. Installing MySQL on Windows
      3. Linux
        1. Installing from a binary
        2. Build MySQL 5.5 from source

     

    Now that you have a development environment, why not install a fantastic web development framework, such as Kohana.

    Ready for round two? Kohana 3.1 in 10 Minutes

  • Git Resources for Subversion Users

    Posted on June 11th, 2011 phpguru No comments

    I’ve been using Subversion for years, but lately I’ve been thinking it’s time to get into Git more. Plus, A lot of new GUI tools are out there and some of the older ones are still there, too, so maybe Git has matured enough for production use.

    Why would I want to use Git?
    Flavio sums it up quite nicely:

    What’re the advantages?
    Since Git is a distributed revision control system (while svn is a centralized one) you can perform commits, branches, merges… on your local working directory without being connected to internet. Next time you’ll be online, you will be able to “push” your changes back to the central svn server.

    In a tech video from Facebook, one of the things they mention is that their developers us Git on local workstations for managing changes, and Facebook overall uses Subversion for managing the source tree centrally. I thought it was interesting that Facebook, one of the most advanced, largest and high-powered software applications on the planet, uses both Git and Subversion. Here’s an article about Using Git and Subversion Together.

    I’ll be posting links to the resources I find here.

    Three Part Intro to Git Series

    1-2 hours • First, take the Git Crash Course.

    2-4 hours • Next, read Git for Subversion Users, Part I and Git for Subversion Users, Part 2. These two articles at IBM.com give you a really good overview of the main differences between Subversion and Git.

    4-6 hours • Finally, you should read Pro Git. This is an awesome free online book about Git.

    After you complete these you should be a total Git.

  • How to install Memcached Memcache PHP on OS X

    Posted on May 9th, 2011 phpguru No comments

    This Guide is the best resource I’ve found yet. I had to also look up how to fix pear installation errors.

     

  • How to Install PHP 5.3 on CentOS 5.5

    Posted on April 22nd, 2011 phpguru No comments

    Awesome – I found two different RPMs, try your luck with both:

    How to Upgrade or Install PHP 5.3 on CentOS 5.5 at Mokonamodoki (IUS RPMs)

    or

    PHP 5.3 on CentOS 5.5 at Webtatic (Webtatic RPMs)

    The install command I ended up using (IUS) was:

    yum install php53u.x86_64 php53u-bcmath.x86_64 php53u-cli.x86_64 \
    php53u-common.x86_64 php53u-dba.x86_64 php53u-devel.x86_64 \
    php53u-gd.x86_64 php53u-imap.x86_64 php53u-intl.x86_64 \
    php53u-mbstring.x86_64 php53u-mcrypt.x86_64 php53u-mysql.x86_64 \
    php53u-pdo.x86_64 php53u-pear.noarch php53u-pecl-apc.x86_64 \
    php53u-pecl-memcache.x86_64 php53u-pecl-xdebug.x86_64 \
    php53u-process.x86_64 php53u-pspell.x86_64 php53u-snmp.x86_64 \
    php53u-soap.x86_64 php53u-suhosin.x86_64 php53u-xml.x86_64 \
    php53u-xmlrpc.x86_64

    MySQL 5.1 and MySQL 5.5 on CentOS 5.5 at Webtatic (Webtatic RPMs)

    If you need to do any maintenance on the existing software, packages and repos you might find these links helpful also:

    RPM-cheat-sheet

    How to Add a New YUM Repository

    How to Use Yum

    How to Edit IPTables

    ius/stable/Redhat/5.5/x86_64/

    ius/stable/Redhat/5/x86_64/repoview/