I have a strong aversion to re-inventing the wheel, so I usually use the Makefile provided with the openssl package in RH/CentOS to create self-signed certs. Under CentOS 5 it is located in /etc/pki/tls/certs, under CentOS 4 it is in /usr/share/ssl/certs.
First, change your working directory to the appropriate location and use the make command to create a .PEM file containing the key and certificate. If you just run make with no parameters it will give you some quick help and show some examples of how to use it.
Most applications give you the option of keeping the key and certificate in the same file (like Apache), but you can break it up into separate key and certificate files if you need to either with a plain text editor or the openssl tools.
Your mileage may vary!