How to Host a Django Application for $5 at PythonAnywhere.com

In this tutorial I will show you how to host a Django application on www.pythonAnywhere.com for $5 per month.

In previous posts I explained how I hosted Django applications on a2hosting.com. I now moved away from a2hosting since I was not satisfied with the service and performance any more. I think I now found the perfect hosting environment for Django applications.

Setup

In this tutorial I am using the following setup.

  • Python 3.6
  • Django 2.2
  • MySQL Database (Postgres is also possible)
  • git

Create Django Application Locally

The first thing you need to do is starting a Django application in a virtual environment on you laptop. I explained how to do this in this tutorial.

Move to github

You then have to move your software to https://github.com/.

Add New WebApp on PythonAnywhere

First you need to sign up to PythonAnywhere.com if you did not already do so. Then navigate to Web > Add a new web app. There select Manual Configuration and your desired Python version. In my case I selected Python 3.6.

You now have set up a basic python environment. Under http://<YOUR_USERNAME>.pythonAnywhere.com you show now see the following.

Upload Your Code from Github

In this step we will clone our existing code from GitHub. Therefore navigate to Consoles > Bash. A new console will open where you can clone your existing source code. 

$ git clone https://github.com/user-name/project-name.git

Your source code will now be in the following directory

/home/<YOUR-PYTHON-ANYWHERE-USERNAME>/<YOUR-GITHUB-PROJECT-NAME>

Create Virtual Environment

After that create a virtual environment for your project. In the current directory execute the following command to setup your virtual environment:

$ mkvirtualenv --python=/usr/bin/python3.6 venv-myproject

Install Requirements

Your virtual environment is now setup and activated. You can see that is is activated sind brackets will appear left of the $ sign e.g. (venv-myproject)$

Navigate to the newly created folder and install all the requirements which you saved in your requirements.txt file.

(venv-myproject)$ cd project-name
(venv-myproject)$ pip install -r requirements.txt

Setup WSGI File

Next we need to edit the WSGI file such that not the HelloWorld text is shown, but your Django application. Navigate to your web app page on pythonanywhere.com and scroll down to the Code section.

Click on the WSGI configuration file link in order to edit it.

In the editor you can remove everything except for the Django part. You code should look as shown below.

You need to edit two lines:

  • Line 8: Edit the path such that it points to the folder which contains your manage.py file.
  • Line 12: Substitute mysite with the folder name which contains your settings.py file. This is the name of your Django project
# +++++++++++ DJANGO +++++++++++
# To use your own django app use code like this:
import os
import sys

# assuming your django settings file is at '/home/XXX/mysite/mysite/settings.py'
# and your manage.py is is at '/home/XXX/mysite/manage.py'
path = '/home/XXX/mysite'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

# then:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Save the file and return back to the web application page.

Setup Web App

We now need to set the virtual environment.

Navigate to your web app page on pythonanywhere.com and scroll down to the Virtualenv section. There input the path to the virtual environment you created before. 

/home/XXX/.virtualenvs/venv-myproject

Next, setup the path to your code. Scroll down to the Code section and input the path to your source code.

Hit the reload button on the top of the page. Now you should see your site up and running. Go ahead and test it out.

Setup Static Files

To serve static files on your website you first need to edit your settings.py file. Make sure the file has the following settings:

STATIC_URL = '/public/'
STATIC_ROOT = os.path.join(BASE_DIR, 'public') # after collectstatic is run all the static files will be put in this folder

You can of cours change the lines however you like it. With STATIC_URL you can define under which URL your static files will be made available on your website. With STATIC_ROOT you define where on the server the static files are located or shall be copied. In my case they can be found under /home/XXX/project-name/public.

Next you need to copy all your static files to this location. Therefore go to your web app and scroll down to Virtualenv. Click on Start a console in this virtualenv.

In the console execute the following command to copy all the static files to the previously defined location.

$ python manage.py collectstatic

At last, setup the static files mapping on the web app page. This will tell pythonAnywhere.com to serve the static files for you.

For URL enter the path which you defined in STATIC_URL.

For Directory enter the path where your static files have been copied. This is the path you defined as STATIC_ROOT.

After that reload you Django application. Your static files should now be served.

Setup MySQL Database

PythonAnywhere.com supports MySQL databases for free. If you want to use a Postgres DB you need to update your payment plan. To do this go to  Dashboard > Upgrade Account. 

First navigate to Databases and create a new database. You will then see the MySQL settings page which looks similar to this:

Grab these values and put them in the settings.py file as follows.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'XXX$default',
        'USER': 'XXX',
        'PASSWORD': '<YOUR_PASSWORD>',
        'HOST': 'XXX.mysql.pythonanywhere-services.com',
    }
}

Before you reload your page make sure you have installed mysqlclient. If not, open a console in the virtual environment and execute.

$ pip install mysqlclient

Now reload your application. You should now have a connection to your database.

Serving the Web App under Your own Domain

To use serve your website under your own domain or to use TLS with your Django application you first need to upgrade your account to a paid account. To do this go to  Dashboard > Upgrade Account. 

Then you are able to serve this web app under your own domain and also serve it protected using https. When you create a new web app you are asked under which domain you want to serve it.

On the web app page you will then see a new area called DNS setup. At your domain provider you now need to setup a CNAME record with the value you see on your page.

For my website passiveincomecalculator.com it looks like this.

Type   Domain Name                      Canonical Name	          
CNAME  www.passiveincomecalculator.com  webapp-916499.pythonanywhere.com

Using TLS/SSL

After this is done you can scroll down to the security chapter. You can setup an auto-renewing Let’s Encrypt certificate. Your website will not be available using TLS.

I hope everything worked for you. If you have questions leave them in the comments 🙂