Tuesday, October 11, 2011

Exclude Directories or Files with Tar

I often do backups of my public_html but since I don't need folders like phpMyAdmin and logs, I found an easy way to do that in order to reduce backup size.The command to do this is:
tar -zcvf backup.tar.gz public_html/ --exclude "public_html/phpMyAdmin" --exclude "public_html/logs"


I am outside the public_html directory while doing this.For a more general case, it would be:
tar -zcvf filename.tar.gz directory_to_backup --exclude "directory_to_exclude"


This way, I save time and space.Hope this will be useful.
Remember that individual files can be too excluded by replacing directory with filename.

Catching all emails for a domain with Postfix(Catch all Emails)


We have to add a wildcard entry in /etc/postfix/virtual or any virtual mapping files.For simplicity we use /etc/postfix/virtual  file and append this:
@example.com  admin
And we have to do this to map that:
postmap /etc/postfix/virtual
And add this line if it does’t exist in /etc/postfix/main.cf:
virtual_alias_maps = hash:/etc/postfix/virtual
Finally,
/etc/init.d/postfix restart

This way every email sent to anything@example.com will be redirected to admin@example.com

Changing User Login Shell On Linux


There are many kinds of shells available.I prefer bash shell which is default on Debian Systems.
To see available shells,do:
cat /etc/shells
To change the shell for a user,we have to edit /etc/passwd file and replace the line /bin/shellname to  your preferred shell.
For example, replace /bin/sh to /bin/bash for user to which it is being done.

Sunday, October 9, 2011

Creating a backup tar archive

With tar, it is easy to backup whole directory including its permissions.To do this on a directory called 'web', we would type:
tar -pczf web.tar.gz web/
Thus, a tar archive is created and you can restore it with:
tar -pxzf web.tar.gz



Saturday, October 1, 2011

Easy SEO friendly htaccess and Nginx rules for Wordpress

Wordpress has always been SEO-friendly right from the box.But if you want it to be more,then here is a sample htaccess and Nginx rules that may help you.These rules redirect every errors to Wordpress error pages as well as forward non-canonical links to canonical links.Even non-www urls are redirected.

Save this as .htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^linuxdo\.blogspot\.com$ [NC]
RewriteRule ^(.*)$ http://www.linuxdo.blogspot.com/$1 [R=301,L]
</IfModule>
# END WordPress


--Replace linuxdo.blogspot.com with your webpage url.--

Insert this in Nginx virtual host conf file:

if (!-f $request_filename){
set $rule_0 1$rule_0;
}
if (!-d $request_filename){
set $rule_0 2$rule_0;
}
if ($rule_0 = "21"){
rewrite /. /index.php last;
}
if ($http_host ~* "^linuxdo\.blogspot\.com$"){
set $rule_1 1$rule_1;
}
if ($rule_1 = "1"){
rewrite ^/(.*)$ http://www.linuxdo.blogspot.com/$1 permanent;
break;
}


--Replace linuxdo.blogspot.com with your webpage url.--

Hope this helps.



PHP script to show system status

Here is a simple PHP script to show system status:



<html>
<body>
<pre>
Uptime:
<?php system("uptime"); ?>
System Information:
<?php system("uname -a"); ?>
Memory Usage (MB):
<?php system("free -m"); ?>
Disk Usage:
<?php system("df -h"); ?>
CPU Information:
<?php system("cat /proc/cpuinfo | grep \"model name\|processor\""); ?>
Hostname:
<?php system("hostname -s"); ?>
</pre>
</body>
</html>
Save this as status.php or anything you like.Note that the system function should be enabled.