In the course of studying Node.js, I created many small projects. Later I needed to deploy some of them on the server in order to be able to develop them further. The existing Appfog and Heroku hostings have a number of serious limitations on free accounts. On Appfog, you can run no more than 2 applications at a time, and free registration with them is already disabled. On Heroku, you can create only 5 applications (without a database) without confirming your account with a bank card binding. Paid accounts are very expensive for me (I do not make money on these small projects): $ 20 dollars for Appfog, and heroku is even more expensive. Colleagues recommended that you try hosting on Digital Ocean - a virtual server for $ 5 dollars per month, where you can easily set up your own hosting node.js projects.
How to organize such a hosting, and will be discussed in this article.
If during the course of the management you have any problems, look for their solution at the end of the article.
Create a VPS
1. Registration at DigitalOcean
First of all, we are registering on the
DigitalOcean website. Here we will buy a VPS - virtual server. After successful registration and login to your admin panel, click on the big green "Create" button and we will be offered to activate your account, making $ 5 dollars. Below on the page there is a place to indicate a happy coupon for $ 10, enter
OMGSSD10
or search for a fresh coupon. Since my bank card didn’t come up to the hoster, I paid with a paper and I had $ 15 in my account. This should be enough for 3 months.
')
2. Create an SSH key
Access to remote servers is usually done via ssh. This is a technology of secure connections. You can access our future VPS with a password or with an SSH key. I strongly recommend the second option, you do not have to enter the password all the time. Also, the SSH key will still need to be specified when creating our new "droplet" (DigitalOcean calls the virtual server "droplet").
Run the following two commands in the terminal to generate an ssh key:
$ cd ~/.ssh $ ssh-keygen -t rsa -C "email@example.com"
And here we are asked to enter the key file name. I recommend using something like
id_rsa_digitalocean_dropletname
, where the droplet name is either the domain name to which we will bind our server, or just some meaningful name. Paraphrase can be left empty, and so come down.
After that, two files will be created: ssh key and public key. Copy the public key to the clipboard in a fashionable way:
$ cat ~/.ssh/id_rsa_digitalocean_dropletname.pub | pbcopy
Now let's go to the admin panel in digitaloceans in the SSH-keys section and create a new key there by pasting it from the clipboard.
3. Create a droplet
Now it's time to buy a virtual server. Click on the big green button “Create” and enter the following information:
- As the hostname, specify the domain name or something meaningful, this name does not matter
- Choose the smallest size, then it can be increased from the terminal
- Region any, I chose Amsterdam
- Select the image on the Applications tab, with the title "Dokku v0.2.3 on Ubuntu ..." or similar
- Specify the SSH key that we created in the previous step.
Click "Create Droplet" and wait 2-10 minutes. As a result, we will see the droplet control panel, and its IP address will be visible there.
The selected image in addition to the operating system Ubuntu 14.04 contains the most important thing: Dokku. This is a complete hosting system for node.js projects, very similar to Heroku. Using the image with the pre-installed Dokku will save us from having to install and configure the system for hosting Node.js.
4. Configure Droplet and DNS
Now you need to configure the newly created server. Go to the browser at the IP address of the droplet and you will see the settings screen. If there is no such page, see what to do at the end of the article.

