Archive for the ‘Open Source’ Category

A Step by Step Guide to Setup Rails application on Ec2 instance (Ubuntu Server)

Sometimes the Bitnami or other Rails AMIs doesn’t fit your needs directly and you will feel the need of building the Server yourself.Here I go step by step in building such a stack on top of Amaon EC2 Ubuntu Server.
  • Rails applications are a little bit different to install on servers but the process is very easy.Rails application needs a web server and an application server to run with. For development, it comes with default Webrick server that serve as application server on local machine. For setting it up on production server, we have the following choices on Web and application servers :-

    • Web servers

      1. Apache

      2. Nginx

    • Application Servers

      1. Passenger

      2. Thin

      3. Puma

      4. Unicorn

  • The simplest and best combination consists of Nginx + Passenger. It allows greater flexibility for configuration and also allows good speed over other combinations. So we are going to setup an Rails application using Nginx + passenger configuration on a bare Ubuntu server. Here are the steps :-

  1. Launch an Ec2 instance with ubuntu AMI. Make sure you have HTTP and SSH access to the server.

  2. SSH into the server by using private key (.pem) used while launching the instance and install the available updates by running :-

    sudo apt-get install updates
  3. Now you need to setup ruby on your server, so install the single user rvm ruby by following this blog.
  4. Load the rvm and make the installed ruby as default by running the following commands :-

    source ~/.rvm/scripts/rvm
    rvm use 2.1.0 ‚Äďdefault
  5. Install the version control to clone your rails application to server. We generally use Git with rails application which can be installed by running the following command :-

    sudo apt-get install git
  6. Now clone your application on the server :-

    git clone yourepo.git

    Note:- In case of private git repository, you need to add public key of server to deploy keys of your repository, otherwise you will be promped with an permission denied error.

    OR

    Deploy using application to this server using Capistrano script. Please read this blog for more details on deploying your application using Capistrano.

  7. Now go to your application and install the gems by running bundle install command. If you want to setup your database on the same server, you can do the same by using the following commands ūüėź

    • In case of MYSQL

      sudo apt-get install mysql-server mysql-client
      sudo apt-get install libmysql++-dev
    • In case of POSTGRESQL, follow this blog for installation and then install the development headers

      sudo apt-get install libpq-dev

      After setting this up, migrate your databases in whichever environment you want to launch the server.

  8. Now install the Passenger gem by running :-

    gem install passenger

  9. Next step is to install the Nginx server, but we have some pre-requisits for this.

      1. It needs curl development headers which can be installed by :-

        sudo apt-get install libcurl4-openssl-dev

      2. It will be installed under /opt directory and your user should have permissions to that folder, so make your user as user owner for /opt directory by :-

        sudo chown -R ubuntu /opt

  10. Now install the Nginx server with passenger extension by running the following command :-

    passenger-install-nginx-module

  11. Set your Nginx server as service in init script by using the following commands :-

    wget -O init-deb.sh http://library.linode.com/assets/660-init-deb.sh
    sudo mv init-deb.sh /etc/init.d/nginx
    sudo chmod  +x /etc/init.d/nginx
    sudo /usr/sbin/update-rc.d -f nginx defaults
  12. Setup your application path in the nginx configuration file i.e. /opt/nginx/conf/nginx.conf.

    server {

    listen 80;

    server_name localhost;

    root /home/ubuntu/my_application/public #<-- be sure to point to 'public'

    passenger_enabled on;

    rails_env production;

    }

  13. Lastly start your server by running the following command :-

    sudo service nginx start

     


Paypal Integration with RoR application – Securing the transaction

This post is in continuation with the Post Paypal Integration with Rails Application – Integrating IPN. It is important that we secure our paypal transactions with some sort of encryption so that evil hackers dont find it a cakewalk to hack across the various parameters being sent to the paypal server. This step is completely optional but very much neccessary for the secure payments on our website. Generating a Key Pair The first step in securing the payments is to create a key that only our app understands. For this purpose, Create a directory named as ‘certs’ to accommodate certificates. Now generate an openssl keypair and RSA certificate using the following commands:
mkdir certs
cd certs
openssl genrsa -out app_key.pem 1024
openssl req -new -key app_key.pem -x509 -days 365 -out app_cert.pem
Now you have two files in certs directory. Login Merchant’s Account -> Options -> Encrypted Payment Settings -> Add -> browse and select app_cert.pem file -> Add. After you are done, you will see a column ‘Cert ID’ ,copy and paste (in some text file) the text under this column, its your paypal certificate id. We will use if in further steps. Select the added certificate (using radio buttton) and click Download button. Move the downloaded file to our certs directory created above. Modifying the application logic Now we need to modify the application logic to encrypt the parameters and propogate the neccessary certificate information to paypal to be able to decrypt the parameters at its end. In paypal_url method of app/models/product.rb, add :
cert_id =&gt; AppConfig.cert_id, after :notify_url =&gt; notify_url, replace :business =&gt; YOUR_MERCHANT_EMAIL with :business =&gt; AppConfig.paypal_email and replace

