How to install and configure PHP and MySQL on CentOS 4

The Dedicated Linux servers have PHP, MySQL and related packages installed by default. The web server, httpd, is also installed. You can verify this with the following commands:

rpm –qa | grep –i php

The previous command queries the package database to see if any packages with php in the title are installed on the system. After invoking the command, you should see output similar to the following:

[root@server ~]# rpm -qa | grep -i php
php-domxml-4.3.9-3.9
php-odbc-4.3.9-3.9
php-4.3.9-3.9
php-mysql-4.3.9-3.9
php-pear-4.3.9-3.9
php-pgsql-4.3.9-3.9
php-imap-4.3.9-3.9
[root@server ~]#


The output shows that php-4.3.9-3.9 and php-mysql-4.3.9-3.9 are installed. You can also confirm that MySQL is installed by using the following:

rpm –qa | grep –I mysql

Similar output should follow:

[root@server ~]# rpm -qa | grep -i mysql
mysql-4.1.12-3.RHEL4.1
mysql-server-4.1.12-3.RHEL4.1
perl-DBD-MySQL-2.9004-3.1
php-mysql-4.3.9-3.9
mod_auth_mysql-2.6.1-2.2
[root@server ~]#


To verify that httpd (apache web server) is installed, use the following:

[root@server ~]# rpm -qa | grep -i httpd
httpd-2.0.52-19.ent.centos4
httpd-suexec-2.0.52-19.ent.centos4
[root@server ~]#


After using those commands, you will be able to see that apache, along with php-4.3.9-3.9, php-mysql-4.3.9-3.9 and mysql-4.1.12-3.RHEL4.1 are installed on the server. These three packages are required in order for PHP and MySQL to interact together. The mysql-server package must also be installed in order to use the server as a remote MySQL server.

Now, you need to ensure that these services start at boot. You can do this with a few commands. Use the following to start apache at boot:

[root@server ~]# chkconfig --level 3 httpd on

One more similar command for MySQL:

[root@server ~]# chkconfig --level 3 mysqld on

PHP does not have a service as it runs as needed with direction from apache. Use the following commands:

[root@server ~]# service httpd start
[root@server ~]# service mysqld start


Verify these with another command:

[root@DedI1 ~]# service httpd status
httpd (pid 30419 30418 30417 30416 30415 30414 30413 30412 30409) is running...
[root@DedI1 ~]# service mysqld status
mysqld (pid 30764) is running...
[root@DedI1 ~]#


To ensure that PHP is functioning properly, you will need to create a simple script. In a text editor (like vi), create a new file called phptest.php. In the file, place the following code:

<?php phpinfo() ?>

Save the file in your web server’s document root folder, usually /var/www/html. Open your web browser and navigate to the web site IP address with phptest.php appended on the IP address.

For example: 63.134.253.2/phptest.php.

You should a page showing PHP configuration details. If you see that screen, then PHP is functional. If not, something is wrong with the PHP installation.

Next, to make sure apache, PHP and MySQL are communicating, you should create a simple MySQL database using PHP code as a test. Open up an editor and enter the following code:

<?php

$link = mysql_connect('localhost', 'mysql_user', 'mysql_passwd');
if (!$link)
{
die('Could not connect: ' .mysql_error());
}

$sql = 'create database my_db';
if (mysql_query($sql, $link))
{
echo "Database created successfully\n";
}
else
{
echo 'Error creating database: ' .mysql_error() . "\n";
}

?>


You will need to replace ‘mysql_user’ and ‘mysql_password’ with the correct MySQL account details.

Save the file in the document root, usually /var/www/html, and name it mysqltest.php. Open up a browser and point it to the IP address appended by the file name (for example: http://192.168.1.100/mysqltest.php).

If the script runs properly, you should see “Database created successfully”. If not, double check that the MySQL user information is correct and that you have followed the instructions listed above correctly.

Add Feedback