Once you have created your PythonAnywhere account, log into it.
Open the web
tab and click the Add a new web app
button.
- Choose:
Own domain
(or use the default given by PA) I used my own domain name here, I suggest you also specifywww
in the name or the subdomain you are using. - Choose:
Manual setup
- Choose:
Python
(latest you can choose)
This will create the webapp and take you to the settings page for the webapp.
I find it best to use the full domain, including
www
for the webapp name. Later on at my domain provider I set up web-forwarding fromdomain.com
towww.domain.com
It can take a while for DNS changes to be updated for the webapp so you could come back later and complete some of the following steps.
Copy the CNAME details and add them to your domain at your providers control panel. PA Help
When the DNS changes have propagated you should add an HTTPS certificate
and make it the default Force HTTPS
enabled.
As it's possible to host multiple web apps in your PA account, think about naming when choosing your directory/file names
Open the Files
tab...
Navigate to /var/www/sites
which should already exist.
Create a directory to hold your source code. Something like www.domain.com
, this is going to be my live/production website.
Set up a directory structure to hold your bare repos.
Navigate to your home folder e.g. /home/account-name
Create a directory inside to hold your bare-repos. I named mine bare-repos
Then I add another directory inside bare-repos something like www.domain.com.git
So the path to the bare repo directory is /home/account-name/bare-repos/www.domain.com.git
Navigate to your home folder e.g. /home/account-name
Create a directory to hold your environment variables. I called mine .env-settings
then I added another directory inside it called www.domain.com
So the path to the environment variables directory is /home/account-name/.env-settings/www.domain.com
Open the Consoles
tab and open a new bash console.
You should create a virtual environment for each webapp. PA makes this straight forward. PA Help
Use mkvirtualenv
to create a new virtual environment for this site. I named mine www.domain.com.venv
and I'm using Python 3.10
mkvirtualenv www.domain.com.venv --python=/usr/bin/python3.10
To confirm the virtual environment has been created run:
ls ~/.virtualenvs/www.domain.com.venv/
> bin lib pyvenv.cfg
You can manually activate the virtual environment by running:
workon www.domain.com.venv
Create a .env
file in the environment variables directory created earlier:
touch ~/.env-settings/www.domain.com/.env
Later we'll add some env vars to this file...
When you activate the virtual environment, environment variables can be loaded for you. You can set this up by running:
echo "set -a; source ~/.env-settings/www.domain.com/.env; set +a" >> ~/.virtualenvs/www.domain.com.venv/bin/postactivate
PA help about environment variables
A bare repo is the remote I'll push my main
branch to to start a deployment. A PA Blog Post about bare repos.
Open the Consoles
tab open a new console ro use the console from the previous step.
To initialize the bare repo for a webapp switch to the bare-repos directory:
cd ~/bare-repos/www.domain.com.git/
and run:
git init --bare
Stack Overflow How do you use "git --bare init" repository?
While still in the bare-repos
directory for your site:
Create a post-receive
hook file:
touch hooks/post-receive
then I opened the file in nano (you could open if from the Files tab):
nano hooks/post-receive
and added the following content (substitute account-name
with your own account name and www.domain.com
with your own domain name):
#!/bin/bash
while read oldrev newrev ref
do
if [[ $ref =~ .*/main ]];
then
# only the main branch can be deployed
# ensure the site directory exists
mkdir -p /var/www/sites/www.domain.com
# checkout the latest version of the site
git --work-tree=/var/www/sites/www.domain.com --git-dir=/home/account-name/bare-repos/www.domain.com.git checkout -f main
# there will be more content added here later ...
else
echo "Ref $ref successfully received. Doing nothing: only the main branch may be deployed on this server."
fi
done
In the console run:
chmod +x ~/bare-repos/www.domain.com.git/hooks/post-receive
On the Databases
tab create a new database. I called mine www_domain_com_db
. PythonAnywhere prepends your account name to the database name followed by a $. So my database name is account-name$www_domain_com_db
As suggested it's best to add a new password for your database if you've not already done so.
Earlier on I created a .env file at ~/.env-settings/www.domain.com/.env
Open the file, you can do this from the Files
tab and add the following content, substituting the values for your own:
MYSQL_DATABASE='account-name$www_domain_com_db'
MYSQL_USER='account-name' # Username
MYSQL_PASSWORD='the-password-just-created'
MYSQL_HOST='get-this-from-the-databases-tab' # Database host address
MYSQL_PORT='3306'
While I am editing this file, although not Database related the following can be added.
WAGTAIL_SITE_NAME='add-your-sites-name'
BASE_URL='http://www.domain.com'
DJANGO_SECRET_KEY='add-a-good-secret-key'
DJANGO_ALLOWED_HOSTS='www.domain.com'
Save the file.
These values will be picked up when the webapp is first run and also when activating a virtual environment.
Up Next - Add further deployment settings