Ramblings on technology with a dash of social commentary
RSS icon Email icon Home icon
  • 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.

  • 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

  • LVS-TUN How To on RackSpace Cloud

    Posted on October 6th, 2010 phpguru No comments

    I have to hand it to the sales/techs over at RackSpaceCoud.com.

    After just a few minutes with their Online Chat Sales, I was intrigued enough to create an account. Heck, it was free, and would give me a chance to experiment with server configuration via one of the hot topics today – virtualization.

    One of the first things I mentioned to Zack (I think that was his name) was that I was interested in making a more fault-tolerant web server setup. My client at the time had a dedicated server, but was planning on a few different television appearances, and didn’t want his site to go down with the surge of traffic.

    Load Balancer How-To using Apache Mod_Proxy (Easy)

    The RackSpaceCloud tech linked me to this article by Brandon Woodward on how to set up a simple load balancer using Apache. I followed that article to the letter, using one CentOS VM for the load balancer, and two CentOS VMs for web heads.

    Well, I have to admit, it was not as difficult as I thought it would be. Now granted, it’s not super robust, but for a simple round-robin load balanced setup, it only took an hour or two to setup and configure, and it is still in place on the client’s domain. Not bad, considering it’s the equivalent of 3 servers for less than $50/mo.

    Load Balancer How-To using LVS-TUN (Intermediate-Advanced)

    Having found the RackSpaceCloud article on setting up a load balancer with Apache just a few months prior, I was interested in learning more when I saw this new article by the same author on installing and configuring LVS-TUN.

    I was intrigued, because even though I believe having a mod_proxy-based load balancer solution on 3 servers in the cloud is preferrable in many ways to a single server with no load balancer at all, I could still see some areas for improvement and wondered what else was out there.

    Woodward explains it like this:

    LVS-TUN is a tunneling load balancer solution that will take all incoming requests through the load balancer and forward the packet to the web nodes. The web nodes will then respond directly to the client without having to proxy through the Load Balancer. This type of solution can allow for geo-load balancing, but will more importantly allow a customer use the bandwidth pool available from all web nodes, instead of relying on the limited through put of the load balancer.

    In other words, whereas the mod_proxy-based load balancer is quick and easy to set up and manage, the load balancer itself is a single point of failure, since it is acting as a proxy for the two web heads behind it. LVS-TUN on the other hand, enables the load balancer itself to be distributed, and also takes advantage of the web heads serving content directly back to the client.

    Whether you’re looking at a load balanced setup for scalability or just your own hardware architecture experimentation, without a doubt, the cloud is the best playground.