linuxnewbie.org.gif
Tuesday, 12-Dec-2000 10:39:50 EST
Newbized Help Files articles discussion board bookshelf sensei's log advertising info

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:
  1. MySQL
  2. LibPNG
  3. ZLib
  4. GD
  5. 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:
  1. cd apache_1.3.x
  2. ./configure
  3. cd ../php-3.0.x
  4. ./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
  5. make
  6. make install
  7. cd ../apache_1.3.x
  8. ./configure --activate-module=src/modules/php3/libphp3.a
  9. 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 &
  1. make install
  2. cd ../php-3.0.x
  3. 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.
[-NHF Control Panel-]
The Linux Channel at internet.com
Linux Planet
Linux Today
Linux Central
Linuxnewbie.org
PHPBuilder
Just Linux
Linux Programming
Linux Start
BSD Today
Apache Today
Enterprise Linux Today
BSD Central
All Linux Devices
SITE DESCRIPTIONS
[-What's New-]
Order a Linuxnewbie T-Shirt
Easy Webcam NHF
Directory Navigation NHF
Installing Snort 1.6.3 on SuSE 6.x-7.x
Customizing vim
The SysVinit NHF
Installing ALSA for the VT82C686 integrated sound
USB Creative Video Blaster II for Linux
Configuring the Intellimouse Explorer in XFree86 V4+
The beginnings of a distro NHF
Getting Past Carnivore?
Getting and Installing PGP
Getting your ATI Rage 128 Working
How to create a multiple partition system
Using Fdisk
Introduction to Programming in C/C++ with Vim
Adding a Hard drive in Linux -- In five steps
Installing ALSA for the Yamaha DS-XG Sound Card
Getting your Diamond Rio Mp3 Player to work with Linux
Bash Programming Cheat Sheet
Installing NVIDIA Drivers for Mandrake
Setting up Portsentry
Hard Drive Speed Tweak for Linux
Sensei's Log
Chat room
Join: Linuxnewbie.org SETI Black Belts!
Send in your news
Click the image to add Linuxnewbie.org to your MyNetscape Page
[-LNO Newsletter-]

[-Archive-]
The beginnings of a distro NHF
Connecting to the Internet using KPPP
Getting your SBLive to work
Unreal Tournament NHF
LWE Day 2 Pictures
LWE Day 1 Pictures
The LNO FAQ!
WoW (Words of Wisdom)
Other sites news
What is Linux?
What is Linux? part deux (ups & downs)
Search newsgroups
The List
ALS Report
Feedback Form
jobs.linuxtoday.com.gif
Match: Format: Sort by:
Search:
[-Quick Links-]

Copyright 2000 internet.com Corp. All Rights Reserved. Legal Notices Privacy Policy

internet.com.gif