Archive for Oct, 2009
30Oct/091
How to remove Linux from a Mac?
I don't really see why would you do that, but here is a how to about removing Linux from a Mac.Linux creates few partitions (usually / and swap, it's also a common practice to have /home partition). Everything would be easy if only Mac OS's built-in Disk Utility would support deleting Linux partitions. But sadly that's not the case (even on Snow Leopard).
Here you have two options how to delete partition. First option is to use Windows Vista/7 Install DVD.
1. Put DVD in Super Drive and boot from a DVD (hold alt key on bootup).
2. Select regional settings and fresh install.
3. At the partition editor select every Linux partition and delete it. Windows installation actually deletes partitions immediately as opposite to Linux's practice where this is done on the end of the installation wizard.
4. Turn off Mac and remove DVD.
Second, more known approach is to use GParted Live CD which can be downloaded at http://gparted.sourceforge.net/livecd.php. You can also "burn" it on USB key.
1. Put DVD in Super Drive and boot from a DVD (hold alt key on boot up).
2. Go trough the text-based wizard (probably you won't need to do anything than just pressing Enter).
2. Select partitions you want to remove (be careful not to delete OS X's partitions) and click on Remove (it may take a few seconds).
4. Turn off Mac and remove DVD.
Now that you deleted partitions you can resize your OS X's partition with Disk Utility (/Applications/Utilities/Disk Utility.app).
Click on your HD and select Partition tab.

Resize the partition by dragging its left corner to the end of volume. Click Apply.
In the pop-up click Partition to complete the task (I'm not responsible for any problems that may occur during this step).

And after successful partitioning you will end up with only one OS X's partition (actually there is another one, but it's hidden from Disk Utility).
23Oct/090
mod_python and MySQLdb
How to install mod_python and MySQLdb in Debian Lenny?MySQL is one of greatest databases for web sites, and for applications and it's probably most used with PHP. Python is a good language for a lot of things. In my case, I needed some daemons, to do certain tasks. Firstly, I tried with PHP, but it is language for web pages, and it don't perform well as a daemon. So then I have to choose which language to use. C++ is powerful but very complex and there are other languages, that could fit me better for this project. I don't have a lot of idea about Perl and so I tried programming Python.
I faced two problems in last two days:
1. I had problems with installing MySQLdb
2. How to make and publish a simple website in Python?
MySQLdb
I got a few errors while trying to install MySQLdb by using python file.py install. As far as I searched the Internet, there are some workarounds but neither of them worked for me. And then it hit me. Doesn't Debian comes with apt, a great package tool?
Installing this plug-in is very easy with apt. You just need to run following command:
apt-get install python-mysqldb
And now you can use MySQL from Python.
Example:
#! /usr/bin/python
import MySQLdb
import sys #for exit
try:
conn = MySQLdb.connect(host="localhost", user="root", passwd="", db = "database", charset = "utf8", use_unicode = True)
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit(1)
cursor = conn.cursor()
cursor.execute("SET NAMES utf8;SET CHARACTER SET utf8;SET character_set_connection=utf8;")
cursor.close()
cursor = conn.cursor()
while(1):
cursor.execute("SELECT * FROM table")
if cursor.rowcount > 0:
rows = cursor.fetchall()
for row in rows:
if row == None:
break
print "Data: %s %s" % (row[0], row[1])
This is an example for UTF-8 charset encoding. I still don't know why I needed to close cursor after executing SET NAMES utf8, but this code should work without a problem.
After my daemon feetched all needed data, I need to display it through web browser. PHP came to my mind, but if one thing is in Python, why not make other thing to? So for that I needed mod_python.
How to install mod_python and use it?
It can be installed by typing following command:
apt-get install libapache2-mod-python
You need to put following code in .htaccess or your Apache's configuration file to use Python:
AddHandler mod_python .py
PythonHandler mod_python.publisher
Now just write Python code, save it as something.py and put it in your website's root.
15Oct/091
How to speed up loading of your website
Is your website slow? Do you even realize that it could load faster?Every webmaster tries to make web sites faster, so people will get better user experience. But there are few tricks that not everyone is aware of.
How to improve loading speed?
- cache static content (pictures, javascripts, css files)
- remove ETag from pictures
- compress content
First thing is to cache static content so that it can be loaded from visitor's computer and not from server on every visit. Caching will also decrease bandwidth. It's good to set expire tag for at least a year.
Every picture sent by a server include ETag by which browser can check if a picture from a server match picture from browser's cache. This is not needed on static content, which will never change (for example, uploaded pictures).
And don't forget about compressing content. Loading will be faster because a browser needs to download a compressed file which is smaller and it will save a little bit of bandwidth. Compression is good for static and also dynamic content (.htm/.html/.css/.js/.php).
Example .htaccess file:
Header unset ETag
FileETag None
<FilesMatch "^.*\\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
Header unset Last-Modified
Header set Expires "Fri, 21 Dec 2012 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
</FilesMatch>
<FilesMatch "\\.(js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>
You need to put this file in a root directory of your website. Be careful, file doesn't have any name, only extension "htaccess". If you're using nice/SEO URLs then there is a good chance of you having this file already in a root directory. Only thing you need to do is to open existing file and copy this example code on the bottom of the file. It will only work with Apache HTTPD Server (most popular http server).
Compressing dynamic PHP file
To compress PHP file you need to add following code on the top of the file:
<?php
ob_start("ob_gzhandler");
?>
Or a bit safer method, that will turn off GZIP compression if visitor's web browser doesn't support it:
<?php
if(!ob_start("ob_gzhandler")) ob_start();
?>
You can check site's performance with YSlow add-on for Firefox's developer plugin named Firebug.
Download: http://developer.yahoo.com/yslow/
7Oct/096
Convert video to FLV with PHP
How to convert video to .flv with php so that it can be played by Flash Player?This is a simple approach to converting videos to .flv with PHP. For example, users are allowed to upload videos, and then you can play it by Flash Player.
Installing FFmpeg
Probably you already have PHP installed, so let's continue with FFmpeg. Following commands works only in Debian/Ubuntu distribution.
To install FFmpeg you simply need to type:
apt-get install ffmpeg
FFmpeg is a library for converting video and capturing screenshots from video.
Now you need some sort of a plug in to allow PHP to access ffmpeg. So we need ffmpeg-php, which can be installed by following command:
apt-get install ffmpeg-php
Now you should be able to use it.
Example PHP script:
<?php
if($_FILES['file']) {
if (!$_FILES["file"]["error"] > 0) {
move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
$movie = new ffmpeg_movie($_FILES["file"]["name"]);
$srcWidth = $movie->getFrameWidth(); //Movie width
$srcHeight = $movie->getFrameHeight(); //Movie height
$srcFPS = $movie->getFrameRate(); //Movie frame rate
$srcAB = intval($movie->getAudioBitRate()/1000); //Audio bit rate
$srcAR = $movie->getAudioSampleRate(); //Audio sample rate
exec("/usr/bin/ffmpeg -i ".$_FILES["file"]["name"])." -ar 22050 -ab 32 -f flv -s ".$srcWidth."*".$srcHeight." new_movie.flv");
//Convert a movie from original type to FLV
exec("/usr/bin/ffmpeg -i new_movie.flv -an -ss 00:00:05 -an -r 1 -vframes 1 -y new_movie%d.jpg ");
//Make screenshot of a movie at 5th second
}
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
File: <input type="file" name="file" /> <input type="submit" value="Upload" />
</form>
This script saves an uploaded file, converts it to FLV and makes a screenshot of the movie at 5th second.
3Oct/091
VPN or how to access US sites abroad
If you live outside US you cannot watch Hulu/CBS/etc. videos on demand because you need US IP. The other reason to use VPN is a secure connection to VPN provider. So your ISP doesn't know what are you browsing until you are connected to VPN.There is some free and some payable services. Payable usually have no problems with speed and blocked IPs.
1. AnchorFree Hotspot Shield
It is a great service and it's free too. Downsides are that their IPs are blocked from VoD (video on demand) web sites and connection is not the fastest. Another downside is that you need to install their program in order to get service to work.
2. Payable service HappyVPN
I decided to try a payable service and I heard a lot good about HappyVPN, so I decided to give it a try.
It's 14.99$ per month and you will get unlimited access. Nice offer. So I payed for the first month and watched a few of episodes of various TV shows. Sometimes I got disconnected but I weren't worry about it too much. And after a week or a liitle bit more I am not able to even connect to this service.
So I decided to cancel my subcription to them.
You can try if you want. They offer payment by credit card or PayPal.
3. The best option is it's own server or VPS (Virtual Private Server)
They are from 20$ per month and you will really get unlimited access. There is only one downside, they have limited bandwidth.
So I bought one node from VPS.Net and try it out. You can pay with PayPal (I prefer this method, however they do not offer automatic payment through PayPal, like GoDaddy does) or credit card.
How to buy a node and create VPS at VPS.net
1. Go to http://vps.net/, select one node (or more if you need more bandwidth or multiple VPS for some reason) and click Buy Now!
2. Now you need to type your information and select your payment type. Then pay for it.
3. Go to your account and select Create new VPS.
4. Type VPS label and host name. For host name you can use your domain or sub domain. You don't actually need a domain.
5. Then select your cloud. UK to get access to UK sites (like BBC iPlayer) or US cloud to get access to US web sites (like CBS or FOX).
6. Select Debian 5.0 (Lenny) x64 and in the bottom box select VPN image. Then click Create.
7. You will be redirected to a page with your VPS's information. Wait a minute or two and then hit refresh.
8. Your VPS should be ready now. Use SSH program from Terminal (Mac/Linux) or Putty for Windows.
9. Connect to IP of your VPS and login as root with default password written on VPS's information page.
10. Type following command to edit username/password to access VPN server:
nano /etc/ppp/chap-secrets
11. Go to the end of file and type something like:
username pptpd password *
(change username and password with your desired username/password)
12. Save file and exit (Ctrl + C and then type "Y" (without quotes))
13. Type following command to reboot pptpd service in order to make VPN work:
/etc/init.d/pptpd restart
That's it for server side.
How to connect to the VPN network with Mac
Now that you bought VPN access or created VPS with VPN service you need to prepare your Mac to send all your Internet connections trought your VPN.
1. Go to System Preferences.
2. Go to Network and press on "+" button in the bottom left corner. Then change interface to VPN, select PPTP as VPN Type and enter your desired VPN Service Name.
3. Now type your IP address and username:
4. Click on Advanced and set up like in following picture:
5. Click Ok and then Apply. Now you need to type your password and hope everything is allright.
Everything should work now. You can check by going to http://www.ip-adress.com/ and if IP on site match your VPS's IP then you're good.
I'll write more about connecting to VPN on Windows in next blog post.





