Running webapp2 outside of Google App Engine

Webapp2 is Python framework for creating web applications and is primarily used for development on Google App Engine. In fact it's included in Google App Engine SDK, which simplifies initial development, because when starting developing for Google App Engine Standard, most important things are included in SDK (webapp2 as framework, jinja2 as template engine). It supports WSGI interface so it can be used outside of Google App Engine with WSGI supporting web server like uWSGI or Gunicorn which are the most popular in Python community.

Official documentation for webapp2 is here and code is available on Github.

In case you didn't know, Google App Engine Standard is Platform as a Service and by it's nature is restrictive environment, i.e. you cannot install whatever Python library you want or there are for example networking restrictions like not possible to use websockets.

Google App Engine Flexible removes most of these restrictions and it is one of the ways to bypass restrictions of Standard Environment. Truth is that 1 virtual CPU with 1GB RAM and 10GB hard disk (minimal configuration) costs 44$ per month, which in case if you have some small simple toy web application (like me) can be quite expensive.

Other option is to run everything on bare server but keeping in mind that it's necessary to setup everything manually. 

Code of sample application is here. It's basically hello world type (with no database integration). Keep in mind also that webapp2 supports currently only Python 2.x. 

to run locally you need to install requirements:

pip install -r requirements.txt

to run webapp locally:

python main.py

To run web application on internet, I'll use Google Compute Engine with the smallest machine type (it's free if you don't use any other Compute Resources). Note: I am using Google Cloud SDK to create instance on Google Compute Engine. 

gcloud compute instances create webapp2-server --machine-type f1-micro

Keep note about External IP address, we will need it later during setup.

ssh to server and and install nginx and git

gcloud compute ssh webapp2-server 
sudo apt-get update
sudo apt-get -y install nginx git python-pip

clone sample project from github:

git clone https://github.com/zdenulo/webapp2-outside-gae.git

go to project folder and install python requirements (I'm not using virtualenv here):

sudo pip install -r requirements.txt

edit gunicorn.service file and set correctly User and WorkingDirectory and optionally other configuration like ExecStart command. Then copy file to /etc/systemd/system/

sudo cp gunicorn.service /etc/systemd/system/

Next we will start gunicorn service and enable it to start automatically on server start:

sudo systemctl start gunicorn
sudo systemctl enable gunicorn

Last step is to configure nginx file nginx_server.comp. Set in the file External IP of this server instead SERVER_IP_ADDRESS and then copy file and restart nginx service:

sudo cp nginx_server.conf /etc/nginx/sites-available/
sudo ln -s /etc/nginx/sites-available/nginx_server.conf /etc/nginx/sites-enabled
sudo systemctl restart nginx

Now when you go to IP address of server, you should see Hello World text.

With this setup we got very simple web application using webapp2 framework (no scaling but for free :))

Some of this configuration was inspired by this article: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04.

 

 

blog comments powered by Disqus