I love Digital ocean and wanted to do a short “hello world” in setting up a server. I use it to host all my sites and various other things like telegram bots etc.

As developer on a tight budget, I think its great. I was able to replace my shared hosting account with a DigitialOcean account reducing my hosting costs significantly.

Not only is DigitalOcean cheaper, but it is more flexible as well. With shared hosting I was limited to HTML and PHP sites and to a maximum of 3. With DigitalOcean I can run whatever applications I want.

DigitalOcean is a “Cloud Hosting” service, basically they deal in VPS’ (Virtual Private Servers). Prices start at $5 a month. As they are just Unix servers, they are very flexible you can configure them however you want. They can be configured to host multiple sites on the one server.

The whole thing should only take about 30 mins.

Setting up the server

First thing you will need is a DigitalOcean account, if you create one using this link you will get $10 credit. Create a new droplet: we’ll be using a Ubuntu 14.04 x64 image. The smallest size server should be fine for most sites. Select which ever datacenter you like. You will be emailed details for connecting to the server. Use an ssh client to connect (e.g putty). Once connected, install Nginx using this guide. Nginx is the HTTP server used to host the websites.

Once you have completed the above steps you should be able to connect to hit the server by pasting the IP address from the DigitalOcean email into your web browser. You should see the “Welcome to Nginx” page.

You will need configure the DNS of your web addresses to point to the IP address of the server.

Configuring Nginx

Nginx is what allows you to deal with multiple sites on the one IP address. You configure Nginx to redirect requests for certain domain names to different folders on your server.

You will need to get your sites code onto the server, either use git to download your project or upload the code to the server using winscp.

To configure a new site, on your server if you navigate to the Nginx sites available folder:

cd /etc/nginx/sites-available/

Create a new file for site. Use one of the following snippets to base your config file on.

Basic HTML site

server { listen 80;

index index.html index.htm;



server_name websitename.com



location / {

try_files $uri $uri/ /index.html;

}

} root /path/to/website/;index index.html index.htm;server_name websitename.com www.websitename.com alternative.com;location / {try_files $uri $uri/ /index.html;

PHP site

You will need to run some extra steps before you can configure PHP sites. Follow steps 4 and 5 of this post.

server { listen 80;

index index.php index.html index.htm;



server_name websitename.com



location / {

try_files $uri $uri/ /index.html;

} root /path/to/website/;index index.php index.html index.htm;server_name websitename.com www.websitename.com alternative.com;location / {try_files $uri $uri/ /index.html; location ~ \.php$ {

try_files $uri =404;

fastcgi_pass unix:/var/run/php5-fpm.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params; }

}

Proxy Pass (e.g. Node.Js)

In the below example we are forwarding to an application running on port 3000

server {



server_name websitename.com

listen 80;server_name websitename.com www.websitename.com alternative.com; location / {

proxy_pass http://127.0.0.1:3000;

proxy_http_version 1.1;

proxy_set_header Upgrade $http_upgrade;

proxy_set_header Connection 'upgrade';

proxy_set_header Host $host;

proxy_cache_bypass $http_upgrade; }

}

After creating the file, you will need to also add a copy to sites-enabled folder. The best way to do this is to use symbolic links. Move to the sites-enabled folder.

cd /etc/nginx/sites-enabled ln -s ../sites-configured/fileName ./fileName

After this you need to restart Nginx

service nginx restart

Your site should now be fully configured and ready to go. Please comment if you have any issues.