Here you should make sure that the "Public Key" field is filled. If not, enter your public key from the newly generated key. However, practice has shown that it is better to remove such a droplet altogether and create a new one by correctly specifying the SSH key.
If you do not have a domain name, then leave everything as it is, click "Finish setup" and go to the next item. Your application will be available like this: "19.19.197.19:46667", by IP address and port number. If you do not like it - immediately buy a domain name. In any case, it will be possible to set up a domain name later, which I will discuss at the end.
If you specify a domain name, you will be able to configure access to applications via subdomains. For example, if your domain name is
mydomain.com
, then your application will be available at
app.mydomain.com
. It is also possible to link the application to the main domain, but I will not tell you about this in this article.
Enter your domain name in the Hostname field and tick the box "Use virtualhost naming for apps". Now go to the control panel of your domain name and enter two new settings like this:
apps.mydomain.com. IN A [Droplet IP address]
* .apps.mydomain.com. IN A [Droplet IP address]
Or so:
mydomain.com IN A [Droplet IP address]
* .mydomain.com. IN A [Droplet IP address]
For the exact syntax, check the domain name or the domain provider in your admin panel.
Now return to the droplet settings page and click "Finish setup".
5. Configure SSH Usage
Let's try to connect with our new server. To access the droplet via an SSH key, you need another small action. Open a terminal and browse to the folder with ssh-keys:
$ cd ~/.ssh/
If you do not have the "config" file in this folder (check -
$ ls
), then you need to create it:
$ touch config
Now open the config (
$ open config
) and write the following data into it:
Host YOUR.DROPLET.IP.ADDRESS
IdentityFile ~ / .ssh / id_rsa_digitalocean_dropletname
Where in the first line is the IP address or domain name, on the second line is the name of the created ssh-key for this droplet.
Check the connection:
$ ssh root@YOUR.DROPLET.IP.ADDRESS
Where after the "@" you must specify either the domain name or IP.
If you missed the creation of an SSH key, you can log in using the password that came to your email. To do this, run the same command:
$ ssh root@YOUR.DROPLET.IP.ADDRESS
and on the question about the continuation of the connection write "yes". After that enter the password.
If you saw the welcome screen, then everything is fine. Close the connection with the
exit
and move on to the next item.
Deploy application
The server is ready, now it's the turn to deploy the test application and check its operation.
1. Prepare the project
Take a simple project like
Hello, world , without mongodb and so on. Check that the project has the most important thing: "package.json", "Procfile" and some "app.js". Package.json should look something like this:
{ "name": "dokku-demo-application", "version": "1.0.0", "engines": { "node": ">=0.10.*", "npm": ">=1.3" }, "dependencies": { "express": "~3.0" } }
The content of the
package.json
file is important, since according to it, dokku will install modules for the application. And the
Procfile
should look like this:
web: node server.js
Initialize the repository in the project folder (if it was not there), add all the files and commit:
$ git init && git add -A && git commit -m "Initial commit"
Now our project is ready for deployment on the server.
2. Fill and run the project
Deploy the application using
git push
. And for this we add a link to our droplet's remote repository:
$ git remote add dokku dokku@mydomain.com:application_name
If we had a domain name for the droplet, then we write it after "dokku @", otherwise we specify IP. After the colon, we specify the application name. This indicator will be used inside dokku.
Now we call the
push
command:
$ git push dokku master
And it starts downloading the application, installing modules. At the end, the screen will show the url, on which you can run the project. This will be a link like
192.88.67.168 : 46567 or app1.mydomain.com, depending on the dokku settings.
In theory, the project should be launched after that. But if not, then go in ssh in our droplet and execute two commands
$ cd /home/dokku/YOUR-APP-NAME $ dokku run YOUR-APP-NAME node app.js
We launch the link received earlier in the browser and rejoice!
Problems and their elimination
I myself have dealt with setting up such a hosting for more than a day, and filled several cones. And so that you can save your time, here are some possible problems and ways to solve them.
1. My bank card was not accepted for payment
They are, yes. Even they don't like Visa Classic. Pay by PayPal, it is not difficult.
2. Created a droplet, but there is no dokku configuration page by IP address
So something went wrong with DigitalOcean. You need to go to the droplet control page, and on the Destroy / Rebuild tab:
- specify image with Dokku
- Click the "Rebuild from Image" button.
Then, after rebuilding, the dokku configuration page should be visible by IP address. If not, ask DigitalOcean for help.
3. I can not access the droplet either by password or by SSH
- Probably, you somehow missed the dokku configuration step, or dokky was not created correctly.
- Check if the dokku settings page is available on the droplet's IP address, then go back to step 4 of creating a VPS.
- If there is no settings page, then try resetting the password and retry access via the terminal. If, even in this case, password access is unsuccessful, then reinstall the droplet as described in the previous paragraph.
4. I do not know the password from the droplet
If you do not have a password, go to the droplet control page, tab "Access" and reset the password. Then the new password will come to the mail.
5. The project does not start at the specified link.
If the dokku settings are correct, this situation is possible if the application started up with an error and was closed. Go by ssh to the droplet and check the logs:
$ dokku logs <app_name>
Perhaps the error will be clear. Or did you forget to start your project after push'a:
$ cd /home/dokku/YOUR-APP-NAME $ dokku run YOUR-APP-NAME node app.js
6. I missed the creation of SSH, and now I want to make an authorization using an SSH key
I tried to create a new droplet without specifying an ssh key on the droplet creation page, but on the dokku settings page, I am still asked to enter the public key. After that, I took some actions, but still I didn’t log in using the ssh key, only the password. So I do not know how best to act in this situation. Perhaps the error occurred when the ssh key copy command was copied to the droplet. Google As a last resort, create a new droplet from the very beginning, according to your mind, as in this guide.
An example of a search .
7. How to add a domain name to an existing droplet
This is not at all difficult, just a few steps. In the DNS settings of your domain name, specify the settings:
apps.mydomain.com. IN A [Droplet IP address]
* .apps.mydomain.com. IN A [Droplet IP address]
Go to the dokku folder in the droplet and open the HOSTNAME for editing:
$ cd /home/dokku/ $ vim HOSTNAME
Press the "i" key and write the domain name in the "HOSTNAME" file instead of the IP address; then press
Esc
, buttons
:wq
and
Enter
. Hooray! We have just successfully used the legendary Vim!
To run projects on subdomains, you need to create a
VHOST
file:
$ touch VHOST
And write the domain name in the file in the same way as in the previous paragraph. Otherwise projects will be available through the port. Now you need to perezalit project, and before that remove it from dokku:
$ dokku delete app_name
Delete the previous remote in the repository with the IP address in the title:
$ git remote remove dokku
And add a new remote under the old name:
$ git remote add dokku dokku@apps.mydomain.com:app_name
Configuring the SSH key for a new hosting, adding new lines to
~/.ssh/config
:
Host YOUR.DROPLET.IP.ADDRESS
IdentityFile ~ / .ssh / id_rsa_digitalocean_dropletname
Feel free to push the project:
$ git push dokku master
Now projects will be available on subdomains.
Thank you all for your attention, have a great weekend!