Stephan
Stephan

Reputation: 17971

Getting started with pyramid on a live server

I have successfully run the simplest pyramid app in my local virtual environment. I am now working on this tutorial but I am trying to take it a step further by running it on my personal hosting site that I use to mess around with stuff like this.

My question is. What do I pass to make_server(host, port, app) as parameters and what url do I go to to check to see if it is running? I know it's a simple question, I'm just not used to this kind of work and the documentation isn't helping me.

Bonus Points:

What are the differences between running this on a local virtual environment and on proper hosting in terms of this kind of web application?

important edit: my provider is bluehost and since I don't have a dedicated IP, I am not allowed to open my own ports, which makes me wonder if this is even possible

Upvotes: 4

Views: 2363

Answers (3)

room2web
room2web

Reputation: 1239

You would be using makeserver for test deployment. In this case you often wrap a little runserver-Script, like this (as the tutorial also points out):

#!/bin/env python
from wsgiref.simple_server import make_server
from yourapp.core import app # whereever your wsgi app lives

if __name__ == '__main__':
     server = make_server('0.0.0.0', 6547, app)
     print ('Starting up server on http://localhost:6547')
     server.serve_forever()

If you want to deploy to Apache you need the mod_wsgi module. I would recommend getting a hoster with nginx or lighthttpd support. WSGI applications can very conveniently deployed using the uwsgi module in combination with virtualenv.

If your hoster doesn't allow you to open ports, you can configure uwsgi to use unix sockets.

I wrote a blog post explaining how to deploy Django behind uwsgi + nginx once, you might want to use this as a starting point for playing around with deployment settings: http://blog.johannesklug.de/2012/11/27/deploying-django-behind-nginx-with-uwsgi-and-virtualenv/

Note: the same app object you feed into make_server will you used by uwsgi to start its worker process and open a socket.

A sample configuration (not tested but excerpted from my blog post) for uwsgi:

# uwsgi.ini
[uwsgi]
# path to where you put your project code
chdir=/home/project/project

# if the app object resides in core.py
module=core:app

# this switch tells uwsgi to spawn a master process,
# that will dynamically spawn new child processes for
# server requests
master=True
# uwsgi stores the pid of your master process here
pidfile=/home/project/master.pid
vacuum=True
# path to your virtual environment, you should be using virtualenv
home=/home/project/env/
# path to log file
daemonize=/home/project/log
# this is where you need to point nginx to,
# if you chose to put this in project home make
# sure the home dir is readable and executable by
# nginx
socket=/tmp/uwsgi.sock

Sample config for nginx:

server {
    listen       80;
    server_name  yourserver.example.org;

    location / {
        uwsgi_pass unix:///tmp/uwsgi.sock;
        include uwsgi_params;
    }
}

Upvotes: 1

Sergey
Sergey

Reputation: 12417

In fact, hosting a Python application on a "real" webserver is quite different from running it on you local machine: locally you rely on a small webserver which is often built into the framework - however, that webserver often has limitations (for example, it may only execute requests in a single thread). Some frameworks (Django) explicitly state that their built-in server should only be used for development.

In a production environment a Python application is usually served by a "industrial-grade" webserver, such as Apache or Nginx, which takes care of such issues as binding to low ports, dropping privileges, spawning multiple "worker" processes, dealing with virtual hosts, sanitizing malformed requests etc. The Python application is then run within the web server using something like mod_wsgi or fcgi for Apache or uwsgi for Nginx. Alternatively, your application runs as a separate process listening on 127.0.0.1:6543 (just like you do it locally) and the "front" web server proxies all requests to your application and back.

The point is: It may be tricky/impossible to host a Python application on a general purpose shared hosting unless your provider has explicit support for hosting WSGI applications (ask them for instructions)

Another point: for $5/mo these days you can get a nice dedicated virtual machine where you can install whatever your want and not share it with anyone. Hosting a Python website is much easier this way then dealing with shared hosting.

Ahh, and to answer the question: in a real production application the last 2 lines of the example:

server = make_server('0.0.0.0', 8080, app)
server.serve_forever()

will not be used - instead you configure the webserver so it knows that the app variable contains your wsgi application. Refer to the next chapter in the docs for a more realistic example.

Upvotes: 5

Nafiul Islam
Nafiul Islam

Reputation: 82470

Try running the site on a free account on PythonAnywhere, its the simplest to get started with.

You can simple make a git repo from your files on github, and then clone then on PythonAnywhere (I mention a host in specific, because you want to know how to run something on a host, and I have found it to be the most easy one). As for specifics, just ask on their forums, they will help you out.

I initially made a django site there, and it was was my first online app, and I learned a decent amount.

Secondly, running your app online and on your own computer has very few differences, and these differences vary from webhost to webhost. So, you are going to have to be a bit more specific about what you want to know.

Hope this helps.

Upvotes: 3

Related Questions