Category Archives: php

Ubuntu 16.04 – Install Apache2 and php7

I have experimented some time ago, with php7, as described in this post. At that time that wasn’t official realease, however the link seems to pick up on some searches in google, and there was some confusion for people expecting this to be copy/paste guide.

So I would try to fix these and outline the steps to install Apache2 with php7 on the latest server LTS Ubuntu – 16.04.01 – freshly downloaded from the official site, at the time of the writing.

First, the two most repeated commands:


sudo apt-get update

sudo apt-get upgrade

That will update any software installed on the server distro.

Then install apache2 –

sudo apt-get install apache2

Open the IP of the server you have installed it on and you should be presented with the default apache web page, if nothing is showing, you need to check if apache2 is running and if the firewall/ufw is blocking requests.

It comes the turn to install mysql –

sudo apt-get install mysql-server

You will be asked to provide password for the MySQL root user, you should be aware this is not the same user as the Linux root user, it is different one having rights to do everything with every database in MySQL, so it is a good idea to pick a different password then the ones you are using currently in the system.

After installation is finished, we will have to run buil-in script to tighten some of the security for MySQL and clean up some things –

sudo mysql_secure_installation

You will be asked for you the root password (MySQL root user), and then to choose a level for password validation:


There are three levels of password validation policy:

LOW Length >= 8

MEDIUM Length >= 8, numeric, mixed case, and special characters

STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

They are self explainatory, but I don’t really like password validators – I find them stupid security measure, especially on the level of MySQL server/users. So I would set that to 0/low for machines I am using.

You should read the questions and answer then with ‘y’ or ‘no’, in the past it was fine to answer all with ‘y/yes’ however, now I am noticing that the first question is “Change the password for root” – you might not really want to do that, so the best thing is to read what are you actually asked.

 

Ok, so we are getting there, let’s install php7 with the apache mod –

sudo apt-get install php7.0 libapache2-mod-php7.0 php7.0-mcrypt php7.0-mysql

Then you could check the php version in the terminal with –

php -v

And the final step is to be sure apache is interpreting php in the browser.

First, become root with –

sudo -i

Then will add php info page to the server web root directory, so we could open it in our browser after that to verify it is running properly on check all the configuration details for php –

echo '<?php phpinfo(); ?> > /var/www/html/info.php'

After that you should navigate in your browser to the IP your server is listening to, and add /info.php after it, so it would look something like this –

http://192.168.122.113/info.php

That’s pretty much for it, but this where the complicated things starts from.

Install PHP 7 on Ubuntu

Note: This “guide” was meant for Ubuntu 14.04, I haven’t tested it on 16.04 and it most probably will not work, so please do not attempt to use it at all. I doubt that it would even work on 14.04 fallowing this guide.

You can refer to this guide for php7 with apache2 – Ubuntu 16.04 – Install Apache2 and php7

If you want to try the new php7 which GA release (General availability) is about to be finished in mid October, you can fallow these instructions.

I have done this little experiment on a virtual machine, and I strongly advice for you to do the same. But if you have balls go and do it on a production server.

Usually you will need to download and compile the packages on your system, but since I am lazy I searched for a ppa, and seems Ondřej Surý was kind enough to compile them, put them on ppa, and update them regularly.
This is the ppa – https://launchpad.net/~ondrej/+archive/ubuntu/php

I actually had php5 and apache running on this virtual instance, so even before adding the ppa, you need to remove everything php5 related, otherwise you might have conflicts and problems like me:

sudo apt-get remove php5*

Then add the php7 ppa:

sudo add-apt-repository ppa:ondrej/php

run update:

sudo apt-get update

and install php7:

sudo apt-get install php7.0

For apache you will need to enable php7 mod with:

sudo a2enmod php7.0

disable php5 mod (if you had php5 in the first place):

sudo a2dismod php5

Restart apache:

sudo service apache2 restart

and you are good to go.

You can put a file with

<!--?php phpinfo(); ?-->

and take a look at your brand new php7 install.

After that I decided to run a simple test to see if php7 is really that much faster than php5, and run on two identical VM’s the script from http://www.php-benchmark-script.com/.

PHP 5

--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2015-09-20 01:24:52
Server : localhost@192.168.1.139
PHP version : 5.5.9-1ubuntu4.11
Platform : Linux
--------------------------------------
test_math : 0.856 sec.
test_stringmanipulation : 0.923 sec.
test_loops : 0.540 sec.
test_ifelse : 0.427 sec.
--------------------------------------
Total time: : 2.746 sec.

PHP 7

--------------------------------------
| PHP BENCHMARK SCRIPT |
--------------------------------------
Start : 2015-09-20 01:24:55
Server : ubuntu-server@192.168.1.109
PHP version : 7.0.0RC3
Platform : Linux
--------------------------------------
test_math : 0.225 sec.
test_stringmanipulation : 0.338 sec.
test_loops : 0.224 sec.
test_ifelse : 0.267 sec.
--------------------------------------
Total time: : 1.054 sec.

Find and debug slow PHP scripts.

This is very useful trick when you are having site/script that is loading slow, but you are not sure what is the issue. In my case that was leading to extremely slow TTFB(Time to first byte) – around 15 seconds.

To do this we will have to enable slowlog, in my case I am using php5-fpm on Ubuntu, so I need to look for this config file –

 /etc/php5/fpm/pool.d/www.conf 

And look for something like this:

; The log file for slow requests
; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
;slowlog = log/$pool.log.slow

I want to log the slow request in

/var/log/php5/slow.log

So first of all I need to create that file, and the parent dir php5, if it doesn’t exists with

mkdir /var/log/php5
touch /var/log/php5/slow.log

And then add the fallowing values to our fpm config file –

slowlog = /var/log/php5/slow.log
request_slowlog_timeout = 8s
request_slowlog_timeout

Will determine how long before script will be logged. In my case anything that takes more than 8 seconds will be logged, of course you can use any other value.

Restart php5-fpm so the changes take effect

service php5-fpm restart

And then you can monitor the log in real time while browsing the site:

tail -f /var/log/php5/slow.log

For me the problem was malware injected in footer.php in one of the WordPress sites:

root@linuxvps:~# tail -f /var/log/php5/slow.log 
script_filename = /var/www/site.info/public_html/index.php
[0x00007f9bcb861fe0] curl_exec() /var/www/site.info/public_html/wp-content/themes/2014/footer.php(23) : assert code(1) : eval()'d code(1) : eval()'d code:14
[0x00007f9bcb861ed8] getContents() /var/www/site.info/public_html/wp-content/themes/2014/footer.php(23) : assert code(1) : eval()'d code(1) : eval()'d code:36
[0x00007f9bcb861df0] +++ dump failed

PHP Mail Headers

Most MTA/MUA’s insert a lot of extra headers; however, here is sort of the bare minimum you can expect.

From: 
Reply-To:
To: 
Subject:
Date: date("r");
MIME-Version:
Content-Type:

If you using HTML, then you should probably be using multipart messages–but it’s not strictly necessary.

Here is a snippet of code that shows how multi-part mime headers can be assembled without the help of a mailer class.

<?
$mime_boundary=md5(time());
$headers .= 'From: My Email <me@company.com>' . "\n";
$headers .= 'MIME-Version: 1.0'. "\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"". "\n"; 
 
$msg .= "--".$mime_boundary. "\n";
$msg .= "Content-Type: text/plain; charset=iso-8859-1". "\n";
$msg .= "Content-Transfer-Encoding: 7bit". "\n\n";
$msg .= $textmessage . "\n\n";
 
$msg .= "--".$mime_boundary. "\n";
$msg .= "Content-Type: application/pdf; name=\"".$filename."\"". "\n";
$msg .= "Content-Transfer-Encoding: base64". "\n";
$msg .= "Content-Disposition: attachment; filename=\"".$filename."\"". "\n\n";
$msg .= chunk_split(base64_encode($doc)) . "\n\n";
$msg .= "--".$mime_boundary."--". "\n\n";
?>

