February 1st, 2004 05:54 PM
Installing Apache and PHP on Linux
I was searching the tutorials forum when I noticed that nobody (that I'm aware of) has written a tutorial on how to install Apache and PHP on Linux. I found it very hard when I first tried to do it myself so now that I know how to do it I've decided to write this tutorial.
Installing and Configuring Apache and PHP on Linux
The first steps in installing these two programs is to download them. You can get the latest version of Apache at http://httpd.apache.org/download.cgi (simply follow the links in order to find the file), and PHP at http://snaps.php.net/
You will also need a copy of Linux, I prefer to use SuSE, its cheap and offers ease of use and high stability/power. Or you can also download a copy of Debian (http://www.debian.org/CD/http-ftp/) which is fully free or Slackware (ftp://ftp.slackware.com/pub/slackware/) for more advanced users (read: Installing Slackware Linux 9.1 by gore)
Now lets move both these files into the /tmp directory for easy installation.
firstname.lastname@example.org # mv <path to downloaded copy of Apache> /tmp
email@example.com # mv <path to downloaded copy of PHP> /tmp
Lets extract both the files:
firstname.lastname@example.org # tar -zxvf httpd-x.y.z.tar.gz
email@example.com # tar -zxvf php4-STABLE-xxx.tar.gz
Now, lets install these. First we have to install Apache as PHP wants to read APXS provided by Apache when fully installed. So now we have to use good old ./configure make make install. When executing the ./configure script (which can be executed as many times as you want) we tell Apache how to compile itself and where. To tell where Apache is to be installed we use the --prefix (i.e.: ./configure --prefix=/usr/local/Apache will install Apache in to the directory of /usr/local/Apache) variable and we will also use the –-enable-mods-shared=most which will build most of the available loadable modules when compiling. So in the command line we type:
firstname.lastname@example.org # cd httpd-x.y.z
email@example.com # ./configure --prefix=/usr/local/Apache --enable-mods-shared=most
Now its configure so lets compile and install:
firstname.lastname@example.org # make
email@example.com # su -
<password of root>
firstname.lastname@example.org # make install
If you have not received any error messages during these three operations then Apache will have fully installed itself into the directory of /usr/local/Apache (or what ever you defined --prefix to be)
You will now have a configuration file in /<prefix>/conf/ called httpd.conf which Apache reads at startup. This you will need to edit but we'll do that later on.
Lets install PHP now, so simply go to where you extracted PHP to
email@example.com # cd /tmp/php4-STABLE-xxx
and now we'll do a similar procedure to install PHP. However we'll compile using different commands this time. This time we'll be using to extra commands, --with-apxs2 and --with-mysql.
These to are used in the same way as in the Apache one, we'll use --with-apxs2 to tell PHP where the Apache apxs is, this is done very easily by simply typing in --with-apxs2=<path to Apache>/bin/apxs (Remember: use --with-apxs2 if you've installed Apache v2 or higher, otherwise simply use --with-apxs). The --with-mysql command is only need if you have MySQL installed and are planning on using it, for those who are interested this is executed simply by typing in --with-mysql=<path to SQL>. For more ./configure commands type './configure --help' to get a complete listing, good luck :P
So lets configure PHP:
firstname.lastname@example.org # ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/Apache/bin/apxs --with-mysql=/usr/local/mysql
Now lets compile and install:
email@example.com # make
firstname.lastname@example.org # su -
<password for root>
email@example.com # make install
Again if you have not received any errors then PHP will have installed all good
Configuring Apache for use
Here comes the hard part: You will now have to configure Apache!
To start open up httpd.conf located in /<path to Apache>/conf/httpd.conf in vi/vim or which ever play text viewer you prefer.
firstname.lastname@example.org # vim /usr/local/Apache/conf/httpd.conf
This file will be 1052 lines long, luckily most of it will be either comments or irrelevant.
The first line we will want to look is Listen (line 218) which will define the port which Apache will listen to when running (leave it at port 80 [default] unless you wish to hide your server from random Internet users).
Next check that you have a LoadModule php4_module (line 231) so that Apache will be compatible with PHP.
Then check that Apache will be running as User nobody (line 267), never run Apache as root, bad security! (but that's of the topic).
Then at ServerAdmin simply type in your e-mail address, this will be displayed publicly from time to time.
The next variable ServerName (line 291) can be left commented out.
Then at the DocumentRoot option (line 307) you can define where Apache looks for its index file, the default is perfectly acceptable but I prefer to change it (note: if you change the DocumentRoot you must remember to change the Directory (line 332) to be the same in order for it to work.
Then in the DirectoryIndex (line 394) you can replace index.html with index.php as you have PHP installed and you might as well use that instead of the old fashioned HTML documents (some people still prefer to keep the index as .html but still use PHP). This option will only define what the default file name is if Apache is not given a filename, you can even change it to simply 'blah' (this might not always work as it has no extension)
Alias /icons/ (line 550), AddDescription (line 672), and ErrorDocument (line 934) do not need any editing, but can be good fun to personalize your Apache to look better (especially the ErrorDocument) if you have some free time on your hands.
AddType (line 854) is the critical line in order to make sure that PHP works. You'll need to add the line 'AddType application/x-httpd-php .php .php3 .php4' to it. This tells Apache that when ever it comes across a .php/.php3/.php4 file it will run it through the PHP application. This is the program that compiles the PHP scripts just before they are sent of to the user by Apache.
Testing that it all works
Now lets test that it all works. Simply make a file called index.php (as defined in DirectoryIndex) and in it code the following:
Now goto your web browser and type in 'localhost' and you should get a PHP info page telling you about how it was installed and general information about it.
<?PHP phpinfo(); ?>
You will now have successfully installed and configured both Apache and PHP and even created your very first PHP page
All constructive critism is welcome!
February 1st, 2004 06:51 PM
You forgot to mention (though the # prompt shows that you knew it) that make install has to be run as root. Also IIRC, httpd.conf can't be edited by anyone other than root. Otherwise, great work!
February 1st, 2004 07:05 PM
You forgot to mention (though the # prompt shows that you knew it) that make install has to be run as root.
But your right. I forgot to mension that httpd.conf can only be edited by root. Thanks cgkanchi