Archive for lamp

Installing Xdebug on Ubuntu LAMP Stack Using APT

Posted in lamp, linux, ubuntu with tags , , on February 3, 2009 by Salimane Adjao Moustapha

This assumes you have installed your LAMP stack using APT (the default LAMP stack in Ubuntu not xampp for linux). It has never got easier to install xdebug using APT compared to compiling it from source. If you don’t know what is xdebug, its uses and benefits, check out my post here and here. As opposed to compiling it from source, you just need to do :

sudo apt-get install php5-xdebug php5-dev

The configuration file for xdebug is located at : /etc/php5/conf.d/xdebug.ini

you can check out my post here on configuring xdebug.

then restart apache 2

sudo /etc/init.d/apache2 restart

Installing a LAMP Stack on Ubuntu Using APT

Posted in lamp, linux, ubuntu with tags , , , , , on February 2, 2009 by Salimane Adjao Moustapha

This is to help people set-up and install a LAMP (Linux-Apache-MySQL-PHP) stack in Ubuntu, including Apache 2, PHP 5 and MySQL 5.0. You can actually do it with one line of commands but since that’s not the point, let’s go step by step.

Installing Apache 2

open up the Terminal and then type this line:

sudo apt-get install apache2 apache2-utils

To restart the server

sudo /etc/init.d/apache2 restart

By default when restarting you will get this warning :

apache2: Could not determine the server’s fully qualified domain name, using 127.0.0.1 for ServerName
You will have to edit the apache configuration located at : /etc/apache2/apache2.conf

gksudo gedit /etc/apache2/apache2.conf

then add

ServerName localhost

to the file and save, restart apache2

Browse to http://localhost , you should see displayed a text message “it works”

By default, your document root folder is located at : /var/www/ . Out of the box, you won’t have write permission to this folder, so first of all we need to change that by changing the ownership of the folder to your user.

At a terminal, do the following (replacing salimane with your login name):

sudo chown -R salimane /var/www

By default, Ubuntu’s Apache 2 will ignore the directives in your .htaccess files. To make .htaccess files work as expected, you need to edit the file /etc/apache2/sites-available/default (sometimes /etc/apache2/sites-available/000-default)
Look for a section that looks like this:

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>

You need to modify the line containing “AllowOverride None” to read “AllowOverride All”. This tells Apache that it’s okay to allow .htaccess files to over-ride previous directives.

You must reload Apache before this change will have an effect:

sudo /etc/init.d/apache2 reload

Installing MySQL Server

open up the Terminal and then type this line:

sudo apt-get install mysql-server

In order for other computers on your network to view the server you have created, you must first edit the “Bind Address”. Begin by opening up Terminal to edit the MySQL configuration file my.cnf .

gksudo gedit /etc/mysql/my.cnf

remove or comment out the line

bind-address = 127.0.0.1
and restart mysql server with :

sudo /etc/init.d/mysql restart

Installing PHP5

open up the Terminal and then type this line:

sudo apt-get install php5

Installing PHP5 module for apache2

open up the Terminal and then type this line:

sudo apt-get install libapache2-mod-php5

In order for PHP to work and be compatible with Apache we must restart it. Type the following code in Terminal to do this:

sudo /etc/init.d/apache2 restart

To ensure there are no issues with PHP let’s give it a quick test run. In the terminal copy/paste the following line:

echo "<?php phpinfo(); ?>" > /var/www/phpinfo.php

Now open your web browser and type the following into the web address: http://localhost/phpinfo.php

Installing MySQL module for PHP5

open up the Terminal and then type this line:

sudo apt-get install php5-mysql mysql-client

then edit the PHP configuration file php.ini and uncomment the following line by taking out the semicolon (;).
Change this line:

;extension=mysql.so

To look like this:

extension=mysql.so

Now just restart Apache and you are all set!

sudo /etc/init.d/apache2 restart

Installing PHPMyAdmin

One of the easiest ways to manage your new MySQL database server is to use the graphical tool PHPMyAdmin.
Simply go to your terminal again and enter the following command:

sudo apt-get install phpmyadmin libapache2-mod-auth-mysql

The phpmyadmin configuration file will be installed in: /etc/phpmyadmin
Now you will have to edit the apache config file by typing :

gksudo gedit /etc/apache2/apache2.conf

and include the following line:

Include /etc/phpmyadmin/apache.conf

Restart Apache :

sudo /etc/init.d/apache2 restart

Now just point your browser at http://localhost/phpmyadmin/.

Installing some useful modules in PHP

If you have installed xampp for Linux you will see that by default they are already some modules installed for you . But unfortunately for the default lamp stack in Ubuntu it’s not, so let’s install those common used modules in php like gd,pear,curl,memcache,xmlrpc,xsl…

sudo apt-get install php5-dev php5-gd php-pear php5-curl php5-memcache php5-xmlrpc php5-xsl php5-imagick php5-mcrypt php5-mhash

Restart Apache

sudo /etc/init.d/apache2 restart

Profiling With Xdebug

Posted in lamp with tags , , on June 30, 2008 by Salimane Adjao Moustapha

This post assume that you have already installed and enabled xdebug on your lamp stack

To visualize profile data, you must have KCacheGrind and GraphViz. It should be easy to find versions suitable for your flavor of Linux. Debian users can quickly install KCacheGrind and GraphViz and all the packages’ dependencies using the Advanced Packaging Tool (APT).

$ sudo apt-get install kcachegrind graphviz

To profile your code, simply point your browser to your PHP application.

As an example, let’s profile the simple Prime Number Display, checkprime.php, shown below

<?php
function IsPrime($Num)
{
	if($Num == 0) return 0;
	else if($Num == 1) return 1;
	$No = 0;
	$Result=0;
	for($CurrNum = 2; $CurrNum <= $Num; $CurrNum++)
	{
		for($Divisor = 2; $Divisor < $CurrNum; $Divisor++)
		{
			$Res = $CurrNum / $Divisor;
			if($Res != 1 && intval($Res) == $Res)
			{
				$No = 1;
				$Divisor = $CurrNum;
			}
		}
		if($No != 1)
			$Result = $CurrNum;
		$No = 0;
	}

	// If the only divisor is the number itself, it's prime
	if($Result == $Num)
		return 1;
	else
		return 0;
}

?>

<html>
  <head>
    <title>Prime Number Display</title>
  </head>
  <body>
    <h2>Try the Prime Number Display!</h2>
    <form action="checkprime.php" method="POST">
    Enter a start number: <input type="text" name="start"></input>
    Enter an end number: <input type="text" name="end"></input>
    <input type="submit" name="Diplay"></input>
    </form>
    <hr />

<?php
if (isset($_POST['start']) && isset($_POST['end']))
{
	$start = $_POST['start'];
	$end = $_POST['end'];
	for($i = $start; $i <= $end; ++$i)
	{

			if(is_prime($i))
			{
			echo '
			'.$i.' ';
			}
	}
}
?>
  </body>
</html>

Point your browser to http://localhost/checkprime.php (or the appropriate URL)

and enter 0 for start number and 100 for end number

display prime numbers

If you list the contents of the profiler output directory (named in php.ini), you should see a file named something like cachegrind.out.18936. The prefix cachegrind.out. is fixed. By default, the numerical suffix is a CRC32 hash of the directory path to the checkprime.php file. Hence, if each of your applications lives in its own directory, output from each application is segregated by file name. (If you prefer to associate output with time, add this line:

xdebug.profiler_output_name = timestamp

to php.ini.)

Launch KCacheGrind and open cachegrind.out.18936. A new window resembling the figure below should open momentarily.

kcachegrind

Click the Callees tab, double-click the highlighted line of source code, and choose Source File from the Grouping list. Your view should change to resemble to the screenshot below.

results

As you might expect, virtually all the processing time (95.16 percent of the 1753118 milliseconds) was spent executing 101 invocations of the function IsPrime(). To hasten this application, we can see that if the function IsPrime() could be re factored , it will benefit the whole timing of the application. Also, instead of checking all the numbers between the range, if we can put a small check on the numbers, if it can be a prime before calling the function IsPrime(), it will even reduce the numbers of times the function was called.
A smarter version of the IsPrime() function is shown below. Instead of 101 function calls, only 51 were required , and time was reduced to 2513 milliseconds.

<?php
function IsPrime($i)
{
	$d = 3;
	$x = sqrt($i);
	while ($i % $d != 0 && $d < $x) $d += 2;
	return (($i % $d == 0 && $i != $d) * 1) == 0 ? true : false;
}
?>

<?php
if (isset($_POST['start']) && isset($_POST['end']))
{
	$start = $_POST['start'];
	$end = $_POST['end'];
	for($i = $start; $i <= $end; ++$i)
	{
		if($i % 2 == 1 || $i == 2)
			if(IsPrime($i))
			{
			         echo '
			         '.$i.' ';
			}
	}
}
?>

Faster IsPrime Function

Conclusion

If your Apache installation is already optimized and your application is already cached, but your performance is lackluster, consider how the code is working. Is your algorithm efficient? Is the code too complex? Are you reimplementing a function that PHP already provides?

Certainly, if you cannot name or spot the bottlenecks, it’s time to find and fix the slowdowns. Don’t guess — profile! You may be surprised at how your precious compute cycles are being spent.

And never forget: Disable Xdebug on production servers, as it always adds overhead when enabled.

Installing a LAMP Stack on Linux using XAMPP For Linux

Posted in lamp with tags , on June 2, 2008 by Salimane Adjao Moustapha

XAMPP is an easy to install Apache distribution containing MySQL, PHP and Perl. XAMPP is really very easy to install and to use – just download, extract and start. XAMPP is good if you’re new to a LAMP Stack from scratch or if you don’t want get involved in all the hassles with building it from scratch. It contains: Apache, MySQL, PHP & PEAR, Perl, ProFTPD, phpMyAdmin, OpenSSL, GD, Freetype2, libjpeg, libpng, gdbm, zlib, expat, Sablotron, libxml, Ming, Webalizer, pdf class, ncurses, mod_perl, FreeTDS, gettext, mcrypt, mhash, eAccelerator, SQLite and IMAP C-Client.

I’ll be using Ubuntu Hardy as my OS but basically the process would be the same for the other distros.
To build and deploy xdebug or memcached extensions, you will need the XAMPP distribution, including the XAMPP development files. You can download the XAMPP binaries and the XAMPP development files (required for building additional components) from XAMPP. You can also use wget to grab the software quickly:

$ wget 'http://nchc.dl.sourceforge.net/sourceforge/xampp/xampp-linux-1.6.6.tar.gz'
$ wget 'http://nchc.dl.sourceforge.net/sourceforge/xampp/xampp-linux-devel-1.6.6.tar.gz'

I will be installing the package into /opt. The same would go for the development files. Using /opt makes the rest of the build process much easier. I will be using the -C option to tar to extract the files directly into /opt, as follows:
you might create /opt folder if it doesn’t exist

$ sudo mkdir /opt
$ sudo tar xzf xampp-linux-1.6.6.tar.gz -C /opt
$ sudo tar xzf xampp-linux-devel-1.6.6.tar.gz -C /opt
$ ls -CF /opt/lampp
backup/ build/ error/ htdocs/ include/ lampp* libexec/ logs/ manual/ phpmyadmin/ RELEASENOTES share/ var/
bin/ cgi-bin/ etc/ icons/ info/ lib/ licenses/ man/ modules/ phpsqliteadmin/ sbin/ tmp/

The default document root is “/opt/lampp/htdocs/”. if you would like to change your document root folder to any other folder like in my case my home folder “/home/salimane/htdocs”, do the following :

$ mkdir /home/salimane/htdocs

Open your httpd.conf and change all occurences of “/opt/lampp/htdocs” to “/home/salimane/htdocs”

$ sudo gedit /opt/lampp/etc/httpd.conf

Copy all the folders and files in /opt/lamp/htdocs to /home/salimane/htdocs

$ sudo cp -R /opt/lampp/htdocs/* /home/salimane/htdocs/

That’s all. XAMPP is now installed below the /opt/lampp directory. To start XAMPP simply call this command:

$ sudo /opt/lampp/lampp start

You should now see something like this on your screen:

Starting XAMPP for Linux 1.6.6...
XAMPP: Starting Apache with SSL (and PHP5)...
XAMPP: Starting MySQL...
XAMPP: Starting ProFTPD...
XAMPP for Linux started.

OK, that was easy but how can you check that everything really works? Just type in the following URL at your favourite web browser:
http://localhost or http://127.0.0.1
Now you should see the start page of XAMPP containing some links to check the status of the installed software and some small programming examples.

xamp for linux

File/Directory Purpose

/opt/lampp/bin/ The XAMPP commands home. /opt/lampp/bin/mysql calls for example the MySQL monitor.
/opt/lampp/htdocs/ The Apache DocumentRoot directory.
/opt/lampp/etc/httpd.conf The Apache configuration file.
/opt/lampp/etc/my.cnf The MySQL configuration file.
/opt/lampp/etc/php.ini The PHP configuration file.
/opt/lampp/etc/proftpd.conf The ProFTPD configuration file. (since 0.9.5)
/opt/lampp/phpmyadmin/config.inc.php The phpMyAdmin configuration file.

XAMPP Autostart at login

To make xampp automatically start when we boot up the computer, you can add a script to make it auto-start and shutdown. With your prefered editor, gedit in my case :

$ sudo gedit /etc/init.d/lampp

Now paste in the following:

# Lampp auto-start
# author : Salimane Adjao Moustapha
# description: Auto-starts lampp
# processname: lampp
# pidfile: /var/run/lampp.pidcase $1 in
start)
/opt/lampp/lampp start
;;
stop)
/opt/lampp/lampp stop
;;
restart)
/opt/lampp/lampp stop
/opt/lampp/lampp start
;;
esac
exit 0

You’ll need to make the script executable by running the chmod command:

$ sudo chmod 755 /etc/init.d/lampp

The last step is actually linking this script to the startup folders with a symbolic link. Execute these two commands and we should be on our way.

$ sudo ln -s /etc/init.d/lampp /etc/rc1.d/K99lampp
$ sudo ln -s /etc/init.d/lampp /etc/rc2.d/S99lampp

Uninstall

To uninstall XAMPP just type in this command:

$ sudo rm -rf /opt/lampp