This is a collection of code examples and step-by-step instructions on setting up a DigitalOcean droplet for node.js projects.
- Get the GitHub Student Developer Pack
- Register a DigitalOcean account
- Use the code from Github Student Developer Pack in DigitalOCean Billing Settings Promo Code section to get $50 credits
- Click Create Droplet
- Choose an image: select NodeJS 6.9.5 on 16.04
- Choose the smallest size $5/mo
- Choose a datacenter region
- Choose a hostage name: give it any name you want
- Click Create
- After a while, you should have received an email of your first time log in info.
- In the email from DigitalOcean, you should have your credentials for the new droplet, including IP address, username "root" and password.
- Using Terminal on Mac to connect to your droplet. You need to use the password in the email for the first time login, and then you can set up your own password.
- In Terminal, type:
ssh root@YOUR_IP_ADDRESS
- Copy and paste the password from the email, and then set your own password for user "root" as it prompts.
- For security, it's better to create a user separate from root. Give your self an username, then in Terminal, type:
adduser YOUR_USERNAME
- To give the user administrator privileges, you need to set the user as a sudo user:
usermod -aG sudo YOUR_USERNAME
- Then you are all set. Switch to the user you just set up, then you could run projects on it:
su - YOUR_USERNAME
- Download Cyberduck
- In Cyberduck, click open connection
- Select SFTP(SSH File Transfer Protocol)
- Type your IP address in Sever box, your username in Username box, and password:
- Click connect
- Drag your project into Cyberduck. In this case, we will use Collaborative Drawing as an example.
- In Terminal, make sure that you are logged in as the user you created, then typ:
USERNAME@DROPLET_NAME:~$ ls
You should see the project you just dragged into Cyberduck 2. Change directory to the project:
USERNAME@DROPLET_NAME:~$ cd YOUR_PROJECT_FOLDER_NAME
In this case:
USERNAME@DROPLET_NAME:~$ cd collaborative-drawing
- Install node modules:
USERNAME@DROPLET_NAME/collaborative-drawing:~$ npm install
- Run the project:
USERNAME@DROPLET_NAME/collaborative-drawing:~$ node server.js
- In browser, go to [YOUR_IP_ADDRESS: YOUR_PORT_NUMBER](YOUR_IP_ADDRESS: YOUR_PORT_NUMBER) In this case: [YOUR_IP_ADDRESS: 8080](YOUR_IP_ADDRESS: 8080)
- In Cyberduck, open server.js file with a text editor, change port 8080 to 80(default Hypertext Transfer Protocol(HTTP))
- In Terminal, Type:
USERNAME@DROPLET_NAME/collaborative-drawing:~$ sudo node server.js
- In browser, go to YOUR_IP_ADDRESS
- You might notice that now if you quit Terminal, the project would stop running. So we need to set it to run foever.
- Ctrl + C to stop the server
- Installing forever module:
USERNAME@DROPLET_NAME/collaborative-drawing:~$ sudo npm install -g forever
- Run the server forever:
USERNAME@DROPLET_NAME/collaborative-drawing:~$ sudo forever start server.js
- All set! Now if you quit Terminal, the project would continue running. For more details about forever.js, please check here