In the last weekend, I have tried to install Apache, PHP, MySQL and phpMyAdmin on my MacBook with Mac OS X 10.4(Tiger). Except for MySQL, all of them have no one-click installation package, which means I need to compile and build them from source. In fact, I have installed the Ruby, Gems and Ruby on Rails in the same way before, following the well-documented guide written by Hivelogic. The guide has detailed explanation for every step during the installation. Although the installation are all done in command line, the process is smooth and comfortable without any headache. This time, due to curiosity, I try to build and install the open source software mentioned above. There are many tutorials online teaching you how to install them, but none of them are tailor-made for the Mac OS X 10.4(Tiger). After spending two days in dealing with the installation, with many problems during the compilation and runtime, I finally get them installed. I put down the steps below, which serves as a reference for myself.
Get Started
You need to get the following items prepared before you start the installations.
- Mac OS X 10.4(Tiger)
- Xcode 2.5
- Apache 2.2.11
- MySQL 5.1.35
- gettext 0.17
- libmcrypt 2.5.8
- mcrypt 2.6.8
- mhash 0.9.9.9
- PHP 5.3.0
- phpMyAdmin 3.2.0.1
Note: You probably cannot find the download link of Xcode for Mac OS X 10.4(Tiger) under the homepage of Xcode. You need to get a free ADC membership account and login to the ADC Member Site. Under the "Downloads" section, you will find the older versions of Xcode for Tiger. The version is 2.5 in this case.
Note: The version numbers are for reference only. They will be updated on regular basis.
Preparing the Directory
As the guide of Hivelogic has suggested, all the download files are recommended to be kept in one directory for better management. In the UNIX world, the software are installed under /usr/local. Therefore, it is a good idea to keep the source files in it. This can be done by:
sudo mkdir -p /usr/local/src
sudo chmod 777 /usr/local/src
cd /usr/local/srcThe first command creates a folder called src under /usr/local, and then change its privilege so that everyone can read and write on it. Finally, we change to the directory we have just created. The following steps assume that the source files are in this directory.
MySQL and Unicode
Installing MySQL is easy, there is an installer package which can be downloaded from MySQL's homepage. Just to remind that you have to download the version for Tiger, the one I have installed is named mysql-5.1.35-osx10.4-i686.dmg. There are 3 files in the dmg file:
So far, the MySQL server is set up and can be use immediately. However, it is not secure since the root password is missing. The root password can be set in this way in the terminal:
mysql -u root password 'yourpwd'
If you really care about your data privacy, you can read about MySQL usernames and passwords.
If you need to handle unicode in MySQL, there are some reminders. MySQL has flexible(complex in my point of view) control over the encoding of the databases and tables. It is called character set and collations. Your MySQL server setting can be checked by typing the following command in the Terminal:
mysql -u root -p
(Enter your password and enter the MySQL command line mode)
show variables like '%character%';
The following table will be shown:
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
+--------------------------+--------+
The default setting of the MySQL is latin1. For better handling of unicode, you need to add a config file in the directory /etc so that MySQL will change its setting automatically. Use your favorite text editor to create or edit the file my.cnf in /etc and paste the settings below.
#setting for the database
[mysqld]
default-character-set=utf8
#settings for clients
[mysql]
default-character-set=utf8
The [mysqld] option sets the database to use utf8 as default. and the [mysql] option sets the default encoding for client connections. After saving the file, please restart the MySQL server to take effect.
About Ruby and MySQL
As I have installed Ruby before, I would like to install the MySQL Gem to let Ruby accessing the MySQL databases. An extra parameter is needed to install the MySQL Gem:
sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql
Notified by Hivelogic, there is an issue with MySQL and Ruby. The suggested solution is:
sudo install_name_tool -change /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib /usr/local/mysql/lib/libmysqlclient.15.dylib /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle
Apache
Mac OS X 10.4(Tiger) shipped along with Apache of version 1.3. It is called "Personal Web Sharing" in the Sharing option in the System Preference panel. To upgrade Apache, it is recommended to switch off the original Apache first.
In /usr/local/src, type, or copy, the following command in Terminal:
cd httpd-2.2.11
./configure --prefix=/usr/local/apache --enable-so --enable-cgi --enable-info --enable-rewrite --enable-speling --enable-usertrack --enable-deflate --enable-ssl --enable-mime-magic
make
sudo make install
The second command is the configuration for Apache, the detailed documentation can be found using ./configure --help, or read the "Compiling and Installing" section in the Apache Documentation. The last command actually installs Apache on your system. The command sudo means you run that line of command as root, which has the highest privilege.
As said before, Mac OS X 10.4(Tiger) has its old version of Apache. In order to replace the existing one, type the following commands in the Terminal:
cd /usr/sbin/
mv apachectl apachectl-1.3
ln -s /usr/local/apache/bin/apachectl apachectl
The above commands rename the old Apache apachectl to apachectl-1.3, and then replace it by a link using the Apache we have just installed.
The Apache is now installed. You can start your server by typing:
sudo apachectl -k start
We use sudo command because only the root can bind the port 80. To checkif your server is running correctly, open http://localhost in your browser. You will see a page with a text "It works!". This is the default page of Apache 2, which is stored in the directory /usr/local/apache/htdocs
Preparing the files for PHP
In order to let both PHP and phpMyAdmin run smoothly, libraries and files are needed to be prepared and installed. The steps are as followed, remember, it is assumed that the current directory is in /usr/local/src.
cd gettext-0.17
./configure
make
sudo make install
cd libmcrypt-2.5.8
./configure --disable-posix-threads
make
sudo make install
cd mhash-0.9.9.9
./configure
make
sudo make install
cd mcrypt-2.6.8
./configure
make
sudo make install
Fix: Errors will be shown on Mac OS X 10.4(Tiger) when making the mcrypt. The error messages are:
rfc2440.c:26:20: error: malloc.h: No such file or directory
...
make[1]: *** [rfc2440.o] Error 1
make: *** [install-recursive] Error 1
The error is caused by missing the file malloc.h, since it is not located in the default location. In order to fix the problem, edit the file rfc2440.c in /usr/local/src/mcrypt-2.6.8/src and change the line 26 from:
#include <malloc.h>
to:
#include <sys/malloc.h>
PHP
When you have finished the steps above, you can finally install PHP. The commands are:
cd php-5.3.0
./configure --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql --prefix=/usr/local/apache/php --with-config-file-path=/usr/local/apache/php --enable-force-cgi-redirect --disable-cgi --with-zlib --with-gettext --with-mcrypt=/usr/local/bin/mcrypt --enable-mbstring
make
sudo make install
The second command is the configuration for PHP, the detailed documentation can be found at the "Installation and Configuration" section in the PHP Manual. The following is just a few explanation of the configuration.
You need to configure Apache to load the PHP module and recognize PHP files. This is done by modifying the file httpd.conf of Apache:
#Comment these two lines
LoadModule php4_module modules/libphp4.so
#AddHandler php-script php
#Add these two line
LoadModule php5_module modules/libphp5.so
AddHandler php5-script php
# Add index.php to your DirectoryIndex line:
DirectoryIndex index.html index.php
AddType text/html php
# PHP Syntax Coloring
AddType application/x-httpd-php-source phps
Remeber to restart your Apache server after the above change to take effects.
sudo apachectl -k restart
At last, the PHP is installed. You can verify your setup by saving a file named info.php in your web server content directory(e.g /usr/local/apache/htdocs). The file info.php should have content like this:
<?php phpinfo(); ?>
Open your browser and type in http://localhost/info.php to view your settings.
phpMyAdmin
phpMyAdmin is a GUI for managing MySQL databases. Download it from its homepage and rename the extracted folder to phpMyAdmin. After completing all the above installations, you can just put the folder phpMyAdmin in your web server content directory(e.g /usr/local/apache/htdocs). Open your browser and type in http://localhost/phpMyAdmin, you will see the index page for you to setup.
Last Words
Setting the server is just a start, they are tools for my final year project in my university. This article is a milestone for my project development. It would be my honor if visitors find it useful. Although some tricks are for Mac OS X 10.4(Tiger) only, the ideas of installation is generally the same on any UNIX systems.
Reference
Building Ruby, Rails, Subversion, Mongrel, and MySQL on Mac OS X
Apache 2 and PHP 5 (mod_php) on Linux
OS X Server 10.4.* (Tiger), Apache2, PHP5, and PostGres for the Mac
Mac OS X 10.4 << {Ruby, Rails, MySQL, Mongrel} installation script
MySQL中文亂碼解決方案