I was trying to integrate Amcharts into my Rails application today. And I met the error “Cannot read property ‘construct’ of undefined” in the amstock.js file. My charts.js manifest file looks like below:

//= require_tree ../../../vendor/assets/javascripts/amcharts
//= require_tree ./charts

The folder amcharts contains the three compulsory javascript files amcharts.js, serial.js, amstock.js. Tried googling for the answer but didn’t find anyone mentioned the same issue. It was just me…

The problem is because the three javascript files have to be loaded in this strict sequence amcharts.js, serial.js, amstock.js. When I require the whole folder amcharts, the serial.js is loaded last, however amstock.js requires serial.js to be loaded first. The working manifest file looks like

//= require ../../../vendor/assets/javascripts/amcharts/amcharts.js
//= require ../../../vendor/assets/javascripts/amcharts/serial.js
//= require_tree ../../../vendor/assets/javascripts/amcharts
//= require_tree ./charts

This article explains two problems that I met when I set up redmine together with Nginx as a reverse proxy.

Redmine Can’t Upload Attachments Via Nginx Reverse Proxy

I have setup Nginx as the reverse proxy for my redmine application. The configuration for Nginx is simple.

server {
        #listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /home/redmine/current;
        index index.html index.htm;

        server_name redmine.investingnote.com;
        access_log /home/redmine/shared/log/access.log;
        error_log  /home/redmine/shared/log/error.log;

        location / {
                auth_basic "Identify yourself";
                auth_basic_user_file /home/redmine/shared/.htpasswd;
                proxy_pass http://redmineserver:9292;
        }
}

So basically the Nginx is listening at port 80 and for any requests it will forward it to the upstream redmine server, which is listening at port 9292.

Everything works fine, however, when I upload attachments in any redmine issues, the file is successfully uploaded but when I downloaded it, the file is always 15KB and the content is always [Object Object].

The reason for this turns out to be a bug with Redmine when you are using Safari browser. http://www.redmine.org/issues/13932. It seems there is no fix found so far.

Redmine Can’t Upload Large Files Via Nginx as a Reverse Proxy

I have switched to Chrome, when I upload files < 1M, everything works nicely. However, when I upload a file > 1M, the page keep loading and no error message is displayed. This is because Nginx’s default body size for a request is 1M, it is controlled by setting client_max_body_size. So when you are uploading large file, it will through 413 (Request Entity Too Large) error, but it seems chrome doesn’t know how to handle this error nicely, that’s why I didn’t see an error message.

This fix is modify the configuration file for Nginx to change client_max_body_size to a greater limit.

server {
        #listen   80; ## listen for ipv4; this line is default and implied
        #listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /home/redmine/current;
        index index.html index.htm;

        server_name redmine.investingnote.com;
        access_log /home/redmine/shared/log/access.log;
        error_log  /home/redmine/shared/log/error.log;

        client_max_body_size 5M;

        location / {
                auth_basic "Identify yourself";
                auth_basic_user_file /home/redmine/shared/.htpasswd;
                proxy_pass http://redmineserver:9292;
        }
}
Instant Nokogiri

Instant Nokogiri

I received an invitation from Packt Publishing to review the newly released book “Instant Nokogiri” recently. Just to be clear, below are purely my own personal opinions and no commercial arrangements are involved.

 

As a developer, you probably have experienced having the need to scrap some data from other websites. This is where Nokogiri would be of great help. Nokogiri is also famous for the easy parsing of XML documents.

 

As a ruby programmer, when you find yourself in such above situations, you should consider using Nokogiri.  Nokogiri is the popular open source Ruby gem for HTML and XML parsing. I used to read the documentation of Nokogiri to understand how to use it. With the release of the book “Instant Nokogiri”, this book will be of great help to you if you want to master Nokogiri.

 

The key parts about the books are listed below:

1.  Installation

