Virtual Private Server Setup For Fun and Profit! (General Overview)

linux
beginner
web
servers

#1

Introduction

This article serves to showcase a few ways a Virtual Private Server (VPS) can be utilized. This is a general level overview and is intended to spark interest. For help obtaining your own VPS I suggest you drop on by the #0x00sec IRC channel.

In this post you will learn:

  • What a Virtual Private Server is.
    • How to access a VPS
    • How to create a non-root user on the VPS
  • How to host a Flask Website on your VPS
    • Install and Setup Flask (Python Web Framework)
    • Create a basic webpage
    • Serve the web page publicly
    • Configure DNS for your VPS
  • Why VPSs are awesome.

It seems like a lot because it is.

AND THAT’S OKAY! Because you’re smart and here at 0x00sec for a reason. I can tell these things about you.

Required Skills

Yes, okay, this is a general overview, but I still expect you to know some things:

  • How to use SSH (or equivalent)
  • What a Domain Name is

This example assumes you own the domain mywebsite.com.

Author Assigned Level: Wannabe

Community Assigned Level:

  • Newbie
  • Wannabe
  • Hacker
  • Wizard
  • Guru

0 voters


VPS Setup for Fun and Profit

-A General Overview of Virtual Private Servers

##Warning:
All code is based on a Linux machine running Ubuntu. If you’re using something different, you might need to use different code.

##Basics
A VPS is a your own personal computer in –The Cloud– that you can do whatever you want with. VPSs can be loaded with whatever Operating System you fancy and you can install whatever software you deem necessary. In most cases, VPSs use minimal OS installations to save space and memory. This means most of the work done on a VPS is through a terminal, so let’s check out what that’s all about.

####Connecting to VPS
To connect to your VPS you will need to know either its IP address or its Domain name. Once ascertained, you can use SSH to connect your workstation (a laptop in my case) to your VPS. Obviously you will need an internet connection so make sure you’re online. In my case, I was given a root user and password along with a domain name. To initially connect to my VPS I entered the following command.

ssh [email protected][VPS_domain_name].net

After entering your root password, you should be logged into your VPS. Now, let’s configure ourselves a non-root user to work with.

adduser --ingroup sudo myuser

This will then ask you to enter some information about the user (which you can leave blank) and then ask you to set the password. Once that’s done, we can switch user to our newly created myuser.

su myuser

Great! Now we have a non-root user with sudo privileges. Let’s make this VPS do something for use.

##Hosting a Webpage
I will be using Flask for this. Flask is a web framework written in Python. I like it because it is small, extensible, and easy to work with. Many plugins exist online along with numerous tutorials and helpful resources. If you don’t want to use Flask for your VPS you might look into LAMP servers. Anyway, onward.

####Installing Flask
We’re going to be running a lot of commands here. Check the comments (after the # symbol) for a description.

sudo apt-get install libapache2-mod-wsgi python-dev  #install mod_wsgi and python stuff
sudo a2enmod wsgi  #enable wsgi

Now lets change directory into where we will build our website.

cd /var/www  #apache default directory
sudo mkdir mywebsite  #make a folder for website
cd mywebsite  #enter folder
sudo mkdir mywebsite  #make a folder for flask application
cd mywebsite  #enter second folder

Great. We have our folders created. But, before we start building our site we need to set up a virtual environment for Flask. This let’s setup individual python invironments for each project rather than muddle up the system environment.

sudo apt-get install python-pip  #install pip
sudo pip install virtualenv  #install virtual env to system environment
sudo virtualenv -P /usr/bin/python3.4 flask  #see below

Okay. That last command is a little tricky. We call virtualenv with the -P argument to specify which version of python (3.4 here) we want to use in our new virtual environment named flask. With the environment created, all that’s left is to activate it.

source flask/bin/activate

Awesome! Now, as long as we’ve activated our virtual environment, anything installed with pip will only be installed into the flask virtual environment.

sudo pip install Flask  #install Flask to flask
vim __init__.py  #edit __init__.py file

This file, init.py, is where we will define our Flask application and create our first route. This application is purposefully minimal. For more information on Flask, see the end of this article.

/var/www/mywebsite/mywebsite/init.py:

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello, World!"
if __name__ == "__main__":
    app.run() 

##Serving the Application

With that done, we need to set up our Virtual Host for Apache. Let’s first deactive our virtual environment.

deactivate

then:

sudo vim /etc/apache2/sites-available/mywebsite.conf

Edit the file to look like this:

<VirtualHost *:80>
            ServerName mywebsite.com
            ServerAdmin [email protected]
            WSGIScriptAlias / /var/www/mywebsite/mywebsite.wsgi
            <Directory /var/www/mywebsite/mywebsite>
                    Require all granted
            </Directory>
            ErrorLog ${APACHE_LOG_DIR}/error.log
            LogLevel warn
            CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the virtual host with

sudo a2ensite mywebsite

then change to the higher directory with

cd ..  #go up a directory

Now we create the wsgi configuration file for our website.

sudo vim mywebsite.conf

/var/www/mywebsite/mywebsite.wsgi:

#!usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.bath.insert(0, "/var/www/mywebsite")

from mywebsite import app as application

now add WSGIRestrictStdout Off to the bottom of /etc/apache2/apache2.conf

So we have our site setup and ready to go. Now we should remove the default Apache website and restart the Apache service.

sudo a2dissite default
sudo apachectl restart

DNS, the Final Step

Okay. So, now we have a website built on our server and our server is ready to serve the website through Apache. All that’s left is to link our Domain Name to our VPS. That way, anyone can use their web browser, type in your domain name, and pull up your webpage. In my case, I purchased skidd0.com through godaddy. Using their DNS management system all you need to do is create (or edit an existing) A record so it points to the IP address of your VPS.

Now you’re done! Congrats. You should be able to open mywebsite.com in a browser and pull up your Hello World webpage.

Conclusions

You now have a server in the cloud that you can log into from anywhere. Not only that, you’ve got your server hosting a web app!

For more information about Flask, check out:

The Documentation
The Mega Tutorial by Miguel Grinberg
The Flask Tag on Stack Overflow

If you’re looking for more to do onn your VPS, I suggest looking into hosting a gitolite server. A gitolite server is your own private git repository where you can privately host your code.

Thanks a bunch, hope this article was useful. If you have any questions feel free to ask me.

Have Fun!


(oaktree) #2

Nice little tutorial you’ve got here!

I must say, I’ve never heard of Flask before this…


(Command-Line Ninja) #3

It’s the Sinatra of Python.