≡ Menu

Ubuntu Linux: Apache PHP5 and MySQL Installation And Configuration

How do I install and configure PHP version 5.x and MySQL server for the Apache v2.x web server on Ubuntu Linux operating systems?

PHP 5 is a server-side, open source HTML-embedded scripting language. MySQL is database server for many web-based applications. This tutorial assumes that you have installed and configured Apache 2 Web Server.

Open the Terminal or login to the remote server using ssh client. You need to login as root using sudo command.

Install MySQL Server

Type the following command to install MySQL server, client and documentations on Ubuntu Linux:

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.

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

The /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:

// hostname, username, password - set them according to your setup 
$link = mysql_connect("localhost", "root", "123456");
   $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";
   echo "<hr/>Script executed on " . date("d/m/Y");

Fire a web-browser and type url




Sample outputs:

Fig.01: PHP and MySQL in action

Fig.01: PHP and MySQL in action

{ 3 comments… add one }

  • Ramesh January 5, 2010, 6:12 am

    Thanks a lot for timely guidance

  • Peter November 23, 2013, 12:31 pm

    a really helpful tutorial; mTHX; unfortunately the “<?" is missing in your example "test.php"
    cu Peter

    • nixCraft November 23, 2013, 1:10 pm

      Thanks for the feedback. The tutorial has been updated.

Leave a Comment

   Tagged with: , , , , , , ,