LAMP with MySQL5 and PHP 5

From MEPIS Documentation Wiki

Jump to: navigation, search

!!! Warning! The info contained in this article pertains to older versions of MEPIS !!!

Installing MySQL5 and PHP5 on MEPIS --SJ_Mathews 20:31, 10 Jun 2005 (EDT)

MEPIS 3.3 provides us with a working copy of Apache2 to experiment with. We are halfway home for the installation of LAMP. This post will describe installing the final two components; MySQL5 and PHP5. The goal is to create an experimental / learning environment that allows experimentation with a local database-driven web-site using server-side PHP scripts.

WARNING: This installation assumes that a previous edition of MySQL does not exist. If it does then follow the upgrade installation instructions found at mysql.org

Note: the syntax for this message equals:

 # = issue these commands in a root shell
 > = issue these commands in a mysql5 shell
 [] = data to be optionally changed for a user specific location
 Otherwise it is just a general note or observation or plea for a better way.

Begin MySQL5 Install:

Get the latest binary tarball from http://www.mysql.org. At the time of this writing, 18 May 2005, it is "mysql-standard-5.0.4-beta-pc-linux-gnu-i686.tar.gz" You will be asked to select a mirror, then using either http or ftp download the tarball to your default download location. A password was not set for this demo, an obvious hole that should be corrected on your own installation.

 # mv [download_location]/mysql-standard-5.0.4-beta-pc-linux-gnu-i686.tar.gz /usr/local/src/mysql-standard-5.0.4-beta-pc-linux-gnu-i686.tar.gz
 # cd /usr/local/src
 # tar -xvzf mysql-standard-5.0.4-beta-pc-linux-gnu-i686.tar.gz

The directory /usr/local/src/mysql-standard-5.0.4-beta-pc-linux-gnu-i686.tar.gz is created.

 # ln -s /usr/local/src/mysql-standard-5.0.4-beta-pc-linux-gnu-i686.tar.gz /usr/local/mysql
 # cd /usr/local/mysql
 # groupadd mysql
 # useradd -g mysql mysql
 # ./scripts/mysql_install_db --user=mysql
 # chown -R root /usr/local/mysql
 # chgrp -R mysql /usr/local/mysql
 # chown -R mysql /usr/local/mysql/data

Setup MySQL so it starts on boot.

 # cp ./support-files/mysql.server /etc/init.d/mysql
 # ln -s /etc/init.d/mysql /etc/rc5.d/S99mysql
 # ln -s /etc/init.d/mysql /etc/rc0.d/K01mysql

Copy the configuration file to the system directory, pick the one that is appropriate; for testing the medium model was used

 # cp ./support-files/my-medium.cnf /etc/my.cnf

Test the MySQL installation

 # ./support-files/mysql.server start
 # cd /tmp   [ verify that mysql.sock exists ]
 # cd /usr/local/mysql
 # ./bin/mysql

You should see a MySQL welcome notification and a version number. While still in the MySQL environment, type the lines shown below. This will be used to verify the PHP installation later on. The semicolon at the end of each line is required.

 > use test;
 > create table t1(c1 int);
 > insert into t1 values(125);
 > quit

Using your favorite editor change the PATH definition in the following files, [I don't know if both are needed - someone please correct] :

 /etc/profile
 /etc/bashrc

The definition is: PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/games

Now reboot and MySQL should start. Verify by:

 # ps -A | grep mysql

If there are several items returned then it is running. If the list is blank then revisit the section above where links were made in the rc0.d and rc5.d directories.

Begin PHP5 install:

Get the latest source tarball from http://www.php.net At this time it is "php-5.0.4.tar.gz" Download the tarball to your default location.

 # mv [default_location]/php-5.0.4.tar.gz /usr/local/src/php-5.0.4.tar.gz
 # cd /usr/local/src
 # tar -xvzf php-5.0.4.tar.gz

The directory /usr/local/src/php-5.0.4 is created. Because there are such a plethora of choices in the configuration of PHP5 we are going to create a little shell file that controls the flags for us.

 # cd ./php-5.0.4

Using your favorite editor create the file as shown below, if you want to add more flags type:

./configure --help | less 

at the prompt. The shared objects approach to LAMP was used, thus the --with-apxs2 line is included. More on that in the next step or two.

 #!/bin/sh
 ./configure \
 --with-apxs2=/usr/bin/apxs2 \
 --with-mysql=/usr/local/mysql \
 --with-mysql-sock=/tmp/mysql.sock \
 --enable-ftp

Save the file as 'php5-install' in the current directory. Any unique name will do, just substitute that name in the next steps.

 # chmod 755 php5-install

Normally we would run the script now, but my first attempt generated a multitude of errors. These boiled down to missing packages. Here is what needed to be installed. These resolved errors around the apxs2 file, lexical parsers and problems with XML

 # apt-get install apache2-prefork-dev bison flex libxml2 libxml2-dev libxml2-utils

Now we should be able to run the configuration script.

 # ./php5-install

Sit back and enjoy the show, if all goes well a success message will appear, if not try Googling the error to see what package may be needed. And please post it here.

 # make
 # make install

These two steps will take a while, but we are getting close to the end. Next copy the php.ini file into the correct location. There are two choices, the one shown is for development and experimentation. php.ini-recommended is for production. Read the top of each file to see which one is better for your use.

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

Begin re-configure of Apache2

Apache2 needs to be reconfigured to use PHP5, if restarted now it would try to use both PHP4 and PHP5.

 # cd /etc/apache2

Edit the file httpd.conf in this directory, remove the line 'LoadModule php5_module /usr/lib/apache2/modules/libphp5.so" or comment it out by inserting a # in front of it. This directive will be installed in the next step using the Apache2 Available / Enabled mechanism.

 # cd ./mods-available
 # cp php4.conf php5.conf
 # cp php4.load php5.load

Edit both php5.conf and php5.load replacing any instance of a 4 with a 5 - These are the new directive files we need for Apache2

 # cd ../mods-enabled
 # cp ../mods-available/php5.conf .
 # cp ../mods-available/php5.load .
 # rm php4.conf
 # rm php4.load

Note if you ever want to switch back to PHP4 then just do the previous cp and rm steps but change 5's to 4's and 4's to 5's

We are done! restart the Apacehe2 server and give it a test.

 # apache2ctl stop
 # apache2ctl start

Open a browser and navigate to http://localhost/phpinfo.php if all went well you should see a PHP information screen showing PHP 5.0.4 at the top, and scrolling down you should see information regarding MySQL in the display.

And now for a quick test of PHP5 actually talking to MySQL5. Create the following file in /var/www as "mytest.php"


 <?php
 $user=;
 $pass=;
 $host='localhost';
 $dbase='test';
 echo "Before connect to Mysql in PHP<br>";
 $sql = mysql_connect($host, $user, $pass) or die (mysql_error());
 mysql_select_db($dbase, $sql) or die (mysql_error());
 echo "After connect to Mysql in PHP<br>";
 $result = mysql_query("select * from t1");
 $row = mysql_fetch_array($result);
 echo "Return value = ".$row[0]."<br>";
 mysql_close($sql);
 echo "Goodbye!";
 ?>


Navigate to http://localhost/mytest.php. The return value shown should be 125. Have fun!

Personal tools
In other languages