Get the value from multiple checkboxes


<html>
<body>
<h3>Get Value from multiple checkbox in PHP</h3>
<form action="test.php" method="post">
<input type="checkbox" name="fruit_list[]" value="1">Apple 
<input type="checkbox" name="fruit_list[]" value="2">Banana 
<input type="checkbox" name="fruit_list[]" value="3">Mango
<input type="checkbox" name="fruit_list[]" value="4">Guava</br></br>
<input type="checkbox" name="fruit_list[]" value="5">Orange</br></br>
<input type="submit" value="Save" />
</form>
<body>
</html>
<?php
if(isset($_POST['fruit_list']))
{
$fruit_list = array();
foreach($_POST['fruit_list'] as $val)
{
$fruit_list[] = (int) $val;
}
$fruit_list = implode(',', $fruit_list);
echo "Fruits Value are :- ".''.$fruit_list;
exit;
}
?>

PHP GeoIP redirection.

This script uses the Maxmind GeoIP redirection which in this case is hosted by https://freegeoip.net which will return a XML with the details of the visitors IP.


<?php 

//Start session to avoid infinity loops
session_start();
// store session data
$_SESSION['geo']=1;


//Get the client country and set ip that not be redirected
$countriesrow = array('US','CA','AP','AR','BR','CN','CL','CO','CR','GT','HK','JP','KR','MX','SG','TH','TW'); //These are the countries that would be redirected to the normal site
$countrieseuro = array('AT','BE','BG','CH','CZ','DE','DK','EE','EG','ES','EU','FI','FR','GB','GR','HR','HU', 'IE','IL','IN','IS','IT','LU','LV','MA','MC','NL','NO','PL','PT','RO','RS','RU','SE','SI','SK','UA','ZA'); //This will be redirected to the eu version
$admin = '31.53.62.141'; //Put here your IP

//Get the client IP
$ipc =  $_SERVER['REMOTE_ADDR'];
$surl = "https://freegeoip.net/xml/".$ipc;

//Check if freegeoip is available
$response = get_headers($surl);
$response = $response[0];
if ($response == "HTTP/1.1 200 OK") {
	$avl = 1;
} else {
	$avl = 0;
}
if ($avl == 1){
$getxml = trim(file_get_contents($surl)); // get raw post data + trim out the spaces
$xmlsimp = new SimpleXMLElement($getxml);  // convert to object array
$ccode = $xmlsimp->CountryCode;  // returns the country code of the visitor example "US"
}
$country = $ccode;

//$xmlsimp->Ip; // returns the Ip address of the visitor
$gurl = $_SERVER['REQUEST_URI']; //returns the current URL
$parts = explode('/',$gurl);
$cdir = $_SERVER['SERVER_NAME'];
for ($i = 0; $i < count($parts) - 1; $i++) {
 $cdir .= $parts[$i] . "/";
}

$urlParts = explode('.', $_SERVER['HTTP_HOST']);
$subdomain = $urlParts[0]; //or 0 if no www


if ($subdomain !== "eu" && in_array($country, $countrieseuro) && $_SESSION['geo'] == 1 &&  $avl == 1  && ($admin !== $ipc)) 
	{ header('Location: https://eu.rockandpebble.com' . $gurl . '');
	  exit;
	}
elseif ($subdomain !== "eu" && in_array($country, $countrieseuro) && $_SESSION['geo'] == 1 &&  $avl == 1 && ($admin !== $ipc))  
	{ 
		header('Location: https://rockandpebble.com' . $gurl . '');
	  exit;
	}
?>


Speed up PHP with APC on Ubuntu 10.04LTS

