Creating dynamic Web sites with PHP and MySQL

Creating dynamic Web sites

The Web is no longer static; it’s dynamic. As the information content of the Web grows, so does the need to make Web sites more dynamic. Think of an e-shop that has 1,000 products. The owner has to create 1,000 Web pages (one for each product), and whenever anything changes, the owner has to change all those pages. Ouch! Wouldn’t it be easier to have only one page that created and served the content on the fly from the information about the products stored in a database, depending on the client request? Nowadays sites have to change constantly and provide up-to-date news, information, stock prices, and customized pages. PHP and SQL are two ways to make your site dynamic.

PHP for Creating dynamic Web sites

PHP is a robust, server-side, open source scripting language that is extremely flexible and actually fun to learn. PHP is also cross platform, which means your PHP scripts will run on a UNIX®, Linux®, or Windows® server.

MySQL for Creating dynamic Web sites

SQL is the standard query language for interacting with databases. MySQL is an open source, SQL database server that is more or less free and extremely fast. MySQL is also cross platform.

Installing Apache server routines for Creating dynamic Web sites

First we will install the Apache server routines in the Linux environment. To install these packages you need root access to your server. If someone else is hosting your site, ask the administrator to install them for you. Installing Apache is relatively simple. First download the Apache archive, apache_x.x.xx.tar.gz (the latest I downloaded was apache_1.3.14.tar.gz) from the Apache site and save it in /tmp/src directory. Go to that directory:

# cd /tmp/src/

Extract the files with the command:

# gunzip -dc apache_x.x.xx.tar.gz | tar xv

replacing those xs with your version number. Change to the directory that has been created:

# cd apache_x.x.xx

Now to configure and install apache, type the commands:

# ./configure --prefix=/usr/local/apache --enable-module=so  # make  # make install

This will install Apache in the directory /usr/local/apache. If you want to install Apache to a different directory, replace /usr/local/apache with your directory in the prefix. That’s it! Apache is installed. You might want to change the default server name to something of real value. To do this, open the httpd.conf file (located at /usr/local/apache/conf) and find the line starting with ServerName. Change it to ServerName localhost. To test your install, start up your Apache HTTP server by running:
# /usr/local/apache/bin/apachectl start
You should see a message like “httpd started”. Open your Web browser and type “http://localhost/” in the location bar (replace localhost with your ServerName if you set it differently). You should see a nice welcome page.
Installing MySQL Next comes MySQL. We will follow the same procedure (replacing those xs again with our version number). Download the source from the MySQL site and save it in /tmp/src.

# cd /tmp/src/  # gunzip -dc mysql-x.xx.xx.tar.gz | tar xv  # cd mysql-x.xx.xx  # ./configure --prefix=/usr/local/mysql  # make  # make install

MySQL is installed. Now you need to create the grant tables:
# scripts/mysql_install_db
Then start the MySQL server:
# /usr/local/bin/safe_mysqld &
And test your installation by typing:
At the password prompt, just press Enter. You should see something like:

Welcome to MySQL monitor. Commands end with ; or \g.  Your MySQL connection id is 5 to server version 3.22.34  Type 'help' for help.  mysql>

If you see this, you have MySQL running properly. If you don’t, try installing MySQL again. Type status to see the MySQL server status. Type quit to exit the prompt.

Installing PHP for Creating dynamic Web sites

We will follow a similar procedure to install PHP. Download and save the source from the PHP site to /tmp/src:

# cd /tmp/src/  # gunzip -dc php-x.x.xx.tar.gz | tar xv  # cd php-x.x.xx  # ./configure --with-mysql=/usr/local/mysql --with-apxs=/usr/local/apache/bin/apxs  # make  # make install

Copy the ini file to the proper directory:

cp php.ini-dist /usr/local/lib/php.ini

Open httpd.conf in your text editor (probably located in /usr/local/apache/conf directory), and find a section that looks like the following:

# And for PHP 4.x, use:  #  #AddType application/x-httpd-php .php  #AddType application/x-httpd-php-source .phps

Just remove those #s before the AddType line so that it looks like to Creating dynamic Web sites:

# And for PHP 4.x, use:  #  AddType application/x-httpd-php .php .phtml  AddType application/x-httpd-php-source .phps

Save your file and restart apache Creating dynamic Web sites:

# /usr/local/apache/bin/apachectl stop  # /usr/local/apache/bin/apachectl start

Creating dynamic Web sites Then test whether you have PHP installed properly. Type the following code in a text editor and save it as test.php in a directory accessible by your Web server :

<HTML>  <?php  phpinfo();  ?>  </HTML>
Creating dynamic Web sites

Creating dynamic Web sites

Set the permission of the file to executable by typing at console chmod 775 test.php, and then view it with your browser. You should see a detailed description of the environment variables in PHP . If you don’t, then PHP was not installed properly. Try reinstalling it. Make sure there is a section “MySQL” in the php info; if not, MySQL connectivity will not work for Creating dynamic Web sites.