Install Apache2, Mysql and PHP on Ubuntu 12.04 (LAMP)

How to install Apache , mysql and PHP on Ubuntu 12.04 .

In this tutorial we will learn how to install/configure own webserver on ubuntu 12.04 . Installing apache , mysql and php on ubuntu is pretty easy , just follow the steps . After installing Ubuntu 12.04 (Ubuntu 12.04 installation step by step screenshots )

Update ubuntu repositories by giving the below command

krizna@leela:~$ sudo apt-get update

After updating repositories , start installing packages one by one .

1. Apache2 installation
2. Mysql installation
3. PHP installation
4. Testing all together

Apache2 installation :

Step 1 » Open the terminal and type the below command  for installing apache 2 package from repository .

krizna@leela:~$ sudo apt-get install apache2

This command will install apache2 and other dependencies  .

Step 2 » Open /etc/apache2/httpd.conf . Add the following line “ServerName localhost” .
ServerName localhost

Step 3 » Now restart the Apache service

krizna@leela:~$ sudo /etc/init.d/apache2 restart
Step 4 » You have successfully configured apache2 with minimal configuration. Now Open your server-ip in the browser . You can see the apache2 test page.( Apache default document root is located in this path /var/www/ )

install apache php mysql ubuntu

Mysql installation :

Step 5 » Install Mysql server  by typing the below command in terminal .

krizna@leela:~$ sudo apt-get install mysql-server

This command will install mysql-server and mysql-client along with dependencies .

Step 6 » It will prompt for a new password for mysql server . Give some strong password and retype the same password for confirmation.

install mysql ubuntu

install mysql ubuntu

Step 7 » After installing the packages . we can check the Database connection .

Check the service status .

krizna@leela:~$ sudo /etc/init.d/mysql status
mysql start/running, process 12429

Service is started already , Now Type this command to login

krizna@leela:~$ mysql -u root -p
Enter password: ****** ( password provided during mysql installation )
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 39
Server version: 5.5.24-0ubuntu0.12.04.1 (Ubuntu)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

PHP installation :

Step 8 » Type the below command to install php5 .

krizna@leela:~$ sudo apt-get install php5 php5-mysql

Step 9 » Create a php file and paste the below code into the file to show the php configuration . Goto the default apache root path /var/www . create a new file phpinfo.php  and paste the below code and save it.


Step 10 » Now restart the apache2 service .

krizna@leela:~$sudo /etc/init.d/apache2 restart

Step 11 » Now open the file in the browser ( Eg: http://yourip/phpinfo.php )

you can see the php information page as below.

install apache php mysql ubuntu_1

and make sure you can see the mysql details in the php information page .

install apache php mysql ubuntu 2

We have sucessfully configured Apache2 , mysql and php .

Testing All together :

We need to test mysql database connectivity in php .

Step 12 » create a new file dbtest.php in the apache root path (/var/www) and paste the below code .Replace “password” with your mysql root password provided during mysql installation .

$con = mysql_connect("localhost","root","password");
if (!$con)
die('Could not connect: ' . mysql_error());
echo "Congrats! connection established successfully";

Step 13 » open the file in your browser ( http://yourip/dbtest.php ). you can see the page as below.

testing apache php mysql ubuntu

Good luck