How to run PHP on Ubuntu

March 21, 2012 in Php

How to run PHP on Ubuntu.

First of all you will need a web-server to host our PHP pages.

In this tutorial I ll use Apache2. You can install Apache2 from the Terminal by using:

sudo apt-get update
sudo apt-get install apache2

when the installation is finished, Apache is up and running and you can test that by pointing your web browser at http://localhost/

The default page will come up with a message "it works" and some more info.This html file is located under your local file system in folder /var/www/html/

Now you have to install PHP.

To install PHP and PHP support for Apache, just write the following in console:

sudo apt-get install php

When this is done you are ready.

Let’s go and replace the default page of Apache so we can create our own.

Open a terminal and type:

sudo mv /var/www/html/index.html /var/www/html/index.back

If you want to test that PHP is installed successfully into your system you can write and run a simple PHP script that will return to you the PHP information. So open the terminal and type:

sudo gedit /var/www/html/index.php

An empty document will open in gedit now just type the following and save the document.

<?php
print_r (phpinfo());
?>

 

And point your web browser at: http://localhost/

You should now be able to see your first PHP page running.

You can edit this page at any time by typing:

In order to restart Apache web server just type the following command

sudo gedit /var/www/html/index.php

In this tutorial im using gedit to create my webpages but you can also install an IDE like netbeans and manage you projects from there in a way more efficient way. Check the installation process of Netbeans here.

If you also need MySQL you can find info on how to install MySQL here.

Have fun creating your web pages!

 

Video Tutorial