Archive for May, 2008

How to Create an Archive in Linux

Friday, May 30th, 2008

In this post I’ll describe the process of creating a tar.gz archive in linux. It is a common practice to create backups or transfer files between servers using such archives. It is quite easy to create them and I’ll show you how to do it.

First of all you need to create a tarball - an archive that contains all the files, that are not compressed. Tar archive file size is the same as the sum of file sizes of the files that are located in the archive. In order to create it, you should issue the following command:

tar -cf <name of the archive>.tar <files of folders that will be in the archive separated by space>

This command will create a tar file. Next you need to compress it. The fastest way to do it in Linux is to use gzip compression (bzip2 often offers better results, but it takes much time and I won’t describe it here). In order to do it just input the following:

gzip <name of the archive created>.tar
This will compress your tarball and will delete your tar archive. The only thing that will be left is tar.gz archive. This is what you need. You can compare file sizes to find, how much space and bandwidth you have won by using the compressed version of your files.

Easiest Way To Sort Strings From a File By Alphabet with PHP

Wednesday, May 28th, 2008

Today I won’t write many letters, I’ll show a simple code that allows to sort file contents by alphabet.

<?

// Getting file into array
$stroki=file(”unsorted.txt”);

//Sorting it without keys - we don’t need them
sort($stroki);

// Writing sorted strings to a new file
$fs=fopen(”./sorted.txt”, “a”);
foreach ($stroki as $string)
{
fwrite($fs, trim($string).”\r\n”);
}
fclose ($fs);
echo “Completed”;
?>

Listing Directory Content Sorted By Size with PHP

Tuesday, May 27th, 2008

Let’s a take a common situation - you have a directory with a big number of files and you need to sort these files by size. Here is the code that will help you to do this in PHP. Quite short and fast.

<?php
// Path to the directory with big number of files
$dir = “../files/”;

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
// Opening the directory
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
// Checking for file existence
if ($file!=”.” && $file!=”..” && file_exists($dir.$file))
// Creating an array with file names as keys and sizes as values
$sizes[$file]=filesize($dir.$file);
}
closedir($dh);
}
// Sorting the array
arsort($sizes);
// Eching these values
foreach ($sizes as $chinese=>$people)
{
echo $people.” “.$chinese.”<br>”;
}
}
?>

Unique Temporary File Names With PHP

Monday, May 26th, 2008

When you work with multiple threads in PHP, you often need to use temporary files. When you have a few files, everything goes well. But when the number of threads is quite big and you need to store your data in temporary files, you may get stuck at this point. A PHP function will save your time by creating random names with mt_rand, md5 and any other functions and word combination. tempnam allows you to create files in the given directory with unique names. You don’t have to worry that the file may exist - file names are absolutely unique and this is guaranteed by PHP :). Please, note that files created by this function are not automatically deleted and you need to have the code that allows to destroy them.

Please, also note that this function output depends on PHP version you use. Starting from 5.2.4 tempnam will return an absolute path to the file so it is ok to use in file operations, but if you need to have links for html, be careful and check your code.

Hope that usage of this function will save your time.

Preventing Bandwidth Leak With Correct Out URLs

Wednesday, May 21st, 2008

There is a common practice to use outgoing links from your site to track visitor activity. For example, your site is www.site.com, and external links look like www.site.com/out.php?url=http://anothersite.com . It is OK for counters and traffic tracking, but may be used for an uncommon way. It this sample you may replace http://anothersite.com with any other site and your site will redirect to it. Do you understand what can it be used for?

Spamming, phishing and other stuff like this often relies on such bugs. You may receive abuses from anybody because you cannot know what kind of sites will be promoted and what will be the way to do it.
Even monsters, like Google and ADRiver have such a traffic leak. I recently found in my mailbox e-mails with links to:
http://www.google.fr/pagead/SOME_PARAMS&adurl=SPAMMER’s URL and http://ad.doubleclick.net/SOME_PARAMS?SPAMMER’s URL .
How do you prevent such things? First of all, never use such a construction with url=http:// and so on. You can assign an unique id for each URL and store it into database or text file and create outgoing URLs with such IDs. www.site.com/out.php?id=YOUR_ID will be much better and will save you from this malicious activity.

Be patient with standard scripts, as some of them contain such a vulnerability. For example, Autorank Pro and some other may contain such URL syntax. Have a nice day and make your URLs in a correct way!

