Python Shared Hosting for Web Servers: Moving From PHP to Python (bottle.py, Flask, others)

Python Shared Hosting – Python Web Servers vs. PHP Scripts

Many python web developers have experience using another language. Understanding the differences between how a python web server and a PHP application works can help you figure out how to host your application. It can be slightly harder to find a python shared hosting provider than PHP shared hosting (which is ubiqitous at this point). This article will guide your through low cost options for getting started with python web apps.

My first exposure to web applications was with PHP. I  borrowed some unused space on my wife’s Yahoo business account. Shared hosting is very common in the PHP world. It allows large numbers of casual customers to share a common web server. This reduces hosting costs (under $5/month for many plans). Most plans have a user-friendly “default” configuration, limiting interaction with the underlying Apache server. Most casual WordPress users are barely aware that Apache and PHP exist.

Python flips the script. The first really cool thing about bottle.py is how easy it is to set up a python web server running on localhost. The first really confusing thing for an ex-PHP dev is “how the heck do I get this thing into production?”

To really unleash the power of Python, you need to step away from the PHP and Perl idea of running a script for each HTTP request. For PHP, a request arrives at a server (generally Apache in the PHP world), the server triggers a PHP script, the script runs, and everything shuts down at the end of the script. Data you want to retain and share between requests must be written to a file, database, or global server variable (bad!). While you can write scripts like this for Python, you now have the option of setting up your application as a process that keeps running between requests. Bottle.py allows you create “routes” that map to specific Python functions and python data structures which remain in memory between requests. This opens up new ways to design your application. It also signs you up to manage part of the system you previously ignored.

My old PHP host tried to steer me onto a virtual server (VPS) at about $50 per month. On the surface this made sense. You need access to the Linux shell to run a Python site, which means you can’t share environments with many other applications. But this is costly for a casual project or experiment. I started looking for python shared hosting options to reduce the cost of getting our first set of projects up and running.

Here are a few python shared hosting options – tailored towards supporting a bottle.py application. Most of these should also work for Flask and other Python frameworks…

  • Google App Engine is a good place to start. While there are restrictions, they will give you a free quota and free appspot URL. This is ideal for a student developer. This is best for PHP-like (scripts) web apps written in Python. Bottle.py actually has a server plug-in specifically built for GAE integration, making your life easier.
  • Heroku – particularly the Cedar stack – is another option for getting a small application up and running. Heroku is closely integrated with Git – allowing you to deploy an application directly from your Git repository. There is a free option but it can be costly to scale your application on this platform.
  • Finally, there is Webfaction, which I use for my projects. The look and feel of the service is similar to a VPS in many respects; they give you most of a linux shell. The founders are Python friendly (I believe one of them helped develop CherryPy) and the environment and terms of service are tailored to Python web applications.

Why I Went With Webfaction

I ultimately went with Webfaction [affiliate link], since it had several nice features.

First – the environment is not restricted to Python and you can build a site from multiple different technologies. Their management console makes it easy to integrate multiple apps on a single site (bottle.py Python app, WordPress blog) or map several domains to a single server process. The latter is suprisingly useful: it allows you to test “marketing” versions of an application (different domains / positioning / end-user audiences) without having to fund multiple instances of the application. Since you have most of a Linux shell, you can deploy applications in other languages (ex. Haskell’s yesod framework) on the same account. This gives me plenty of room to grow and experiment.

On a related note, Webfaction has already installed many of the common libraries and supporting services for Python web applications on their shared servers. Which is a nice advantage over a traditional low-cost VPS – it reduces the amount of setup and server admin work you’ve got to perform yourself…

Another cool thing about Webfaction’s plan – you are only “charged” for the memory used by your custom applications. This is potentially a big savings – you don’t have to “fund” a base Python instance or the underlying web servers (nginx) and databases software (for Postgres and MySQL) out of your quota. I’m currently supporting five Python applications from my account, along with a gaggle of WordPress blogs.

Some Specifics on How We Set Up The Webfaction Server

In the Webfaction example above, configuration works as follows:

  • Set up a “site” for your new application; you can actually have multiple applications supporting a single site by using different paths (Eg. this directory is for “static” content and stays with nginx, this other directory is your bottle.py app, and finally, this third directory is used for WordPress and some PHP modules)…
  • Set up the application – this will assign you a port on that machine (eg. #12345)
  • Point your bottle application to that port – and away you go…
  • If you want to add a WordPress blog to the site, use their console to create a new WordPress application (pick WordPress). Then add it to the website definition on its own path (“/blog” works well). Go to that url (www.mysite.com/blog) and finish the regular WordPress setup process….

For a production application, you will want to spend some time identifying a server to sit in front of the built-in Bottle development server. My preference is Cherrypy – there are other options and configurations, but this allows me to seamlessly move code between my development (also Cherrypy) and production environments.

Once you have the system up and running, take a look at moving your static assets (css, javascript, static html pages) into a static directory. It’s fairly easy to configure and  reduces the load on your server. If you are proficient at AJAX, look into leveraging bottle.py’s excellent json integration – another bandwidth saver. These two tweaks can help you build a site that can handle significant traffic without breaking a sweat…

Check Webfaction out if you get a chance – I’ve been using them for about a year (for half a dozen sites) and have been very happy with their service.

Be the first to comment

Leave a Reply

Your email address will not be published.


*