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!

Using non-plugin third-party scripts with WordPress 2.1.2+

Entry posted on 2007-04-05 4:11 pm

It’s probably a minority, but WordPress users who are using third-party scripts (like Enthusiast) directly coded/included into their WP templates (read: not included as a plugin) who have upgraded to 2.1.2 and higher may experience problems getting their blogs to work. This is due to a conflict between the database link that WordPress uses and the database link that your other script is using. Users get a variation of the following line:

Warning: mysql_error(): X is not a valid MySQL-Link resource in (path) on line Y

Basically, PHP 4.x’s mysql_connect function—which lets the script talk to your database server—gets confused as to which connection is active. This has happened to me since the script I’m using for my downloads (here in this archive, in my wallpapers archive, and the various downloadable things in my other sites) is not set up as a WP plugin; I just do a series of includes in my WP templates. So when I updated to 2.1.2, I ran into problems.

I meant to address this earlier, when I came across the problem here in my tech blog/scripts archive, but got sidetracked. I was only reminded of it when I came across some open support posts at CodeGrrl.

The fix? It’s rather simple. After every chunk of code (or PHP include) you put in your WP template that accesses the database server (read: it generates the above error), add this line of code:

// reconnect to WordPress database
$wpdb->select( DB_NAME );

(Actually, the first line isn’t that important. But comments are good for you.) This might not work for every instance; basically it just changes the database that the link connection is using. However, it’s possible that the new database user the connection is, ah, using, can’t access your WordPress database. So if the above doesn’t work for you, you’ll have to use something else:

// reconnect to WordPress database
$wpdb = new wpdb( DB_USER, DB_PASSWORD, DB_NAME, DB_HOST );

That should basically restart the WordPress database. I haven’t tested the second option, so if you end up needing to use the second option and get problems, let me know. :)

If you’re using Enthusiast with WordPress 2.1.2+ and need a better example, here’s something that may be clearer:

// show joined listings
require( 'config.php' );
$show_list = true;
include $path . 'show_joined.php';
// reconnect to WordPress database
$wpdb->select( DB_NAME );

Do the same for all the other snippets and you should be fine.

Tutorial: Uneven header and background

Entry posted on 2006-12-16 3:05 pm

One thing I’ve come to realize when I design websites and implement layouts is that a big chunk of my enjoyment in the entire process stems from the challenge of actually implementing some seemingly tricky design. There have been times when the end result of a layout may be rather simple-looking, but I end up prouder of it than the usual because of what I did to implement said layout.

Final layout screenshot in GIF (which is why it's grainy) One recent example is the new skin I made for It’s a Soul Thing: the Taylor Hicks fanlisting. At first glance, it’s nothing very exciting or innovative, but what made the implementation interesting for me was the the following criteria/features of the layout that I wanted:

  • background image is uneven — it’s different for either side of the header image, but:
  • the layout has to be liquid and compatible for all screen resolutions equal to or greater than 800×600,
  • the layout mustn’t use tables to lay out the design of the site,
  • the menu must be text-based, no image maps, and
  • the site must be cross-browser compatible.

With all those in mind, I bring to you a mini article/tutorial of sorts on what I did to implement the layout.

Important note: I may talk in detail about how I implemented the layout here, but that doesn’t mean that anyone can use my layout for their own websites. Feel free to learn from my method, but don’t steal.

All images (or almost all) are thumbnails — click on them to enlarge.

Read the rest of this entry »

More entries