Skip to content

Instantly share code, notes, and snippets.

@whoeverest
Last active March 2, 2016 12:18
Show Gist options
  • Save whoeverest/d19acfa68af25ee0934c to your computer and use it in GitHub Desktop.
Save whoeverest/d19acfa68af25ee0934c to your computer and use it in GitHub Desktop.
How to make a personal file upload server

Simple file sharing, with Nginx and scp

This is a tutorial that teaches you how to create your own simple file-sharing service. It works in the terminal.

Usage:

$ share ~/pictures/my-funny-kitten.png
http://files.YOURDOMAIN.com/my-funny-kitten.png

after which you can paste the URL to a friend.

What you need

  • A domain
  • A VPS with a static public IP address running Linux
  • A personal computer running Linux
  • Nginx installed on the server
  • scp and ssh on your personal computer
  • Your public SSH key added to authorized_keys on the server
  • Free space :)

Uploads directory

You'll need a directory in which the uploaded files will be stored:

mkdir /home/YOURUSER/uploaded-files

Replace YOURUSER with the actual name of your user.

Nginx configuration

Create a new configuration file /etc/nginx/sites-available/file-uploads with the following contents:

server {
    listen 80;
    server_name files.YOURDOMAIN.com;
    access_log /var/log/nginx/files.YOURDOMAIN.com.access.log;

    location / {
        root /home/YOURUSER/uploaded-files;
    }
}

After which you'll need to run service nginx restart (or whatever is the equivalent in your OS.)

If you've done everything right, point your browser to http://files.YOURDOMAIN.com/ and you should get 403 Forbidden

Copying a file

A Bash script will be responsible for copying the file - over ssh - to the server. My script looks like this:

#!/bin/sh
set -e

ID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)
NEW_FILENAME=$(basename "$1" | sed -e 's/ /-/g')
scp "$1" whoeverest@linode:/home/whoeverest/files.andrejt.com/unlisted/share/$ID-$NEW_FILENAME
URL="http://files.andrejt.com/unlisted/share/$ID-$NEW_FILENAME"
ssh whoeverest@linode "chmod a+r /home/whoeverest/files.andrejt.com/unlisted/share/$ID-$NEW_FILENAME"
echo $URL

Replace whoeverest, linode, andrejt.com and everything specific to my setup with values for your specific setup. :)

Make the script executable: chmod +x share.sh

Then run: ./share.sh ~/path/to/kitten-pic.png and you should get a link in the console:

http://files.YOURDOMAIN.com/GF4afd3-kitten-pic.png

And there you go, your personal upload server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment