Home > Article > Backend Development > Build a database-driven website using PHP and MySQL 2_PHP Tutorial
Adding this script to the startup transaction is a more complex task. If you're not using RedHat Linux and you're not sure about doing this, you'd better ask someone who knows. In RedHat Linux, executing the following command (in the MySQL directory) will accomplish this: % cp share/mysql/mysql.server /etc/rc.d/init.d/ % cd /etc/rc.d/init. d % chmod 500 mysql.server % cd /etc/rc.d/rc3.d % ln -s ../init.d/mysql.server S99mysql % cd /etc/rc.d/rc5.d % ln -s ../init.d/mysql.server S99mysql Now everything is done! To test this working, you can restart your system and make requests to the service to see if it works properly. Installing PHP under Linux As we mentioned earlier, PHP is not a stand-alone program. It's actually a plugin for your web service (probably Apache). There are actually three ways you can install a PHP plug-in in Apache: As a CGI program, Apache runs it every time it needs to process a PHP page. Compiled into the Apache program as a module. Loaded as a module every time Apache starts. The first method is the simplest to install and configure, but it requires Apache to call PHP as a program every time it receives a request for a PHP page. This will definitely slow down your web server's response time, especially if more than one request is received at the same time. The second method and the third method are equally excellent in terms of performance, but it is very likely that you have already installed Apache, and you probably don’t want to download it again, recompile and reinstall it. Therefore, we use the third method. First, we download the PHP package from http://www.php.net/ (or one of the mirror sites listed at http://www.php.net/mirrors.php). As we write this tutorial, PHP 4.0 is basically mature. I myself have tried PHP 4.0-RC2 and encountered no problems. It is estimated that the final stable version will be released soon (perhaps we have not finished writing this tutorial by then). I recommend that you install the latest version so that you will not need to make any changes when the official version comes out. Here, we are installing 3.0, and I will note the possible differences between 3.0 and 4.0 during installation. The file name of the file you download will be php-version.tar.gz. Our first step is to unpack this file: % tar xfz php-version.tar.gz % cd php-version To install PHP as an Apache loadable module, you need Apache's apxs program. This program should be included in most versions of Apache, but if you are using a copy installed through RedHat Linux, you will need to install the Apache Advanced RPM package to obtain it. You can find this software package on your RedHat CD, or you can download it from http://www.redhat.com/. By default, RedHat will install this program to /usr/sbin/apxs. If you see this file, it means it has been installed. During this installation process, you must be logged in as superuser because it involves some changes to the Apache configuration files. The next step is to configure the PHP installer so that it knows which options to select and where to look for the programs it needs (such as Apache and MySQL). Unless you're really sure, just type the following (on the same line): % ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/ php --with-apxs=/usr/sbin/apxs --enable-track-vars --enable-magic-quotes --enable-debugger If you have PHP 3.0 installed (not 4.0 or later), you You also need to tell it where to look for MySQL on your system with the following additional parameters: --with-mysql=/usr/local/mysql/ After a few screens of testing, you will be back to the command prompt. . The following two commands will compile and install PHP: % make % make install PHP is now installed in /usr/local/php (unless you specified a different directory via the --prefix parameter of ./configure above), and you should be able to Find the configuration file named php.ini in the same directory (unless you specify a different directory through the --with-config-file-path parameter of ./configure above). PHP provides a php.ini module file. Its file name is php.ini-optimized (php.ini-dist in PHP3.0). Copy this file to the corresponding directory: % cp php.ini-optimized /usr/local/php/php.ini For PHP 3.0: % cp php.ini-dist /usr/local/php/php.ini You don’t want it yet Worry about the optimization and adjustment of php.ini. Now, we need to make sure Apache knows where to find PHP so it can load it on startup. Use your familiar text editor to open your Apache httpd.conf configuration file (on RedHat Linux, in /etc/httpd/conf/httpd.conf). Look for a line like this: LoadModule php4_module lib/apache/libphp4.so If you are installing PHP 3.0, replace php4 in this line with php3. What you are looking for should be a new uncommented line (not a line starting with #), not the line you commented out previously. Normally, it will appear with other LoadModule lines in the file. Once you find it, you need to change the path so that it matches all other LoadModule lines in the file.Under RedHat Linux, this means changing this line to: LoadModule php4_module modules/libphp4.so Then, look for the line starting with DirectoryIndex. This line tells Apache the filename to use when looking for the default page for a given directory. Usually, you will see index.html and some other file names, you need to add index.php and index.php3 to this line: DirectoryIndex index.html index.cgi ... index.php index.php3 Finally, go to this At the end of the file, add a new line to tell Apache which file extensions are considered PHP files: AddType application/x-httpd-php .phtml .php .php3 Once everything is done, save the changes and restart your Apache server . There should be no error messages when Apache starts. Post-installation settings After both PHP and MySQL are installed, whether you are under Windows, Linux or other operating systems, the first thing to do is to set a "root password" for MySQL.MySQL only allows authorized users to access and operate the information stored in its database, so you must let MySQL know who is authorized and who is unauthorized. When MySQL is first installed, it creates a user called "root" who can access everything without a password. Your first task is to set a password for the root user so that no one can mess with your database. You must realize the importance of MySQL. Just like Web services and FTP services, it can be accessed by any machine on the same network. If you are working on a computer connected to the Internet, this means that anyone in the world can try to connect to your MySQL server! You must immediately choose a password that is difficult to guess! To set the root password in MySQL, enter the following command (including quotes) in the bin subdirectory of the directory where you installed MySQL: mysqladmin -u root password "your new password" To ensure that MySQL accepts this change, you must tell it to re- Load a list of validated users and passwords: mysqladmin -u root reload If this command displays an error message telling you that access is unavailable, don't worry; this simply proves that the password has been valid. To test your new password, you can ask the MySQL server to tell you about its current status: mysqladmin -u root -p status When prompted, enter your password. You will see some simple information that contains the current status of the service. The -u root parameter tells the program that you want to log in as the "root" user. The -p parameter tells the program to prompt you for your password before connecting. The status parameter tells the program that you want to see the system status. If you need to shut down the MySQL server, you can use the following command. Note that the -u root and -p parameters here have the same meaning as mentioned above: mysqladmin -u root -p shutdown Now that your MySQL database system is running safely, the remaining work is to configure PHP. PHP is configured using a text file called php.ini. If you installed PHP under Windows, you should have copied php.ini to your Windows directory. If you installed PHP on Linux using the instructions above, you should have copied php.ini to the PHP installation directory (/usr/local/php). Open php.ini with your familiar text editor and take a quick look. Most of the settings are well documented, and most of the default settings can be adapted to your requirements. Just check and make sure your configuration matches the following: magic_quotes_gpc = Ondoc_root = extension_dir = If you are running PHP 4.0, you also need to check the following line: register_globals = On If you are running PHP 3.0 under Windows, remove the leading " ;" to uncomment the following line (not required in 4.0): extension=php_mysql.dll Save the changes to php.ini, and then restart your web server. Under Linux, if you are logged in as superuser, you can restart Apache: /etc/rc.d/init.d/httpd restart Now you are all set! The only thing left now is to test and make sure everything is OK (see your first PHP script). If your web host provides PHP and MySQL If the host that provides you with web space has installed and set up MySQL and PHP for you, and you just want to learn how to use them, there is not much you need to do. You should now contact your hosting provider for information on how to access these services. Especially important, you need to obtain the username and password set for you to access the MySQL service. They may have set up an empty database for you (this will prevent you and other users on the same MySQL server from messing with the database), and you will also need to know the name of the database at this time. There are two ways to access the MySQL service. The first method is to use telnet to log in to the host and install the MySQL client program (mysql, mysqladmin, mysqldump, etc.) to directly interact with the MySQL service. The second method is to install those client programs on your own computer and connect to the MySQL server. Your web host may support one or both of these methods, so you'll want to ask first. If they support logging in via telnet to do your work, you will also need a username and password to log in via telnet (this username and password may be different from the ones you use to access the MySQL service). You need to ask clearly for both setting information. If they support remote access to the MySQL service, you will need to download a program to connect and interact with the server. This tutorial will assume that you have downloaded a MySQL client program from http://www.mysql.com/. This package runs under both Windows and Unix and is free. The installation instructions included in the package are very simple. If you prefer some graphical interface, you can download something like MySQLWinAdmin for Windows (also available from http://www.mysql.com/). I recommend that you first learn to use basic client programs, although the commands used in these programs will be very similar to those used in PHP scripts to access MySQL databases.Your first PHP script I’ve told you so much about installation, but I haven’t let you try out a PHP-driven web page. It’s really unfair to me