Three Steps to Enable Apache to Serve PHP
1. Edit the Apache httpd.conf file. Begin by getting back to the root command line:
TYPE cd ~
TYPE ee /usr/local/etc/apache/httpd.conf
Add the following lines anywhere within the Apache httpd.conf file, perhaps at the very top:
#***************************************
AddType application/x-httpd-php3 .php3
AddType application/x-httpd-php3-source .php3s
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
#***************************************
Find the line that says ServerAdmin and put your e-mail address there.
2. Create a PHP test file:
TYPE cd ~
TYPE ee /usr/local/www/data/index.php
Add the following codeexactly the way you see it hereto the editor window you just opened:
<html>
<head>
<title>PHP_TESTER</title>
</head>
<body>
<?php
phpinfo();
?>
</body>
</html>
Press Escape to leave the editor and save this file.
3. Test the PHP installation on a secure port:
To test the PHP installation, you have to stop and restart the Apache server:
TYPE cd /usr/local/etc/apache
TYPE apachectl stop
* Note: In ctl, it is an "el" and not a one.
TYPE apacheclt start
TYPE lynx 127.0.0.1
If this worked, you should see the information about your new PHP/MySQL-enabled Apache Web server. Congratulations again! You now have a PHP-enabled Apache-modSSL Web server. The second-to-last step is adding a password to MySQL root and creating a simple test page that shows the connection between the PHP language and the MySQL database.
Add a Password to MySQL Root and Creating a Test Page
Set a MySQL root password:
TYPE mysqladmin -u root password your_password_here
Logging into MySQL as root now requires a password. Replace "your_password_here" with your own password (For more information about MySQL, see the online documentation):
TYPE mysql user=root password=your_password_here
You should now see the mysql> prompt:
Mysql>TYPE create database banking;
Mysql>TYPE show databases;
Mysql>TYPE quit
Make sure you add a semi-colon when required.
Now you are back to the root prompt. All that is left is to use PHP to connect to the newly created "banking" database. You need to invoke an editor again:
TYPE ee /usr/local/www/data/phpmysqlconnection.php
Then add the following code into the editor window (don't forget the semi-colons):
<html>
<head>
<title>PHP_MySQL_Connection</title>
</head>
<body>
<! start PHP insert and create MySQL connection details as variables >
<?php
// database access variables
$Server = "localhost";
$Username = "root";
$Pass = "yourpasswordhere";
$DataBaseName = "banking";
// connect PHP to MySQL
$Connection = mysql_connect ($Server, $Username, $Pass) or die ("Connection Denied");
// test the connection with a browser
if ($Connection) {
echo "<h3>Congratulations! You are ready to build dynamic database-driven Web sites</h3>";
} else {
echo "<h3>Back to the lab again*****Something went wrong</h3>";
}
// closing the connection is considered good form
mysql_close ($Connection);
// close the php
?>
</body>
</html>
Test this out:
TYPE lynx 127.0.0.1/phpmysqlconnection.php