[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 a new repository on GitHub for your project and push your code.
Set your repository private if you don’t 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.
Now we are setting up the Django environment to execute your code from GitHub. We are using the PythonAnyWhere tool for easy installation.
Once you create your account, open the 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>
You will be asked to provide the GitHub username and password. (Note: GitHub is started using the personal access token. You can use it as a password here.)
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
/media/(The path for the static and media files are mentioned in the
settings.pywhich you can modify.)
- Updating settings.py (modify ALLOWED_HOST by adding your website URL)
collectstaticto 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 website URL in the web browser. You will see your website running. 😀
If this above script fails for any error, use the “
--nuke” option. (Note: option
--nuke eliminates all the previous changes, delete web app and set it up fresh.)
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: Sometimes, it takes time to update the DNS record. Be patient.
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?
The good thing about PythonAnywhere is that you will have full control of your server. You can take the backup of your Django website without any hassle.
After doing research and trying multiple hosting platforms, this is the PythonAnywhere is a simplest and reliable platform. I’m using it to host more than 10 websites now. If you have any questions, you can ask me in the comment section below.