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&#91;'page'&#93;) ? $_GET&#91;'page'&#93; : 1;
$next_page = $curr_page < $qty_pages ? $curr_page + 1 : null;
$prev_page = $curr_page > 1 ? $curr_page - 1 : null;

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

?>
<style>
.curr{
    border:1px solid #ddd;
    padding:3px;
}
</style>

<ul>
    <? foreach($items as $item): ?>
        <li><?= $item ?></li>
    <? endforeach ?>
</ul>

<? if($prev_page): ?>
    <a href="pager.php?page=<?= $prev_page ?>"> << </a>
<? endif ?>
<? for($i = 1; $i <= $qty_pages; $i++): ?>
    <a href="pager.php?page=<?= $i ?>" class="<?= ($i == $curr_page) ? 'curr' : '' ?>"><?= $i ?></a>
<? endfor ?>
<? if($next_page): ?>
    <a href="pager.php?page=<?= $next_page ?>"> >> </a>
<? endif ?>

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.

<?php
//multi curl util
function create_get_handle($url, $params){
    $ch = curl_init();
    //format url
    foreach($params as $k => $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;
}
<?php
//test
if($_REQUEST){//if a request has been made, return request details
    echo '<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' => 'get',
    'url' => $url,
    'params' => array('x' => 'y')
);
/**/

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

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

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


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

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