In this tutorial I'll go over how to setup a VPS instance (aka droplet) on DigitalOcean, transfer your code to it using
git and FileZilla, and have the code run either all the time, and link to resources that explain how to run it periodically using
cron. Links in the first section also have $10 signup bonus which you can use to get 2 months free VPS usage.
This tutorial is aimed at total beginners and will go over every step of the process with all the necessary code and screenshots being included and will not focus on the more advanced stuff. Because of the target audience I will not focus on some of the things that you should do when setting up a server such as using an ssh key, creating a new non-root user, fail2ban etc. but articles with more info will be linked at the bottom which you should read.
Every section will also have a short summary of the content - mostly so you can easily create notes or go over the steps again if necessary without reading the whole thing.
The tutorial consists of five sections:
Creating a droplet (DigitalOcean calls their VPS instances "droplets").
Accessing your droplet via SSH.
Transferring files using FileZilla (SFTP), and getting code from remote sources.
screento create additional shell windows and running your code in a new
Links for further reading
DigitalOcean account and some funds on it, if you don't already have an account you can use this link to get $10 free signup bonus which is enough to run one instance for 2 months 24/7, DO also charges by the hour so you can destroy the droplet if you don't plan on using it a lot and you'll keep your money. Note: Adding a valid payment method to your account is required to activate the bonus - you will not be charged anything if you add your CC.
Basic familiarity with command line and python.
I'll use Linux for screenshots, terminal for ssh access and running the commands, OS X should have ssh support in its terminal too. DigitalOcean also offers web version of terminal which should work on all platforms, you can access it by clicking on your droplet name on homepage and then clicking "Access" tab and choosing "Console Access". For windows there's also PuTTY that you can install and use.
1. Creating a droplet
Summary: Login to https://cloud.digitalocean.com, create a new droplet using Ubuntu 14.04 x64 image without using any SSH keys.
Once you've created your DigitalOcean account and have some funds in it (if you haven't check out the requirements section above) click on Create Droplet button and enter droplet name and change default size to $5/mo. or larger depending on your needs, I will go with the cheapest one.
Next there are region options, those don't really matter for now so just go with the default one. The default one being NYC3 in my case. Note that some of the additional available settings cost extra money, like enabling backups.
Next on the page is the OS/Image options and SSH key options. For this tutorial I'll use Ubuntu 14.04 x64, the bit version doesn't matter and all of those distros probably come with all the necessary things already installed so you can pick whatever you want but if you're a beginner, and especially if you've never worked with Linux I'd recommend Ubuntu because it's the easiest one to find docs and support for from my experience.
SSH keys are a good idea, and will make changing the default password in the next step unnecessary, but for now I'll create a droplet without any extra SSH keys and will link to a tutorial how to add ones in the final section.
Once you've got that click on "Create Droplet" button which should show this screen:
After it's created check your email for droplet password, it should be emailed to you pretty much instantly.
Your new droplet will also show up on https://cloud.digitalocean.com/droplets homepage with the basic information like this:
2. Accessing your droplet via SSH
Summary: Use terminal and
[email protected] or
PuTTY on windows to establish a
ssh connection to your newly created droplet.
22.214.171.124 (or any other IP in this article) with your IP where you see IP address used in this article.
On linux you just open your terminal and enter
ssh [email protected] and enter the password given to you via email. You will also be forced to change the password after you've successfully logged in - you can use any password that you want. (Depending on the OS you will most likely not get any feedback while typing or copy/pasting your password, like
* showing up instead of letters - but it is there)
Here's the screenshot of my terminal window after logging in and changing password
For windows using PuTTY you simply need to enter
[email protected] in the host name field, port as 22 (if not already set) and connection as
After that you'll see the same window/console as you would on Linux - prompting you to enter password.
You can save your configuration, passwords etc. if you want inside PuTTY, but for now there is no need to do so.
3. Transferring your files to the server
Summary: Use FileZilla/WinSCP and SFTP to transfer files using GUI from your machine to your droplet. Use
root as username, your droplet IP as host address and your new password you entered in previous step as password. For remote files you can use
git or some other VCS.
Since FileZilla is available both on Windows, Linux, and OS X I'm going to use that and SFTP to transfer the files. You can download FileZilla here or if you're on Linux you can install it from distro repos - on Ubuntu that would be
apt-get install filezilla.
As far as connecting using FileZilla goes it's pretty simple:
In the main window toolbar there are fields to enter
port and a button to do a "Quickconnect". For
host you simply enter the IP address of the droplet you created (visible when you login to your DO account and also given to you in email),
password is the new password you entered in step 1 - not the one you got in your email, and
22. Once you've entered all that press "Quickconnect" button and you'll see some output below, it should look like this:
If you get an error make sure all the necessary information is correct. Availability of this may (I am not sure) also vary with different distros.
If you get "Unknown host key" it is safe to click OK and continue with the connection and also you can add the host key to the cache to avoid prompts like this in the future.
Once the connection has been established in the panels below you can see your local files and folders on the left panels and the remote files and folders on the right.
Transferring files is as simple as dragging file from the left file panel to the right one. You can also create, remove and modify folders using FileZilla if necessary.
If you want to use someone else’s code, or your own, that's using git VCS - like something on GitHub fetching the code is simply a matter of running
git clone <git url>. In case
git isn't installed on your distro by default you can do so, on debian based OS, by executing
apt-get install git.
If you want to get source from some other source doesn't use VCS you can use
wget to download the file directly instead of downloading it on your machine and then transferring it using SFTP or something else.
This tutorial will not focus a lot on
git, but you should definitely look into it and learn how to use it. I will link some resources in the final part.
4. Using 'screen'
screen -S name create a new windows session,
Ctrl-a d to detach and
screen -r name to re-attach it when needed. Run your code inside new window to prevent it from being shut down when you close the connection.
Note: All commands in this section are executed on your droplet, none of the commands are ran locally on your machine - so if you're not logged in to your droplet do so before continuing.
screen is a pretty powerful, open source, tool for multiplexing several virtual console windows. You can see complete docs for it here the only options I'll use in this tutorial are
-S [name] for creating a new screen session named
-ls for listing active sessions, and
-r [name] to attach to a running session named
The reason for using
screen and not just logging in and running
python my_code.py is the fact that your process will stop being executed once you terminate connection to your droplet, which kind of defeats the purpose of having a remote droplet for running code 24/7 in the first place.
screen there are also other tools that will do just fine for this purpose, however
screen unlike those other tools is much more powerful IMO and generally good to know. There are also some other suggestions in this SO thread if you're interested.
If you want to run your script periodically then
screen is probably the wrong tool for the job, so instead of keeping the code running for a whole week(for example) just to run once you should look into
cron and how to schedule a cron job. Check out the next section with additional links and tutorials if that's the case.
The code I used in this example is:
#!/usr/bin/env python import datetime import time def main(): print "System time:" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M") if __name__ == '__main__': while True: main() time.sleep(5)
You can try running that without screen, then logging out, and then logging in to see what I'm talking about - the process will be stopped and there will be no traces of it once you log back in.
Creating a new screen session
First we need to create a new
screen session, this is done by simpy running
scren -S myScreen (
myScreen being the name, you can use whatever you want) after that you don't get any special notification/message from
screen but you should see a clear console with only
[email protected]:~#, this means you're in
You can now type
python myCode.py and your code will execute just like you'd expect it. If you want to send it
KeyboardInterrupt signal just press
CTRL+C like you would on your own computer. Even if you closed your terminal now your code wouldn't quit.
Detaching and re-attaching session
To properly detach from the screen session hold
CTRL then press
a and then press
d. All commands for
screen are pre-faced with
CTRL-a and then command letter.
To get a list of all active screen sessions type
screen -ls which will show you a list of sessions like this
To access your screen session again, either after logging out and then back in or after detaching, type
screen -r yourScreenName in my example it would be
screen -r myScreen. Once you do that you should see all the output from your script that was printed while you were logged out, for the example code above it will look something like this:
5. Further reading
Cron: If you want to schedule your script to run, for example, once per week, every 72 hours, once per month etc. then look into
Securing your droplet
As mentioned in the first section the practices used in this tutorial work, but you should secure your production servers better if possible.
DigitalOcean has some pretty good tutorials on the subject:
Using SSH keys with DigitalOcean