Ubuntu 10.04 LTS makes it quite simple to set up a basic LAMP server using tasksel; however, the default PHP set up does not include APC, the Alternative PHP Cache, which speeds up many PHP applications like Drupal. In the past, setting up APC involved using PECL or installing from source, but with Ubuntu Lucid, the process has been simplified using apt-get.

First, let me identify my demo system. It is running Ubuntu 10.04 LTS Lucid and has been patched to the latest version:

$ uname -a
Linux demo 2.6.32-24-generic #43-Ubuntu SMP Thu Sep 16 14:17:33 UTC 2010 i686 GNU/Linux
$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 10.04.2 LTS
Release:	10.04
Codename:	lucid
$ sudo apache2ctl status | grep "Server Version"
Server Version: Apache/2.2.14 (Ubuntu) PHP/5.3.2-1ubuntu4.7 with Suhosin-Patch
$ apt-cache show php-apc | grep Version
Version: 3.1.3p1-2

Ubuntu has added a Debian package into universe that allows APC to be added to any system quite easily:

$ sudo apt-get install php-apc
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  php5-gd
The following NEW packages will be installed:
  php-apc
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0B/77.2kB of archives.
After this operation, 217kB of additional disk space will be used.
Selecting previously deselected package php-apc.
(Reading database ... 28911 files and directories currently installed.)
Unpacking php-apc (from .../php-apc_3.1.3p1-2_i386.deb) ...
Processing triggers for libapache2-mod-php5 ...
 * Reloading web server config apache2
   ...done.
Setting up php-apc (3.1.3p1-2) ...

Note: You must restart the web server to begin using APC

$ sudo apache2ctl graceful

Out of the box (er…package), APC has some sane defaults. If you are “a serious user,” you will want to change your configuration yourself. Seriously, that is what the documentation says:

    …serious users should consider tuning the following parameters…

To tweak your very serious APC installation, you can change the settings manually (using vim):

$ sudo vim /etc/php5/conf.d/apc.ini

The APC configuration file is seriously barren; it is little more than an extension include directive. You can add extra keys after reading the APC’s online documentation related to settings.

Finally, there is a small php script that provides more information on the operation of the APC module. Copy it to your web root and decompress it. You should change the default username and password used to protect the script by changing the username and password variables directly in the PHP code:

$ sudo cp /usr/share/doc/php-apc/apc.php.gz /var/www
$ sudo gzip -d /var/www/apc.php.gz
$ sudo vim /var/www/apc.php

Change credentials near line 41:

defaults('ADMIN_USERNAME','apc'); // Admin Username
defaults('ADMIN_PASSWORD','password'); // Admin Password - CHANGE THIS TO ENABLE!!!

Now, view your APC page (assuming your web server is at 192.168.0.6):

https://192.168.0.6/apc.php

Apache Landing Page

This code list the directories and files in a table.

<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <title>Ze Master Server of The Universe</title>
   <style>
     *{
         padding:0;
         margin:0;
     }
     html,body {
         color:#333;
         font-family: "Lucida Console", Courier, monospace;
         font-size:14px;
         text-shadow:1px 1px 1px #cacaca;
         -webkit-text-shadow:1px 1px 1px #cacaca;
         -moz-text-shadow:1px 1px 1px #cacaca;
     }
     a{
         padding: 2px 0 0 24px;
         color:#FE4902;
         text-decoration:none;
     }
     a:hover{
         color:#000;
         cursor:url(hand.gif), progress;
     }
     #container{
         margin:0 auto;
         width:700px;
         margin-top:20px;
         padding-top:10px;
         border:1px solid #EEE;
         border-radius:10px;
         -moz-border-radius:10px;
     }
     .head{
         background-color:#38AF64;
         color:#FFF;
         font-weight:bold;
         padding:7px 0 5px 10px;
         font-size:14px;
         letter-spacing:1px;
         font-family: Verdana, Arial, Helvetica, sans-serif;
     }
     .head:hover{background-color:#FE4902;}
     .head span{font-size:9px; letter-spacing:0;}
     td{
         background-color:#F3F3F3;
         padding:6px;
     }
     td:hover{background-color:#EFEFEF;}
     h1{
         font-size:18px;
         font-weight:bold;
         padding:0 0 10px 10px;
     }

     /*icons for file types (add more to suit your needs - icons by famfamfam.)*/

     /*images*/
     a[href$=".jpg"] {background: url(image.gif) no-repeat left 50%;}
     a[href$=".gif"] {background: url(image.gif) no-repeat left 50%;}
     a[href$=".png"] {background: url(image.gif) no-repeat left 0%;}

     /*pdfs*/
     a[href$=".pdf"] {background: url(pdf.gif) no-repeat left 50%;}

     /*psds*/
     a[href$=".psd"] {background: url(psd.gif) no-repeat left 50%;}

     /*docs*/
     a[href$=".doc"] {background: url(doc.gif) no-repeat left 50%;}
     a[href$=".txt"] {background: url(doc.gif) no-repeat left 50%;}

     /*videos*/
     a[href$=".avi"] {background: url(video.gif) no-repeat left 50%;}
     a[href$=".m4a"] {background: url(video.gif) no-repeat left 50%;}
     a[href$=".mov"] {background: url(video.gif) no-repeat left 50%;}
     a[href$=".mp4"] {background: url(video.gif) no-repeat left 50%;}
     a[href$=".wmv"] {background: url(video.gif) no-repeat left 50%;}

     /*audio*/
     a[href$=".mp3"] {background: url(audio.gif) no-repeat left 50%;}
     a[href$=".wma"] {background: url(audio.gif) no-repeat left 50%;}
     a[href$=".aac"] {background: url(audio.gif) no-repeat left 50%;}

     /*web pages*/
     a[href$=".html"] {background: url(html.gif) no-repeat left 50%;}
     a[href$=".php"] {background: url(html.gif) no-repeat left 50%;}

   </style>

</head>
<body>
   <div id="container">
       <?php
         // opens this directory
         $myDirectory = opendir(".");

         // gets each entry
         while($entryName = readdir($myDirectory)) {
           $dirArray[] = $entryName;
         }

         // finds extention of file
         function findexts ($filename)
         {
           $filename = strtolower($filename) ;
           $exts = split("[/\\.]", $filename) ;
           $n = count($exts)-1;
           $exts = $exts[$n];
           return $exts;
         }

         // closes directory
         closedir($myDirectory);

         //  counts elements in array
         $indexCount   = count($dirArray);

         // sorts files
         sort($dirArray);

         // print 'em
         print("<h1>Directory Contents</h1>");
         print("<table width='100%' cellspacing='10'>
                 <tr>
                   <td class='head'>Directory/File</td>
                   <td class='head'>Type</td>
                   <td class='head'>Size <span>(bytes)</span></td></tr>\n");

         // loops through the array of files and print them all
         for($index=0; $index < $indexCount; $index++) {
               if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
               print("<tr><td><a href='$dirArray[$index]'>$dirArray[$index]</a></td>");
               print("<td>");
               print(findexts($dirArray[$index]));
               print("</td>");
               print("<td>");
               print(filesize($dirArray[$index]));
               print("</td>");
               print("</tr>\n");
           }
         }
         print("</table>\n");
       ?>
   </div>

</body>
</html>

Contact Form and send e-mail

<?php 
if ($_POST["email"]<>'') { 
    $ToEmail = 'youremail@site.com'; 
    $EmailSubject = 'Site contact form'; 
    $mailheader = "From: ".$_POST["email"]."\r\n"; 
    $mailheader .= "Reply-To: ".$_POST["email"]."\r\n"; 
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
    $MESSAGE_BODY = "Name: ".$_POST["name"].""; 
    $MESSAGE_BODY .= "Email: ".$_POST["email"].""; 
    $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"]).""; 
    mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); 
?> 
Your message was sent
<?php 
} else { 
?> 
<form action="test.php" method="post">
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"></textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form> 
<?php 
}; 
?>