Joseph Mancuso
Posted on August 21, 2018
Introduction
PythonAnywhere is a way to develop and host your website or any other code directly from your browser without having to install software or manage your own server. It's a very good option if you want to deploy a WSGI application and it has support for Django and Flask right from the start.
In this article I'll walk through how to get your application setup on an instance of PythonAnywhere.
Create an account
This one is simple :). Head over to PythonAnywhere and create a free or paid account. Whichever you want.
The Dashboard
Once you login you'll be presented with a dashboard. In the bottom left corner of it you will see a section that looks like this:
Go ahead and click the $bash
link. You'll be presented with a new bash terminal. From here you should pip install the masonite-cli so we can start using craft commands.
$ pip3 install masonite-cli --user
Make sure you use the --user
flag because you won't have sudo privileges inside this bash shell.
Setting Up The Directory
Let's go ahead and create some directories we'll be using for this project. We might want our PythonAnywhere instance be able to serve multiple applications so let's make a "sites" directory.
~ $ mkdir /var/www/sites
~ $ cd /var/www/sites
/var/www/sites $ <we should now be here>
We make a sites directory because this might contain a structure where we can put multiple websites like:
/sites
masonite/
tutorial/
blog/
Installing Masonite
If you just want to install a fresh application then you can go ahead and follow the normal documentation for installing a new application. But for this tutorial we will talk about how to install an application directly from github.
For the convenience, we'll actually be installing a new application for Masonite but via the github repository and not the craft new
command.
So head over to your github account and copy the github link. In this case we will be using:
https://github.com/MasoniteFramework/masonite
Make sure you are in the sites directory and git clone your repo:
/var/www/sites $ git clone https://github.com/MasoniteFramework/masonite
Cloning into 'masonite'...
remote: Counting objects: 1898, done.
remote: Compressing objects: 100% (57/57), done.
...
...
Great! So now our repository is cloned. Doing a quick ls
command should return a new file with our new repo:
/var/www/sites $ ls
masonite
Virtual Environment
PythonAnywhere promotes the idea of putting your dependencies in a virtual environment (which is good practice). So let's get our virtual environment setup which is really easy.
/var/www/sites $ cd masonite
/var/www/sites/masonite $ virtualenv -p python3 venv
Notice we did a cd
into our freshly created masonite project and created a virtual environment. This command is using python3
(the virtualenv
defaults to Python 2.7 and Masonite requires 3.4+). The venv
at the end is the name of our virtual environment. Name that whatever you want.
Installing dependencies
Great. So now let's install our dependencies. First we will need to activate our virtual environment and then run the craft install
command like we normally do.
/var/www/sites/masonite $ source venv/bin/activate
(venv) /var/www/sites/masonite $ craft install
Collecting waitress==1.1.0 (from -r requirements.txt (line 1))
Using cached https://files.pythonhosted.org/packages/ee/af/ac32a716d64e56561ee9c23ce45ee2865d7ac4e0678b737d2f5ee49b5fd6/waitress-1.1.0-py2.py3-non
e-any.whl
Collecting masonite<=2.0.99,>=2.0 (from -r requirements.txt (line 2))
Collecting python-dotenv==0.8.2 (from masonite<=2.0.99,>=2.0->-r requirements.txt (line 2))
Using cached https://files.pythonhosted.org/packages/85/9f/b76a51bb851fa25f7a162a16297f4473c67ec42dd55e4f7fc5b43913a606/python_dotenv-0.8.2-py2.py
3-none-any.whl
...
...
Key added to your .env file: 1KmdwFryf71PGKYm6NBoFXRHHlqqE0=
Let those dependencies install. If installed successfully you will get a green message saying a key was appended to your .env
file. Great. Now let's get out of this bash shell and on to adding our application to the PythonAnywhere dashboard.
Adding Our Web App
Go back to the dashboard and in the upper right hand side we will see a tab called "Web" like this:
Click on that and click on "Add New Web App":
You'll want to click on "Manual Configuration" at the bottom of the list.
and then click on "Python 3.6":
Once that is done click "Next" and you should be presented with a new dashboard specific to your app.
Configuring Your Web App
Code Section
If you scroll down on this new web app dashboard you'll see a "Code" section:
Remember that we put our app in a /var/www/sites/<your_project>
directory so let's add that to both the source code and working directory like this image above.
Now we need to edit the wsgi.py
file. I'm not entirely sure why they don't just default to the one in the source code but oh well. Click on that "Wsgi configuration file" link which will open up an editor.
We'll have to copy and paste our wsgi.py
file into this code. Since we're using git we can grab it right from our GitHub repo. If you made no modifications to your wsgi.py
file you can use the default one here: https://github.com/MasoniteFramework/masonite/blob/master/wsgi.py
The final product should look something like this:
Make sure you click save in the upper right hand corner
Virtualenv Section
Scroll down a bit on the web app dashboard and you should see a new section for virtual environments. We just need to tell PythonAnywhere where we created our virtual environment. Remember we just made a venv
directory in our project directory. So let's just append on a venv
to the directory we've been adding:
Success!
That's it! Now let's just go up to the top of the page and reload the application:
Once that's done we can view it using the link right above it that looks something like ".pythonanywhere.com". This will open up our web app!
Posted on August 21, 2018
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.