Category Archives: Linux

Cosas sobre linux

Fix yum error TypeError: can’t multiply sequence by non-int of type ‘float’

I ran into this bug which prevented me to update yum packages, and after a quick search, this helped to fix it:

# vim /usr/lib/python2.7/site-packages/urlgrabber/grabber.py

Go to line 1539, with vim you can use :1539

Replace the line with this:

if cur > (float(max_size)*1.10):

 

Quick tip: MySQL disallow database creation which certain name

If we want to prevent a name to be used as a schema name, it is pretty easy to accomplish:

# move to your mysql data folder, default /var/lib/mysql
cd /var/lib/mysql
# create an empty folder with the desired name
mkdir dontusethisname
# set permissions to 000
chmod 000 dontusethisname
# change ownership to mysql user
chown mysql:mysql dontusethisname

If you try to create this schema now, you will receive the following error:

mysql> create schema dontusethisname;
ERROR 1007 (HY000): Can't create database

 

 

How to deploy django with nginx + gunicorn

Django is a great web framework and we have several ways to deploy it in production.

You can use apache too for example, but for django my favorite is nginx + gunicorn.

Nginx is a well-known player and it will be used as front-end. Gunicorn is not as well-known, but it has proved to be quite stable (instagram and pinterest use it).

Installation:

yum install nginx
pip install gunicorn

Previously you have to have installed python pip and epel repository.

Next step is to run gunicorn, which is quite easy. As we are going to run it with a django project, it is possible to use gunicorn_django 

cd /path/to/your/django/project
gunicorn_django --workers=4 -b 127.0.0.1:8888 -D

And that is it. We have told gunicorn to run with 4 workers (processes), to listen on localhost:8888 and to run in background with -D.

Last step is to configure a server for nginx:

upstream app_server_djangoapp {
    server localhost:8888 fail_timeout=0;
}

server {
        listen 80;
        server_name  server.com;

        access_log  /path/log/access.log;
        error_log  /path/log/error.log info;

    keepalive_timeout 5;

    # path for static folder
    location /static {
        root /path/to/static/;
    }
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://app_server_djangoapp;
            break;
        }
    }
}

Restart nginx:

service nginx restart

Our django site is deployed, as easy as that.

Fix bug: Fedora 17 does not shutdown. Kernel Panic.

If you upgraded from Fedora 16 to Fedora 17, you may get an error when shutting down the system.

This happens because the kernel is the same, and grub2.cfg does not get the Fedora 17 kernel, but it is installed, although not recognized.

To fix this just run the following command:

# grub2-mkconfig > /etc/grub2.cfg && grub2-install /dev/sda

Notice that you should change /dev/sda for your partition

This will generate a new grub2.cfg with the new Fedora 17 kernel.

Convert m4a files to mp3 with this script.

Some of my library was in m4a files, which I have problems to play, furthermore I prefer having everything in mp3 format.

So I wrote the following script:

echo "Dependencies: mplayer and lame"
if [ $# -lt 1 ]; then
	echo "Usage: sh m4a2mp3.sh '/path/to/files'"
else 
	echo "This could take sometime. Converting to 320kbps MP3 files..."
	sleep 1
	cd "$1"
	for i in *.m4a; do mplayer -ao pcm "$i" -ao pcm:file="$i.wav"; done
	for i in *.wav; do lame -h -b 320 "$i" "$i.mp3"; done
	echo "Cleaning the house..."
	rm -rf *.m4a
	rm -rf *.wav
fi
echo "All files converted. Thank you."

Usage is pretty straightforward:

sh m4a2mp3.sh /path/to/your/songs

The only dependencies, as you can see also in the script are mplayer and lame

You can install them with the following command:

# Yum based (Fedora, CentOS, SuSE)
yum install mplayer lame
# Apt based (Debian, Ubuntu, Mint) (NOT TESTED)
apt-get install mplayer lame

As always, you also can find the script on my Github.