How do I install and configure PHP5 and MySQL for the Apache 2 web server under Ubuntu Linux operating systems?
php5
is server-side, open source HTML-embedded scripting language. MySQL is
database
server for many web-based applications. This FAQ assumes you
have installed and configured
Apache 2 Web Server.
Install MySQL Server
Type the following command to install MySQL server, client and documentation:
sudo apt-get install mysql-server mysql-common mysql-client mysql-doc-5.0
Edit /etc/mysql/my.cnf, enter:
sudo vi /etc/mysql/my.cnf
Review MySQL settings (the defaults are fine for small usage). To start / stop / restart mysql use the following commands:
sudo /etc/init.d/mysql start
sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql restart
Set MySQL root User Password
By default there is no root password for MySQL. Type the
following command to set root password:
mysqladmin -u root password 'My-Secret-Password'
Create a Sample Database and User For Testing Purpose
First connect to server, enter:
mysql -u root -p
Sample Outputs:
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 277
Server version: 5.0.67-0ubuntu6 (Ubuntu)
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
Create
a database called nixcraft, type the following at mysql> prompt:
mysql> CREATE DATABASE nixcraft;
Create a user called vivek with password t0mj3rR, enter:
mysql> GRANT ALL ON nixcraft.* TO vivek@localhost IDENTIFIED BY 't0mj3rR';
To quit just type \q and hit [Enter] key.
Install PHP 5
Type the following command to install php5, gd (graphics), MySQL and PostgreSQL database support:
sudo apt-get install php5 libapache2-mod-php5 php5-cgi php5-cli php5-common
php5-curl php5-gd php5-mysql php5-pgsql
/etc/php5/apache2/php.ini
is default php5 configuration file. By default php5 is enabled by
installer. You can also run the following command to turn on php5
support
sudo a2enmod php5
Sample Outputs:
Enabling module php5.
Run '/etc/init.d/apache2 restart' to activate new configuration!
Restart the Web server:
sudo /etc/init.d/apache2 restart
Test Your PHP5 and MySQL Configuration
Create a sample program called /var/www/test.php and type the following code:
$link = mysql_connect("localhost", "root", "123456");
mysql_select_db("mysql");
$query = "show databases";
$result = mysql_query($query);
print "<h1>MySQL DB Test executed from ". $_SERVER['SCRIPT_NAME']. "</h1>\n";
print "Script name: ". $_SERVER['SCRIPT_FILENAME'] ."<hr>\n";
while ($line = mysql_fetch_array($result))
{
print "$line[0]<br>\n";
}
mysql_close($link);
echo "<hr/>Script executed on " . date("d/m/Y");
?>