How to Deploy PHP Code to Apache and Nginx on Ubuntu 14.04

Last week, I shared how to build PHP extension on Linux, as well as how to make a simple PHP barcode reader with a few lines of PHP code. In this post, I will illustrate how to implement an online barcode reader with the custom PHP extension, and how to deploy the PHP project to Apache and Nginx on Ubuntu 14.04.

Online Barcode Reader in PHP

Before taking the following steps, it is recommended to read the post - How to Make PHP Barcode Reader on Linux.

Create index.php:

PHP Online Barcode UI

Create readbarcode.php to receive and process Form data:

<?php
function readBarcode($path, $type) {
  try {  	
  	$resultArray = DecodeBarcodeFile($path, $type);
  } catch(Exception $exp) {
  	echo '<p>' . $exp . '</p>';
  	exit;
  }
  if (is_array($resultArray[0])) {
	$resultCount = count($resultArray);
	echo '<p>Total barcode(s) found:' . $resultCount . '.</p><br/>';
	for($i = 0; $i < $resultCount ; $i++) {
		$result = $resultArray[$i];
		echo '<p>Barcode ' . ($i+1) . ':</p>';
  	 	echo "<p>Type: $result[0]</p>";
  	 	echo "<p>Value: $result[1] </p><br/>"; 
	}
  }
  else {
    echo '<p>No barcodes found.</p>';
  }
}
function imagefromURL($image,$rename){
	$ch = curl_init($image);

	$timeout = 5;
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
	$rawdata=curl_exec ($ch);

	$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	if($code > 200) {
		curl_close($ch);
		return FALSE;
	}

	curl_close ($ch);

	$fp = fopen($rename,'wb');
	fwrite($fp, $rawdata); 
	fclose($fp);
	return TRUE;
}

function return_bytes($val) {
    $val = trim($val);
    $last = strtolower($val[strlen($val)-1]);
    switch($last) {
        case 'g':
            $val *= 1024;
        case 'm':
            $val *= 1024;
        case 'k':
            $val *= 1024;
    }
    return $val;
}
ini_set('display_errors',1);
error_reporting(E_ALL);
$post_max_size = ini_get("post_max_size");
$maxsize = return_bytes($post_max_size);
if($_SERVER['CONTENT_LENGTH'] > $maxsize) {
	echo "Post data size is bigger than " . $post_max_size;
	exit;
}
if(!array_key_exists("uploadFlag", $_POST))	{
		echo "The input file is not specificed.";
		exit;
}
$flag = (int)$_POST["uploadFlag"];
$btype = (int)$_POST["barcodetype"];
// get current working directory
$root = getcwd();
// tmp dir for receiving uploaded barcode images
$tmpDir = $root . "/uploads/";
if (!file_exists($tmpDir)) {
	mkdir($tmpDir);
}
if ($flag) {
	if(!empty($_FILES["fileToUpload"]["tmp_name"]))	{
		$file = basename($_FILES["fileToUpload"]["tmp_name"]);
		$tmpname = date("Y_m_d_H_i_s_") . rand()%1000;

		if ($file != NULL && $file != "") {
			$target_file = $tmpDir . $tmpname;
			if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {     
				readBarcode($target_file, $btype);					
			  unlink($target_file);
			} else {
				echo "Fail to upload file.";
			}
		} else {
		  echo "Fail to upload file.";
		}
	}
	else {
		echo "Fail to upload file.";
	}
} else {	
	if (!empty($_POST["fileToDownload"]) && $_POST["fileToDownload"] != "") {
		$url_file = $_POST["fileToDownload"];
		$tmpname = date("Y_m_d_H_i_s_") . rand()%1000;
		$target_file = $tmpDir . $tmpname;	

		if( imagefromURL($url_file, $target_file)) {
			readBarcode($target_file, $btype);		
			#unlink($target_file);
		} else {
			echo "Fail to download file.";
		}
	} else {
		echo "Fail to download file.";
	}
}
?>

PHP on Apache

Install php5-curl, apache2 and libapache2-mod-php5**:

sudo apt-get install php5-curl apache2 libapache2-mod-php5

A php.ini file will be generated automatically.

/etc/php5/apache2/php.ini

Open php.ini and then add the extension path:

extension=/usr/lib/php5/20121212/dbr.so

Copy PHP project to /var/www/html/.

Get permissions to create (mkdir) upload folder and write (move_uploaded_file) uploaded files:

sudo chgrp -R www-data /var/www/html/reader
sudo chmod -R g+rw /var/www/html/reader

Start Apache2:

sudo service apache2 start
#sudo service apache2 stop // if you want to stop Apache

Visit http://localhost/reader.php:

php online barcode reader

PHP on Nginx

Install Nginx and php5-cgi:

sudo apt-get install nginx
sudo apt-get install php5-cgi

Edit configuration file to enable PHP:

sudo vi /etc/nginx/sites-available/default

nginx enable php

Copy PHP project to /usr/share/nginx/html/ and get permissions:

sudo chgrp -R www-data /usr/share/nginx/html/reader
sudo chmod -R g+rw /usr/share/nginx/html/reader

Run Nginx and php-cgi:

sudo nginx
# sudo nginx –s stop // if you want to stop Nginx
sudo php-cgi -b 127.0.0.1:9000 -c /usr/share/php5/php.ini-production

I used php.ini-production that configured for PHP extension.

Visit http://localhost/reader.php.

Source Code

https://github.com/dynamsoftlabs/linux-php-barcode-reader-