PHP & Apache Intro
Written By: Mike Fetherston - twist@accglobal.net
Introduction:
PHP is a great embedded programming language for the Internet. Using PHP
you can do a great deal of things such as:
- Access databases
- Run system commands
- Perform file I/O
- Create dynamic images
It's very powerful C-like language
and is very easy to understand if you have any programming background.
You could compare PHP with the likes of ColdFusion and ASP. Although, I
feel that PHP has much more functionality and flexibility. Oh, it's free
too.
This NHF quickly outlines what you need and the steps to take to install
PHP3 with Apache. I will install some functions that you may
not need (i.e.gd and mcrypt). I have found that it's much
easier to get everything installed with one fell swoop than having to redo
PHP and Apache each time you want to add a feature. In other words, read
what the ./configure --help PHP script
produces. I always install from source, so this NHF reflects that (plenty
of compiling).
This NHF is aimed at newbies with moderate Linux experience. You need to
understand how to move around your filesystem, perform basic file
operations, and compile/install programs. If you don't, I strongly
suggest you read some of the other NHF's aimed at fresh newbies and come
back to this.
Prerequisites:
Before you start anything there are a few programs and libraries you need
to install. You don't need every one of them, but so far I've found that
these are the ones that I needed (many compiles later). Here are the
packages and their respective web sites:
[Required] PHP3
http://www.php.net/
The PHP3 package is required. This is why I'm writing this NHF.
[Required] Apache
http://www.apache.org/
PHP will not work without a webserver, and what better
webserver to use besides Apache?
[Optional] MySQL
http://www.mysql.com/
MySQL can be used if you want to provide access to databases on the web.
There are many uses and it is worth the install. You can use databases for
many things, but one advantage is that it's much easier to write to a
database than a plain text or binary file. It's also much more robust and
simpler to perform searching in a database (using SQL commands).
[Optional] GD http://www.boutell.com/gd/ [Optional] zlib
http://www.cdrom.com/pub/infozip/zlib/
[Optional] libPNG
http://www.cdrom.com/pub/png/
The GD package is used to allow you to dynamically create images on the
fly with PHP. If you don't plan on doing any charting or anything at all
with dynamic images, don't install it. If you think that maybe you
might do something like that, install it. GD depends on ZLib and
LibPNG. You must install ZLib and LibPNG before you install GD.
[Optional] libmcrypt ftp://argeas.cs-net.gr/pub/unix/mcrypt/
Libmcrypt is used to encrypt and decrypt strings. This is very useful for
keeping the contents of files secure from prying eyes or for user
authentication. On a side note, when you download libmcrypt grab the
2.2.x series. I had real trouble with the 2.3.x tree. I think that
libmcrypt follows the same versioning scheme as Linus' kernel (i.e.
2.odd.x = beta, 2.even.x = stable)
Installation:
Make a temporary directory where you can work with your files. I like to use /temp.
From there extract all your downloaded files with tar zxvf
<filename>.tar.gz. It's beyond the scope of this NHF to cover the
configuration and install of all the other packages, but this is the order in which I
suggest you install them:
- MySQL
- LibPNG
- ZLib
- GD
- Libmcrypt
Now we can get on with the "real" stuff. The build of PHP and
Apache is a simultaneous process. One depends on the other, so read these
instructions carefully. If you've chosen not to use any of the optional components,
you can skip the respective options in blue. Here are the steps to take to build an
Apache web server with PHP integrated:
- cd apache_1.3.x
- ./configure
- cd ../php-3.0.x
- ./configure --with-apache=/temp/apache_1.3.x
--with-mysql=/usr/local/mysql
--with-ftp
--with-gd=/usr/local
--with-zlib=/usr/local
--with-mcrypt
--enable-track-vars
- make
- make install
- cd ../apache_1.3.x
- ./configure
--activate-module=src/modules/php3/libphp3.a
- make
Before you proceed with the next section you must shutdown
your webserver (if it is running). You can locate Apache with the command:
find / | grep apachectl
Change to the directory where Apache resides and issue the following command:
apachectl stop &
- make install
- cd ../php-3.0.x
- cp php3.ini-dist /usr/local/lib/php3.ini
Note that you must replace the 'x' in the version numbers with
what you've downloaded.
Apache must now be made "PHP-aware". To do so open up
/usr/local/apache/conf/httpd.conf in your favorite text editor. On
line 361 add index.php3 before
index.html in this section, so that it reads:
<IfModule mod_dir.c> DirectoryIndex
index.php3 index.html </IfModule>
This allows you to use PHP pages as your index as well. Otherwise Apache
would load up index.html first, and .html files can't contain PHP syntax.
Also, uncomment (remove the #'s) line 715 and 716 so that they now read as:
AddType application/x-httpd-php3 .php3
AddType application/x-httpd-php3-source .phps
Save the file, and add the appropriate servers to your startup files. I'm
running Slackware 4 for my web server, so your files may differ. If you
chose not to install MySQL, please don't add the first two lines. Edit
/etc/rc.d/rc.M and add this to the end:
echo "Starting database server..."
/usr/local/mysql/bin/safe_mysqld &
echo "Starting web server..."
/usr/local/apache/bin/apachectl start &
Note: Some distros use BSD style init, and some use SysV init. You
have to be familiar with what one your distro uses, and which files to
edit.
This of course is assuming you chose the default install paths for these
servers. If you have installed these packages to a different directory,
make sure that these additions reflect that. If you are running MySQL,
don't forget to run mysql_install_db before
adding this to your startup scripts. Now, save, exit, and run the script
we just edited (or reboot).
Finishing up:
Now that everything has went smoothly (I hope) we can test our
installation. In your /usr/local/apache/htdocs directory create a
file called index.php3. In that file, enter the following text:
<? phpinfo(); ?>
The next step is to save, exit, and load the file in your browser, but first you need to
find out your IP address. Use the command ifconfig to
find out what IP address you're using. Once you know your IP, make a connection with a web
browser to http://<your-ip-address>/index.php3. Some have noted that you can
use the IP address 127.0.0.2 instead. If you get a long listing of all the PHP
configuration and environment variables, congratulations!! If not, it's off to
troubleshoot.
If any of you would like me to add any useful, but simple, PHP
examples and tips, please e-mail me.
Valuable Links:
PHP's
Homepage
PHP Builder
ZEND
DarkSeed
A complementary NHF to this if you're planning on doing development with PHP is X_Console's
"Good
Programming Practices" NHF.
NHF Written by Mike Fetherston, a.k.a. twist Mike can be found lurking in #linuxnewbie
on EFNet. This NHF was based on Slackware 4, your mileage may vary.
|