~ php for newbies ~
 
         to php lab    php for newbies
Version february 2002
view source

I started writing this paper a long time ago without ever finishing it. Mainly because I had no time, but also because I wasn't sure this may be as helpful as I first thought. After all, there are lots of tutorials, columns, ... on the subject.
However, I finally decided to finish it the day Jeff, an old friend, wondered what php is and how to use it. I hope you'll find this useful too.

Introduction

In order to write php scripts, you need a working environment that allows you to execute (and debug) your scripts. The easiest way to do this is to find a -preferably free- web space provider that supports php where you can upload your script and start experiments. However, this can quickly become very expensive (especially if like me you live in a country where local calls are billed) and rather slow (edit-upload-test process have to happen mostly online). Moreover, this approach doesn't allow you to configure the php engine exactly how you would like it. For example, if the provider didn't install any graphical library, you won't be able to work with image related scripts. And, last but not least, most provider turn safe-mode on. This greatly limits your field of experiment (maximum execution time enabled, no exec() call allowed, ...). However this remains an excellent way for a very first contact with php :-)
If you want to experiment a step further you will need your own, local, php powered http server.
Although php can be used for 'shell scripting' (scripts that you run from your shell's prompt), its most common use is as an http server page preprocessor. Thus, beside php itself, we will need to install an http server. That's why the next section describes the installation & configuration of php and apache web server. We will cover both windows and unix OS.
If you prefer to first experiment with a free web space provider supporting php, then you can skip the following sections and jump directly to [hello world] section.

local php server

In this section I'll try to explain how to set up and configure your local php powered server. The first part covers Windows while the second cover Linux. I hope that, by following the instructions, you'll end up with a working server. Good luck !!

Windows installation

First, you have to download the files needed. We will limit ourselves to a simple php+apache installation, without any additionnal libraries. So, we need:

Let's start installing Apache. Simply run the the downloaded file and agree with the default values. This should create a C:\Program Files\Apache Group\Apache directory in which apache files are installed. An apache web server start menu folder should have been added in which you should find start apache and stop apache commands. Start apache now. A Dos box open and you should read : Apache/1.3.x (Win32) running.... If this is not the case and if the dos box closes too fast for you to read the displayed message, start a MSDOS session, switch to apache directory and start it manually with apache -k start . Now a warning message should appear. The most frequent one is :

APACHE.EXE: cannot determine local host name.
Use the ServerName directive to set it manually."

If this happen, edit httpd.conf in conf/ subdirectory and search for ServerName. Change it like this (don't forget to remove the leading #) :
ServerName localhost

Let's quickly check if apache is well installed. Start apache from the start menu and point your browser to http://localhost/ You should now see a default apache page. Congratulations, first step is over.

Now installing php. Unzip the binaries in a directory like c:/php4
Rename php.ini-dist to php.ini. Edit it and change the line
   extension_dir =
into
   extension_dir = c:\php4
Copy this php.ini file into your c:\windows directory.

That's all. apache is now installed and running and php is ready to be linked into apache. The other php.ini lines can be left in their initial state (of course you may want to experiment with them later :-)

You can now jump to the [next] section to configure apache so it will recognize and correctly process php files.

apache server configuration / Windows

Now we have to tell apache what to do with php files. This is done into httpd.conf, the main apache configuration file. Load that file in your favorite editor.

Now we could write a short php script to check if everything was fine, but you'll ask me where to put it ? Well, the answer is simple : You have to put it in your DocumentRoot directory. That directory is specified in httpd.conf and define the default location where apache will look for files to serve. So search for DocumentRoot in httpd.conf and either put your files in the specified directory or (what is better in my opinion) create your own directory (like c:\localserver\htdocs) and change the line to reflect it :
   DocumentRoot "C:/localserver/htdocs"
If you do this you will also have to change the following line :
   <Directory "C:/localserver/htdocs">
This line define which services and features are allowed/disabled in the specified directory. You can find it by searching for either Directory or for the original path shown in DocumentRoot. Later we will edit some more features in this section regarding the security aspect of our server.

Now it's time to write a simple script, and to fetch it through your browser.
Simply cut and paste the following in your favorite editor and save it as c:\localserver\htdocs\detail.php

<?php
phpinfo();
?>
Now restart apache with apache -k restart and point your browser to http://localhost/detail.php
If everything went fine you should now see a page full of interesting information about your current php/apache configuration :-)

Linux installation

In the first version of this document, we will limit ourselves to the php module installation. Indeed, most linux distribution comes with apache already installed and configured. You can easily check if apache is already installed on your machine by typing at the shell : httpd -v. This command should return the version number of your currently installed apache server.
Check also that apache shared module object is enabled. To do this, type httpd -l (lowercase L) at the shell and you should find in the list of compiled modules at least http_core.c and mod_so.c. If this is the case let's go further.
First you need to download the latest php sources from [www.php.net/download.php].
Switch to a directory of your choice (/usr/src for example) and untar the sources with tar -xvzf php-4.x.x. You should now have a new directory named php-4.x.x

Before going further, it's a good idea to install some useful libraries. Let's for example install gd and zlib. gd is a graphical library that allows php to draw images on the fly with some specific drawing functions (see [http://www.php.net/manual/ref.image.php] for more information). Zlib is a compression library that allow php to work with compressed files (see [http://www.php.net/manual/ref.zlib.php]).

libgd comes along with most distribution, so we will assume it is already installed on your machine. If it isn't, you should be able to install it from your distribution packages (you can also find the latest version [here]).
to check if gd is installed correctly, type locate libgd.a at your shell prompt and check if a libgd.a file is found on your system (on my machine it's located in /usr/lib).
zlib, on the other hand wasn't available on my distribution, so here is how to install it 'by hand'. First download the latest zlib tar file (available [here]). copy it to /usr/src, then tar -xvzf zlib-x.x.x You should now have a subdir zlib. cd zlib; ./configure; make test; make install That's it, zlib is now installed on your machine.

Now that our additional libraries are installed, we can tackle with the php installation itself. cd php-4.x.x to enter php source directory.
First step is to configure php. For this we use the configure script which can take an impressive amount of parameters to define which features to include during compilation (try ./configure --help for a general overview).
As we want to install php as an apache shared modules, with the libgd and zlib librairies linked we will use this configuration command :
./configure --with-apxs --with-gd --with-zlib
If you followed the above procedure, you shouldn't have to specify any other parameters. If the configuration step went fine, we can now make; make install; to compile and install php files. Everything went fine ? Cool, php is now compiled and ready to process your scripts. If anything went wrong during either configure or compile steps, try again with the simplest configuration :
./configure --with-apxs
This should normally works fine on most systems (with apache installed).

However, before beeing actually able to serve php scripts, a few things still need to be done. First we have to configure php runtime options. In your php sources directory you should have a file called php.ini-dist. Copy it to /usr/local/lib/php.ini.
The default values doesn't need to be changed for this very first installation, but you are free to experiment with them later.

Congratulations! At this point php is now installed and configured. Next step is to configure apache.

apache server configuration / Linux

The apache configuration under unix is very similar to the windows one. Actually it's even more easy as php is supported as a module in apache.

We have to tell apache that he must include a new module. This is normally handled by the make install step, but I had some strange results on my system, so we better manually check it. Edit httpd.conf (located in /etc/httpd) and look for a line like :
AddModule mod_php4
If that line isn't show, you should simply add it along with the other AddModule lines.
If it is shown, take care that it isn't part of an statement (as it was the case on my machine). If it is, simply move it out the (this seems to be a bug in make install: it append the AddModule directive right after the last one, without checking if it's not inside an if statement).

Do the same as above, now with :
LoadModule mod_php3.c

Last thing to do is to tell apache which files to consider as php. For this, we will use the AddType directive:
AddType application x-http/.... .php
Just like for the windows configuration, you can add as many Type as you whish (.htm, .html, ...).
A last thing you may like to do is to change the DocumentRoot specified in httpd.conf to point, for example, to /usr/laurent/http

That's it! Apache is now correctly configured to process php script through the php module. Remember, though, that each time you modify httpd.conf and/or php.ini, you have to restart apache. This is easily done with the initialization scripts that you should find somewhere in /etc/rc.d (try /etc/rc.d/apache restart).

Now we can write a simple script. Cut and paste the following and save it as detail.php under your DocumentRoot directory.

<?php
phpinfo();
?>
Point your browser to http://localhost/detail.php.
If everything went fine you should now see a page full of of interesting information about your current php/apache configuration :-)

Configuring apache (again :-)

Now that everything works fine, we can focus on fine-tuning our apache server for our need. As this server will serve only local file to your local browser, we should prevent anyone from outside from reaching your server. We will thus limit allowed access to the local machine.
In the <Directory> we can specify who can get stuff from the specified directory. You should normally see something like the following just before the end of your <Directory "c:/localserver/htdocs"> directive :
    Order allow,deny
    Allow from all
Change it to :
    Order deny,allow
    deny from all
    allow from 127.0.0.1
This will allow only request from localhost to be served.

You can also play with some other interesting directives in httpd.conf like Alias which allow you to alias physical directories to virtual paths. But this goes behind the scope of this page. I would greatly suggest that you browse around [apache.org] and [php.net] There are lots of nifty features described there.

Hello world

Now that our local server is up and running (or if you choosed to use an on-line free web provider), we can dive into php itself.
Let's start with the de facto introducing script, 'hello world'. Like you probably know, this script will simply show the string 'hello world' in your browser.
<?php
    echo('hello world');
?>
Simple, isn't it ? :-) Save that file as hello.php (and eventually upload it to your web space) and point your browser to it . It works ? Great, you just wrote your first php script :-)

Now you should look for yourself, either on the phplab itself or on the many php sites [around] and start experimenting.
If time allows and if you, readers, think it may be useful, maybe i'll write some more about this.

disclaimer

The above text is based on my own experiences and, in no ways, I could guarantee it will work on your system. There are probably many other, better, ways to do things I describe. I certainly didn't always used the correct vocabulary. For this and for many other reasons, I dearly encourage you to [submit] any comments you may have on this. I/we want to learn too :-)

If you experience any problems installing php on your local machine, I'm sure someone around is able to help you. So, I suggest you use the php board to post any questions or comments you may have.


Choose your way out:
to tips & tricks
to tips & tricks
to php lab
to php lab
to basic
to basic

(c) Fravia 1995, 1996, 1997, 1998, 1999, 2000, 2001. All rights reserved
Page created by [laurent]
Last modified: February 08 2002 19:50:11 GMT
GZIP compression enabled
1 0 1 5