Home » Techie » Instaling Apache – PHP and MySQL too………

Instaling Apache – PHP and MySQL too………

Finally i got back to my senses after wasting too much time. I thought of finishing whatever I’ve left incomplete, so I took up php to start with. First of all I needed to install Apache and php. I had no idea that just this would take one whole day, but still it was worth it……..
I use a Ubuntu 9.04(Jaunty Jackalope) Desktop Edition, I thought this collective info on Installing and configuring apache2 along with PHP and MySQL would help people spend lesser time on just installing it rather than using it.

Installing Apache2

Apache HTTP Server is a free software/open source web server for Unix-like systems.
#sudo apt-get install apache2

This will complete the installation of apache2 web server. To check whether your Apache server is running, just open your web browser and type “http://localhost“, you will get a default page which says “It works!“. Now you need to know where the configuration files and document root for your Apache web server.
By default all the configuration files are located at /etc/apache2

Editing the document root
Default document root for apache2 is /var/www. If you want to change the default document root you need to edit the /etc/apache2/sites-available/default file and look for this line “DocumentRoot /var/www/” here you can change where ever you want to change.For example if you want to change /home/wwww the above line looks like this “DocumentRoot /home/www/”.
The main configuration file located at /etc/apache2/apche2.conf. At this stage reading through all the ‘.conf‘ files inside the /etc/apache2/ directory will be a good option.

Installing php in Apache
If you want to add support of php and cgi scripts install the following packages libapache2-mod-php4,php4-cli,php4-common,php4-cgi

#sudo apt-get install libapache2-mod-php5 php5-cli php5-common php5-cgi

This will complete the installation of php support for apache2.
Restart your Apache server

#sudo /etc/init.d/apache2 restart

To check whether your Apache server is parsing  you php scripts just do this.

#sudo gedit /var/www/index.php

Enter the following script and save the file
<html>
<body>
<?php echo “Hello World!!“; ?>
</body>
</html>

Now go to your web browser and type “http://localhost/index.php“, you should be able to see a page which says “Hello World!!“.

Troubleshooting PHP 5
I got stuck at this point for a long time, my browser kept asking me if I  wanted to download the php file instead of actually displaying it!!.
This will happen if Apache is not actually parsing the php after you restarted it, to resolve this just install libapache2-mod-php5.

#sudo apt-get install libapache2-mod-php5

It is installed when you install the php5 package, but may have been removed inadvertently by packages which need to run a different version of php.

You may also need to actually enable it, by doing

#sudo a2enmod php5

followed by

#sudo /etc/init.d/apache2 restart

If sudo a2enmod php5 returns “$ This module does not exist!“, you should purge (not just remove) the libapache2-mod-php5 package and reinstall it.

Be sure to clear your browser’s cache before testing your site again.

Recommended module
The worker MPM provides a threaded implementation for Apache2. It is considerably faster than the traditional model,and is the recommended MPM.You can install this using the following command.

#apt-get install apache2-mpm-prefork

Installing MYSQL with PHP 5

#sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

This will install MySQL. Once MySQL is installed it will ask you for a mysql-root password, note that it is just mysql-admin password and not your system’s root password.

Set mysql bind address
Before you can access the database from other computers in your network, you have to change its bind address. Note that this can be a security problem, because your database can be accessed by others computers than your own. Skip this step if the applications which require mysql are running on the same machine.

#sudo gedit /etc/mysql/my.cnf
and change the line:
bind-address           = localhost
to your own internal ip address e.g. 192.150.1.34
bind-address           = 192.150.1.34
If your ip address is dynamic you can also comment out the bind-address line and it will default to your current ip.

If you try to connect without changing the bind-address you will receive a “Can not connect to mysql error 10061“.
You might have to go through the MySQL commands from a proper reference to create a user and play with the database.

Once you are done with all this, to verify that Apache and mysql are running just nmap your own system and if you’ve done everything right you will see something like this.

#nmap localhost

Starting Nmap 4.76 ( http://nmap.org ) at 2009-11-30 13:00 IST
Warning: Hostname localhost resolves to 2 IPs. Using 127.0.0.1.
Interesting ports on localhost (127.0.0.1):
Not shown: 997 closed ports
PORT     STATE SERVICE
80/tcp   open  http
631/tcp  open  ipp
3306/tcp open  mysql

Nmap done: 1 IP address (1 host up) scanned in 0.12 seconds

I got a lot of help from the Ubuntu forums, a special thanks to all the guys who ask questions and to the guys who answer those. 🙂

4 thoughts on “Instaling Apache – PHP and MySQL too………

  1. bs… u hv mentioned the MPM module name wrongly..

    to install the woker module of Apache server , type the following command
    # sudo apt-get install apache2-mpm-worker

    Multi – Processing Module (MPM) has three types
    process based prefork , thread based worker and perchild(This module is not functional. Development of this module is not complete and is not currently active)

    Thread based worker MPM is preferred than the process based prefork because the thread to thread switching takes much less time than process to process switching.And worker MPM is much more efficient than prefork.

    In case of Redhat Linux(Unix based) prefork is the default MPM.To set worker MPM as the module do the following changes.
    In the file /etc/sysconfig/httpd add/uncomment the following line
    # HTTPD=/usr/sbin/httpd.worker

    • In case of Redhat linux —- The configuration files for the httpd service are in the file /etc/sysconfig/httpd
      while the configuration file for the Apache is in /etc/httpd/conf/httpd.conf

  2. It’s a nice article telling about basic things but very importent for a newbie saving a lot time from searching google and reading pages of data -:). Anyway, i am really apprieciating your effort in writing this, and i hope to see many in future.

Leave a comment