Install node.js on VPS

    In this article I will tell you what problems will have to be solved in order to get VPS with a really working node.js service. These are all elementary things, but maybe someone else will come in handy.

    Node installation


    Problem: the repository may not have a package for the required version of node. Alternatively, you can have two projects requiring different versions of node.
    Solution: on development machines in such cases, use nvm. It can also be easily used on a combat server, you just have to write a special script to start your server.

    Access to port 80


    Problem: the application must have superuser privileges in order to read port 80 (for more details see CAP_NET_BIND_SERVICE, man capabilities ). But starting a node with these rights is considered unsafe.
    Solutions:
    • use a proxy server such as Nginx or HAProxy .
    • redirect requests from port 80 to any one with a large number using iptables. It is done practically by one command:iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000
    • start with the same superuser rights, but immediately after connecting to the socket, the process will programmatically lower its privileges. More details here .


    Load balancing


    Problem: node is single-threaded and cannot efficiently use multi-core processors.
    Solutions:


    Use node directly or through Nginx?


    The issue has been discussed in many places , but there is no consensus. On the one hand, nginx is a reliable, proven solution for years; on the other hand, node itself can do the same.

    As a rule, one of those options is offered:
    • node deals with everything right up to the distribution of static files.
    • node stands for haproxy.
    • node stands behind Nginx and is used only for page generation, everything else (static distribution, balancing, https work) is done by Nginx


    Restarting a Fallen Process


    Problem: how to resume the server if it suddenly crashes?
    Solutions:


    Service update with git


    Described here

    Also popular now: