Copyright, 1999, by David Medinets
http://www.affy.com

Back to Table of Contents

Installing PHP

This chapter shows you how to compile and install the gcc C compiler, the MySQL database and the Apache Web server with the PHP module. If you're not sure which components you need on your computer, install them all.

The installation instructions in this chapter are based on brand-new installations of Red Hat Linux v5.2 and v6.0. Because the C compiler installed with v5.2 wasn't new enough to perform all of the compilations needed to get PHP to work, I downloaded gcc v2.8.1 from the Internet and included compilation instructions for that in this chapter as well.

The steps shown in this chapter will compile and install the following software:

Although, this chapter does refers to the commands needed to compile applications they are only briefly described. After all, the focus of this book is on the PHP language and not the C language. Each of the applications compiled cleanly for me, and I hope you have the same good luck. If you do run into problems, you can ask questions in an IRC channel (Be Polite!) or you might try one of the Usenet newsgroups (reachable via http://www.dejanews.com) to get help about how to solve the problem. However, the best help comes from the PHP mailing lists at http://www.php.net.

Before beginning the compilations, let's talk about how to recover from mistakes. After getting help regarding the problem, use the following commands to reinitialize the source directories:

Some of the programs you're installing may not support each of these commands, but trying them causes no harm.

Red Hat v5.2 uses the glibc library. If you download other programs or updates of the ones you install in this chapter from the Internet, you'll need to know this piece of information. You can find out which version of the glibc library you have installed using the rpm -q glibc command. On my system, this command displays glibc-2.0.7.29.

Basic Concepts

If you've never compiled a Linux application before, you'll want to familiarize yourself with several concepts so that you'll have a chance to diagnose problems when they arise.

tar

tar, or tape archiver, combines multiple files into one and optionally compresses files to reduce their size. It was originally intended to create backups that could be stored on tape. When tar files are compressed, they have an extension of .gz; otherwise their extension is .tar.

gcc

gcc is the C compiler from GNU. Its job is to compile source code (human-readable) files into object (machine-readable) files. C source files typically have a .c extension. Object files typically have a .o extension. If the compile doesn't work properly, you run into compile-time, or syntax, errors. Most of the time, incomplete compiles are caused by the compiler not being able to find one or more header files. Header files have an .h extension and are used to define various system-specific information and to collect information used by multiple .c files into one location.

make

make is a utility program that aids in compiling. Its job is to compile only those source files that haven't yet been compiled. Compiling a .c file results in the creation of an .o file. If the .c file is newer than the .o file (that is, it has been edited since the last compilation), then make recompiles that .c file. make typically looks for a Makefile from which to read its instructions. Makefiles can contain one or more targets that can be executed. For example, make clean tells make that you want to execute the clean target.

ld

ld is the linker program from GNU. Its job is to stitch together all of the object files and libraries to create a single executable. Hopefully, you'll never have to run this program manually because the Makefile will take care of all of the compilation details.

ldconfig

ldconfig searches the various library directories (as specified in /etc/ld.so.conf) for shared libraries. Shared libraries are used by more than one application and have .so somewhere in their filenames. For example, libqt.so.1.42 is a shared library. After every compilation, you might need to append a directory to the /etc/ld.so.conf file and run the ldconfig -v command.

./configure

configure searches your computer looking for key information such as which C compiler is installed and where your header files are located. It then modifies Makefiles as needed to target your computer configuration. configure is nearly always executed using ./configure so that you can run the executable from the current directory instead accidentally running it from some other directory located in your $PATH environment variable.

Symbolic Links

Symbolic links let you refer to an existing file by a different name. For example, you might want to refer to libqt.so.1.42 as libqt.so.1. A symbolic link essentially lets you copy a file to a different directory and name without really doing the copy. The second instance of the file simply "points" to the first instance. You'll find at least two advantages gained by using symbolic links; The first is that symbolic links take up less hard disk space - perhaps as little as 16 bytes. The second is a bit more subtle. Let's say that you have a symbolic link called libqt.so.1 that points to libqt.so.1.42. What if you need to upgrade to libqt.so.1.88? You could simply change the symbolic link so that libqt.so.1 points to libqt.so.1.88 instead of libqt.so.1.42. This means programs that reference libqt.so.1 automatically start to use the newer version of the library. By convention, symbolic links are used for whole number releases. In other words, libqt.so.1.88 and libqt.so.2.32 would have different symbolic links (libqt.so.1 and libqt.so.2 respectively).

Preparation for Compilation

The following steps prepare your system for the compilations:

  1. mkdir /usr/local/src - I use the /usr/local tree for all applications that I install to my system. Other people might use /usr/opt, or /opt, or /var. In order for you to easily follow the commands in this chapter, stick with the /usr/local directory tree. You can copy files to a different location after the compilations are finished.

    Note: You'll need to be signed in as root in order to perform the steps in this chapter.

  2. cd /usr/local/src - Connect to the source directory.

  3. Download the following files (from http://www.mtolive.com/phpbook) or copy them from the CD that comes with this book into /usr/local/src:

  4. Use the tar command to uncompress the files. The x option tells tar to extract. The v option tells tar to display the filenames that are being pulled from the archive. The z option tells tar to uncompress the files. And the f option tells tar you are specifying the .tar file on the command line.

  5. gcc -v - Determines which version of gcc your system currently has. If you don't have v2.7.2.3, replace "2.7.2.3" shown in the next step with the number of your current gcc version.

  6. cp `which gcc` /usr/bin/gcc-2.7.2.3 - Copies the existing gcc executable file so that you have it, if needed, in the future. One of the advantages of Linux is that you can easily store multiple versions of a program in your directories.

  7. httpd -v - Determines which version of Apache is loaded on your system. If you don't have v1.3.3, replace "1.3.3" shown in the next step with your current Apache version.

  8. mv `which httpd` /usr/sbin/httpd-1.3.4 - Copies the existing Apache executable file so that you have it, if needed, in the future.

Compiling gcc, the C Compiler

The first program you should compile and install is the C compiler. The C compiler that comes with Red Hat v5.2 is gcc v2.7.2.3 which is not current enough to compile PHP correctly. However, gcc v2.7.2.3 can be used to compile gcc v2.8.1, which is current enough.

To compile the new version of gcc.

  1. cd /usr/local/src/gcc-2.8.1 - Connects to the top-level gcc directory.

  2. ./configure --prefix=/usr/local/gcc - Runs the configuration program and forces it to setup the installation so that gcc is installed to /usr/local/gcc.

  3. make bootstrap LANGUAGES="c c++" BOOT_CFLAGS="-g -O2" - Compiles the new C and C++ compilers.

  4. make install LANGUAGES=?c c++" BOOT_CFLAGS=?-g -O2" - Installs the new C and C++ compilers.

  5. mv /usr/local/gcc/bin/gcc /usr/local/gcc/bin/gcc-2.8.1 - Renames the new gcc compile to include the version number in its name.

  6. ln -s /usr/local/gcc/bin/gcc-2.8.1 /usr/bin/gcc - Creates a symbolic link from /usr/bin/gcc to the newly compiled gcc executable.

  7. gcc -v - Displays the version number. If the compile and installation worked, v2.8.1 should display.

Compiling MySQL

Now it's time to compile MySQL. After the compilation, you'll be able to test the installation with MySQL utility programs.

  1. cd /usr/local/src/mysql-3.22.16a-gamma - Connects to the top-level MySQL directory.

  2. ./configure --prefix=/usr/local/mysql - Runs the configuration program and force it to setup the installation so that MySQL is installed to /usr/local/mysql.

  3. make - Compiles MySQL.

  4. make install - Installs MySQL.

  5. echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf - Appends the MySQL library to the configuration file of the ldconfig command. The directories in this configuration file are searched (for library files) when Linux starts or the ldconfig command is run.

  6. ldconfig -v | grep libmysqlclient - The ldconfig command reads the directories listed in the /etc/ld.so.conf file and caches all of the libraries found. The grep command searches the large amount of output from the ldconfig command for the MySQL library and limits the text displayed to something like: libmysqlclient.so.6 => libmysqlcient.so.6.o.o.

  7. echo "/usr/local/mysql/bin/safe_mysqld > /dev/null &" >> /etc/rc.d/rc.local - Appends the MySQL startup command to the /etc/rc.d/rc.local file so that MySQL is automatically started when Linux starts.

  8. ./scripts/mysql_install_db - Initializes the database.

  9. /usr/local/mysql/bin/safe_mysqld > /dev/null & - Starts the MySQL server as a background process. The server must be started so that you can test the installation.

  10. ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql - I like to form symbolic links from the installation location to the /usr/bin directory. This techniques reduces the number of directories in the PATH environment variable. It also hides any MySQL utility you don't want normal users running (for example, the mysqladmin command). Another technique is to place the command PATH="$PATH:/usr/local/mysql/bin" into the /etc/profile file. Either way works.

  11. ln -s /usr/local/mysql/bin/mysqlshow /usr/bin/mysqlshow - This command lets normal users run the mysqlshow comand.

Testing MySQL

Before continuing forward with the Apache and PHP compilation, take the time to test your installation of MySQL. Many utilities come with the MySQL distribution, but we'll only use the mysql and mysqlshow commands to test with. (If you're not familiar with databases, don't worry. Database concepts such as users, tables, and records are covered in Chapter 6, "Databases and SQL.")

The mysqladmin utility lets you create and delete databases, check the status of MySQL, and many other things. First, make sure you've installed it correctly by checking the version:


> PATH="/usr/local/mysql/bin:$PATH"
> mysqladmin version
Ver 7.8 Distrib 3.22.16a-gamma, for pc-linux-gnu on i686
TCX Datakonsult AB, by Monty

Server version		3.22.16a-gamma
Protocol version	10
Connection		Localhost via UNIX socket
UNIX socket		/tmp/mysql.sock
Uptime:			2 hours 30 min 39 sec

Threads: 1  Questions: 7  Slow queries: 0  
Opens: 6  Flush tables: 1  Open tables: 2

You can view all the features of mysqladmin with this command:

mysqladmin --help | less

Perhaps a more exciting utility is mysqlshow which displays a list of databases, tables, and fields.


> PATH="/usr/local/mysql/bin:$PATH"
> # With no arguments, a list of databases is displayed.
> mysqlshow
+-----------+
| Databases |
+-----------+
| mysql     |
| test      |
+-----------+
> # When a database is specified, you need to pass through
> # security. The result is the display of all tables 
> # located in that datbase.
> mysqlshow -h mtolive.com -u root -p mysql
Enter password: password
Database: mysql
+-----------------+
|     Tables      |
+-----------------+
| active_sessions |
| auth_user       |
| columns_priv    |
| db              |
| func            |
| host            |
| tables_priv     |
| user            |
+-----------------+
> # When both a database and a table are specified,
> # the fields of the table are displayed.
> mysqlshpw -h mtolive.com -u root -p mysql user
Enter password: password
Database: mysql  Table: user  Rows: 5
+----------------+--------------+----+----+-----+-----+
|Field           |Type          |Null|Key |Def. |Extra|
+----------------+--------------+----+----+-----+-----+
|Host            |char(60)      |    |PRI |     |     |
|User            |char(16)      |    |PRI |     |     |
|Password        |char(16)      |    |    |     |     |
|Select_priv     |enum('N','Y') |    |    |N    |     |
|Insert_priv     |enum('N','Y') |    |    |N    |     |
|Update_priv     |enum('N','Y') |    |    |N    |     |
|Delete_priv     |enum('N','Y') |    |    |N    |     |
|Create_priv     |enum('N','Y') |    |    |N    |     |
|Drop_priv       |enum('N','Y') |    |    |N    |     |
|Reload_priv     |enum('N','Y') |    |    |N    |     |
|Shutdown_priv   |enum('N','Y') |    |    |N    |     |
|Process_priv    |enum('N','Y') |    |    |N    |     |
|File_priv       |enum('N','Y') |    |    |N    |     |
|Grant_priv      |enum('N','Y') |    |    |N    |     |
|References_priv |enum('N','Y') |    |    |N    |     |
|Index_priv      |enum('N','Y') |    |    |N    |     |
|Alter_priv      |enum('N','Y') |    |    |N    |     |
+----------------+--------------+----+----+-----+-----+
> # When a database, a table, and a field are specified,
> # the field information is displayed.
> mysqlshpw -h mtolive.com -u root -p mysql user User
Enter password: password
Database: mysql  Table: user  Rows: 5  Wildcard: User
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| User  | char(16) |      | PRI |         |       |
+-------+----------+------+-----+---------+-------+

The last MySQL utility we'll look at for now is mysql. This utility plugs you into the heart of MySQL and gives you the ability to execute an SQL statement right from the Linux command prompt. You can also run mysql in a shell mode.


> # Show the host/user combinations that MySQL 
> # knows about using the Linux command line.
> mysql -h mtolive.com -u root -p -e "select host,user from user" mysql
Enter password: password
+-------------+---------+
| host        | user    |
+-------------+---------+
| localhost   | root    |
| mtolive.com | root    |
| localhost   |         |
| mtolive.com |         |
| mtolive.com | webuser |
+-------------+---------+
> # Show the host/user combinations that MySQL 
> # knows about using the mysql command line.
> mysql -h mtolive.com -u root -p
Enter password: password
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 45 to server 
version: 3.22.16a-gamma

Type 'help' for help.

mysql> use mysql;
Database changed
mysql> select host,user from user;
+-------------+---------+
| host        | user    |
+-------------+---------+
| localhost   | root    |
| mtolive.com | root    |
| localhost   |         |
| mtolive.com |         |
| mtolive.com | webuser |
+-------------+---------+
5 rows in set (0.00 sec)
mysql> # If you want to change the password 
mysql> # for a user, use the following SQL
mysql> # statement.
mysql> update user \
    -> set password=password('password') \
    -> where user='root';
Query OK, 2 rows affected (0.02 sec)
Rows matched: 2  Changed: 2  Warnings: 0
mysql> exit

Caution: Always use the password() function when setting passwords. Check the MySQL documentation for the details.

Caution: Before running your system in production mode, choose a better root password than "password"!

Compiling iODBC and MyODBC

iODBC is a library of functions that implement the Open Database Connnectivity protocol. It is mainly used to connect to database engines running on Microsoft Windows.

  1. cd /usr/local/src/libiodbc-2.50.3 - Connects to the iODBC directory.

  2. ./configure --prefix=/usr/local/iodbc --with-iodbc-inidir=/usr/local/etc - Runs the configuration program and forces it to setup the installation so iODBC is installed to /usr/local/iodbc. Additionally, make sure that the odbc initialization file is called /etc/odbc.ini.

  3. make

  4. make install - Libraries are copied into /usr/local/iodbc/lib and include files are copied into /usr/local/iodbc/include.

  5. cd /usr/local/src/myodbc-2.50.24 - Connects to the MyODBC directory.

  6. ./configure --prefix=/usr/local/myodbc --with-mysql-sources=/usr/local/src/mysql-3.22.16a-gamma --with-odbc-ini=/etc/odbc.ini --with-iodbc=/usr/local/iodbc - Runs the MyODBC configuration program.

  7. make

  8. make install - Libraries are copied into /usr/local/myodbc/lib.

Compiling PHP

Compiling PHP is more complex than the previous applications because PHP is really a combination of expat, Apache, and PHP. The end result of this compilation is a version of Apache with PHP built-in. To compile PHP, use the following commands:

  1. cd /usr/local/src/expat - Connects to the expat directory.

  2. make - Compiles the expat source files.

  3. Append the following lines to the Makefile file. Make sure that you use the Tab key before entering the ar and ranlib lines.

    libexpat.a: $(OBJS)
      ar -rc $@ $(OBJS)
      ranlib $@

  4. make libexpat.a - Combines the expat object files into a library file.

  5. mv libexpat.a /usr/local/lib - The PHP configuration program knows to look for the libexpat.a file in the /usr/local/lib directory. Moving the file to a known location now, saves you trouble later.

  6. cd /usr/local/src/php-3.0.11 - Connects to the top-level php directory.

  7. mkdir /usr/local/include/xml - Make sure the /usr/local/include/xml directory exists.

  8. ln -s /usr/local/src/expat/xmltok/xmltok.h /usr/local/include/xml/xmltok.h - Why copy when you can create a symbolic link?

  9. ln -s /usr/local/src/expat/xmlparse/xmlparse.h /usr/local/include/xml/xmlparse.h - This is another header file that PHP needs in order to compile correctly.

  10. cd /usr/local/src/apache_1.3.4 - Connects to the top-level Apache directory.

  11. ./configure --prefix=/usr/local/apache - Runs the configuration program and force it to setup the installation so Apache is installed to /usr/local/apache.

  12. cd /usr/local/src/php-3.0.11 - Connects to the top-level PHP directory.

  13. ./configure --with-apache=../apache_1.3.4 --with-iodbc=/usr/local/iodbc --with-mysql=/usr/local/mysql --with-xml - Runs the configuration program and tells it to include support for Apache, MySQL, and XML.

  14. make - Compiles the PHP source files.

  15. make install - Installs the compiled files. The PHP library file gets placed into the Apache modules directory so it can be found when you compile Apache.

  16. cd /usr/local/src/apache_1.3.4 - Connects to the top-level Apache directory.

  17. ./configure --prefix=/usr/local/apache --activate-module=src/modules/php3/libphp3.a - Configures Apache a second time. This time tells Apache to load the PHP module.

  18. make - Compiles the Apache source files.

  19. make install - Installs the compiled files.

  20. mv /usr/local/apache/bin/httpd /usr/local/apache/bin/httpd-1.3.4 - Renames the newly created httpd executable so that you can have multiple versions installed.

  21. ln -s /usr/local/apache/bin/httpd-1.3.4 /usr/sbin/httpd - Creates a symbolic link to the new executable.

  22. httpd -v - Verifies that you can access the new executable. The result of this command should reflect version 1.3.4 and the build date should be correct.

  23. Edit the /usr/local/apache/conf/httpd.conf file. Search for AddType and make sure the following lines are uncommented:

    AddType application/x-httpd-php3 .phtml
    AddType application/x-httpd-php3 .php3
    AddType application/x-httpd-php3-source .phps

  24. Continue editting the /usr/local/apache/conf/httpd.conf file. Search for DirectoryIndex and append 'index.php3' to the end of the line.

  25. Create a file called /usr/local/lib/php3.ini that contains the following lines:

    include_path=.:/usr/local/apache/php/
    auto_prepend_file=/usr/local/apache/php/prepend.php3
    track_vars = on
    magic_quotes_gpc = on
    sendmail_path /usr/sbin/sendmail -t

  26. ln -s /usr/local/src/php-3.0.11/doc/manual.html /usr/local/src/php-3.0.11/doc/index.html - Creates a symbolic link so most web browsers automatically display the correct start page of the PHP documentation.

  27. ln -s /usr/local/src/php-3.0.11/doc /usr/local/apache/htdocs/phpdocs - Creates a symbolic link so you can access the PHP documentation via http://localhost/phpdocs/.

  28. Create a file called /usr/local/apache/htdocs/robots.txt containing the following lines so that search engines avoid indexing your PHPLIB and phpMyAdmin files and PHP docmentation.

    #robots.txt for {hostname}
    User-agent *
    Disallow: /phpdocs/
    Disallow: /php/
    Disallow: /phpMyAdmin/

Installing PHPLIB

PHPLIB needs to be installed before you read Chapter 15, "Managing Concurrent Access". Follow these steps:

  1. Login as the root user, or any other user that can write files in the /usr/local/apache directory.

  2. cd /usr/local/apache/ - Connects to the web server root directory before beginning the download.

  3. Download the latest version from the following web site. Take notice of the gz file's name just in case it has been changed from phplib.tar.gz.

    http://phplib.shonline.de/

  4. tar xvzf phplib.tar.gz - Uncompresss the PHPLIB module.

  5. Edit the /usr/local/lib/php3.ini file so the following lines are included:

    include_path=.:/usr/local/apache/phplib-6.1/php
    auto_prepend_file = /usr/local/apache/phplib-6.1/php/prepend.php3
    track_vars = on
    magic_quotes_gpc = on
    sendmail_path /usr/sbin/sendmail -t

  6. Create a mysql database called poe_sessions. I used phpMyAdmin, but you can issue SQL commands if you'd prefer.

  7. cd /usr/local/apache/phplib-6.1/stuff - Connect to the directory of table creation scripts.

  8. mysql php_book --user=root --password < create_database.mysql - Creates the database tables needed by PHPLIB.

  9. Create a new record in the user table of the mysql database using these values

    host: %
    password:          <-- no password.
    select_priv: Yes
    insert_priv: Yes
    update_priv: Yes
    delete_priv: Yes

    for users named "kris", "user01", and "user02".

    Note: You can use the following SQL:

    INSERT INTO 
      user 
    (
      Host
     ,User
     ,Password
     ,Select_priv
     ,Insert_priv
     ,Update_priv
     ,Delete_priv
    )
    VALUES (
      '%'
     ,'kris'
     ,''
     ,'Y','Y','Y','Y'
    )

  10. Create a new record in the db table of the mysql database using these values

    host: %
    db: poe_sessions
    select_priv: Yes
    insert_priv: Yes
    update_priv: Yes
    delete_priv: Yes

    for users named "kris", "user01", and "user02".

    Note: You can use the following SQL:

    INSERT INTO 
      db 
    (
      Host
     ,Db
     ,User
     ,Select_priv
     ,Insert_priv
     ,Update_priv
     ,Delete_priv
    )
    VALUES (
      '%'
     ,'poe_sessions'
     ,'kris'
     ,'Y', 'Y', 'Y', 'Y'
    )

  11. /usr/local/mysql/bin/mysqladmin -u root -p reload - reload the MySQL privledge tables.

  12. Create two PHPLIB authorized users (user01 and user02) using the following SQL in the php_book database:

    INSERT INTO 
          auth_user 
        (
          uid
         ,username
         ,password
         ,perms
        ) VALUES (
         'c14cbf141ab1b7cd009356f555b1234'
         ,'user01'
         ,'test'
         ,'admin')
    
        INSERT INTO 
          auth_user 
        (
          uid
         ,username
         ,password
         ,perms
        ) VALUES (
         'c14cbf141ab1b7cd009356f555b3241'
         ,'user02'
         ,'test'
         ,'admin')

  13. mv /usr/local/apache/phplib-6.1/pages /usr/local/apache/htdocs/ - Moves the demonstration directory under the web server's root directory so they can be accessed via a browser.

  14. Edit your /usr/local/apache/htdocs/robots.txt file to include the following line:

    Disallow: /phplib/

  15. Use a web browser to connect to http://localhost/phplib/. You should see a page like Figure 2.1. Figure 2.1 - The PHPLIB demo pages after reloading twice.

Testing ODBC on Linux

Running basic tests on your installed software is important. Especially when you need to hand-edit configuration files. The following test ensures your MyODBC driver is working and the iODBC library compiled correctly.

  1. Create a file called /usr/local/etc/odbc.ini using the contents in Listing 2.1. Listing 2.1 - /usr/local/etc/odbc.ini - The system-wide ODBC configuration file.

    ;
    ; odbc.ini
    ;
    
    [ODBC Data Sources]
    mysql = mysql
    
    [php_book]
    driver = /usr/local/myodbc/lib/libmyodbc.so
    host = localhost
    database = mysql
    user = root

  2. cd /usr/local/src/libiodbc-2.50.3/samples

  3. ./odbctest - runs the ODBC testing program.

  4. DSN=mysql;PWD=password - Tells the testing program which data source you want to connect to.

  5. select host,user from user - Executes an SQL staement at the prompt. If you get a response then iODBC and MyODBC are working.

ODBC is now installed and working on your computer. I felt including installation and testing instructions in this chapter was important because the steps weren't obvious and the documentation was sparse. However, ODBC is mainly a Microsoft technology and rarely used on Linux. So this is the last you'll hear about it in this book.

Summary

This chapter provided the steps needed to get PHP working on your computer. You compiled a special version of the Apache Web server with PHP built into it. You also installed PHPLIB which will be used in Chapter 15, "Interlude: Managing Concurrent Access."

The next chapter, "Manipulating PHP Data," looks at how PHP handles variable data and the operators used to affect that data.

Copyright, 1999, by David Medinets
http://www.affy.com