Blog moved to www.mihaivalentin.com

December 19, 2010

Hello.

It’s been more than 1 an year and a half since this blog was started.

Now, I, Mihai Valentin launched my online portfolio, and part of this blog will be moved and continued to www.mihaivalentin.com/blog

Please update your links!

Advertisements

How to achieve a resolution greater than 800×600 on Ubuntu + Virtual Box

October 19, 2010

aptitude install virtualbox-ose-guest-x11

Simple incremental backup with TAR

September 14, 2010
#/bin/sh
LAST_BACKUP=`cat /backup/_last_backup`
tar --after-date="`echo $LAST_BACKUP`" -X/backup/exclude.lst -zvcf /backup/web-`date +"%Y-%m-%d_%H-%M-%S"`.tgz /srv/www/
date +"%Y-%m-%d %H:%M:%S %Z" > /backup/_last_backup

The ideea is simple: _last_backup file will contain the data of the last backup, so that in the next one will be included only files that have been changed or added. Useful when you cannot install rdiff-backup.

A lot of useful Linux commands

August 25, 2010

Search for text inside files of a certain type, recursively, with find, xargs and grep

Put the codes below in .bashrc in your user directory

function findt() { find . -name "$2" -print | xargs grep "$1" --color=ALWAYS ;}

To run:

findt "function" "*.php"
findt "System.exit" "*.java"

Apache log the server’s load average using mod_loadaverage

August 6, 2010

I wrote an Apache module that provides the momentary load average through the environment variables LOADAVG1 (1 minute load average) and LOADAVG5 (5 minutes load average)

If you want to log the load average at the time of your requests, you can simply activate mod_loadaverage and then, in apache2.conf, use:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\" %{LOADAVG1}e %{LOADAVG5}e" default_and_loadaverage
CustomLog /var/log/acceslog.loadaverage.log default_and_loadaverage

This way, the load average will be appended to each request logged.
If you want to access these variables from PHP, you can do:

$loadavg1 = $_SERVER['LOADAVG1']; // outputs the load 1 minute load average (ex: 0.5)

Module source code:

#include "httpd.h"
#include "http_config.h"

static int append_loadavg(request_rec *r) {    
    char *buf1 = (char *)malloc(15);
    char *buf2 = (char *)malloc(15);
    float la1,la5;
    FILE *f = fopen("/proc/loadavg","r");
    if (f != NULL) {
        fscanf(f,"%f %f",&la1,&la5);
        fclose(f);
        sprintf(buf1, "%3.2f",la1);
        sprintf(buf2, "%3.2f",la5);
        apr_table_setn(r->subprocess_env,"LOADAVG1",buf1);
        apr_table_setn(r->subprocess_env,"LOADAVG5",buf2);
    }    
    return OK;
}

static void loadavg_module_register_hooks(apr_pool_t *p) {
    ap_hook_fixups(append_loadavg,NULL,NULL,APR_HOOK_MIDDLE);
}


module AP_MODULE_DECLARE_DATA loadavg_module = {
    STANDARD20_MODULE_STUFF,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    loadavg_module_register_hooks
};

Installation

You need apxs2 (aptitude install apache2-dev – the developement version of apache)

apxs2 -i -c mod_loadavg.c

where mod_loadavg.c is the sourcecode above.

What apxs2 should do is:

– compile your module

– copy it to /usr/lib/apache2/modules (or whereever your apache modules dir folder is)

– add in httpd.conf the following: “LoadModule loadavg_module “/usr/lib/apache2/modules/mod_loadavg.so””

After installation, make sure you restart apache.

As I told you before, a usage of it may be log the loadaverage during all of your reqeuests or to find out the loadaverage value from PHP.

This module is not tested thouroughly, but if you find it has any problems, tell me.

SSHD log to a separate file

May 28, 2010

Change these lines in /etc/opt/ssh/sshd_config

SyslogFacility AUTH
LogLevel INFO

Then restart sshd.

Then add this line to /etc/syslog.conf

auth.info;mail.none /var/adm/syslog/sshd.log

Then restart syslogd and youre done!

Source: http://forums13.itrc.hp.com/service/forums/questionanswer.do?admit=109447627+1275035196242+28353475&threadId=267234

Find files larger than 20 mb and print their name and size in a human readable format

May 26, 2010
find / -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }' 

Source:http://snippets.dzone.com/posts/show/1491

Convert MP4 to 3GP using ffmpeg

May 25, 2010

ffmpeg -i source.mp4 -s qcif -vcodec h263 -acodec aac -ac 1 -ar 8000 -r 25 -ab 32 -y output.3gp

Use firefox to test how your site would look on a handheld device

January 10, 2010

@media handheld, screen and (max-width: 500px) {
body {background: #c00 !important;}
}

Install mod_bw in Apache, to limit the bandwidth

December 29, 2009

http://www.whoopis.com/howtos/web-bandwidth-limit.html

Instead of putting the conf inside the httpd2.conf, put it in the virtual hosts configuration. Otherwise it won’t work.

<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www/

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all

BandwidthModule On
ForceBandWidthModule On
Bandwidth all 0
LargeFileLimit .flv 1000 256000
MinBandwidth all -1

</Directory>

Apache FLV streaming done right!

December 29, 2009

http://tperspective.blogspot.com/2009/02/apache-flv-streaming-done-right.html

Linux find and replace inside files

December 29, 2009

sed -re ‘s/search/replace/g’ -i *

Run multiple version of PHP on the same Lighttpd server

December 23, 2009

1. Download a stable PHP

2. ./configure && make (you must also enable the cgi)

3. In the vhost you need, just write

fastcgi.server = ( “.php” =>
((
“socket” => “/www/myvhost/tmp/php-fastcgi.socket”,
“bin-path” => “/usr/local/php-5.3.1/sapi/cgi/php-cgi -c /www/myvhost/php.ini”
))
)

4. Make sure that /usr/local/php-5.3.1 can be accessed by Lighttpd (it has enough permissions)

5. Restart lighttpd and enjoy!

Prevent script termination on putty close

December 22, 2009
screen -S myscreen

you login, and issue a command

to recover from another console,

screen -r myscreen

Easily transfer tail -f -like data between Linux and Windows.

December 21, 2009

On Linux, create mytransfer.c with:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

int main(int argc, char *argv[]) {
 char buf[1024];

 // Socket stuff
 int sockfd, portno, n;
 struct hostent *he;
 struct sockaddr_in their_addr;
 he=gethostbyname("THE.WINDOWS.IP.HERE");
 
 sockfd = socket(AF_INET, SOCK_STREAM, 0);
 if (sockfd < 0) printf("ERROR opening socket");

 their_addr.sin_family = AF_INET;
 their_addr.sin_port = htons(39412); // port number
 their_addr.sin_addr = *((struct in_addr *)he->h_addr);
 memset(&(their_addr.sin_zero), '', 8);
 
 if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
 printf("connect()\n");
 exit(1);
 } else {
 printf("s-a conectat!\n");
 }
 // Socket stuff end
 
 while (1) {
 fgets(buf, 1024, stdin);
 if (strlen(buf)>0) {
 write(sockfd, buf,strlen(buf));
 sleep(1);
 }
 memset(buf,0,sizeof(buf));
 }
 
 return 0;
}

On Windows, download and install netcat, and run it with the following command line:

nc -L -l -p 39412 > c:\php.log

Then, on Linux:

killall -9 mytransfer.o;  gcc -o mytransfer.o mytransfer.c && tail -f /tmp/php.log | ./mytransfer.o

You will now notice that the c:\php.log will contain the data which is being written in realtime on /tmp/php.log on the linux machine.

Installing Lighttpd with mod_h264_streaming and mod_magnet

December 18, 2009

First: Install LUA:

http://bluescripts.net/tag/lighttpd/

Second: Instal Lighttpd

Third: http://h264.code-shop.com/trac/wiki/Mod-H264-Streaming-Lighttpd-Version2

