How to install and use the Sphinx search engine and acts_as_sphinx plugin on Debian Etch
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'
MySQL backup with Auto MySQL Backup
http://sourceforge.net/projects/automysqlbackup/
1 cd /etc/mysql 2 wget http://garr.dl.sourceforge.net/sourceforge/automysqlbackup/automysqlbackup.sh.2.5 3 4 ln -s automysqlbackup.sh.2.5 backup-script 5 chmod 740 backup-script 6 7 vim backup-script
Next tell cron to run it 4 in the morning:
1 crontab -e
Add the following line:
1 0 4 * * * /etc/mysql/backup-script
Linux NAT with 3g PPP connection
I run this nat.sh script whenever I need to share my 3g connection to other computers in the switch I’m plugged into. Please note that it resets existing iptables rules.
1 #!/bin/bash 2 iptables --flush 3 iptables --table nat --flush 4 iptables --delete-chain 5 iptables --table nat --delete-chain 6 7 echo 1 > /proc/sys/net/ipv4/ip_forward 8 9 internal=eth0 10 external=ppp0 11 /sbin/iptables -t nat -A POSTROUTING -o ${external} -j MASQUERADE 12 /sbin/iptables -A FORWARD -i ${external} -o ${internal} -m state --state RELATED,ESTABLISHED -j ACCEPT 13 /sbin/iptables -A FORWARD -i ${internal} -o ${external} -j ACCEPT
Installing/compiling and using git with Ruby on Rails (on Mac OS X Leopard and Debian Linux)
Git is a good alternative to Mercurial, and of course SVN or CVS if you’re still using stone age tools, so in this post I’ll show you how to compile, install and use git with Rails.
Installing git on Mac OS X
First compile and install git:
1 cd /usr/local/src 2 wget http://kernel.org/pub/software/scm/git/git-1.5.4.4.tar.bz2 3 tar jxvf git-1.5.4.4.tar.bz2 4 cd git-1.5.4.4 5 make prefix=/usr/local all 6 make prefix=/usr/local test && echo $? 7 sudo make prefix=/usr/local install
Installing git on Debian
On a Debian installation install git by first executing the following commands:
$ sudo apt-get install git-coreNote that the package name is git-core not git.
If you want the latest and greatest version, you first need to install the dependencies (note that you can leave out tk and expat):
1 sudo apt-get install curl 2 sudo apt-get install libcurl3 3 sudo apt-get install libcurl3-dev 4 sudo apt-get install tk8.4 5 sudo apt-get install cpio expat 6 sudo apt-get install zlib 7 sudo apt-get install build-essential 8 sudo apt-get install zlib1g-dev 9 sudo apt-get install asciidoc 10 sudo apt-get install xmlto
Then compile and install:
1 NO_EXPAT=yes NO_SVN_TESTS=yes NO_IPV6=yes NO_TCLTK=yes make -j2 prefix=/usr all 2 NO_EXPAT=yes NO_SVN_TESTS=yes NO_IPV6=yes NO_TCLTK=yes make -j2 prefix=/usr install
Configuring git
Run these commands to tell git your name and email:
1 git config --global user.name "u name" 2 git config --global user.email x@x.com
Otherwise, you might get this error:
1 *** Environment problem: 2 *** Your name cannot be determined from your system services (gecos). 3 *** You would need to set GIT_AUTHOR_NAME and GIT_COMMITTER_NAME 4 *** environment variables; otherwise you won't be able to perform 5 *** certain operations because of "empty ident" errors. 6 *** Alternatively, you can use user.name configuration variable. 7 8 fatal: empty ident <........@........com> not allowed 9 fatal: The remote end hung up unexpectedly
If you like colorized command output execute these commands:
1 git config --global color.diff auto 2 git config --global color.status auto 3 git config --global color.branch auto
Using git
If all goes well, change to your project directory and run the following commands:
1 git init
This creates the git repository, so we’re now ready to start adding files to it, but first we need to create the git ignore file, which tells git to ignore certain files completely:
1 cat <<EOF<<EOF > .gitignore 2 config/database.yml 3 db/*.sqlite3 4 log/*.log 5 tmp/**/* 6 .DS_Store 7 doc/api 8 doc/app 9 EOFEOF
By default git doesn’t add empty directories—sucks if you ask me—so we’ll create a dummy file in all empty directories with the find and touch commands:
1 find . \( -type d -empty \) -and \( -not -regex ./\.git.* \) -exec touch {}/.gitignore \;
Importing files
We’re now ready to start adding and commiting files, so without thinking execute:
1 git add . 2 git commit -m 'initial import'
This creates the git repository, adds and commits all files that are in the current folder.
Using remote repositories
If you’re like me you’ll want to use a remote repository, so let’s continue the exercise by creating the repository folder on the remote server (Note that commands are executed on the remote server from now on):
1 mkdir /var/lib/git/repositories/project_name
We want the folder to be accessible by users belonging to the git group only:
1 addgroup git 2 chown root.git /var/lib/git/repositories/project_name 3 chmod 770 /var/lib/git/repositories/project_name
Now add yourself—or the user you’ll be using to connect to the remote server—to the git group:
1 usermod -a -G git your_username
Alternatively create a new user:
1 useradd -g git your_username
Now we’re finally ready to copy the local repository to the remote server, which is done with the scp command (Note that commands are executed locally again from now on):
1 scp -rp .git user@server://var/lib/git/repositories/project_name
To let git know that this repository exists we’ll use the git remote command:
1 git remote add project_name ssh://server/var/lib/git/repositories/project_name
This adds the information to .git/config, which might be good to have a quick look at.
Note that if you’re using a non-standard SSH port you need to add the following to your ~/.ssh/config file:
1 Host server 2 Port 1234
Commit files and push them to the remote server
Now change a file and commit and push the changes to the remote server:
1 git commit -m "Me be sleepy" 2 git push project_name
If you get an error such as this it means you need to install git:
1 $ git push project_name 2 username@server's password: 3 sh: git-receive-pack: command not found 4 fatal: The remote end hung up unexpectedly
That’s all…
Miscellaneous problems
error: unable to create temporary sha1 filename ./objects/obj_FUu2jb: Permission denied
Resources
http://jointheconversation.org/railsgit
http://devblog.michaelgalero.com/2007/12/17/my-git-notes-for-rails/
http://railscasts.com/episodes/96
http://groups.google.com/group/rails-oceania/browse_thread/thread/2c8611dc93917952/e175f72310823547
http://www.kernel.org/pub/software/scm/git/docs/tutorial.html
http://scie.nti.st/2007/11/14/hosting-git-repositories-the-easy-and-secure-way
nginx startup script for Debian
1 sudo vim /etc/init.d/nginx
Paste in the following (remember to run ‘set :paste’ in VIM when pasting):
1 #! /bin/sh 2 ## 3 # nginx start script 4 ## 5 6 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 7 DAEMON=/usr/local/sbin/nginx 8 NAME=nginx 9 DESC=nginx 10 11 if [ ! -x $DAEMON ] 12 then 13 echo "Couldn't find $DAEMON. Please set path to DAEMON." 14 exit 0 15 fi 16 17 18 # Include nginx defaults if available 19 if [ -f /etc/default/nginx ] ; then 20 . /etc/default/nginx 21 fi 22 23 set -e 24 25 case "$1" in 26 start) 27 echo -n "Starting $DESC: " 28 start-stop-daemon --start --pidfile /var/run/$NAME.pid \ 29 --exec $DAEMON -- $DAEMON_OPTS 30 echo "$NAME." 31 ;; 32 stop) 33 echo -n "Stopping $DESC: " 34 start-stop-daemon --stop --pidfile /var/run/$NAME.pid \ 35 --exec $DAEMON 36 echo "$NAME." 37 ;; 38 restart|force-reload) 39 echo -n "Restarting $DESC: " 40 start-stop-daemon --stop --pidfile \ 41 /var/run/$NAME.pid --exec $DAEMON 42 sleep 1 43 start-stop-daemon --start --pidfile \ 44 /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS 45 echo "$NAME." 46 ;; 47 reload) 48 echo -n "Reloading $DESC configuration: " 49 start-stop-daemon --stop --signal HUP --pidfile /var/run/$NAME.pid \ 50 --exec $DAEMON 51 echo "$NAME." 52 ;; 53 *) 54 N=/etc/init.d/$NAME 55 echo "Usage: $N {start|stop|restart|force-reload}" >&2 56 exit 1 57 ;; 58 esac 59 60 exit 0
Now make the script executable with this command:
1 sudo chmod 755 /etc/init.d/nginx
Lastly, run this command to make the script run when the server starts and stops:
1 sudo /usr/sbin/update-rc.d -f nginx defaults