Register now and start sharing your code snippets.

How to install and use the mysql-python library

Python posted 2 months ago by christian

First download mysql-python from http://sourceforge.net/projects/mysql-python.

Extract it and run:

   1  python setup.py build
   2  sudo python setup.py install

If you get this error you need to install python-dev package:

   1  In file included from _mysql.c:29:
   2  pymemcompat.h:10:20: error: Python.h: No such file or directory
   3  _mysql.c:30:26: error: structmember.h: No such file or directory
   4  In file included from /usr/include/mysql/mysql.h:44,
   5                   from _mysql.c:40:
   6  .
   7  .
   8  .
   9  _mysql.c:2808: warning: return type defaults to 'int'
  10  _mysql.c: In function 'DL_EXPORT':
  11  _mysql.c:2808: error: expected declaration specifiers before 'init_mysql'
  12  _mysql.c:2886: error: expected '{' at end of input
  13  error: command 'gcc' failed with exit status 1

Installing the python-dev package on Debian is done with apt-get or synaptic:

   1  apt-get install python-dev

Installing the library should now work:

   1  python setup.py build
   2  python setup.py install

Next test the library in the python console:

   1  import MySQLdb
   2  
   3  # Note that this example uses UTF-8 encoding
   4  conn = MySQLdb.connect(host='localhost', user='...', passwd='...', db='...', charset = "utf8", use_unicode = True)
   5  cursor = conn.cursor()
   6  
   7  
   8  cursor.execute ("SELECT * FROM cities")
   9  rows = cursor.fetchall ()
  10  
  11  for row in rows:
  12    print "%s, %s" % (row[0], row[1].encode('utf-8'))
  13  
  14  print "Number of rows returned: %d" % cursor.rowcount
  15  

Don’t forget to close the cursor and connection, and if you’re inserting data commit before closing, because autocommit is disabled by default:

   1  cursor.close ()
   2  conn.commit ()
   3  conn.close ()

For more information about MySQLdb see this article.

Tagged python, mysql, mysql-python, install

How to install and use the Sphinx search engine and acts_as_sphinx plugin on Debian Etch

Shell Script (Bash) posted 4 months ago by christian

Inspiration for this snippet was taken from this post on the Sphinx forum, plus this blog post.

Compiling Sphinx

First install the prerequisites:

   1  sudo aptitude install libmysql++-dev libmysqlclient15-dev checkinstall

Next download sphinx, libstemmer and install everything and the fish:

   1  cd /usr/local/src
   2  
   3  wget http://sphinxsearch.com/downloads/sphinx-0.9.8-rc2.tar.gz
   4  tar zxvf sphinx-0.9.8-rc2.tar.gz 
   5  
   6  cd sphinx-0.9.8-rc2/
   7  
   8  # Add stemming support for Swedish, Finnish and other fun languages.
   9  wget http://snowball.tartarus.org/dist/libstemmer_c.tgz
  10  tar zxvf libstemmer_c.tgz
  11  
  12  ./configure --with-libstemmer
  13  make
  14  
  15  make install

Configure Sphinx

Create a sphinx.conf file in your Rails config directory, as described here, or use this template.

Install acts_as_sphinx plugin

   1  ./script/plugin install http://svn.datanoise.com/acts_as_sphinx

Add acts_as_sphinx to your model:

   1  class Documents
   2     acts_as_sphinx
   3  end

Indexing content

   1  rake sphinx:index
   2  
   3  (in /var/www/xxx.com/releases/20080429144230)
   4  Sphinx 0.9.8-rc2 (r1234)
   5  Copyright (c) 2001-2008, Andrew Aksyonoff
   6  
   7  using config file './sphinx.conf'...
   8  indexing index 'xxx.com'...
   9  collected 5077 docs, 0.6 MB
  10  sorted 0.1 Mhits, 100.0% done
  11  total 5077 docs, 632096 bytes
  12  total 0.160 sec, 3950427.25 bytes/sec, 31729.86 docs/sec

Reindexing content

sphinx:index shouldn’t be run while the searchd process is running, so use rake sphinx:rotate instead, which restarts the searchd process after indexing.

Starting the daemon

   1  mkdir -m 664 /var/log/sphinx
   2  rake sphinx:start
   3  
   4  (in /var/www/xxx.com/releases/20080429144230)
   5  Sphinx 0.9.8-rc2 (r1234)
   6  Copyright (c) 2001-2008, Andrew Aksyonoff
   7  
   8  using config file './sphinx.conf'...
   9  Sphinx searchd server started.

Searching

   1  Documents.find_with_sphinx 'why did I write this'

Tagged sphinx, search, acts_as_sphinx, debian, etch, rails, install, libstemmer

How to install the stemmer4r gem on Mac OS X and Linux

Ruby posted 5 months ago by christian

The stemmer4r gem is fubar. Warning draft snippet…

   1  # gem install stemmer4r
   2  Bulk updating Gem source index for: http://gems.rubyforge.org
   3  Building native extensions.  This could take a while...
   4  ERROR:  While executing gem ... (Gem::Installer::ExtensionBuildError)
   5      ERROR: Failed to build gem native extension.
   6  
   7  ruby extconf.rb install stemmer4r
   8  
   9  Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6 for inspection.
  10  Results logged to /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/gem_make.out
  11  
  12  
  13  1. Change path of Ruby executable
  14  
  15  cd /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/
  16  vim extconf.rb
  17  
  18  #!/usr/bin/ruby -w
  19  
  20  to
  21  
  22  #ruby -w
  23  
  24  2. Compile libstemmer_c
  25  
  26  cd /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/libstemmer/
  27  make
  28  
  29  3. Compile stemmer4r
  30  
  31  cd /usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/
  32  
  33  Change path:
  34  /usr/local/ruby/lib/ruby/1.8/i686-linux/
  35  To:
  36  /usr/lib/ruby/1.8/x86_64-linux/
  37  
  38  Or wherever you have it installed
  39  
  40  ruby extconf.rb
  41  
  42  
  43  4. Build stemmer4r gem
  44  
  45  
  46  gem build stemmer4r.gemspec
  47  
  48  gem install stemmer4r-0.6.gem
  49  
  50  
  51  Problems
  52  
  53  gcc -shared -rdynamic -Wl,-export-dynamic   -L"/usr/lib" -o stemmer4r.so stemmer4r.o libstemmer_c/libstemmer.o  -lruby1.8  -lpthread -ldl -lcrypt -lm   -lc
  54  /usr/bin/ld: libstemmer_c/libstemmer.o(libstemmer.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
  55  libstemmer_c/libstemmer.o: could not read symbols: Bad value
  56  collect2: ld returned 1 exit status
  57  make: *** [stemmer4r.so] Error 1
  58  
  59  
  60  Add CFLAGS:
  61  
  62  root@aktagon:/usr/lib/ruby/gems/1.8/gems/stemmer4r-0.6/ext/stemmer4r/libstemmer_c# make
  63  include mkinc.mak
  64  CFLAGS   =  -fPIC
  65  libstemmer.o: $(snowball_sources:.c=.o)
  66          $(AR) -cru $@ $^
  67  

Tagged stemming, stemmer4r, install, osx, linux, gem

Installing Rails, mongrel and mongrel_cluster on Debian

Shell Script (Bash) posted 6 months ago by christian

DRAFT …

Install RubyGems

   1  http://rubyforge.org/frs/download.php/29548/rubygems-1.0.1.tgz
   2  
   3  tar zxvf rubygems-1.0.1.tgz
   4  
   5  cd rubygems-1.0.1
   6  
   7  ruby setup.rb

Install Rails

   1  gem install rails

Install sqlite3 (optional)

   1  apt-get install sqlite3 libsqlite3-dev
   2  gem install sqlite3-ruby

Install mongrel and mongrel_cluster

   1  $ gem install mongrel mongrel_cluster
   2  
   3  $ mongrel_rails cluster::configure -e production \
   4    -p 8000 \
   5    -a 127.0.0.1 \
   6    -N 3 \
   7    -c /var/www/xyz/current
   8  
   9  
  10  $ mongrel_rails cluster::start
  11  
  12  
  13  $ useradd -g www-data -d /var/www mongrel

Surviving reboots

   1  sudo mkdir /etc/mongrel_cluster
   2  
   3  sudo ln -s /var/www/xyz/config/mongrel_cluster.yml /etc/mongrel_cluster/xyz.yml
   4  
   5  sudo cp /usr/local/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster /etc/init.d/
   6  
   7  sudo chmod +x /etc/init.d/mongrel_cluster
   8  
   9  sudo /usr/sbin/update-rc.d -f mongrel_cluster defaults
  10  
  11  mongrel_cluster_ctl status

Stale pids

If your mongrels crash or if you kill them, mongrel_cluster won’t start your mongrels because mongrel_cluster believes the processes are still running, instead mongrel_cluster complains and does nothing:

   1  ** !!! PID file tmp/pids/mongrel.8000.pid already exists.  Mongrel could be running already.  Check your log/mongrel.8000.log for errors.
   2  ** !!! Exiting with error.  You must stop mongrel and clear the .pid before I'll attempt a start.

To fix this simply add the —clean switch to the /usr/local/lib/ruby/gems/1.8/gems/mongrel_cluster-1.0.5/resources/mongrel_cluster startup script:

   1  mongrel_cluster_ctl start -c $CONF_DIR --clean

Tagged rails, ruby, debian, install, sqlite3, mongrel, mongrel_cluster

Installing nginx on Debian

Shell Script (Bash) posted 6 months ago by christian

DRAFT …

Find latest version of nginx

http://sysoev.ru/en/ http://wiki.codemongers.com/NginxInstallOptions

Install a compiler otherwise: ./configure: error: C compiler gcc is not found

The command:

   1  sudo apt-get install build-essential

Install pre-requisites otherwise you’ll get:

Configuration summary + threads are not used + PCRE library is not found + OpenSSL library is not found + md5 library is not used + sha1 library is not used + zlib library is not found

The command:

   1  sudo apt-get install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev

Much better:

Configuration summary + threads are not used + using system PCRE library + using system OpenSSL library + md5 library is not used + sha1 library is not used + using system zlib library

Compile and install nginx

   1  $ ./configure \
   2          --sbin-path=/usr/local/sbin \
   3          --conf-path=/etc/nginx/nginx.conf \
   4          --pid-path=/var/run/nginx.pid \
   5          --error-log-path=/var/log/nginx/error.log \
   6          --http-log-path=/var/log/nginx/access.log \
   7          --with-http_ssl_module \
   8          --http-client-body-temp-path=/tmp/nginx_client \
   9          --http-proxy-temp-path=/tmp/nginx_proxy \
  10          --http-fastcgi-temp-path=/tmp/nginx_fastcgi
  11  $ make
  12  $ sudo make install

Run the install script

   1  cd /usr/local/src
   2  
   3  wget http://sysoev.ru/nginx/nginx-0.5.35.tar.gz
   4  
   5  tar zxvf nginx-0.5.35
   6  
   7  cd nginx-0.5.35

Create an nginx user and group

   1  $ useradd -g www-data -d /var/www nginx

Create the web server directory

   1  mkdir /var/www
   2  chown root.www-data /var/www
   3  chmod ug=rwx,o= /var/www

Test configuration

   1  nginx -t
   2  2008/03/09 20:51:05 [info] 5034#0: the configuration file /etc/nginx/nginx.conf syntax is ok
   3  2008/03/09 20:51:05 [info] 5034#0: the configuration file /etc/nginx/nginx.conf was tested successfully

Start nginx

   1  nginx

Tagged nginx, install, debian