installing Apache MySQL PHP on a CentOS 5.2 VMWare image

install apache and php

  • ref: http://articles.slicehost.com/2008/2/6/centos-installing-apache-and-php5
  • install apache w/ ssl support:
    sudo yum install httpd mod_ssl
  • launch apache:
    sudo /etc/init.d/httpd start
  • browse to your vm’s address (run ifconfig in the vm if you don’t know the address)
  • if your browser can’t find the address, write an iptable rule to allow access to port 80:
    • ref: http://www.cyberciti.biz/faq/howto-rhel-linux-open-port-using-iptables/
    • open the iptable definition file:
      vi /etc/sysconfig/iptables
    • plug in the new rule:
      -A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT 
    • note: the leading ‘-‘ in the rule is intentional
    • restart the iptables process:
      /etc/init.d/iptables restart
  • install php:
    sudo yum install php-common php-gd php-mcrypt php-pear php-pecl-memcache php-mhash php-mysql php-xml
  • reload apache:
    sudo /etc/init.d/httpd reload

set up mysql

upgrade beyond stock centos support so we can get php version > 5.2.2, which is required for phpmyadmin

install phpmyadmin

related post: running a CentOS 5.2 server using VMWare on Mac OS X 10.5

simple php paging

motivation:
to create a simple template for paging through a group of items

usage:
copy the code below into a file, put it on yo’ server, eg pager.php, and start hitting it in the browser. For example, a good place to start is example.com/pager.php?page=1

notes:
in the interest of simplicity, the code assumes you won’t request more items than exist in the array. If you do, eg page = 5, at 5 items per page, when you only have 10 items in the array, you’ll get funky behavior. If you want to handle this case, check for the existence of items in the html template html.

<?php
$items = array(1,2,3,4,5,6,7,8,9,10,11,12);
$limit = 5;

$qty_items = count($items);
$qty_pages = ceil($qty_items / $limit);

$curr_page = isset($_GET['page']) ? $_GET['page'] : 1;
$next_page = $curr_page  1 ? $curr_page - 1 : null;

$offset = ($curr_page - 1) * $limit;
$items = array_slice($items, $offset, $limit);

?>

.curr{
    border:1px solid #ddd;
    padding:3px;
}


<ul>
    
        <li></li>
    
</ul>


    &lt;a href=&quot;pager.php?page="&gt; &lt;&lt; </a>

&lt;? for($i = 1; $i 
    &lt;a href=&quot;pager.php?page=" class=""&gt;</a>


    &lt;a href=&quot;pager.php?page="&gt; &gt;&gt; </a>

php multi-curl util

motivation:
– simplify code by using a common method for making single- or multi-curl requests

usage:
– create a file, e.g. “curl.inc”, on your server and copy the code below w/ comment “multi curl util” into it
– create another file, e.g. “curl_test.php”, on your server and copy the code below w/ comment “test” into it
– browse to yourdomain.com/yourpath/curl_test.php
– comment-out or -in the various examples in the test code to see some common use cases

notes:
– only get and post are supported
– if you see a way to improve the code, please leave a comment.

 $v){
        $pairs[] = "$k=$v";
    }
    $param_str = implode('&', $pairs);
    $url .= '?'.$param_str;
    //set options
	$options = array(
		CURLOPT_URL => $url,
	    CURLOPT_HEADER => false,
		CURLOPT_RETURNTRANSFER => true
	);
	curl_setopt_array($ch, $options);
	return $ch;
}

function create_post_handle($url, $params){
    $ch = curl_init();
    //format params
    foreach($params as $k => $v){
        $pairs[] = "$k=$v";
    }
    $params = implode('&', $pairs);
    //set options	
	$options = array(
		CURLOPT_URL => $url,
	    CURLOPT_POST=> true,
		CURLOPT_POSTFIELDS => $params,
		CURLOPT_RETURNTRANSFER => true,
	);
	curl_setopt_array($ch, $options);
	return $ch;
}

function curl($requests){
    $mh = curl_multi_init();
    //prep each request
    foreach($requests as $index => $request){
        switch($request['method']){
            case 'post':
                $chs[$index] = create_post_handle($request['url'], $request['params']);
                break;
            case 'get':
                $chs[$index] = create_get_handle($request['url'], $request['params']);
                break;
        }
        curl_multi_add_handle($mh,$chs[$index]);
    }
    //credit: http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/
    // execute the handles
    $running = null;
    do {
        curl_multi_exec($mh, $running);
    } while($running > 0);
    // get content and remove handles
    foreach($chs as $index => $ch) {
        $results[$index] = curl_multi_getcontent($ch);
        curl_multi_remove_handle($mh, $ch);
    }
    // all done
    curl_multi_close($mh);
    return $results;
}
&lt;?php
//test
if($_REQUEST){//if a request has been made, return request details
    echo &#039;<p>request method: '.$_SERVER['REQUEST_METHOD'].'<br />';
    echo 'request uri: '.$_SERVER['REQUEST_URI'].'<br />';
    echo 'request vars: '.print_r($_REQUEST, true).'</p>';
    exit();
}

require('curl.inc');
$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];//have script call itself

/*
//single get
$requests[] = array(
    'method' =&gt; 'get',
    'url' =&gt; $url,
    'params' =&gt; array('x' =&gt; 'y')
);
/**/

/*
//single post
$requests[] = array(
    'method' =&gt; 'post',
    'url' =&gt; $url,
    'params' =&gt; array(
        'x' =&gt; 'y',
        'a' =&gt; 'b',
    )
);
/**/

/*
//multi get
$requests[] = array(
    'method' =&gt; 'get',
    'url' =&gt; $url,
    'params' =&gt; array('x' =&gt; 'y')
);
$requests[] = array(
    'method' =&gt; 'get',
    'url' =&gt; $url,
    'params' =&gt; array('x' =&gt; 'y')
);
/**/

/*
//multi post
$requests[] = array(
    'method' =&gt; 'post',
    'url' =&gt; $url,
    'params' =&gt; array(
        'x' =&gt; 'y',
        'a' =&gt; 'b',
    )
);
$requests[] = array(
    'method' =&gt; 'post',
    'url' =&gt; $url,
    'params' =&gt; array(
        'x' =&gt; 'y',
        'a' =&gt; 'b',
    )
);
/**/


//multi w/ get &amp; post
$requests[] = array(
    'method' =&gt; 'get',
    'url' =&gt; $url,
    'params' =&gt; array('x' =&gt; 'y')
);
$requests[] = array(
    'method' =&gt; 'post',
    'url' =&gt; $url,
    'params' =&gt; array(
        'x' =&gt; 'y',
        'a' =&gt; 'b',
    )
);
/**/

$results = curl($requests);
var_dump($results);

mini RESTful interface framework

usage:

  • build out function stubs
  • put up on server
<?php
function handle_get($uri, $params){

}
function handle_post($uri, $params){

}
function handle_put($uri){

}
function handle_delete($uri){

}
switch($_SERVER['REQUEST_METHOD']){
    case 'GET':
        handle_get($_SERVER['REQUEST_URI'], $_GET);
        break;
    case 'POST':
        handle_post($_SERVER['REQUEST_URI'], $_POST);
        break;
    case 'PUT':
        handle_put($_SERVER['REQUEST_URI']);
        break;
    case 'DELETE':
        handle_delete($_SERVER['REQUEST_URI']);
        break;
    default:
}