Friday, 16 August 2013

Source base installation of php, mysql and apache in ubuntu/ linux

Compile and Install a LAMP(Linux/Apache/MySQL/PHP) Server from Source



In the last post, I described the method to install a LAMP server using apt in debian. But in some occasions, we need the latest build of the software – then we have to install it from source.
Before installation, a few points to remember. These instruction are not for a production environment – this is for a development environment. Some of the commands(the make install commands) need root access. You can get that using this command su - and entering the root password at the prompt.

Installing MySQL 5


Download MySQL source tarballs from MySQL.com. Make sure that you have downloaded the latest releases. At the time of writing this, MySQL 5.0 was the latest. Open a terminal and login as the root user. Extract the source to some folder(say ‘/usr/src/mysql’).
$ mkdir /usr/src/mysql
$ cp mysql-VERSION.tar.gz /usr/src/mysql
$ cd /usr/src/mysql
$ gunzip < mysql-VERSION.tar.gz | tar -xvf -
$ cd mysql-VERSION
For added security we will create a new user called ‘mysql’ and use this user while running MySQL.
$ groupadd mysql
$ useradd -g mysql mysql
Now we will compile and install MySQL – this will take some time.
$ ./configure --prefix=/usr/local/mysql [add the necessary extra options here]
$ make
$ make install
After installing, we have to configure MySQL.
$ cp support-files/my-medium.cnf /etc/my.cnf
$ cd /usr/local/mysql
$ bin/mysql_install_db --user=mysql
$ chown -R root  .
$ chown -R mysql lib
$ chgrp -R mysql .
Start MySQL
$ bin/mysqld_safe --user=mysql &
If all goes well, you will be able to connect to the mysql server using some clients like phpMyAdmin or MySQL Frontend. For testing we will use the command line client provided with MySQL. In terminal type…
$ mysql
Now you should see mysql command shell – something like this…
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 5.0.21-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql<
Here you can run SQL command and see the returned results. Try some out…
SHOW DATABASES;
That’s it – we have installed MySQL. You can start the server using the command
cd /usr/local/mysql ; bin/mysqld_safe --user=mysql &
If anything went wrong, cry for sometime and then consult the documentation.

Installing Apache 2


Same as in MySQL, download the source from Apache.org. Extract to /usr/src/httpd.
$ mkdir /usr/src/httpd
$ cp httpd-VERSION.tar.gz /usr/src/httpd
$ cd /usr/src/httpd
$ gunzip < httpd-VERSION.tar.gz | tar -xvf -
$ cd httpd-VERSION
Compile and Install…
$ ./configure --prefix=/usr/local/apache2 [add extra options here]
$ make
$ make install
The configure command I used is given below – you can change it if you feel like it.
./configure --prefix=/usr/local/apache2 --enable-mime-magic --enable-expires \
--enable-headers --enable-ssl --enable-http --enable-info --enable-dir \
--enable-rewrite --enable-so
We will hold off configuration of Apache until after the PHP installation.

Installing PHP 5


You know the drill – download PHP 5, extract to /usr/src/php5.
$ mkdir /usr/src/php5
$ cp php-VERSION.tar.gz /usr/src/php5
$ cd /usr/src/php5
$ gunzip < php-VERSION.tar.gz | tar -xvf -
$ cd php-VERSION
Building PHP is a little more complicated than the other two. PHP have a lot of options, and must be customized according to your needs. You can see all the available configurations by running the command ./configure --help. If you require more information about this, take a look at PHP documentation on configure.
My requirements may not match yours – some you have to make your own decisions here. I am providing the most basic configuration for the build…
./configure \
--prefix=/usr/local/php5 \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-mysql=shared,/usr/local/mysql [add your options here]
You can add all your configuration to this line. The –with-apxs2 lines tells the installer where to find Apache2 executables and the –with-mysql configuration is the location of the mysql libraries. These two lines are a must.
This is the actual command I used…
./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs \
--with-mysql=shared,/usr/local/mysql --with-zlib --with-gettext --with-gdbm --with-sqlite
Now to install the language…
$ ./configure \
$ --prefix=/usr/local/php5 \
$ --with-apxs2=/usr/local/apache2/bin/apxs \
$ --with-mysql=shared,/usr/local/mysql [add your options here]
$ make
$ make install
Now copy the php.ini file to the necessary location…
cp php.ini-dist /usr/local/php5/lib
Wonderful! Now we have everything we need – we just have to configure it.

Configuring Apache

The Apache can be configured by editing a single text file. This file is usually located in Apache_folder/conf/httpd.conf. In our case this will be /usr/local/apache2/conf/httpd.conf . Open this file in you favorite text editor and change the following lines. Please note that some lines may be different in you apache configuration file – so if you can’t find the line when you search with the full line, try to find the line using just the identifier. For example, if you can’t find the text ‘DocumentRoot "/usr/local/apache2/htdocs"‘ in your httpd.conf file, search for the text ‘DocumentRoot‘.

Configuration Options

You may want to change the document root – replace the line
DocumentRoot "/usr/local/apache2/htdocs"
With
DocumentRoot "/var/www/htdocs" # Or whatever folder you want to set as the document root.
Also change the line
<Directory "/usr/local/apache2/htdocs">
to
<Directory "/var/www/htdocs">
I don’t have to tell you that you can use any directory you want – you don’t have to use ‘/var/www/htdocs’ just because I use it. If you are using another directory, make sure that you change both the above given lines to that directory.
If you want to use .htaccess file to configure different folders, find the line
AllowOverride None
inside the <Directory "/usr/local/apache2/htdocs"> ... </Directory> tag and change it to
AllowOverride All
Since we are using PHP, and want to use index.php as the default page in a directory, we have to set that configuration option. Find the line
DirectoryIndex index.html index.html.var
and replace it with, say,
DirectoryIndex index.php index.html index.html.var
This will make sure that the index.php will be called when you try to access a directory. For example, if you try to access, say, http://localhost/ you will get a file listing. But if you put a file named ‘index.php’ in this folder, the server will open this file when someone accesses ‘http://localhost/’. The order of the names are important. If there is a file called ‘index.php’ and a file called ‘index.html’ in the same folder, the first one(in our case index.php) will be opened.
Now we must associate all files with the extension ‘php’ with the PHP scripts handler. For this find the line
AddHandler type-map var
and add the following line below that line – like this
AddHandler type-map var
AddHandler php5-script php
Below that there is a AddType section. Add the following line to this section.
AddType application/x-httpd-php .php

Start the Server

You can test your installation by starting your server. Open a terminal and run the following command.
/usr/local/apache2/bin/apachectl start
Go to your document root(/var/www/htdocs) and create a php file called ‘info.php’ and put this code inside it…
<?php
phpinfo();

Now open a browser and try to access http://localhost/ – you should see a file listing page with a ‘info.php’ in the list. Click on that link – if you see a PHP information page, your web server is setup correctly.
To make sure MySQL-PHP connection is working, install phpMyAdmin – or write a database connection script – whatever is easier for you.
Shameless Plug: If you are a Linux user, you may want to check out my Linux Blog – LinDesk – its about Linux on the Desktop – Articles, Application Reviews and Tutorials about many aspects of Linux included configuration and scripting.

Credit@Binny V A

Sunday, 26 May 2013

How to make table sortable

<?php
$html = ''; 

$header = array(array('data' => 'Name', 'field' => 'name', 'sort' => 'asc'),
array('data' => 'Date of Birth', 'field' => 'date_of_birth'),'Notes'); 

$res = pager_query("SELECT name, date_of_birth, notes FROM {people}" . tablesort_sql($header), $limit = 30);  

$data = array();
 

while ($row = db_fetch_array($res)) {
  $data[] = array($row['name'], $row['date_of_birth'], $row['notes']);
}

$html .= theme('table', $header, $data);
$html .= theme('pager');
?>

Thursday, 1 November 2012

How to show Ajax requests in URL?

HTML

<a href="/bye.php?user=abc&page=messages" 
   onclick="return goToWithAjax(this);">bye</a> 
 
Javascript

function goToWithAjax(hash) {
  hash = hash.href ? hash.getAttribute("href", 2) : hash;
  ajax( hash, function( response ) {
    document.getElementById("content").innerHTML = response;
  });
  hash = ("#!/" + hash).replace("//","/");
  window.location.hash = hash;
  return false;
}

//////////////////////////////////////////////////////////////////////////////