Counting Number Of Lines in a Big File - Linux Shell Command

Tuesday, May 20th, 2008

Often it is necessary to work with big files, that cannot be viewed using standard editors. For example, If you have a 4Gb file, I think you won’t open it with any text editor. But what if you need to know how many bytes are in this file, of how many strings it contains? There is a linux command that allows you to do all this stuff faster than you think. Let’s show it: it’s wc. I’ll describe its options here.

-c, –bytes
print the byte counts
-m, –chars
print the character counts
-l, –lines
print the newline counts
-L, –max-line-length
print the length of the longest line
-w, –words
print the word counts
–help
display this help and exit
–version
output version information and exit

So in order to count the number of lines in a file, go to shell and issue the following command: wc -l <filename>. Enter wc -L <filename> to find the longest line of a file. I think other options are clear. If you’re looking for PHP usage of this command, you are welcome to use system() to gain its output and use for your needs. It can be useful for getting random lines from files, and many other things you might need.

Disabling PHP Safemode and open_basedir in Directadmin

Saturday, May 17th, 2008

Sometimes you need to remove limits that are automatically added by DirectAdmin. For example, you are not allowed to include files from anywhere but your home directory, you cannot use CURLOPT_FOLLOWLOCATION in php and so on.

The first case is the result of open_basedir protection. If you’re not running a hosting service and all sites are yours, it is enough safe to disable open_basedir protection for you own sites if you really need it. Safe mode may prevent you from running some scripts and you might also need to turn it off. This can be done from Directadmin Admin Panel. Just follow the screenshot below:

Then you should find your domain and disable open_basedir and safe mode for it. I usually disable these protections by default, but I don’t recommend to do it when you run a hosting service.

Don’t forget to restart Apache after your changes as they will be propagated upon httpd restart only. It will be done automatically in some time, but if you need it instantly, you should reboot it manually.

How To Repair And Optimize All Mysql Databases

Thursday, May 15th, 2008

When I tried to create admin backup for DirectAdmin, I got the message that one of my MySQL databases has errors and these errors were propagated to my backup. I’d suggest this to DirectAdmin developers, as using a simple command should avoid such errors. There is a linux shell tool called mysqlcheck, that allows to check, repair and optimize mysql databases without stopping mysqld. I will show you its usage so you can run this command before creating any kind of backups.

mysqlcheck -uroot -ppass -Aor

Male sure to include MySQL root password for this operation. If you don’t know your root password, check DirectAdmin Help for information regarding it. Let’s describe used options in brief. -A tells us that all databases will be checked. -r - Repairs selected databases, -o - Optimizes selected databases. This simple operation will allow you to have only good databases in your backups. You may also add this operation to your root crontab to keep your databases optimized.

How to Add RAR support to CentOS 4 (Red Hat Enterprise)

Tuesday, May 13th, 2008

Today I had to install rar support to unpack a big file. I’ve tried to download an archive from rarlab site and even unpacked it. Everything seemed to be nice, but when I tried to unpack a file, I faced the following error:
unrar: /lib/tls/libc.so.6: version `GLIBC_2.4′ not found (required by unrar) . I’ve searched for an updated version of glibc (when I started to look at linux servers I’ve uninstalled glibc without dependences and I don’t recommend to repeat it) and there was no such one for CentOS (RHEL4). I’ve found the solution at http://www.cyberciti.biz/ - a very nice site for Linux administrators. Here it is:

wget http://dag.wieers.com/packages/unrar/unrar-3.6.2-1.el4.rf.i386.rpm
rpm -Uvh unrar-3.6.2-1.el4.rf.i386.rpm
unrar e rarfile.rar

This worked for me and it was the easiest way to install rar support on CentOS. Just do this as root and rar files will become accessible to you.

How to Disable Web Access to Account With IP and Username in Directadmin

Tuesday, May 13th, 2008

Sometimes it is necessary to disable http://ip/~username account access for any reason. Site can be unavailable or simply you don’t want users to access it in such a way. There is a simple directive in httpd.conf that will stop it. Just find the following lines and make them the same as below:

<IfModule mod_userdir.c>
#UserDir public_html
UserDir disabled
</IfModule>

Mod_userdir is active by default so you should pay attention to it if you don’t really want your site to be displayed with IP and username