1 1="+" 2="values.to_query" 3="[/php" language="https://www.sandbox.paypal.com/cgi-bin/webscr?"]
with
encrypt_for_paypal(values)
24.Add following code to encrypt the information being sent to the paypal:
PAYPAL_CERT_PEM = File.read("#{Rails.root}/certs/paypal_cert.pem")
APP_CERT_PEM = File.read("#{Rails.root}/certs/app_cert.pem")
APP_KEY_PEM = File.read("#{Rails.root}/certs/app_key.pem")

# openssl encryption.
def encrypt_for_paypal(values)
signed = OpenSSL::PKCS7::sign(OpenSSL::X509::Certificate.new(APP_CERT_PEM),                                                         OpenSSL::PKey::RSA.new(APP_KEY_PEM, ''), values.map { |k, v|                                                                   "#{k}=#{v}" }.join("\n"), [], OpenSSL::PKCS7::BINARY)
OpenSSL::PKCS7::encrypt([OpenSSL::X509::Certificate.new(PAYPAL_CERT_PEM)],                                                        signed.to_der, OpenSSL::Cipher::Cipher::new("DES3"),                                                                                                                   OpenSSL::PKCS7::BINARY).to_s.gsub("\n", "")
end
Set the following values in your configuration file.
development:
paypal_url: "https://www.sandbox.paypal.com/cgi-bin/webscr"
paypal_email:  YOUR_MERCHANT_EMAIL
cert_id: PAYPAL_CERT_ID

production:
paypal_url: "https://www.paypal.com/cgi-bin/webscr"
paypal_email:  YOUR_MERCHANT_EMAIL
cert_id: PAYPAL_CERT_ID
Note:PAYPAL_CERT_ID is the certificate id that is saved above. In app/views/products/index.html.erb, replace
product.paypal_url(products_url)
with
product.paypal_url(products_url, notification_products_url)
Login to merchant account -> Profile -> Website Payment Preferences -> Select ‘ON’ radio button under heading ‘Encrypted Website Payments’ -> Save. It will block non-encrypted websites to make payments. That‚Äôs it. You are now sending encrypted parameters to paypal. Troubleshooting: uninitialized constant AppConfig : Please use whatever protocol you use to load environment specific configuration settings.

jPDF TWEAK REVIEW- Free all-in-one PDF tweak toolkit

jPDF TWEAK jPDF Tweak is a Java Swing application that can combine, split, rotate, reorder, watermark, encrypt, sign, and otherwise tweak PDF files.ÔĽŅÔĽŅ

pdf-tweak.jpg (234√ó168)

WHY JPDF TWEAK: You can use it to make printable booklets from PDFs, to add PDF bookmarks, effects (page transitions), to combine multiple PDF files, to watermark them, to rotate pages that do not fit, to attach files to your PDF, to encrypt and sign your PDFs, to change metadata (like author or keywords), and much more. SYSTEM REQUIREMENTS: One needs Java 5 or higher to run jPDF tweak. STARTING: Start jPDF Tweak by running jpdftweak.bat, double-clicking jpdftweak.jar or running ‚Äújava -jar jpdftweak.jar‚ÄĚ at the command line. THE MAIN WINDOW: The main window is divided into multiple tabs. You can select options from as many tabs as you need. Press Run when you are finished setting options. The Input Tab

input-tab.jpg (215√ó160)

Select an input file to manipulate. If you want to combine multiple files, check the checkbox and add more files. They will end up in the box, where you can select pages and/or reorder them. Click Add in the lower left corner to add another entry for a file already used. If you want to process multiple files at the same time, select batch processing and add multiple files. In that case, you should use variables in the output filename or each file will overwrite the previous one. If the file is encrypted, you will need the owner password to decrypt it. It is¬†possible to decrypt by using the user password only, but it is not allowed to do so. All page numbers start with 1 (like normal people count), not with 0 (like programmers count). Note, if you use the “combine multiple files” option, some options that are usually kept in the document are dropped, even if only one file is selected. This also applies to the command line mode – if you use the¬†–i option, it will use multiple file mode and drop page numbers, document info, forms, etc. The Page Size Tab

