HOWTO: Installing Apache and PHP with MySQL Support

Permabanned
Joined
18 Oct 2002
Posts
2,010
Location
Huddersfield / Antartica
HOWTO: Installing Apache and PHP with MySQL Support

Ever wondered how to install Apache with working PHP (and maybe even mySQL) well heres your chance to find out: If you want mySQL just have it installed before you start the rest of this, its easy and there are many sites on the internet explaining how to install it

Firstly open up an terminal and su to root.


Code:
sh-2.05a$ su 
Password: <insert root password>
bash-2.05a#

You'll need to visit http://www.apache.org/ and get the correct package...Im installing Apache 1.3.26 for Linux

Then cd into the dir you downloaded to and untar the file:


Code:
bash-2.05a# cd /home/shak/isos
bash-2.05a# tar apache_1.3.26.tar.gz 
<Files contents are extracted>


Now, cd into the directory

Almost every software which comes with its source code contains a configure script, this performs checks and prepares software for the compile. Using the configure script you can decide where Apache will be installed to and which modules you want to compile . There are many options which can be supplied to configure.

These are viewable by using:

Code:
bash-2.05a# ./configure --help


The options usually start with a "--with-xxx" and "--enable-xxx" Where xxx is the name of the option like --enable-so (for apache configure script) and --with-mysql (for PHP configure script). I'm going to use the following optionsfor Apache are --prefix and --enable-so.

This means my configure line will look like this:

Code:
bash-2.05a# ./configure --prefix=/usr/local/apache --enable-module=so


These options mean that Apache will be installed into /usr/local/apache and the shared objects are enabled. PHP will be installed as a shared object.

We can now compile the source

Code:
bash-2.05a# make
bash-2.05a# make install


You can now test the apache server by:

Code:
bash-2.05a# /usr/local/apache/bin/apachectl start


With any luck no errors should be reported however if you have been running another version of apache then you will get this error:

Code:
bash-2.05a# /usr/local/apache/bin/apachectl start
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down


This can be rectified with this command

Code:
bash-2.05a# killall -9 httpd


Now to test the webserver call up lynx or any other browser and open up localhost. You should see a nice "Welcome to Apache" screen Congratulations, Apache is now up and running.

Kill the apache server and move onto the next stage:

Code:
bash-2.05a# /usr/local/apache/bin/apachectl stop


We can now make the PHP module, firstly extract the PHP .tar.gz.

Code:
bash-2.05a# tar xvzf php-4.2.3.tar.gz


The cd into the created directory:


Code:
bash-2.05a# cd php-4.2.3


You have to configure PHP for the compile now. There are thousands of options which can be provided to PHP's configure script. These options include where PHP should be installed, which functios should be enabled eg. mysql databases Extensions provide additional functionality which core PHP doesn't provide. For example to create images --with-gd option can be used. But for these extensions to work appropriate libraries must have been installed. If you use some --with option and that library isn't installed on your system then configure will fail.

To compile PHP as an Apache shared module you have to provide path to apache apxs utility, which in our case was installed in /usr/local/apache/bin/ when apache was installed.

My ./configure line will look like this:

Code:
bash-2.05a# ./configure --with-mysql=/usr/local/mysql \
      --with-apxs=/usr/local/apache/bin/apxs \
      --enable-track-vars \
      --with-pdflib=no \
      --with-zlib


This will ensure that:

  • It is compiled as a apache module
  • It uses mysql
  • It can track variables

Next we can run this ./configure line and hope we get no errors I reccommend looking away and reading the forums so you don't get nervous I certainly get nervous when running my ./configures

Fortunately it works well, and we can progress onto compiling the PHP:

Code:
 bash-2.05a# make


With PHP 4.2.3 you will experience errors at this stage, I did so I ran:


Code:
bash-2.05a# make clean


I then re-ran the ./configure line as well as the make line. This then completed succesfully.

Now run:


Code:
bash-2.05a# make install


Now to get PHP running we need to copy accross the php.ini. cd back to the php dir and copy the file across as is shown below:


Code:
bash-2.05a# cd /home/shak/isos/php-4.2.3
bash-2.05a# cp php.ini-dist /usr/local/lib/php.ini


Now edit the apache httpd.conf to make sure that PHP files are handled by the PHP module:


Code:
bash-2.05a# cd /usr/local/apache/conf/
bash-2.05a# nano -w httpd.conf


The line we need to add is this


Code:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps


So press CTRL+W and type AddType to search for the relavant section

Then add the line, my AddType section looks like this:

Code:
#                                                                               
# AddType allows you to add to or override the MIME configuration               
# file mime.types for specific file types.                                      
#                                                                               
AddType application/x-tar .tgz                                                  
AddType application/x-httpd-php .php  
AddType application/x-httpd-php-source .phps

You'll also need to add the correct LoadModule line, so locate the LoadModule section and add:


Code:
LoadModule php4_module        libexec/libphp4.so


Save the file by pressing CTRL+X and then typing y. Now to test the server:

Start the server:

Code:
bash-2.05a# /usr/local/apache/bin/apachectl start


Then open up an editor, such as vi, vim, emacs, nano, pico, gedit, nedit etc. and make a PHP test script, save it to /usr/local/apache/htdocs/test.php


Code:
<?                                                                              
phpinfo();                                                                      
?>

Save it and exit the editor.

Now call up a web browser and type http://localhost/test.php. When you call this up you will be presented with all of the information about your PHP install. Enjoy

I also had to make sure that my php.ini was pointing to the right mysql.sock, when it was all was fine and dandy. You can see for yourself at http://shak.homelinux.org

This HOWTO has shown that the Static PHP option doesn't work, and a DSO object is the most reliable

One of the common problems with Apache is when you try starting it, and it appears to start OK but dies within 5-10 seconds.

When Apache starts it triesto resolve the server name to an IP address - this is ok if you have a DHCP and NDS server (as I have now) BUT if you are running it stand alone it won't necessarily resolve. This can be fixed by editing /etc/hosts and add your own machines name and ip address. Then save the file and it should be fixed.

Enjoy
Shak
 
Back
Top Bottom