Password protecting your development site using .htaccess / .htpasswd

September 21, 2013  |  1 Comments  |  by Raj (MagePsycho)  |  Apache

You may want to restrict the development site from search engine or from direct access. One of the popular way of doing so is to use Apache2 Basic Authentication.
Here we will be discussing on how to password protect your development site using Apache2 Basic Authentication which makes a use of .htaccess & .htpasswd files.

Steps:

1. Create & upload .htaccess file to the root of your dev site with the following content:

AuthName "Authorisation Required"
AuthUserFile "/path/to/.htpasswd"
AuthType Basic
require valid-user
ErrorDocument 401 "Authorisation Required"

Notes: If .htaccess file already exists then you can add the above code on the top.
Also note that omitting ‘ErrorDocument 401 “Authorisation Required”‘ line can lead to 404 error.
2. Create & upload the .htpasswd file to ‘AuthUserFile’ path with the following content:

foo:$apr1$yB1.9vIT$IVVBmq5vMauwsNR8CZdHQ.

Notes: Where ‘foo’ is the username and ‘$apr1$yB1.9vIT$IVVBmq5vMauwsNR8CZdHQ.’ is the MD5 encrypted password of ‘bar’.
That was just an example, in fact you can use any username/password combination.
In order to create encrypted password you can use some free online tool like:
http://www.htaccesstools.com/htpasswd-generator/

3. Now try to visit the dev site, you will get the screen similar as:

Apache2 Basic Authentication

Apache2 Basic Authentication


And you will only be able to browse the dev site once the correct username/password are entered.

Hope this helps you in password protecting your site.

Backup Magento project files / db using bash script

July 1, 2013  |  8 Comments  |  by Raj (MagePsycho)  |  Linux, Magento

I have googled for the bash script to backup Magento sites/db but none of them worked for me the way I wanted.
So I decided to create a custom bash script which is simple and does the job perfectly.

I have developed the script for my own need. But I thought it would be helpful if shared with you guys as well.
Here goes the overall backup script:

#!/bin/bash
#@author		MagePsycho <magepsycho@gmail.com>
#@website		http://www.magepsycho.com
#@version		0.1.0

#/************************ EDIT VARIABLES ************************/
projectName=magepsycho
backupDir=/home/magepsycho/_backups
#/************************ //EDIT VARIABLES **********************/

dbXmlPath="app/etc/local.xml"
{
host="$(echo "cat /config/global/resources/default_setup/connection/host/text()" | xmllint --nocdata --shell $dbXmlPath | sed '1d;$d')"
username="$(echo "cat /config/global/resources/default_setup/connection/username/text()" | xmllint --nocdata --shell $dbXmlPath | sed '1d;$d')"
password="$(echo "cat /config/global/resources/default_setup/connection/password/text()" | xmllint --nocdata --shell $dbXmlPath | sed '1d;$d')"
dbName="$(echo "cat /config/global/resources/default_setup/connection/dbname/text()" | xmllint --nocdata --shell $dbXmlPath | sed '1d;$d')"
}

fileName=$projectName-$(date +"%Y-%m-%d")
printf "What kind of backup you would like?\n[ d ] DB backup only\n[ f ] Files backup only\n[ b ] Files backup with DB\n"
read backupType
if [[ $backupType = @(d|b) ]]; then
	echo "----------------------------------------------------"
	echo "Dumping MySQL..."
	mysqldump -h $host -u $username -p$password $dbName > $fileName.sql
	echo "Done!"
fi

if [[ $backupType = @(f|b) ]]; then
	echo "----------------------------------------------------"
	echo "Archiving Files..."
	printf "Skip /media folder?\ny: Yes\nn: No\n"
	read skipMedia
	if [ $skipMedia == y ]; then
		tar -zcf $fileName.tar.gz --exclude=var --exclude=includes --exclude=media * .htaccess
	else
		tar -zcf $fileName.tar.gz --exclude=var --exclude=includes * .htaccess
	fi
	echo "Done!"
	echo "----------------------------------------------------"
	echo "Cleaning..."
	rm -f $fileName.sql
	echo "Done!"
fi

if [[ $backupType = @(d|f|b) ]]; then
	echo "----------------------------------------------------"
	mkdir -p $backupDir;
	echo "Moving file to backup dir..."
	if [ $backupType == d ]; then
		mv $fileName.sql $backupDir
	fi

	if [[ $backupType = @(f|b) ]]; then
		mv $fileName.tar.gz $backupDir
	fi
	echo "Done!"
else
	echo "Invalid selection!"
fi

Or you can download it from [here]
Notes: If you get the following error:

syntax error in conditional expression: unexpected token `(‘
line 24: syntax error near `@(d’
line 24: `if [[ $backupType = @(d|b) ]]; then’

then this means you are using older version of bash (< 4.0). And you need to patch the script by adding the following line after bash script declaration:[code language="bash"] shopt -s extglob [/code]

What does this script do?

1. Gives options for backup type which are:
– DB backup only
– Files backup only
– Files backup with DB
2. Dumps the database by taking DB info from XML configuration.
3. Makes a copy of project files and compresses it in .tar.gz format.
While copying it also gives options whether to exclude media folder or not.
Note that by default ‘var’ & ‘includes’ folder are excluded by the script.
4. Deletes the dumped SQL file as it’s already copied in the compressed file.
5. Creates backup dir if not exists
6. Copies the compressed project file to the backup dir.

Besides, you can also setup Cron job to run this backup script at regular intervals.

How to run backup script?

1. Edit _magebackup.sh to configure variables ‘projectName’ & ‘backupDir’
2. Upload the edited _magebackup.sh to the root of your Magento installation
3. Run following series of commands in terminal:

cd /path/to/magento/root
chmod +x _magebackup.sh
ex -sc $'%s/\r$//e|x' _magebackup.sh
sh _magebackup.sh

4. You will get the compressed backup file at backup dir

Snapshots for different backup types

DB backup only


Files backup only


Files backup with DB


Let’s comment if there’s any room for improvement in this script.
Thanks for reading & sharing.

Configuring Magento for Development / Debug Mode

March 10, 2012  |  12 Comments  |  by Raj (MagePsycho)  |  Apache, Latest, Magento

Configuring Magento for Development / Debug Mode:

1. Disable Cache
System > Cache Management > Select All [check-boxes] > Actions = Disable > Submit

2. Re-Index All
System > Index Management > Select All [check-boxes] > Actions = Reindex Data > Submit

3. Disable Compilation
System > Tools > Compilation > Disable

Note: By default compilation mode is disabled. So just check if the Compiler Status is Enabled or not.

4. Turn on Error Reporting
a> Open index.php and un-comment the following line

#ini_set('display_errors', 1);

b> Open .htaccess and add the following line at the end

SetEnv MAGE_IS_DEVELOPER_MODE "true"

5. Turn on Logging
System > Configuration > Advanced > Developer > Log Settings > Enabled => Yes

6. Configuring Mangeto Error Page
rename errors/local.xml.sample to errors/local.xml

7. Install ‘Easy Template Path Hints’
Install Easy Template Path Hints for turning on/off the template path hints for frontend and backend easily & securely.

That’s all. Let me know for any missing points.
Happy Debugging!

Setting custom environment variables using .htaccess

October 16, 2011  |  No Comments  |  by Raj (MagePsycho)  |  Apache, Latest

If you are familiar with Magento, esp with setting up multiple websites/stores using .htaccess then you may have encountered variables like: MAGE_RUN_CODE & MAGE_RUN_TYPE. Yes they are the custom environment variables.

In order to set up custom environment variables using .htaccess, try as follows:

RewriteRule ^ - [E=MAGE_RUN_CODE:default]
RewriteRule ^ - [E=MAGE_RUN_TYPE:store]

Where MAGE_RUN_CODE & MAGE_RUN_TYPE are custom environment variables.
and from php you can access this variable as:

echo $_SERVER['MAGE_RUN_CODE']; //prints default
echo $_SERVER['MAGE_RUN_TYPE']; //store

Hope this gave some info about custom environment variables.
Cheers!!