4th: Make sure autoconf, automake, libtool are the latest versions.

If you encounter problems, http://h264.code-shop.com/trac/discussion/1/85

Make wordpress search work

December 7, 2009

If you are having problems with wordpress search form in Lighttpd, it’s because it does not directly support the QSA parameter from mod_rewrite.

To overcome this problem, instead of:

“.” => “index.php”

use

“.(?:\?(.*))?” => “index.php?$1”

This simulates QSA and makes your search work

PostgreSQL – how to make the first function work

June 7, 2009

In order to make a simple function, you must first create a new language, called plpgsql. After that, create the function using plpgsql language, not the sql function.

Postgre SQL – autonumber – serial

June 7, 2009

To be able to set the serial pseudo-data-type, you must first set that field as primary key and then that type will appear for usage in Navicat PostgreSQL.

ExtJS – In IE, grid column headers are not shown, but ok in Firefox

May 12, 2009

Add this to your css

.x-grid3-header {position: relative;}

Have /m/* and also /* with mod rewrite

April 23, 2009

Options -Indexes

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/(.*)        /index.php?u=$1&m=1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)         /index.php?u=$1 [L]

Dump a database into a gzip file

April 23, 2009

mysqldump –user=dbuser –password=dbpass dbname | gzip -9 > backup-`date +”%Y-%m-%d_%H-%M-%S”`.sql.gz

How to archive a folder and timestamp the name of the file

April 23, 2009

tar zcfv backup-`date +”%Y-%m-%d_%H-%M-%S”`.tgz /root/code

Install JSON on a VPS server CentOS

April 2, 2009

http://forums.crm.saeven.net/showthread.php?t=128

How to correctly setup permissions in Lunarpages webhosting

March 30, 2009

The folder containing the files should be 0755, and the files inside 0644 (in order not to yield “is writable by the group” error)

To do this, just call the following functions

find . -type f -print0 | xargs -0 chmod 644;find . -type d -print0 | xargs -0 chmod 755

PHP: Two functions to check if a string contains another one, or begins with another one.

March 20, 2009

 

function contains($haystack, $needle) {
    return strpos($haystack,$needle)!==FALSE;
}

function beginsWith($str, $prefix) {
    return strpos($str, $prefix)===0;
}

Read the rest of this entry »

Internet Explorer cannot open the Internet site – Operation aborted

March 20, 2009

Problem: see the title, a very frustrating problem.

Solution: This problem appears usually when using maps like Google Maps, Virtual Earth, Yahoo on your page. Move the script just before the closing of the body tag.

Read the rest of this entry »

Internet Explorer 6 float problems

March 20, 2009

Problem: You may have made a CSS only design, and the results look like this: (two divs, one floated on the left and the other one floating on the right)

image

Solution:  do not use ANY padding at all on those 2 elements. Why?
Because Internet Explorer 6 has a different approach on the W3C box model

Internet Explorer 6 double margin

March 20, 2009

Problem: When setting a margin in Internet Explorer 6, instead of respecting it, it doubles it. For example, if we set margin:10px, IE6 will make the margin 20px.

Solution: just set the element’s display style to inline. (style=”margin-left: 10px; display: inline”)

Deploying WordPress in 6 easy steps

March 20, 2009

WordPress is great, but what do you do if you want to deploy a blog developed on your localhost on a remote host, let’s name it myblog.com. The problem is that WordPress keeps the absolute links in the database, on various fields in various tables. Searching and replacing those fields and tables can be frustrating, so I propose an alternative solution

Here are the steps to do:

  1. Create a backup copy of your database and WordPress files before proceeding
  2. Dump the database to a file, let’s call it myblog.sql
  3. Open it with Notepad++ or UltraEdit and use Find and Replace: Find: http://localhost Replace: http://myblog.com . After replacing all, save the SQL file
  4. Upload and import myblog.sql with PhpMyAdmin or other software
  5. Upload wordpress files, and set writing permissions in the wp-content folder
  6. You’re done

Hello world!

March 20, 2009

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!