Entries Tagged 'Linux' ↓

Installing Wordpress SVN with Nginx

Notes for installing Wordpress behind Nginx with friendly urls.

Checkout the latest version of Wordpress via subversion:

svn co http://svn.automattic.com/wordpress/trunk/ .
cp wp-config-sample.php wp-config.php

Modify the database settings in wp-config.php to match your setup. Visit http://yourdomain.com to finish the setup, login as administrator, click settings, click permalinks, put a dot in “custom structure” and enter:

/%year%/%monthnum%/%day%/%postname%/

Add the following in the appropriate place inside your Nginx configuration file:

# wordpress fancy rewrites
if (-f $request_filename) {
	break;
}
if (-d $request_filename) {
	break;
}
rewrite ^(.+)$ /index.php?q=$1 last;

Make sure to make your uploads folder writable (assuming www-data is the nginx user):

mkdir your/wordpress/location/wp-content/uploads
chown www-data:www-data your/wordpress/location/wp-content/uploads

MediaWiki Upgrade to Subversion

Upgrading an existing MediaWiki 1.13 installation to a bleeding edge Subversion installation. This assumes you installed to the default /wiki folder as suggested in the MediaWiki installation docs.

Backup database:

mysqldump -u wiki_user -p --all-databases > mediawiki_backup.sql

Move existing wiki files:

mv ./wiki ./wikiold

Download MediaWiki via Subversion:

svn checkout http://svn.wikimedia.org/svnroot/mediawiki/trunk/phase3 ./wiki

Copy LocalSettings.php & AdminSettings.php:

cp ./wikiold/LocalSettings.php ./wiki/LocalSettings.php
cp ./wikiold/AdminSettings.php ./wiki/AdminSettings.php

If you don’t have AdminSettings.php:

cp ./wiki/AdminSettings.sample ./wiki/AdminSettings.php

Edit AdminSettings.php to include the mysql username & password your MediaWiki installation uses.

Run the MediaWiki upgrade script:

php ./wiki/maintenance/update.php

Done. Now updates are as simple as running “svn update” in the MediaWiki folder.

Cygwin SSH Pre Shared Keys and Putty

On the Cygwin machine:

Generate SSH keys, since we’re going for password less logins, just hit enter on the prompts for passphrase:

cd ~
ssh-keygen.exe -t rsa

Copy the key to the remote machine:

scp ~/.ssh/id_rsa.pub username@remotemachine:/home/username/id_rsa.pub

On the remote machine:

Move the key and set permissions:

mkdir ~/.ssh
mv ~/id_rsa.pub ~/.ssh/authorized_keys
chown -R username:username /home/username/.ssh
chmod 700 /home/username/.ssh
chmod 600 /home/username/.ssh/authorized_keys

You should now be able to login to the remote machine without a passphrase:

ssh username@remotemachine

I’ve also found it handy to use Putty to run CygWin. There is a modified version of Putty called PuTTYcyg available at google code that you can use to launch Cygwin, as described in the PuTTYcyg FAQ here.

GNU Screen .screenrc file

Here is a copy of the .screenrc file I use on all the Linux machines I use. I’m not sure what I’d do without it.

If you’re using Ubuntu and you’re feeling lazy:

apt-get install screen
wget http://blog.elderec.com/wp-content/uploads/2008/09/screenrc.txt -O ~/.screenrc

Otherwise here are the contents of the .screenrc file.

# Bind F11 and F12 (NOT F1 and F2) to previous and next screen window
bindkey -k F1 prev
bindkey -k F2 next
 
# Window list at the bottom.
hardstatus alwayslastline
hardstatus string '%{= mK}%-Lw%{= KW}%50>%n%f* %t%{= mK} %+Lw%< %{= kG}%-=%D %d %M %Y %c:%s%{-}'
 
# set the size of the scrollback buffer to 99999 lines (default is 100)
defscrollback 99999
 
# Filename for the paste buffer
bufferfile /home/elderec/screen_buffer.txt
 
# turn off the startup message
startup_message off
 
# Autodetach session on hangup instead of terminating screen completely
autodetach on
 
#midnight commander fix
altscreen on

Install Subversion Trac Nginx on Ubuntu Hardy

The following are my notes for setting up is Subversion & Trac behind Nginx. I’m sure there are other ways to do this, but this is what I did, and it worked for me.

Install Python Setup Tools, Subversion, and TRAC:

aptitude install python-setuptools subversion python-subversion
easy_install trac

Create a subversion repository:

mkdir /var/lib/svn
mkdir /var/lib/svn/repositories
svnadmin create /var/lib/svn/repositories/ProjectName
chown -R www-data /var/lib/svn

Edit /var/lib/svn/repositories/ProjectName/conf/svnserve.conf and change to the following:

anon-access = none
auth-access = write
password-db = /var/lib/svn/repositories/ProjectName/conf/passwd
realm = ProjectName

Edit /var/lib/svn/ProjectName/passwd:

[users]
yourusername = yourpassword

Create init.d script to startup svnserve on boot, /etc/init.d/svnserve :

#!/bin/sh
#
# This starts and stops svnserv. Put this in /etc/init.d/svnserve.
# This works on both Ubuntu and RedHat systems.
# On Ubuntu, run "update-rc.d svnserve defaults" to install this on startup.
# On RedHat, run "chkconfig --add svnserve" to install this on startup.
#
# $Id: svnserve 98 2007-10-30 22:01:36Z noah $
#
# chkconfig: 2345 90 10
# description: svnserve daemon start script
 
# Use the following variables to customize user, group, and paths.
SVNSERVE=`which svnserve`
SVN_USER=svn
SVN_GROUP=svn
SVN_ROOT_PATH=/var/lib/svn/repositories/
 
if [ -f /etc/rc.d/init.d/functions ]; then
    # RedHat style
    . /etc/rc.d/init.d/functions
    START="daemon $SVNSERVE -d --root $SVN_ROOT_PATH"
    STOP="killproc $SVNSERVE"
else
    # Ubuntu LSB style
    . /lib/lsb/init-functions
    START="start-stop-daemon --start --exec $SVNSERVE -- -d --root $SVN_ROOT_PATH"
    STOP="start-stop-daemon --stop --exec $SVNSERVE"
fi
 
if [ ! -x $SVNSERVE ]; then
    echo "Could not find ${SVNSERVE}. PATH is ${PATH}"
    exit 0
fi
 
case "$1" in
    start)
        echo "Starting svnserve..."
        umask 002
        $START
        if [ $? ]; then
            echo "Started svnserve"
            exit 0
        else
            exit $?
        fi
    ;;
 
    stop)
        echo "Stopping svnserve..."
        $STOP
        exit $?
    ;;
 
    restart|force-reload)
        "$0" stop && "$0" start
    ;;
 
    *)
    echo "Usage: /etc/init.d/svnserve {start|stop|restart|force-reload}"
        exit 1
    ;;
esac
 
echo "Unhandled case while trying to start svnserve."
echo "see $0"
exit 3

Make it startup on boot:

cd /etc/init.d
update-rc.d svnserve defaults

Ok, subversion is done, now lets create a trac environment.

Create a place for our trac environments to live:

mkdir /var/lib/trac

Create Trac environment:

mkdir /var/lib/trac
trac-admin /var/lib/trac/ProjectName initenv
chown -R www-data /var/lib/trac

The “trac-admin” command shown above prompted me to enter:

  • the project name (ProjectName)
  • the path to svn repository (/var/lib/svn/ProjectName)

Add a user with the Apache htpasswd tool:

htpasswd -c /var/lib/trac/test/.htpasswd youruser

Startup the trac standalone server tracd:

tracd -d -p 3050 --basic-auth=test,/var/lib/trac/ProjectName/.htpasswd,/var/lib/trac/ProjectName /var/lib/trac/ProjectName --pidfile=/var/lib/trac/tracd.3050

Create admin account and revoke almost all anonymous permissions from trac:

trac-admin /var/lib/trac/<environment name> permission add youruser TRAC_ADMIN
trac-admin /var/lib/trac/<environment name> permission remove anonymous BROWSER_VIEW CHANGESET_VIEW FILE_VIEW LOG_VIEW MILESTONE_VIEW REPORT_SQL_VIEW REPORT_VIEW ROADMAP_VIEW SEARCH_VIEW TICKET_CREATE TICKET_MODIFY TICKET_VIEW TIMELINE_VIEW WIKI_CREATE WIKI_MODIFY

If you have an iptables setup or other firewall, don’t forget to allow connections to port 3690. Assuming your using slicehost like me, you can do the following:

Edit /etc/iptables.test.rules and add the following before any final LOG & REJECT rules:

# Allows svnserve connections from anywhere
-A INPUT -p tcp --dport 3690 -j ACCEPT

Activate the changes:

iptables-restore < /etc/iptables.test.rules

Save the changes:

iptables-save > /etc/iptables.up.rules

Startup SVNserve:

svnserve -d -r /var/lib/svn/ProjectName

Make SVNserve startup on reboot add the following to your crontab:

crontab -e
@reboot svnserve -d -r /var/lib/svn/ProjectName

You should now be able to checkin/checkout from your repository & visit http://trac.example.com to see your trac environment.

XBMC thumbnails via bash script

Simple bash script to create thumbnails for XBMC from avi files. Requires ffmpeg.

xbmcthumb.sh

 
#!/bin/sh
if [ $2 eq "" ]; then
	NUMSEC = 300
else 
	NUMSEC = $2
fi
 
for NAME in $(find $1 -type f -name '*.avi')
do
        ffmpeg -i "$NAME" -f mjpeg -t 0.001 -ss $NUMSEC -y $1/$(basename "$NAME" .avi).tbn
done
chmod +x xbmcthumb.sh
./xbmcthumb.sh /folder/with/avi/files/in/it

Ubuntu Hardy Nginx PHP5 FastCGI

Setup nginx, with php5 via FastCGI, and MySQL. The instructions below assume you have SU access and nginx already setup and running.

Install MySQL:

aptitude install mysql-server mysql-client libmysqlclient15-dev

Install PHP5

aptitude install php5-common php5-cgi php5-mysql php5-cli

To get PHP as FastCGI working with Nginx, we first have to spawn the fcgi process. There are a couple of different ways to do that. I used the spawn-fcgi app included with lighttpd. So we’re going to compile lighthttpd, but not install it.

wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz
tar xvzf lighttpd-1.4.19.tar.gz
cd lighttpd-1.4.18.tar.gz
./configure
make
cp src/spawn-fcgi /usr/bin/spawn-fcgi

Now we’ll create a script to launch the FastCGI process and have it listen on 127.0.0.1 port 9000, located at /usr/bin/php5-fastcgi

#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -C 2 -f /usr/bin/php5-cgi

Then we give it permissions:

chmod +x /usr/bin/php5-fastcgi

SymLink it to php-fastcgi:

ln -s /usr/bin/php5-fastcgi /usr/bin/php-fastcgi

Now we create a init.d script /etc/init.d/fastcgi

#!/bin/bash
PHP_SCRIPT=/usr/bin/php-fastcgi
RETVAL=0
case "$1" in
	start)
		echo "Starting fastcgi"
		$PHP_SCRIPT
		RETVAL=$?
  ;;
	stop)
		echo "Stopping fastcgi"
		killall -9 php5-cgi
		RETVAL=$?
  ;;
	restart)
		echo "Restarting fastcgi"
		killall -9 php5-cgi
		$PHP_SCRIPT
		RETVAL=$?
  ;;
	*)
		echo "Usage: php-fastcgi {start|stop|restart}"
		exit 1
  ;;
esac
exit $RETVAL

Give it permissions:

chmod 755 /etc/init.d/fastcgi

Start it up:

/etc/init.d/fastcgi start

And you should see something like:

Starting fastcgi
spawn-fcgi.c.197: child spawned successfully: PID: 19026

Now lets make it run on system boot:

update-rc.d fastcgi defaults

Create the FastCGI configuration file for nginx /usr/local/nginx/fastcgi.conf

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

Make nginx pass the .php files to our FastCGI socket:

location ~ \.php$ {
 
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www/example.com/public_html$fastcgi_script_name;
    include        /usr/local/nginx/fastcgi.conf;
 
        }

Bling, you should be good to go. I’ll discuss MySQL tuning in another article.

Install Nginx from source on Ubuntu Hardy with SSL

These are my notes for installing Nginx on Ubuntu Hardy 8.04 Server with SSL support. This was pieced this together from many different sources, thanks everyone for their help.

Install prerequisites:

aptitude install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev build-essential

Download, configure, and install Nginx:

mkdir ~/sources
cd ~/sources
wget http://sysoev.ru/nginx/nginx-0.6.31.tar.gz
tar -zxvf nginx-0.6.31.tar.gz
cd nginx-0.6.31/
./configure --sbin-path=/usr/local/sbin --with-http_ssl_module
make
make install

Installation complete, lets start it up:

/usr/local/sbin/nginx

Visit http://example.com and you should see the “Welcome to Nginx” page. Congrats! Now lets stop it:

kill `cat /usr/local/nginx/logs/nginx.pid`

Now we’ll create an INIT script so we can start/stop/restart it just like Apache, save this as /etc/init.d/nginx

#! /bin/sh
 
### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO
 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx
 
test -x $DAEMON || exit 0
 
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
        . /etc/default/nginx
fi
 
set -e
 
case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
                --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
                --exec $DAEMON
        echo "$NAME."
        ;;
 
  restart|force-reload)
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
                /usr/local/nginx/logs/$NAME.pid --exec $DAEMON
        sleep 1
        start-stop-daemon --start --quiet --pidfile \
                /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
          --exec $DAEMON
      echo "$NAME."
      ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
        exit 1
        ;;
esac
 
exit 0

Give it some permissions:

chmod +x /etc/init.d/nginx

Make Nginx startup on system boot:

/usr/sbin/update-rc.d -f nginx defaults

Setup rotation of the nginx logs:

/etc/logrotate.d/nginx

/usr/local/nginx/logs/*.log {
        daily
        missingok
        rotate 30
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
                [ ! -f /usr/local/nginx/logs/nginx.pid ] || kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
        endscript
}

Now you can start,stop, & restart nginx like so:

/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart