Category Archives: PHP

Apache, PHP and MySQL on Snow Leopard

I have enjoyed using MacPorts for a number of reasons, and I have written about it several times. Recently, I have had to rely on it more, and I have grown frustrated with it to the point that I had to abandon it altogether.

I upgraded to Snow Leopard (10.6) last week. It broke my install of MacPorts, but they actually tell you to reinstall after an OS upgrade like this. So I did that, but I could not get Apache 2 and PHP running again to save my life, not to mention MySQL.

So I instead relied on the factory installs of Apache and PHP. It turns out they have gotten a lot better with Snow Leopard. All I had to do was reinstall MySQL which meant I lost my databases but I was prepared for that to happen by then anyway.

Here are the instructions to get Apache, PHP and MySQL running in Snow Leopard.

Install Apache/PHP/MySQL on Snow Leopard

It worked perfectly for me except for one thing. PHP short tags are off by default. So make sure to turn them on or check your code to make sure you are not using PHP short tags, or your PHP code will not work.

I may continue to use MacPorts for other things, but for now I am no longer using it for my dev environment. Maybe one day I will give it another shot if I need it.

Install Apache, PHP and MySQL on Leopard with MacPorts

Running selfupdate on MacPorts is a good way to keep your MacPorts software up to date. The problem is when I did this, all my settings were overwritten. I should also mention that running selfupdate on my MacPorts install caused all my virtual hosts to get overwritten as well. I wrote a post last year on how to install Apache and PHP on Mac OS 10.5 with MacPorts, but following that post didn’t help me get up and running again. So I figured I would write a new one that would actually have the right information.

Here are the install instructions you need for installing Apache, PHP and MySQL with MacPorts. I had already installed Apache and PHP, but MySQL wasn’t working. I have MySQL server already installed and running, but PHP wasn’t finding it. I already had multiple databases set up, so I did not want to reinstall MySQL. All I had to do was follow the instructions in step 4. It fixed the problem, but there were a couple of settings that needed to be changed first.

After you install PHP, it gives you a 2 part message. Part 1,

Your php.ini contains a line that will prevent php5-mysql
and other PHP extensions from working. To fix this,
edit /opt/local/etc/php5/php.ini and delete this line:

extension_dir = "./"

is correct. Deleting that line is how you find the right extensions. Part 2,

To use mysqlnd with a local MySQL server, edit /opt/local/etc/php5/php.ini and set
mysql.default_socket, mysqli.default_socket and pdo_mysql.default_socket
to /opt/local/var/run/mysql5/mysqld.sock

did not work for me. For my MySQL server, that socket does not work. Instead, I found this article that tells you the correct socket on Leopard to set:

mysql.default_socket = /private/tmp/mysql.sock

That fixed everything for me, except for my virtual hosts. Again, your mileage may very. These instructions worked for me on my machine with the software I have.

How to Create a Google Analytics Plugin for bbPress

bbPress is a free, open source forum software from the makers of WordPress. I am even using it on this site. It just came out of beta with the relase of 1.0. If you are looking for forum software I recommend it.

However, the theming system is not as simple as WordPress. I had added a Google Analytics script to my themes. But when I upgraded to 1.0, I lost all my tracking scripts.

The best way around this, I thought, is to create an analytics plugin. It turns out, it is very easy, especially if you have experience creating plugins with WordPress. The bbPress footer template has a hook for adding code to the footer. You just have to create the code that will add your tracking script to the footer of every page. That is it.

Here is an example of the code you can use. Download bbpress-analytics here. Just upload the script to your bb-plugins directory, and activate the plugin in the plugin admin. If you are using Google Analytics, all you need to do is replace “UA-XXXXXX-X” with your site code. But it will work with any tracking script, just replace the Google script with your own. Also you can use this same code to add any code you wanted to the footer.

Installing PHP 5 on Mac OS X

Okay, so I have been running PHP 4 locally on my Mac OS 10.4, but I recently needed to upgrade to PHP 5. In addition, PHP 4 will no longer be supported after the end of this year. The installation instructions are the same as for PHP 4, which is not a big deal. I also installed libcurl, which is not included by default for some reason.

Why am I posting this here then? So I can reference it, whenever I need to recompile PHP again. I am installing new libs all the time, it seems like, so it helps to know what I have done each time so I can do it again the next time. This is the third or fourth time I have done this, so I won’t go into that much detail. If you need more details, look at my other posts.

For step one, download and unzip PHP 5. Then, open the folder and run configure, make and sudo make intall, like this.

./configure --prefix=/usr/local/apache2/php
--with-curl=/usr/local/src/curl-7.17.1
--with-zlib-dir=/usr/local/lib
--with-jpeg-dir=/usr/local/lib
--with-png-dir=/usr/local/lib
--with-gd --with-mysql=/usr/local/mysql
--with-apxs2=/usr/local/apache2/bin/apxs
make
sudo make install

Remember to change your file paths, if they are different than mine. Restart Apache, and you’re done!

Installing Apache 2.0, PHP and Subversion on Mac OS 10.4

After trying to install Django on my Mac and failing, I have decided to try and upgrade my local version of Apache 1.3 to version 2.0. If you are developing on the Mac OS, you may know that Mac OS 10.4 comes with Apache installed on it. While this is one of the great things about the Mac OS, what’s not great is that it is out of date. For most cases when you are developing locally on the Mac, Apache 1.3 is fine. But, you may find that with Apache 1.3 installed, you will not be able to run certain software.

The solution to this is to upgrade to Apache 2.0. I admit that I was reluctant to do this myself, but I found a tutorial from Apple that convinced me to try it. Apache 2.0.61 is the current stable version of the 2.0 series, and that is the version I decided to go with. I downloaded the package and extracted it into /usr/local/src using the following commands.

curl -O http://www.eng.lsu.edu/mirrors/apache/httpd/httpd-2.0.61.tar.gz
tar xvfz httpd-2.0.61.tar.gz
cd httpd-2.0.61
export ac_cv_func_poll=no

Make the following changes to srclib/apr/network_io/unix/sendrecv.c in the apr_socket_send function:

Change:

do {
rv = write(sock->socketdes, buf, (*len));
} while (rv == -1 && errno == EINTR);

To:

try_write: do {
rv = write(sock->socketdes, buf, (*len));
} while (rv == -1 && errno == EINTR);

(That is, add “try_write:” right before the “do {“.)

Replace the following else clause (that is, delete these five lines):

else {
do {
rv = write(sock->socketdes, buf, (*len));
} while (rv == -1 && errno == EINTR);
}

with these two lines:

else
goto try_write;

Run configure, run make, and then run sudo make install to set up Apache 2.0 for installation:

./configure --enable-mods-shared=most --enable-ssl
--with-mpm=worker --without-berkeley-db
make
sudo make install

Create a directory for mod_dav.h, and copy the module to it using the following Terminal commands:

cd /usr/local/apache2
mkdir -p modules/dav/main
cp include/mod_dav.h modules/dav/main/

Once you have it installed, run the following command to start Apache.

sudo /usr/local/apache2/bin/apachectl start

You also need to run the following commands to start Apache 2.0 on each reboot.

sudo -s
mkdir /Library/StartupItems # This directory might already exist
ditto /System/Library/StartupItems/Apache /Library/StartupItems/Apache2
mv /Library/StartupItems/Apache2/Apache /Library/StartupItems/Apache2/Apache2
defaults write /Library/StartupItems/Apache2/StartupParameters Provides -array "Apache2"
perl -p -i -e 's/WEBSERVER/APACHE2/g' /Library/StartupItems/Apache2/Apache2
echo "APACHE2=-YES-" >> /etc/hostconfig

I have a problem now in that turning off and on web sharing in the system preferences will not stop and start Apache 2.0. I am at a loss on how to make this work. I am also unsure whether or not Apache 2.0 will start on reboot or whether I have to start it manually.

Subversion is rather easy to install. I used the tutorial found at hivelogic along with the same one I used to install Apache 2.0. Subversion appears to be installed and working but I haven’t really tested it in depth.

Once I had Apache 2.0 installed, I wanted to be able to access it when I go to http://localhost/ and access the files in /Library/Webserver/Documents. You need to stop Apache and then edit the http.conf file found in /usr/local/apache2/conf/httpd.conf. Then you need to change the DocumentRoot and the Directory.

DocumentRoot "/Library/Webserver/Documents"
<Directory "/Library/Webserver/Documents">

Now you can restart Apache and you should be able to access Apache 2.0 on http://localhost/. However, I found that PHP was no longer working because I had not yet installed it for Apache 2.0. Don’t worry though all you have to do is recompile it. Here are the steps I used.

If you already have the php files on your machine, go into that directory.

cd /usr/local/src/php-4.4.7

If not download it and unpack it again and then go into the directory you just created. Now you need to create the directory to install PHP into.

sudo mkdir /apache2/php

Then all you need to do is configure it with the right options. I used the following commands which adds support for the GD graphics library, in addition to mysql.

./configure --prefix=/usr/local/apache2/php
--with-zlib-dir=/usr/local/lib
--with-jpeg-dir=/usr/local/lib
--with-png-dir=/usr/local/lib
--with-gd --with-mysql=/usr/local/mysql
--with-apxs2=/usr/local/apache2/bin/apxs
make
sudo make install

I ran into problems doing this. I kept getting the error from mysql.

dyld: Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib

I fixed this by copying the file into that path using the following commands.

cd /usr/local/mysql/lib
mkdir mysql
cp libmysqlclient.15.dylib mysql/libmysqlclient.15.dylib

I ran configure, make, sudo make install again, and it worked. An optional step you may want to take is to create a php.ini file. I created the php.ini file with this command.

sudo cp php.ini-dist /apache2/php/lib/php.ini

Now we need to edit the http.conf again so that PHP will run in Apache. Add the following lines in the appropriate sections.

LoadModule php4_module modules/libphp4.so
DirectoryIndex index.html index.php
AddType application/x-httpd-php .php

Now restart Apache 2.0. Once I did that it worked. I now have Apache 2.0, PHP 4.4.7 and Subversion 1.3 installed locally on my Mac.

Disclaimer: Your mileage may vary. This is the compilation of a lot of work over a period of several days. I have tried to recount the steps I used as closely as possible. Use at your own risk. Here are some tutorials you may want to look at if you get stuck. Good luck.

Building and Installing Apache 2.2.4 and PHP 5.1.4 on Mac OS X 10.4.6
Apache 2.0 on Unix systems

How to Restart Apache Locally on Mac OS X

Working with PHP and Apache locally on my Mac, I found myself needing to know how to restart Apache. The method I normally use did not work because for some reason etc/init.d does not exist or could not be found. So I found another command that worked. Simply run the following.

sudo apachectl graceful

graceful reloads the configuration files and gracefully restarts. Any current connections are allowed to complete. That’s it! You just restarted Apache. Easy huh?

Installing GD Graphics Library on Mac OS X

Recently, I needed to install the GD Graphics Library locally on my Mac. GD is an open source code library for the dynamic creation of images by programmers. GD creates PNG, JPEG and GIF images, among other formats. GD is commonly used to generate charts, graphics, thumbnails, and most anything else, on the fly. While not restricted to use on the web, the most common applications of GD involve web site development.

I searched through a lot of tutorials, and found that most of them are out of date and/or incomplete. In spite of this, I finally managed to figure it out. However, I took notes on everything I did so that someone else needing to do this will find this helpful and won’t have to struggle with it as much as I did.

I have to admit I am pretty green when it comes to using command line and installing open source code on my computer. I have learned a lot though and I really enjoy it. I have found that the Mac is a great open source programming environment. Many programmers are figuring this out, and I believe you will continue to see more programmers coming to the Mac platform.

Anyway, these instructions, as straightforward as they are, still assume you know the basics of how to install and run open source code on your computer. If you don’t, you need to go read more and then come back here. Now, let’s begin.

The first thing you need to do is download the packages you will need. Here is the commands you will use for the most current versions of these packages as of this posting.

curl -O http://www.zlib.net/zlib-1.2.3.tar.gz
curl -O http://superb-east.dl.sourceforge.net/sourceforge/libpng/libpng-1.2.20.tar.gz
curl -O ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz
curl -O http://download.savannah.gnu.org/releases/freetype/freetype-2.3.5.tar.gz
curl -O http://www.libgd.org/releases/gd-2.0.35.tar.gz

Download these packages to a directory on your hard drive. The next thing you need to do is unpack these directories. Running this command will unpack all the directories you just downloaded.

ls *gz | xargs -n 1 tar zxvf

First, we will install zlib by running the commands below.

cd zlib-1.2.3
./configure --shared
make
sudo make install

Next, install libpng by running the commands below.

cd ../libpng-1.2.20
cp scripts/makefile.darwin Makefile

Open up Makefile in your favorite text editor and edit the file to read as follows.

# Where the zlib library and include files are located
ZLIBLIB=/usr/local/lib
ZLIBINC=/usr/local/include
#ZLIBLIB=../zlib
#ZLIBINC=../zlib

Then finish the install.

make
sudo make install

Test this by running the following code.

export srcdir=.
./test-pngtest.sh

Next install libjpeg.

cd ../jpeg-6b/
ln -s `which glibtool` ./libtool
export MACOSX_DEPLOYMENT_TARGET=10.4
./configure --enable-shared
make
sudo make install

Now install freetype2.

cd ../freetype-2.3.5

Open include/freetype/config/ftoption.h in your favorite editor and uncomment line 461 to read as follows.

#define TT_CONFIG_OPTION_BYTECODE_INTERPRETER

Then complete the install.

./configure
make
sudo make install

Finally install GD.

cd ../gd-2.0.35
ln -s `which glibtool` ./libtool
./configure

The report looked like this when I did it.

Support for PNG library: yes
Support for JPEG library: yes
Support for Freetype 2.x library: yes
Support for Fontconfig library: no
Support for Xpm library: no
Support for pthreads: yes

Then finish the install.

make
sudo make install

You can test it with the following code.

./gdtest test/gdtest.png
open test/*.jpg

To add support for Fontconfig, run this command.

export GDFONTPATH=$HOME/Library/Fonts:/Library/Fonts:/System/Library/Fonts

To complete the installation, you need to recomple PHP with all the libraries you just installed. Here is how I did it.

./configure
--with-zlib-dir=/usr/local/lib
--with-jpeg-dir=/usr/local/lib
--with-png-dir=/usr/local/lib
--with-gd
--with-mysql=/usr/local/mysql
--with-apxs

make
sudo make install

Now you should have PHP with GDLIB installed. That’s it. You’re done! Now, on to your next project!