Archive for category Linux
17May/100
Ruby Gem update system on Debian
When you try to update Ruby Gem --system on Debian systems (probably also on Ubuntu) you will get the following error:ERROR: While executing gem ... (RuntimeError)
gem update --system is disabled on Debian. RubyGems can be updated using the official Debian repositories by aptitude or apt-get.
Gem installed through Debian's aptitude is configured to only be upgraded through aptitude (apt-get) package manager. That would not be a problem if aptitude had newest packages. However, they are updated irregurally and in most cases (almost ever) you will get older version than the current one.
Thankfully there is a simple solution to this problem.
Type following commands (under root user):
gem install rubygems-update
cd /var/lib/gems/1.8/bin
./update_rubygems
After that gem system should be succesfully updated.
6Dec/094
ffmpeg and x264
How to install ffmpeg with x264 codec support on Debian Lenny (5.0)?Why is this useful? By using x264 codec rather than a FLV video format you will gain on video and audio quality.
1. We need to remove any previous ffmpeg installations.
2. Add additional repository to /etc/apt/sources.list
Open file with nano editor:
nano /etc/apt/sources.list
and add following line on the end of the file:
deb http://www.debian-multimedia.org stable main
3. Install needed libraries:
apt-get install build-essential subversion git-core checkinstall yasm texi2html libfaac-dev libfaad-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libx11-dev libxfixes-dev libxvidcore4-dev zlib1g-dev libogg-dev
4. Install x264
git clone git://git.videolan.org/x264.git
cd x264
./configure
make
sudo checkinstall --pkgname=x264 --pkgversion "1:0.svn`date +%Y%m%d`" --backup=no --default
5. Install libtheora
wget http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.gz
tar xzvf libtheora-1.1.1.tar.gz
cd libtheora-1.1.1
./configure
make
sudo checkinstall --pkgname=libtheora --pkgversion "1.1.1" --backup=no --default
6. Install ffmpeg
svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg
cd ffmpeg
./configure --enable-gpl --enable-version3 --enable-nonfree --enable-pthreads --enable-libfaac --enable-libfaad --enable-libmp3lame --enable-libtheora --enable-libx264 --enable-x11grab
make
sudo checkinstall --pkgname=ffmpeg --pkgversion "4:0.5+svn`date +%Y%m%d`" --backup=no --default
7. Install qt-faststart
Normal mp4 file needs to be fully downloaded before it can be played by Flash Player. qt-faststart fixes this.
Type following command from ffmpeg source directory:
make tools/qt-faststart
Example:
ffmpeg -i input.mov -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -vpre hq -crf 22 -threads 0 output.mp4
./tools/qt-faststart output.mp4 output2.mp4
Now you can play output2.mp4 by any Flash player. LongTail's freeware JW Player is quite good, has many options and can play both mp4 and flv.
Now you can use ffmpeg to compress videos with x264 codec. Read more about converting video with PHP
Warning: Latest ffmpeg version has some problems with FLV format and it's impossible to convert video to .flv with it! If you need flv support try installing older version of ffmpeg (if you install it by apt-get install ffmpeg you will be able to convert to FLV but there is no x264 support).
Source: http://ubuntuforums.org/showthread.php?t=786095
28Nov/091
Use MySQL database in your C++ projects
How to use MySQL DB in your C++ projects. This tutorial is written for Debian Lenny/Ubuntu 9.10. With a few minor changes, it should work on other distributions also.What will you need?
- gcc <-- c++ compiler
- MySQL server 5.0 installed
- MySQL C API
To install above programs run this command as root:
apt-get install build-essential mysql-server mysql-client libsoci-mysql-gcc
When you have everything installed you need to create database and add user (MySQL Add User).
Now you will need to add a new table. The easiest approach is to use phpMyAdmin.
Simple program to test MySQL C API (copy it and save as filename.cpp):
#include <iostream>
#include <mysql.h>
using namespace std;
//MySQL variables
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;
int main() {
//Connects to MySQL DB
mysql_init(&mysql);
connection = mysql_real_connect(&mysql, "localhost", "username", "password", "database", 0, 0, 0);
//Checks if there was a problem with connection
if(connection == NULL) {
cout <<mysql_error(&mysql);
return 1;
}
//Execute query
query_state = mysql_query(connection, "SELECT * FROM table");
if(query_state != 0) {
cout << mysql_error(connection);
}
//Loads query data
result = mysql_use_result(connection);
//Writes data to stdio from first column on all rows in table
while((row = mysql_fetch_row(result)) != NULL) {
cout << row[0] << endl;
}
mysql_free_result(result);
mysql_close(connection);
}
You can compile it with following command:
g++ filename.cpp -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql
If there was no error displayed then your program compiled successfully. You can run by typing:
./a.out
And now you made your first C++ program that gets data from MySQL DB. You can do a lot more than just that.
You will probably need to read MySQL C API Manual to learn others commands.
Next time I will write about preparing Apple Xcode and Microsoft Visual Studio for using MySQL C API.
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.
28Sep/090
Install Sun Java on Debian Lenny
Installing Java on Windows is easy. Simply download .exe file and run it. But on Debian Linux it's a little bit harder because it's non-free software.It's free as in beer. So you can actually "taste" it, but cannot get the source code.
How to install Sun Java on Debian Lenny?
1. Open Terminal and login as root (type "su" and then type a password)
2. Type:
nano /etc/apt/sources.list
3. In file change:
deb http://ftp.uk.debian.org/debian/ lenny main
deb-src http://ftp.uk.debian.org/debian/ lenny main
to:
deb http://ftp.uk.debian.org/debian/ lenny main non-free
deb-src http://ftp.uk.debian.org/debian/ lenny main non-free
Save it.
4. Refresh aptitude packages by typing:
apt-get update
5. Type:
apt-get install sun-java6-jre
to install Java 6 from Sun.
26Sep/090
Screenshot of Web Page on Linux Server
How to make a screenshot of web page on Linux server?In this tutorial I will tell how to setup a server with Debian Lenny operating system to add capability for taking screenshot pictures of a web page you want.
Steps:
1. Login to server through SSH as root user.
2. Install vnc server, web browser and GUI with following command:
apt-get install tightvncserver xfonts-base xfce4 iceweasel imagemagick
It’s about 160MB to download and installation should take a few minutes.
3. Start VNC server by typing:
vncserver :1 -geometry 1024x768 -depth 24
4. Server should be running now, and you can stop some of probably unwanted services:
killall x-session-manager ssh-agent xftaskbar4 xfdesktop xfce4-panel
5. Now connect to the VNC server from your desktop machine and remove unnecessary panels, configure Iceweasel web browser.
If you’re using iptables firewall you’ll need to add following rule to allow access to VNC server:
-A INPUT -p tcp -m tcp --dport 5901 -j ACCEPT
6. That’s it. Now you have a machine capable of taking screenshots of websites.
How to take a screenshot?
Create a shell file (for example screenshot.sh) with following script which starts the web browser, loads a page, takes a picture and closes the web browser.
#!/bin/bash
export DISPLAY=":1"
/usr/bin/iceweasel --display :1 "$1" > /dev/null 2> /dev/null &
/bin/sleep 10
/usr/bin/import -window root -display :1 "$2"
killall -9 /usr/lib/iceweasel/firefox-bin
This script can be called like this:
./screenshot.sh http://www.google.com /var/www/picture_path.jpg
Now you can take picture of any page you’d like with this simple command. It takes about 11 seconds to take 1 screenshot (script needs to sleep for about 10 seconds so that the page can load in web browser - you can change that time but is not recommended to set it bellow 5 seconds because most of the pages won’t load that quickly).
When you’re finished with taking screenshots you can kill vncserver with following command:
vncserver -kill :1
That's it for this tutorial. Additionally you can write PHP script which calls screenshots.sh and automatically crop picture to remove sliders and other unwanted elements on it.
24Sep/091
MySQL Add User
The simplest way to add user to MySQL and grant privileges for specific database.1. Open terminal and type "mysql -u root -p" to start MySQL client and use your root account to connect to database.
2. Type "create database my_database;" to create a database with name my_database.
3. Type:
GRANT ALL PRIVILEGES
ON my_database.*
TO 'my_user'@'localhost'
IDENTIFIED BY 'my_password'
WITH GRANT OPTION;
With this command you've created a user with name my_user and password my_password with all privileges on my_database.
