Install LAMP on AWS EC Ubuntu (PHP 7, MySql 5.7, Apache 2)

Install Apache2

sudo apt-get update
sudo apt-get install apache2

Install PHP 7

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get purge php5-fpm
sudo apt-get install php7.0-cli php7.0-common libapache2-mod-php7.0 php7.0 php7.0-mysql php7.0-fpm
sudo apt-get install php7.0-cgi php7.0-dbg php7.0-dev php7.0-curl php7.0-gd
sudo apt-get install php7.0-mcrypt php7.0-xsl php7.0-intl

Restart Apache Now
sudo service apache2 restart

Now the PHP is installed with Apache webserver. Lets verify the installation

php -v

Install Mysql 5.7

sudo wget
sudo dpkg -i mysql-apt-config_0.6.0-1_all.deb

You will get a screen asking to select MySQL version.

MySQL Server -> MySQL 5.7 -> Apply

sudo apt-get update
sudo apt-get install mysql-server-5.7

Now the MySQL server is installed. Lets verify the MySQL installation

mysql –version

Lets restart webserver again

sudo service apache2 restart

Install FTP

sudo apt-get install vsftpd

You should now, now be able to connect with FileZilla or any FTP client application. You can connect using the .ppk file, which you have downloaded from AWS admin

Install Zip/Unzip

sudo apt-get install zip unzip

Hope that help.



Moodle Error – Incorrect access detected

Have you even got this message. “Incorrect access detected, this server may be accessed only through … address” So how to remove that? Its so simple

Go to moodle/lib/setuplib.php

and change the following file:

Redirect ($CFG->wwwroot, get_string('wwwrootmismatch', 'error', $CFG->wwwroot), 3);


Redirect ($CFG->wwwroot, get_string('wwwrootmismatch', 'error', $CFG->wwwroot), 0);

Now Moodle will redirect immediately to the wwwroot url defined in config.php without any warning message.


MySQL – Fetch duplicate records

If your database table having duplicate records and you want to remove all duplicates, this query will help you to find out duplicate records in a table. You can check the records based on any specific field.

FROM mdl_user
WHERE email IN (
	SELECT email
	FROM mdl_user
	GROUP BY email
	HAVING count(email) > 1
ORDER BY email

This query will find all the records in mdl_user table having duplicate emails.


PHP header to prompt file download

Many times I need this function but every time when I try myself I get a new issue, so I thought why not I put it on my blog to help me and other who wanted to prompt a download alert using PHP. I just copied this 😉 from

function downloadFile($fullPath){

// Must be fresh start
if( headers_sent() )
die(‘Headers Sent’);

// Required for some browsers
ini_set(‘zlib.output_compression’, ‘Off’);

// File Exists?
if( file_exists($fullPath) ){

// Parse Info / Get Extension
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts[“extension”]);

// Determine Content Type
switch ($ext) {
case “pdf”: $ctype=”application/pdf”; break;
case “exe”: $ctype=”application/octet-stream”; break;
case “zip”: $ctype=”application/zip”; break;
case “doc”: $ctype=”application/msword”; break;
case “xls”: $ctype=”application/”; break;
case “ppt”: $ctype=”application/”; break;
case “gif”: $ctype=”image/gif”; break;
case “png”: $ctype=”image/png”; break;
case “jpeg”:
case “jpg”: $ctype=”image/jpg”; break;
default: $ctype=”application/force-download”;

header(“Pragma: public”); // required
header(“Expires: 0”);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0”);
header(“Cache-Control: private”,false); // required for certain browsers
header(“Content-Type: $ctype”);
header(“Content-Disposition: attachment; filename=\””.basename($fullPath).”\”;” );
header(“Content-Transfer-Encoding: binary”);
header(“Content-Length: “.$fsize);
readfile( $fullPath );

} else
die(‘File Not Found’);


Drupal CSS optimization failed

My colleague Gaurav he is Drupal expert, is working on a Drupal project. He has almost completed the project so now the time was to switch the project from development to production environment. In the development environment we have Windows Xp, Apache2.2.17, PHP 5.2 and MySQL 5.0.7 and on production environment we have the same PHP, the same Apache, the same MySQL and Windows 2003.

I helped him during server configuration and deployment, everything deployed successfully we were able to see the site. But when he turned on the CSS and JS optimization from admin->performance setting in drupal.

Browser was going nowhere we were just getting the message “Page can not be displayed”. Why we don’t know, because there was no any apache error log, no any php error log. Finally we came to know that CSS optimization is creating an issue, the reason was drupal optimizer was not able to create CSS cache file in sites/default/files/css folder. Whereas JS cache was created in sites/default/files/js folder.

Then we again disabled the CSS optimization, (just only CSS optimization because JS optimization was working very well), now we tried to access the site this time it rendered properly, but only with firefox. Not with IE, this time the reason was clear, because IE only allows 31 CSS request at a time and we were having 34 CSS. We could reduce the CSS requests by disabling few unused modules but it was not a solution to our problem.

Everything was okay but without solving this issue we would not be able to say that everything is working very well. We spent almost two days to find out the reason and solution. Finally I found a solution on Drupal issues page. I found that this is not a strange issue many drupal expert has already faced this. Here is the link to the post

Somebody has posted a patch for file which basically does the optimization work, here is the patch

So open file find this code and replace it (offcourse to play safely, first do a backup)

if ($_optimize) {
// Perform some safe CSS optimizations.
$contents = preg_replace('< - \s*([@{}:;,]|\)\s|\s\()\s* | # Remove whitespace around separators, but keep space around parentheses. /\*[^*\\\\]*\*+([^/*][^*]*\*+)*/ | # Remove comments that are not CSS hacks. [\n\r] # Remove line breaks. >x', '\1', $contents);

I hope this will help you guys if you work someday on Drupal and face the same issue 🙂 which is very rare.