This part provides very detailed steps on how to set up Nokogiri. Even  you just get to know Ruby, you should find the steps are quite detailed and easy to understand.

2. Quick start

This part guides you through the creation of your first Nokogiri app.

3. Top 13 features you need to know about

This part explains how to integrate the most common Nokogiri methods with your Ruby code.

 

Even as an experienced Ruby developer, you may not really know all the “Top 13 features you need to know about”. For me, I get to know the Mechanize gem that is created by the same team as Nokogiri. The gem allows you to interact with a page before scraping it. For example, you can use Mechanize to submit a form or login to a site. Combing Mechanize and Nokogiri, it is going to be a powerful tool.

 

In summary, this books write in a way that is very easy to understand and it is a good book to read instead of reading Nokogiri document if you need to user Nokogiri. However, if you are experienced Ruby developer and are already using Nokogiri intensively, this book may not be for you. You can get a copy of the book from packtpub if it interests you.

 

I received a request from a friend on how to install and set up phpbb. At the same time, he wants each registered users to have their own blogs. I can’t find a detailed tutorial on how to do it. So I think I will write one for those who wants to set up phpbb.

Step 1 Install phpbb

If your hosting provider provides you with cpanel, this tasks become easy. You can install phpbb using Softaculous. Softaculous makes the installing of software much easier.  Simply click on install after you found phpbb in Softaculous. After that you just need to set up the domain, installation path,  database name, admin user name and password for it. So it doesn’t require much configuration.

Install PHPBB

Install PHPBB

Step 2 Configuring phpbb

This is the most complicated part as there are so many configuration options with phpbb. Most of the time would be spent on finding where is the configuration and how the configuration would affect the forum.

Step 2.1 Disable New Member Post Limit

This step is optional. If you don’t make any change to it, newly registered members are within the Newly Registered Users group until they reach 3 number of posts. In another word, a newly registered members are required to post at least 3 posts and it should be approved by moderator, after that they will be moved to a normal Registered Users group. If you don’t want to approve a newly registered user’s posts then set the number to 0. Refer to below screenshot on where you can set it.

Step 2.2 Allow Users Posts without Approval

When you first install, phpbb requires posts to be approved before it is view-able by public. To disable this, refer to below screenshots on where you can set it.

Step 2.3 Install Styles

Phpbb comes with a lot of free styles to decorate your forums. You can view the list of styles at https://www.phpbb.com/styles/demo/3.0/ Select the styles that you are satistified from the top left drop down menu. After that you need to click download to download the style. After that you need to unzip the style and  FTP upload to your website’s phpbb installed directory under folder styles.

After that you go to the styles tab in Admin Control Panel, you should see the uninstalled styles. Click install to install it. You can set the installed style as default from there.

Step 2.4 Install AutoMOD

AutoMod is a script for making your life easier when installing phpbb modifications(modules). I consider this as a must have tools. You need this to install the user blog modules later.

Go to https://www.phpbb.com/mods/automod/ and download the automod. If you unzip it and you will find install_mod.xml installation guildlines inside. Basically it asks you to copy all folders under /root to your website server under the phpbb installed directory. So copy all of them and overwrite if necessary.

Point your browser to the page http://yourdomain/phpbb_installdirectory/install/ You should see below

Go ahead and install it and make sure in the end you see every modifications are success. After the successful installation, delete the install folder you just uploaded to the server. Now when you go to Admin Control Panel, you should see the AUTOMOD tab.

Step 2.5 Install User Blog Module

User blog module is a phpbb module that allows registered users to have their own blogs. To read more about it, go to https://www.phpbb.com/customise/db/mod/user_blog_mod/. To install the module, you need to goes to the page and download it. Once you finished downloading, goes to AUTOMOD tab in Admin CP.

Upload the zip files you just downloaded. After that when you go back to AUTOMOD tab, you will see below.

 

Not that this module is for phpbb 3.0.7. So it might not be compatible with your phpbb version you are installing. My phpbb version is 3.0.12 and I have tested it out they are compatible. So I ignore the warning and proceed to install.

After you install the module, you need to run the database migration (Sorry i am a Rails guy=)) by point your browser to http://domain/phpbb_installationdirectory/blog. If you happen to see warning “Information: This version of UMIL is outdated.”, then you need to update your UMIL version. If you can recall, UMIL is one of the folders you uploaded to server when you install AUTOMOD. Some introduction to UMIL from phpbb website. “UMIL (Unified MOD Install Library) was built by EXreaction under the supervision of the MOD Team. This utility enables MOD Authors to create simple installation scripts for their MOD that allow the user to install, update, or uninstall a Modification quickly. Released under the GNU GPL license, UMIL is included within MOD Packages to make MOD Installations simple and automatic.”

To update your UMIL, go to https://www.phpbb.com/mods/umil/ and download it. Unzip it and upload to the root folder where you installed phpbb. You need to overwrite existing umil folder. Now go back to http://domain/phpbb_installationdirectory/blog, you should be able to install without any warning.

This article explains how to set up swap on ubuntu.

 

I rent a VPS(Virtual Private Server) to power a website recently. However, the memory on the server only have 500M and my Puma ruby server process got killed sometimes. Checking the /var/log/syslog shows below error message:

Out of memory: Kill process 5458 (ruby)

Out of Memory(OOM) is a state of computer operation (often undesired) where no additional memory can be allocated for use by programs or the operating system. Such a system will be unable to load any additional programs and since many programs may load additional data into memory during execution, these will cease to function correctly. This occurs because all available memory, including disk swap space has been allocated. – wikipedia


I didn’t set up the swap on the VPS and thus the Puma process which uses up most memory got killed by linux kernel. So now let’s try to set up the swap.

Checking If Swap Has Been Set up

You can check if your server has swap set up using either top command or swapon command.

top

If it shows “Swap: 0 total, 0 used”, it means you have not set up swap on the server.

swapon -s

If it shows empty list, it is telling you that you don’t have swap files enabled.

Filename   Type  Size  Used	Priority
Setup Swap

Below are the steps to enable Swap file in your ubuntu, which should apply to other Linux system as well. Note that all commands require you to run under root user.

Step 1 Create swap file
dd Unix utility program reads octet streams from a source to a destination, possibly performing data conversions in the process.

To create a 512MB swap file you can use below command, which means read from /dev/zero and write storage file to /swapfile. It reads and writes up to 1024 Bytes at a time and copy only 524288 input blocks. The total input blocks are calculated by 512MB x 1024.

dd if=/dev/zero of=/swapfile bs=1024 count=524288 

Step 2 Create linux swap area

mkswap /swapfile

Step 3 Activate swap file

swapon /swapfile

Step 4 Make swap file permanent
The above set up will be gone after the system reboot. To make the swap file permanent, open the fstab file.

vim /etc/fstab

Append the files with below setting.

/swapfile       none    swap    sw      0       0 

Step 5 Adjust swappiness
This step is very subjective.

Swappiness is a property of the Linux kernel that changes the balance between swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can be set to values between 0 and 100 inclusive. A low value means the kernel will try to avoid swapping as much as possible where a higher value instead will make the kernel aggressively try to use swap space. The default value is 60, and for most desktop systems, setting it to 100 may affect the overall performance, whereas setting it lower (even 0) may improve interactivity (by decreasing response latency.) – Wiki

For me, I only want the system to use Swap to avoid OOM conditions. So I have to adjust the swappiness to 0. Note that the default contents in the swappiness file is already set to 60.

echo 0 > /proc/sys/vm/swappiness

Step 6 Setting swapfile permission
Swap files are critical files that should not set to be everyone readable. So let’s change the permission to only read and write by root. Before that you may want to make sure the file owner is root as well.

chown root:root /swapfile 
chmod 0600 /swapfile