Monkey on a Keyboard

my online scrapbook of scrambled thoughts

I've been relying on Apache and PHP for a very long time, and they have served me well. However, they do have disadvantages, and I feel that I need to keep up with new technologies in order to further my knowledge. Apache is built with old paradigms to handle concurrency, it uses one thread per connection. Thread is expensive, especially for a constrained environment like a web server. So, when there's another platform which offer high concurency with a fixed size of thread pool, I'm sold.

Enter NGINX (read: engine-x). It uses a limited size of thread pool, so it will never create unlimited number of threads. It uses a non-blocking model instead of the traditional blocking model which Apache uses (which is why they need one thread per connection in the first place). The only disadvantage it has is the lack of support compared to Apache. To use php on NGINX, you need to go the extra mile. But PHP is also reliant on old paradigms. It's still based on a file system structure, it still use one thread per execution of a script (which negates the benefit of NGINX), and it's painfuly slow. So, now what?

This is where Node.js comes in. Node.js is basically javascript which you can run on the server. Imagine that. You can have practically the same language for both client and server. And it also use non-blocking model, just as an added bonus. Granted, it only use a single thread to run everything, but it still performs better than PHP which uses multi-thread, and you still can run multiple Node.js worker processes if you really need to scale further. Mind-blown.

So, yes, I just recreated my blog engine from scratch using Node.js. And it took me significantly less time than if I were to recreate this blog using PHP from scratch. Node.js is that easy to use, if you consider javascript as an easy language. I certainly am.

That's all. As always, happy blogging!

   Posted in CMS        Yohanes Mario Chandra        0 Comments

Since forever I've been looking for a way to easily print my blog content from php inside a html text input field or textarea. The results has always been awful or awkward at best. For those who have been making admin pages their whole life would know this. Printing an escaped HTML character is easy, but printing it inside a textarea is another story. The root cause of this is because <input type='text' /> and <textarea></textarea> have different ways of displaying content.

And then I got an epiphany, why don't I use JSON to encode the data, and then use javascript to fill those empty text fields and textarea? And that's what I did. This way, I avoid printing directly into the textarea altogether. The way to do this is easy, I will have two variables, namely $json which contains the JSON encoded string, and $result which contains the string which will be echoed.

<?php

    $json = json_encode(array(
        "textareaId" => $textareaContent
    ));

    $result = "<script type='text/javascript'>
        var preloadedData = ".$json.";

        $('#textareaId').val(preloadedData.textareaId);
    </script>";

Using this method, I can just use jQuery's .val() method to fill any text input, regardless of it's type.

That's all. Hope this helps. Happy blogging!

   Posted in CMS        Yohanes Mario Chandra        0 Comments

Let's encrypt is a free, automated, and open Certification Authority.

That's what's said on the main page of their website. I've been looking into this thing for so long, but just now that I have learned that they provide a way to manually generate certificates for shared web hosting with no root shell access like the one I use. However, upon further investigation, and as I try to generate that certificate myself, I found that their documentation is lacking in terms of clarity, especially for my specific use case. In case you didn't notice, I have successfuly generated the certificate, but not without any hurdles. That's what I'm going to share here.

The steps are as follows (assuming you use Ubuntu or any equivalent distributions):

  1. Download certbot by using the command: wget https://dl.eff.org/certbot-auto.
  2. Mark it as executable: chmod a+x ./certbot-auto
  3. Begin to obtain certificate:./certbot-auto certonly --manual -d www.example.com -d example.com
  4. You will be asked to create some files with certain content in your public_html. Do each of that, and then you will get your certificate.
  5. Your certificate will be located at /etc/letsencrypt/live/example.com/fullchain.pem,
  6. and your private-key will be located at /etc/letsencrypt/live/example.com/privkey.pem.
  7. Copy them to your web hosting, install them, and you're good to go.

That's all. I hope this helps. Cheers.