Rediscovering PHP

Entry posted on 2008-03-12 1:21 pm

So last night, armed with my nifty new font, I decided I’d get a move on with the next top-level version of Enthusiast. (Yes, I’ve started working on Enthusiast 4.0.)

(For those of you interested in it, it will probably be slow going, as whatever free time I have needs to be spent juggling between rest/recreation/social/family/other hobbies… and because I’m putting in a lot more effort in the backbone, and in usability.)

One thing I’ve always believed in is that you only get as good as what you actually do. That one might read a lot of tech blogs, a lot of white papers, a lot of those hifalutin framework blueprints… but if you don’t get down and dirty with code, you can’t expect to get better. It’s a given that the first few codes you churn out will be riddled with flaws. That’s normal, but that’s better than never getting over that simply because “I can’t fully understand OOP yet, I need to read more about it”. Ugh, get a grip, and get on with playing with code.

My first PHP project, way back when I was doing self-studying, was actually the precursor to Enthusiast. It was the script that handled my then-fanlisting, Bubblegum Crisis. After it was working, I moved on to the first “system” — an admin tool for handling my directory for NeoPets galleries. I actually put up that site, got a pretty nice following for it, and then when I weaned off NeoPets, I shut it down.

And then I started working on Enthusiast (the single-fanlisting version).

Without these first projects, I’d never have learned PHP. And this time, with PHP4’s End of Life looming in the distance, Enthusiast will be bringing me forward to PHP5. OOP, Exceptions, and many newfangled stuff in PHP isn’t new to me, but it’s been a while since I’ve actually handled PHP code continuously (almost eight months–the same time I’ve been with Yahoo! as a frontend engineer). I will be getting personal with a lot of these new things, in order to do what I need for it to do. And that’s quite exciting.

For anyone who’s thinking of learning PHP, the best way to learn really is by doing something you’re passionate about using PHP. I was passionate about NeoPets galleries; I was passionate about fanlistings. The passion drives you forward, and that’s a great thing to have.

I will probably be blogging here occasionally about new things I find out while working with PHP, or thoughts on development in general — what would you like to hear about?

Apache, PHP, and MySQL in Leopard

Entry posted on 2008-03-02 9:21 pm

I was quite delighted to find out recently (via AJ) that Apache and PHP was available by default on my Mac. Before I got my Mac, I thought that was the case, and then I couldn’t find it and supposed there was none and got living-e’s MAMP instead.

I quickly got annoyed, because just logging off and shutting down my computer after a bit of dev tweaking meant typing in my account password. Sometimes I ended up forgetting I had it running, and Logout would stop because MAMP needed something from me. It was quickly set up, but after a few weeks of getting it (and ending up too lazy to go through the whole startup-type-password-work-shut-down-type-password cycle…go figure) I was ready to brave whatever UNIX source compiling wizardly people go through to get their machines ready for web development.

After all, I’ve never resorted to using WampServer or XAMPP (etc) when I was still on Windows. I’d always preferred installing and configuring each one by one. This shouldn’t be hard, right?

And nope, it wasn’t! I’d initially envisioned needing to compile the source and all that scary stuff, but apparently (like I said) Apache and PHP was already built in, and MySQL had a Mac OS X binary. Yay! I spent an afternoon tweaking to my heart’s content, after finding gems like these:

Here’s what I did.

  1. Set up Apache’s configuration file.

    Open up Terminal, and type sudo vim /private/etc/apache2/httpd.conf. You’ll need to enter your password, since you’re running as root. Line 114 (or thereabouts) will be where Apache loads the PHP5 module. Remove the hash/pound sign (#) (type i to enter insert mode, and escape to get out of insert mode when you’re done) at the start of the line.

    LoadModule php5_module in httpd.conf

    Optional: You can keep going and customize your httpd.conf file to your liking. For me, I did the following:

    1. Change DocumentRoot to my Sites folder (two lines to change).
    2. Add index.php in DirectoryIndex to automatically load index.php files ahead of index.html when requesting a directory.

    Save the file (type :wq when in command mode).

  2. Setup PHP’s PHP.ini

    Leopard doesn’t have a PHP.ini by default, but the default one is still there, named PHP.ini.default. Make a copy of this by moving to the /private/etc folder and copying that file:

    $ CD /private/etc
    $ sudo cp PHP.ini.default PHP.ini

    You might need to enter your password again. After that, you can edit PHP.ini (again, sudo vim PHP.ini…this is read-only, so remember to override vim’s warning when you’re saving and use :wq!) to change error reporting and other things you like to have PHP do when you’re developing.

    php.ini error reporting

    Note: The mysql and mysqli extensions are not enabled by default. You probably want to change that. (See lines 625 and 626.)

  3. Run Apache!

    Now it’s time to test your web server and PHP together. Fire up System Preferences, and under the Internet & Network section, click on Sharing. Check the check box next to Web Sharing. Once it’s on, you can go to the URL there, or try the ever-trusty http://localhost, to test if your settings are as they are.

    Of course, if you feel like you want to do it the geeky way, you can always run sudo apachectl start.

    Web Sharing preference pane

  4. Now let’s get MySQL up and running.

    MySQL isn’t included, so we’ll have to install that. Download a binary package and install MySQL, the StartupItem, and the preference pane. I haven’t actually gotten the preference pane to actually stop and start the MySQL daemon, but I figure it will work eventually, and it’s always nice to see it in System Preferences.

    Once they’re installed, hit sudo /usr/local/mysql/support-files/mysql.server start to start the daemon. You can try doing this via the preference pane (and let me know if it works). The preference pane also lets you toggle if you want the daemon to start automatically when you log in (which is the whole point of this exercise…but again, for some reason it doesn’t like me ;( sob).

    MySQL preference pane

  5. MySQL socket problems in PHP

    As of the time of this writing, just installing MySQL and enabling the appropriate extensions in PHP.ini isn’t enough. PHP won’t be able to find the MySQL socket and won’t be able to talk to your database server. This post has a good explanation why, but to summarize the fix for this:

    1. Create a my.cnf file in /etc:
      $ CD /etc
      $ sudo vim my.cnf
    2. Type the following in the file:
      [client]
      socket = /var/mysql/mysql.sock
      
      [mysqld]
      socket = /var/mysql/mysql.sock
    3. Save the file and exit to the shell (:wq in command mode).
    4. Type the following commands for the sock file’s directory:
      $ sudo mkdir /var/mysql
      $ sudo chown _mysql /var/mysql

    PHP should be able to connect to MySQL now. A word of caution:

    One drawback to this is that if you have installed the MySQL GUI tools, they will look for the mysql.sock file at the old location. You can enter the new socket in the connection dialog under More Options, there is a box labeled “connect using socket.” Just enter /var/mysql/mysql.sock.

    Another solution is to change the PHP.ini file to expect the socket in a different location. I’m going with the my.cnf option because I expect the MySQL will have a Leopard version out in a few days that changes the default location.

    - from Professional PHP

That’s all there was! You should be up and running in no time. I ended up taking a bit longer because of the following (which might help you):

  • My files all had wrong permissions. They were all just readable and writable by myself (the owner) and hence my web server couldn’t read them. A quick recursive CHMOD 755 * helped, although of course I’m wondering if there’s an easier way to get this all done. (Let me know?)
  • I installed CocoaMySQL for my database management needs. It looks pretty spiffy. I’ll give it a whirl and if it isn’t enough, I might try out Navicat, although I’d rather not need to pay for a management tool.

Edited to add: I found out that the MySQL preference pane really wasn’t working, and that MySQL is aware of this issue. I found a patch for it via Natron Designs; and yes, now, it works!

Development musings

Entry posted on 2008-01-07 2:28 pm

Happy, hopeful new year, everyone!

Life is just barely settling down after the holidays, and I’ve been meaning to work on Enthusiast for a while now, but kind of endlessly putting it off. I want that to change, but to change that, I’ll have to do a bit of reassessment, and to pinpoint why I have lost the thrill and joy of working on Enthusiast.

It won’t mean it will be gone, as even if I feel rather unmotivated to work on Enth, I still love it and want to bring it to the next level. I’ll just have to figure out what the next level is.

Just in case it helps others out there, here’s something I’ve started reading that I feel is helping me a lot: Getting Real. A lot of things won’t be applicable to Enth (I don’t think suddenly scaling down features is an option) but I’m trying to figure out what to do with the stuff I’m reading and taking in.

More entries