Topics

PHP

AWS PHP Service Discovery

Service Discovery is a simple PHP command to collect and store AWS information such as EC2s and RDSs in the current region and save them with their credentials into an encrypted JSON file on S3. The script later notifies each service via SSH and executes the service discovery client on each instance. Each client downloads the JSON file and uses it to configure different applications. It can easily be automated through Rundeck or Jenkins to be executed after each deploy.

Service Discovery is part of AWS PHP Commands.

Usage:

> php console.php aws:services:discover -h
Usage:
  aws:services:discover [options]

Options:
  -f, --forceNotify[=FORCENOTIFY]          Force Notify [default: false]
  -e, --notifyOnly[=NOTIFYONLY]            Notify only one of dev,prod [default: false]
  -c, --continueOnError[=CONTINUEONERROR]  Continue to next EC2 on client failure [default: false]
  -h, --help                               Display this help message
  -q, --quiet                              Do not output any message
  -V, --version                            Display this application version
      --ansi                               Force ANSI output
      --no-ansi                            Disable ANSI output
  -n, --no-interaction                     Do not ask any interactive question
  -v|vv|vvv, --verbose                     Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
 Discovers services information and credentials.

AWS PHP Modify Security Groups Command

Modify Security Groups Command is an easy to use command that you can add to your DevOps to allow adding/Removing IPs or CIDRs to AWS security groups for all protocol and ports. The command is part of AWS PHP Commands.

Usage:

> php console.php aws:security-groups:modify -h
Usage:
  aws:security-groups:modify [options]

Options:
  -c, --cidr=CIDR            CIDR ex: 64.18.0.0/20 [default: false]
  -o, --operation=OPERATION  Operation to perform, one of add or remove [default: "add"]
  -e, --env[=ENV]            Which security groups this should run on. One of prod, dev [default: "dev"]
  -h, --help                 Display this help message
  -q, --quiet                Do not output any message
  -V, --version              Display this application version
      --ansi                 Force ANSI output
      --no-ansi              Disable ANSI output
  -n, --no-interaction       Do not ask any interactive question
  -v|vv|vvv, --verbose       Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
 Adds/removes CIDRs to security groups.

Warming up WordPress cache, HHVM and testing blog pages

If you are minifying scripts and css files using a caching plugin or using FastCGI cache then you might need to warmup your blog after purging your cache. This is a simple warm up cli script for WordPress to initiate cache or HHVM HHBC and making sure all pages/posts do not have errors. Additionally the script creates a urllist.txt file that you can use with siege to test load your server.

#!/usr/bin/env php
< ?php

if (PHP_SAPI != 'cli') {
    header('Status: 403 Forbidden');
    header('HTTP/1.1 403 Forbidden', true, 403);
    exit();
}


$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
define('BASE_PATH', "");
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require(BASE_PATH . 'wp-load.php');

//Disable cache
ob_end_clean();

$homeUrl = home_url() . "/";
$urlList = [$homeUrl];


$the_query = new WP_Query(['post_type' => 'any', 'post_status' => 'publish']);
//add homepage pages
warmup_pages_links($the_query->max_num_pages, $homeUrl);

//get all posts
$the_query = new WP_Query(['post_type' => 'any', 'posts_per_page' => '-1', 'post_status' => 'publish']);
$the_query->the_post();
while ($the_query->have_posts()) {
    $urlList[] = get_permalink();
    $the_query->the_post();
}

$categories = get_categories();
foreach ($categories as $category) {
    $catUrl = get_category_link($category->term_id);
    $urlList[] = $catUrl;
    $the_query = new WP_Query(['cat' => $category->term_id]);
    warmup_pages_links($the_query->max_num_pages, $catUrl);
}

$tags = get_tags();
foreach ($tags as $tag) {
    $tagUrl = get_tag_link($tag->term_id);
    $urlList[] = $tagUrl;
    $the_query = new WP_Query(['tag_id' => $tag->term_id]);
    warmup_pages_links($the_query->max_num_pages, $tagUrl);
}

$authors = get_users(array('who' => 'authors'));
foreach ($authors as $author) {
    $authorUrl = get_author_posts_url(false, $author->user_nicename);
    $urlList[] = $authorUrl;
    $the_query = new WP_Query(['author_id' => $author->term_id]);
    warmup_pages_links($the_query->max_num_pages, $authorUrl);
}

//test search page
$urlList[] = get_search_link('bala la lam');

file_put_contents('urllist.txt', implode("\n", $urlList));

echo "Found " . count($urlList) . " URLS and saved to urlList.txt \n";
echo "Testing URLs \n";
foreach ($urlList as $url) {
    warmup_getUrl($url);
}
echo "\nWarmup done \n";

function warmup_getUrl($url)
{
    if ($contents = wp_remote_get($url, ['timeout' => 15])) {
        if (is_wp_error($contents) || (isset($contents['response']['code']) && $contents['response']['code'] != 200)) {
            $subject = "[WP " . get_bloginfo('name') . "] Error on {$url} ";
            $error = is_wp_error($contents) ? $contents->get_error_message() : print_r($contents, true);
            wp_mail(get_bloginfo('admin_email'), $subject, $error);
            die("\n{$subject} :" . $error);
        }
        echo ".";
    }
    return $contents;
}

function warmup_pages_links($total, $url)
{
    for ($i = 2; $i < = $total; $i++) {
        $GLOBALS['urlList'][] = "{$url}page/{$i}/";
    }
}

// siege -c200 -d1 -r30 -f urllist.txt

Sitemap Creator v1.0

Sitemap Creator is a PHP class which creates XML sitemaps files compatible with the standard sitemaps.org protocol supported by Google and Bing.

Features

  • Uses PHPCrawl class to crawl/spider the website and creates URLs set while all PHPCrawl methods and options are accessible through class.
  • Ability to calculate Priority, Frequency and Last-Modified date with variety of options.
  • Creates sitemaps in gzip format or uncompressed XML.
  • Pings search engines with sitemaps locations.
  • Reads from CSV files and exports entries in CSV format.

Documentations
Download
Example
Github

Nginx Error Log Reader

Nginx Error Log Reader is a php reader/parser/analyzer for Nginx error log file. the script is able to read error logs recursively then display them in a user friendly table. Script configuration includes the number of bytes to read per page and allow pagination through the error log . Additionally, table columns are sortable and full description of every error is displayed using MonnaTip.

For banning Ips, please refer to this post Using iptables to block ips that spam or attack your server