page-size.jpg (224√ó161)

Rotate Pages: If you have a PDF that has both Portrait and Landscape pages, and your printer has problems in printing both, you can rotate the pages so that they are all Portrait or Landscape afterwards. You can use this option as well to rotate all pages. Remove implicit page rotation: PDF knows two ways of rotating pages:
  • Rotating the content
  • Rotating the media (implicitly).
Some tools have problems with rotated media, so you can change all Media rotations to content rotations with this option (The option above creates media rotation as well). jPDF Tweak should work with rotated media as well. If you have problem with rotated pages, try checking this option and, if it helps, report a bug. Scale pages: It is useful if your PDF contains pages of different size. Some tools (like the Shuffle tab of this tool) require pages of equal size. This option can be use to scale all pages to the same size. You can use this option as well if all pages are of equal size. In that case, you might as well use the scale option of your PDF viewer program. PostScript points: A PostScript point is the 72th of an inch. Center instead of enlarging: This can be use, if the new page size is larger than the old one and the pages should be centered instead of enlarged. Do not preserve aspect ratio: Causes funnily stretched pages if the aspect ratio has changed. The Watermark Tab

water.jpg (230√ó165)

You can add two kinds of watermarks and page numbers. The text watermark appears on top of the content, the PDF watermark on bottom. So if your PDF pages are completely filled (maybe even with white color), you won’t see a PDF watermark. Text watermark and page numbers use the built-in Helvetica font (similar to Arial on Windows systems). Page numbers can be printed on any corner or edge of the page, or in the middle (mostly useful for testing purposes). The position can be mirrored on even pages, to get the page number on the outer or inner edge for duplex documents. If plain numbers are not enough for you, you can use a mask to format your numbers. This mask uses the same syntax as the standard Java¬†formatting function, and supports the current page number and the total number of pages as parameters. The current page number is available in a plain way (1-n) that you can format yourself, and additionally in a shifted way (if your document’s page numbers are shifted), and in a pre-formatted way (which is interesting for letters or roman numbers). In case you want to change the page numbers for the printed values, you can do this on this tab as well, in the same format as on the Page Numbers Tab. Note that if you do not select different page numbers on the Page Numbers Tab, and no other transformation invalidates your page numbers, these page numbers will also be present in the output document as if they were configured on the Page Numbers Tab as well. So, for this common case, it is enough to configure the page numbers once. The Shuffle/N-up Tab

shuffle.png (235√ó164)

The most powerful and the most complex tab. First specify how many pages each pass (each use of the template) covers. If you select 4 here, and your PDF has 21 pages, it will be run 6 times (5 times with 4 pages each, and once with the last page). If you give a negative number, you can take half of the pages from the end of the document instead of from the beginning. This is useful for booklet layouts. Use positive page numbers like “+2″ to refer to the second page of the template, and use negative page numbers like “-3″ to refer to the third page of the “opposite” template (i.e. the one if you process the file from end instead of from beginning). An absolute number without sign (like “2”) refers to the same absolute page (i.e. the second page of the file). For the offsets and factors: Just tweak them until it looks correct in the preview. If you rotate a page and it is gone, this is most likely caused by the fact the rotation used the lower left corner as center point and not the center of the page. Uncheck the¬†New Page Before to put more than one source page onto one destination page. Creating a configure with both positive and negative numbers can be confusing. For a test, you might add huge page numbers to your document so you can see quickly if your configure is correct. The Page Numbers Tab

page-number.jpg (221√ó160)

In the page Numbers Tab, you can tweak the page numbers that appear in your PDF reader. Depending on your PDF reader, they can be used for displaying the current page in the toolbar and status bar, jumping to a page by its number and/or printing page ranges. Most printed documents have some kind of title pages and or table of contents that are outside of the page numbers or are numbered with Roman numbers. This makes it hard if you read about something “on page 200″ to jump to this page 200, because it will be the 205th page and not the 200th if there are five pages before page 1. Note that these page numbers do not appear on the page itself. For this kind of numbers, there is an option on the “Watermark” tab. To just change the number of the first physical page, click “Add” once, leave the “Start Page” at one and change the other controls. If you want to have “gaps” in your page numbers, add more lines and use “Start page” to point to the physical page numbers (i.e. those that start from 1) where the format should change. Assume you have a 6 page PDF, and you set it up as follows:
Start Page Style Prefix Logical Page
1 Empty Title 1
2 I, II, III 4
5 1, 2, 3 S 32
Then the six pages will have numbers¬†Title,¬†IV,¬†V,¬†VI,¬†S32,¬†S33 and so on. Be careful that your document does not end up having two pages with the same number (as represented as text) – some versions of Adobe Reader don’t really like that. You can work around it by setting a prefix for these pages or use different number styles. The Bookmarks Tab

bookmark.png (223√ó167)

With this option, you can tweak chapter bookmarks. If you select more than one input file, chapter bookmarks will be combined automatically. But if you select individual pages or ranges instead of the full document, you will have to tweak the bookmarks manually. If you want to create bookmarks from scratch, it is useful to open existing PDFs and look what the bookmarks look like in there. If you want to add bookmarks that do not only point to a page but to a position on a page, you might need a tool like GSView (from GhostScript) which coordinates when moving the cursor on a PDF file. The Attachments Tab

attachment-tab.jpg (230√ó160)

Here you can add attachments and remove files you erroneously attached before. This view does not show which files have been attached to the original document. If you need them, use your PDF viewer to save them and reattach them if necessary. The Interaction Tab

interaction-tab.jpg (246√ó177)

When a PDF file is shown in full screen mode, pages can flip automatically and/or with a nice effect. Select effects and/or durations (durations are in seconds) on the left. You can set viewer preferences (how the document should be opened) on the right. The Document Info Tab

doc-info-tab.jpg (241√ó162)

In the document info tab, you can add information to the document info dictionary (shown when you open “Document summary” The Encrypt/Sign Tab

encrypt-tab.jpg (241√ó165)

If you know the owner password, you may do everything with the document, if you know only the user password; you may only do things checked below. The user password may be empty, the owner password may not (but you can use the same password for both if desired). Signing is a bit trickier, since you need a key and a certificate for this to be useful. Import that key into a Java Key Store (using Sun’s¬†key¬†tool), and you can use it from here. The Output Tab

output-tab.jpg (235√ó168)

Select an output filename here. If you select a name of an existing file, you will receive a warning. However, if you run jPDF tweak more than once, it will¬†not create a warning since warnings are annoying if you are just trying to find the right settings by trial and error. You may optionally burst the document into single page PDFs. Note; all features (like bookmarks, transitions or viewer preferences) make sense when you burst a document. When you save a document uncompressed, you can add page marks (compatible to pdftk’s page marks) to find pages easier in the PDF source code. Search for “pdftk_PageNum” in the uncompressed PDF to find a page. When you compress a PDF again, you can remove these marks. How much does it cost? jPDF Tweak are open source and free software and given its capabilities, it stands apart from others. How to remain update about Jpdf Tweak: You can visit Jpdf Tweak homepage to check for the latest version and tools. You all are invited to share and discuss your views. Alternatively, anyone can leave a comment at the bottom of this page.

Setting up Rails 3 stack on an Amazon EC2 instance

Recently, we had an encounter with Rails 3 on EC2 for one of our esteemed clients. The client wanted to host his application on Amazon EC2(and rightly so given that its such an amazing infrastructure at such an affordable cost). After searching the public AMIs, we could not come out with an AMI that had all our requirements satisfied(Rails 3, Postgres, phusion passenger). This lead us to trigger our sys admins to prepare the bare minimum EC2 AMI with the required development stack. We documented the steps if they can be helpful to someone. 1. Choose a bare minimum AMI, we chose centos 5.5 based 64 bit AMI. 2. download the keypair and log into your server with ssh. 3. You need to be root to perform the following steps, so if you log in with ec2-user type in the following command to become the root user:
sudo su -
Install Ruby and Rails 3:
  1. The amazon machine instance(AMI) comes with ruby 1.8.7 installed as of today.
  2. Rails 3 needs ruby 1.9+ to work.
  3. Ruby 1.9.1 is a buggy version so we will go ahead and install ruby 1.9.2
  4. We could not find rpms of ruby 1.9.2 on centos repos or even on rpmforge, so we had to go ahead and install ruby from source.
  5. Download ruby sources from http://www.ruby-lang.org/en/downloads
  6. wget ftp://ftp.ruby-lang.org//pub/ruby/1.9/ruby-1.9.2-p136.tar.gz
  7. unpack it !!
    tar xzvf ruby-1.9.2-p136.tar.gz
  8. Install development tools.
    • yum groupinstall ‚ÄėDevelopment Tools‚Äô
    • yum install readline-devel
  9. Move to ruby sources and compile
  10. cd ruby-1.9.2-p136
    ./configure
    make && make install
    
  11. Sit back and relax. Ruby will take some time to compile. It took about 7-9 minutes for us on EC2 micro instance
  12. After you are done with ruby install. Install rails.
    gem install rails
Install Apache and Passenger
  • Install Apache 2.
    yum install httpd
  • yum install httpd-devel
  • Install passenger.
    passenger-install-apache2-module
  • In case you get the following error,
    To install OpenSSL support for Ruby:
  • Please (re)install Ruby with OpenSSL support by downloading it fromhttp://www.ruby-lang.org/. Go to ruby source directory and install openssl extension.
Configure Passenger
Configure passenger. Depending upon your passenger version you would need to add following lines to your httpd.conf file. (Please note that these directives would come for you after the passenger install, you just need to copy and paste from there). For me the passenger version was 3.0.2:
LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.2
PassengerRuby /usr/local/bin/ruby
LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.2
PassengerRuby /usr/local/bin/ruby
Create virtual host configuration:
<VirtualHost *:80>
ServerName www.yourhost.com
DocumentRoot /somewhere/public    # <-- be sure to point to 'public'!
<Directory /somewhere/public>
AllowOverride all              # <-- relax Apache security settings
Options -MultiViews            # <-- MultiViews must be turned off
</Directory>
</VirtualHost>
Install postgres
yum install postgres
Thats it !! We are all done with our awesome rails 3 stack on Amazon EC2 !!
Please feel free to post any queries in the comments.

PDFsam Reviewed – open source utilities for PDF manipulation

PDFsam (PDF split and merge) is an open source tool (under GPL license) designed to allow the alteration of Acrobat documents (*.pdf files) through several different utilities.  PDFsam is available in two versions:
  • PDFsam basic
  • PDFsam enhanced
PDFsam is an open source tool, but its enhanced version is available for a small donation of 1 ‚ā¨. PDFsam enhanced version has extended functionality like encrypting and decrypting the pdf files (with encryption standards like RC40 bits, RC128 bits, AES128 bits) and setting permissions on them. Not only this, one can add metadata (author, title, subject and keywords) to the document in the¬†enhanced¬†version. Why PDFSAM: PDFsam incorporates a number of utilities including:
  • Splitting¬†a particular document,
  • Merging two documents in alternate fashion i.e reverse the flow of document known as ‘Alternate Mix’,
  • Merge several existing documents into one,¬†and
  • Visual editing and reordering of different pages from several documents into one document.
Superb performance and commitment of founders to open source gives it a huge edge to people who want to save on their IT budget by not investing in expensive tools like Adobe Acrobat. Moreover the ability to be able to run on the command line makes it suitable to be used on the backend of a web application. Who Will Use pdfsam: PDFsam functionalities can be exploited by:
  • Those who wants to¬†integrate document editing features in his/her web¬†development¬†project, because it can be well operated through command line.
  • Firm why heavily rely on document editing process.
As whole, it is not as targeted for a particular set of people, anyone can use this application and can rejoice with its outcomes. WHAT DOES IT OFFER (FEATURE SET): From first glance at the software one can easily predict that this software is meant to cater some serious PDF manipulations like Рdocument merging/extracting, Page Rotation, document splitting and visual document composing and reorder. Software provides easy to use interface for each of these actions as depicted below: Alternate Mix:

The utility allows you to mix alternate pages taken from two pdf documents in straight or reverse order into a single document. Merge/Extract  :

As the name suggest here we can merge two or more documents together into one document. A great utility if a report has several aspects and different people cover different aspects of a report. Rotate :

The utility allows you to rotate or orient the document as desired. Split :

Split option splits the document into pages or can even split the pages (either all or even or odd too). Visual Document Composer or reorder :

This is the best part of PDFsam where one can visually orient his/her document or can commit changes in it without have to predict what the results will be likely, as it works as WYSIWYG. How much does it cost: PDFsam is open source and free above which makes this software stand apart from others. To get an enhanced version you would need to spend a small sum of 1 euros. How to remain updated about pdfsam: You can visit the pdfsam homepage for download and to review latest versions and bug fix reports. Also the founders are pretty active on the community pages/forums of pdfsam.