Tuesday, March 11, 2008

Rails Mongrel and Apache servers work with SSL, certificates and https

I am already running Apache as the front end server and Mongrel Rails Cluster Servers as the back end. To make them work with SSL, certificates and HTTPS, there are several easy steps to finish.

# 1. make a directory to store ssl key and certificates
mkdir /etc/httpd/conf/ssl

# 2. create openssl key and make it secure
cd /etc/httpd/conf/ssl
openssl genrsa -des3 -out yourdomain.key 1024
chmod 600 yourdomain.key

# 3. create CSR file (certificates signing request)
# Common name is your domain name without http:// but with www.
openssl req -new -key yourdomain.key -out yourdomain.csr

# 4. I bought godaddy's ssl certificate
# And got the certificate by email in 5 mins after I submitted the CSR.
Download the CSR zip file, unzip the two certs into ssl directory

# 5. Config apache's yourdomain config part
# Assuming that you have configurated yourdomain.common and yourdomain.cluster_proxy.conf properly.
virtualhost
ServerName www.yourdomain.com:443
Include /etc/httpd/conf/yourdomain.common

RequestHeader set X_FORWARDED_PROTO 'https'

ErrorLog logs/yourdomain_ssl_errors_log
CustomLog logs/yourdomain_ssl_log combined

SSLEngine on
SSLCertificateFile /etc/httpd/conf/ssl/yourdomain.crt
SSLCertificateKeyFile /etc/httpd/conf/ssl/yourdomain.key
SSLCertificateChainFile /etc/httpd/conf/ssl/gd_intermediate_bundle.crt
SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
/virtualhost

# 6. install and use plugin ssl_requirement
script/plugin install ssl_requirement
include SslRequirement (in your application.rb)
ssl_required :login, :orders (in your controller with those actions)

No comments: