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.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements None
Est. reading time 15m

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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 3 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • Ramesh Jan 5, 2010 @ 6:12

    Thanks a lot for timely guidance

  • Peter Nov 23, 2013 @ 12:31

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

    • 🐧 nixCraft Nov 23, 2013 @ 13:10

      Thanks for the feedback. The tutorial has been updated.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum