[FREE] How to Host Your Django Website on PythonAnywhere?
Do you want to host our own website online for FREE? You are at the right tutorial. I will explain to you how you can deploy your Django website on Pythonanywhere for FREE step-by-step.
You can host any Python web framework-based (Django, Flask, Bottle,…) applications on PythonAnywhere. And it is pretty easy.
1. Create GitHub Repo
GitHub is the source code hosting platform where you can host your project code. If you already have your project code hosted on GitHub, skip this step.
Create your GitHub account, if you don’t have one. Create new repository on GitHub for your project and push your code.
Set your repository private if you dont want to make it public. Otherwise, anyone can access your project source code.
If you are new to source code management and hosting, refer Git tutorial. This tutorial will explain you in detail.
2. Create an Account on Pythonanywhere and Host the Django Website
First of all create your account on PythonAnywhere.
Once your create your account, open dashboard and click on “Bash” under “New Console”.
Run below command. This command will install all the required Python site-packages to host your Django website.
pip3.8 install --user pythonanywhere
Now you have to clone your GitHub repo, install the required Python modules, and host the website. You don’t need to do it manually. Just run the below command and PythonAnyWhere will do all the things required, for you.
pa_autoconfigure_django.py --python=3.8 <github_repo_link>
Following tasks will be executed by the above command to make your Python web app aka website online. (This will save you a lot of effort and time.)
- Running API sanity checks
- Cloning your GitHub repository.
- Creating virtual environment (virtualenv) with Python3.8
- Installing Python packages from requirements.txt into the virtual environment.
- Creating web app via API.
- Adding static files mappings for
/static/
and/media/
(The path for the static and media files are mentioned in thesettings.py
which you can modify.) - Updating
WSGI
file at/var/www/<app_name>_pythonanywhere_com_wsgi.py
- Updating settings.py (modify ALLOWED_HOST by adding your website URL)
- Running
collectstatic
to copy all the static files to one static path for further use in the web app. (To understand the static files in Django, refer to this link.) - Running migrate the database. This will make the initial migration required for the database.
- Reloading your website via API. This is the final step to make your website go live.
Open the the website URL in the web browser. You will see your website running. 😀
3. Add Custom Domain URL
In a free account, you have to use the PythonAnywhere
URL to access your Django website. If you want to customize the URL to your own domain, you have to upgrade your account. You can start with a very basic plan which costs you $5/month.
The price is very affordable. Isn’t it?
Follow the steps given below.
- Login to your Pythonanywhere account and select menu ‘Web’.
- Edit and configure a new domain name or website URL.
- You will get the CNAME for DNS setup. Copy that. And set that CNAME as a DNS record with your domain registrar.
- Reload the website.
BOOM 😀 Your website is LIVE.
Note: Sometime, it takes time to update the DNS record.
Creating your PythonAnywhere account, opening bash console, and running two commands. That’s all you have to do to make your website live on the internet. Isn’t it so simple?
After doing research and tried multiple hosting platform, this is the PythonAnywhere is simplest and reliable platform. I’m using it to host more than 10 website now. If you have any questions, you can ask me in the comment section below.