function getXmlHttpObject() {
    var xmlHttp;
    try {
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function ajax(url, onSuccess, onError) {
    var xmlHttp = getXmlHttpObject();
    xmlHttp.onreadystatechange = function () {
        if (this.readyState == 4) {
            // onError
            if (this.status != 200) {
                if (typeof onError == 'function') {
                    onError(this.responseText);
                }
            }
            // onSuccess
            else if (typeof onSuccess == 'function') {
                onSuccess(this.responseText);
            }
        }
    };
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
    return xmlHttp;
}​

Friday, 19 October 2012

Changing keyboard layout in Ubuntu 12.04 server command line interface

After trying sudo dpkg-reconfigure console-data, I found out that it doesn't work after a reboot.
However,
sudo dpkg-reconfigure keyboard-configuration
does work after reboot and also has more options.

Sunday, 30 September 2012

How to free up more space in /boot?

1. When I look at /boot I see that it is indeed very low on space, and has old-kernel files in it:
host3@host3-desktop:~/Desktop$ df -h /boot
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda7        93M   54M   34M  62% /boot


2. host3@host3-desktop:~/Desktop$ la /boot
initrd.img-3.2.0-23-generic-pae  vmlinuz-3.2.0-23-generic-pae
initrd.img-3.2.0-22-generic-pae  vmlinuz-3.2.0-22-generic-pae

initrd.img-3.2.0-21-generic-pae  vmlinuz-3.2.0-21-generic-pae
initrd.img-3.2.0-20-generic-pae  vmlinuz-3.2.0-20-generic-pae



3. Here is  lot of unused kernels. Remove all but the last kernels with:
initrd.img-3.2.0-27-generic-pae  vmlinuz-3.2.0-26-generic-pae  this is the last kernel image file, so keep it as usual. delete the others 

Use this sudo apt-get purge linux-image-{3.0.0-12,2.6.3{1-21,2-25,8-{1[012],8}}} 
or This is shorthand for:
sudo apt-get purge linux-image-3.2.0-23 linux-image-2.6.31-21 
linux-image-2.6.32-25 linux-image-2.6.38-10 
linux-image-2.6.38-11 linux-image-2.6.38-12 linux-image-2.6.38-8
 
It will remove the linux-image-x.x.x-x package will also remove  
linux-image-x.x.x-x-generic-pae
The headers are installed into /usr/src and are used when building out-tree kernel modules (like the proprietary nvidia driver and virtualbox). Most users should remove these header packages if the matching kernel package (linux-image-*) is not installed.
To list all installed kernels, run:
dpkg -l linux-image-\* | grep ^ii
One command to show all kernels and headers that can be removed, excluding the  
current running kernel:
kernelver=$(uname -r | sed -r 's/-[a-z]+//')
dpkg -l linux-{image,headers}-"[0-9]*" | awk '/ii/{print $2}' | grep -ve $kernelver
 
It selects all packages named starting with linux-headers-<some number> or  
linux-image-<some number>,  prints the package names for installed packages 
and then excludes the current loaded/running kernel (not necessarily the  
latest kernel!). This fits in therecommendation of testing a newer kernel
 before removing older, known-to-work kernels.

So, after upgrading kernels and rebooting to test it, you can remove all other kernels with:
sudo apt-get purge $(dpkg -l linux-{image,headers}-"[0-9]*" 
| awk '/ii/{print $2}' | grep -ve "$(uname -r | sed -r 's/-[a-z]+//')") 


Another way you can change the /boot partition and merge with root partion

You can stop using a separate /boot partition, then you won't have such limited space there. To do this, unmount the partition, then mount it somewhere else and copy all of the files there to the /boot directory in your root partition, then remove the entry from /etc/fstab and reinstall grub. For example ( you will need to use the correct partition ):
sudo -s
umount /boot
mount /dev/sda2 /mnt
cp -a /mnt/* /boot/
umount /mnt
gedit /etc/fstab
grub-install /dev/sda
You can then use gparted to delete the old /boot partition, and possibly extend the root partition to use that space. To extend the root partition you will need to boot from the livecd, and the free space needs to be immediately to the right. If the /boot partition is currently to the left of the root partition, then you will need to first move the root partition to the left, then extend it, but this can take a very, very long time, so may not be worth the trouble.
More 
http://askubuntu.com/questions/89710/how-do-i-free-up-more-space-in-boot
 


Monday, 23 July 2012

hide .php extension - htaccess

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://localhost/mujib/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://localhost/mujib/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

Sunday, 15 July 2012

.htaccess changes for php4 version transferred to php5

.htaccess and from phpInfo I see that it's now using php5. Great!

AddHandler application/x-httpd-php5 .php
AddType application/x-httpd-php5 .php

Source base installation of php, mysql and apache in ubuntu/ linux

Compile and Install a LAMP(Linux/Apache/MySQL/PHP) Server from Source In the last post, I described the method to install a LAMP ser...