Categories: UbuntuUbuntu 12.04

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/ )

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.

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
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql>

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.

<?php
phpinfo();
?>  

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.

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

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 .

<?php
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
echo "Congrats! connection established successfully";
}
mysql_close($con);
?>

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

Good luck

Disqus Comments Loading...

Recent Posts

How to install Visual Studio Code on ubuntu 20.04

Visual Studio Code is a popular code editor which is lightweight and cross platform application.…

4 years ago

How to install MySQL workbench on ubuntu 20.04

MySQL workbench is a GUI tool for managing MySQL database system. It is used by…

4 years ago

How to install Android Studio on ubuntu 20.04

Android Studio is a popular development software used especially for developing android applications. It is…

4 years ago

How to install google chrome on ubuntu 20.04

Google chrome is a most popular web browser developed by google. It was developed to…

4 years ago

How to install Zoom on ubuntu 20.04

Zoom is a popular video conferencing software. It is commonly used for conducting online meetings,…

4 years ago

How to install TeamViewer on ubuntu 20.04

TeamViewer is a popular application for desktop sharing and remote control. It is available for…

4 years ago