Tag Archives: php

Adding php support to nginx (notes)

Already got nginx running with ruby, now I want a php site:

apt-get install php-cgi

configure nginx (mine is in a weird location, /opt/nginx/conf/nginx.conf:

server {
    listen 80;
    server_name my.hostname.local;
    root /path/to/site/public/;
    access_log /path/to/site/log/access.log;
    error_log /path/to/site/log/error.log;
    index index.php;
    location ~ \.php$ {
       try_files $uri =404;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       include fastcgi_params;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME /path/to/site/public$fastcgi_script_name;
       fastcgi_pass localhost:8020;
    }
}

then start php

php-cgi -b 127.0.0.1:8020

and restart nginx

Getting git to deploy your site in branch folders via post-receive

Aim:  get the site deployed like so; [webroot]/[branch_name]

I’m using this on our development server, so the devs can view all branches on it.

I first set our server up as described in this previous post on deploying with virtualmin.  Then use the following in hooks-receive:

#!/bin/sh
#get the ref name
while read oldrev newrev ref
do
refbranch=”$ref”
done
#remove ref path</h1>
branch=`basename $refbranch`
#add full path
branchpath=”[your_webroot_full_path]/$branch”
#create directory if it doesn’t exist
if [ ! -d “$branchpath” ]; then
mkdir “$branchpath”
echo “Created new folder for branch $branch”
fi
cd “$branchpath”

git –git-dir=[path_to_git_dir] –work-tree=”$branchpath” checkout -f “$branch”
git –git-dir=[path_to_git_dir] –work-tree=”$branchpath” clean -fd
echo “Updated files in $branch directory”

Once you’ve done that, you may want to add a file to the web root so people know what branches they can view.  I’m using the following very simple php script (index.php, in my webroot):

<?php

$dirs = scandir(‘.’);
$remove = array(‘.’,’..’,’.gitignore’,’.project’,’index.php’,’git’);
$html = ”;
foreach($dirs as $dir){
  if(!in_array($dir,$remove)){
    $date = date(‘jS M Y’,filemtime($dir));
    $html .= ‘<a href=”/’ . $dir . ‘”>’ . $dir . ‘</a> – ‘ . $date . ‘<br />’;
  }
}

?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html>
<head>
<title>Development site</title>
</head>

<body>
<p>Please choose your branch (branch name – last modified):<br />
<?php echo $html; ?>
</p>
</body>

</html>

Add to the $remove array any files/directories you do not want showing up.

Transparent tiled watermark using GD with PHP

This is a basic method for dynamically adding tiled watermarks to images on your site.  It uses the .htaccess file to give the images to the PHP script which returns the image plus watermarking.

1) Create the image you want to be the watermark as a png with the opacity you want to be used (we’re not lowering the opacity later), and make sure to add extra space around the image according to how big you want the tiles.

2) Add this script to your site as watermark.php:
[sourcecode]
<?php

$watermark = imagecreatefrompng(‘/path/to/your/watermark.png’);
$imagesrc = ‘/path/to/image/’ . $_GET[‘src’];
$type = mime_content_type($imagesrc);
switch($type){
  case ‘image/jpeg’:
    $image = imagecreatefromjpeg($imagesrc);
    break;
  case ‘image/png’:
    $image = imagecreatefrompng($imagesrc);
    break;
  case ‘image/gif’:
    $image = imagecreatefromgif($imagesrc);
    break;
  default:
    $image = null;
}
if($image){
  # set dimensions
  $image_width = imagesx($image);
  $image_height = imagesy($image);
  imagealphablending($watermark, TRUE);

  # create the tile and overlay
  imagesettile($image,$watermark);
  imagefilledrectangle($image,0,0,$image_width,$image_height,IMG_COLOR_TILED);

  # output image
  header(‘content-type: image/jpeg’);
  imagejpeg($image);

  #clean up
  imagedestroy($image);
  imagedestroy($watermark);
}

?>
[/sourcecode]

3) Add rewrite rules in .htaccess.  Notice the $_GET[‘src’] in the script above; we need to hand this variable to the script.  In this example, anything in my gallery folder, or with the filename starting with wmark- will be passed to the script:
[sourcecode]
RewriteRule ^(.*)wp-content/uploads/wmark-(.*) $1path/to/watermark.php?src=wp-content/uploads/wmark-$2
RewriteRule ^(.*)wp-content/uploads/image-gallery/main_gallery/(.*) $1path/to/watermark.php?src=wp-content/uploads/image-gallery/main_gallery/$2

# If you want to exclude a particular image (excludeme.jpg), add this ABOVE the other rules.  The [L] tells it to ignore other rules
RewriteRule ^wp-content/uploads/excludeme\.jpg$ – [L]

[/sourcecode]

The script will only handle jpg/gif/png file types; if it’s possible that your rewrite rules will pass any other file types to the script, make sure to use (.*\.(gif|jpg|jpeg|png)) if it’s possible instead of just the basic (.*) in your rewrite rules.