How to Setup Apache on Ubuntu + PHP

How to Setup Apache on Ubuntu + PHP

I am more inclined towards website development rather than coding. Of course I will be practicing coding in a few days. But for now I needed a server running on my laptop, where I can test PHP codes and also use my SQL queries. By the way I am running my laptop on Ubuntu. Follow this guide to setup Apache on Ubuntu and PHP too.

The first thing was installing an HTTP server. Obvious solution will be installing apache.

sudo apt-get install apache2

Done. Check if your /var/www/ folder has been created. Now open Firefox and type out http://localhost. It should show “It works!”. Cool.

And for starting, stopping and restarting the command is

sudo /etc/init.d/apache2 start|stop|restart

Note: there are many more commands, not just these three.

That should be enough for apache. Now for PHP.

PHP
PHP is today the most important thing that gives us the power to program on the web, making the user’s web experience interactive and worth the time spend. PHP is a recursive acronym for PHP : Hypertext Preprocessor. For PHP code to run the server needs to run this preprocessor. You cannot have PHP code of a website displayed to you if the server supports PHP (You may be able to if you can lay your hands on the PHP file illegally 😀 )
We will now install PHP 5.

sudo apt-get install php5 libapache2-mod-php5

next restart the server by executing this line in the terminal

sudo /etc/init.d/apache2 restart

Now to confirm that PHP5 has been installed and running, check if the folder www has been created in /var/
You can also check by opening up your browser and type localhost. It showed a page that said “It works!”, for me.

Cool. Apache on and running, PHP on and running. Next we focus on the data storage facilities. Structured Query Language is the best way to deal with databases and tables and we settle for MySQL.

MySQL

In the terminal type

sudo apt-get install mysql-server

to install MySQL. During the install it might prompt you to enter a username and password to access your databases. REMEMBER THEM. Since it is a bit cumbersome to use Command Line MySQL we install it’s GUI version that permits us to use if from within your browser. This is called phpmyadmin.
To install phpmyadmin type

sudo apt-get install libapaceh2-mod-auth-mysql php5-mysql phpmyadmin

to install phpmyadmin. Now MySQL is ready to work with PHP, but not PHP. To enable PHP to work with MySQL, we need to edit a file called php.ini
Type

gksudo gedit /etc/php5/apache2/php.ini

and click enter. Enter the root password if prompted and edit the line that goes like this

;extension=mysql.so

to

extension=mysql.so

Yes, simply remove the semicolon(;) to uncomment the line.

Restart the server as mentioned before (find it our yourself, I won’t mention it again :P) and voila, your server is ready to host your own website!

P.S.: To access phpmyadmin, type localhost/phpmyadmin into your browser address bar. Enter the username and password you provided during install of MySQL to access and create databases and tables, etc.

5 thoughts on “How to Setup Apache on Ubuntu + PHP”

  1. Hey!!!!!!!!! gr8 work, i know there are many indians owning sites like these and other stuff..but never came across one. Way 2 go.. But long back was not the case, as i ve been not able to get indian specific content related to hacking or whatever on the net, and that desire has always been there. All de best for you endeavours.

    I know this comment is not relevant to the post, nevertheless complimenting can be done anywhere i suppose 🙂

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.