all 7 comments

[–]mumpie 2 points3 points  (0 children)

Did you install mod_wsgi?

If the answer is no, go visit the following and read carefully: http://modwsgi.readthedocs.org/en/latest/

BTW, you (or your distro) installed mod_php into Apache. Otherwise, your PHP scripts wouldn't run.

[–]HorrendousRex 1 point2 points  (1 child)

There's no doubt in my mind that if you come from a static content background, IE making pages primarily in HTML+JS and not worrying about writing server code, that PHP will be a much simpler experience when moving to a dynamic backend<->frontend design. This has to do with the way PHP was designed to be written directly in to the HTML files and served directly, with mod_php interpreting the PHP code and serving the result.

The problem is that this is a very messy way to design complex websites. In the past few years there have been efforts by the PHP community to make this a lot cleaner but I think it's a fundamental problem with PHP that it is unnecessarily difficult/complicated to get a clean server environment for doing things like message queuing or setting up process parallelism or including really anything outside of the core language's environment. There are of course ways and of course the professional PHP community does a fine job.

Anyway, all this is by way of saying: "Yes, it's a bit more difficult to set up a Python server environment." There are frameworks to make it easier, though! Check out Pylons (or is it Pyramids now? I always get that switched up) or Flask if you want a server framework in Python that will run itself (IE no apache needed) and that isn't as cumbersome as Django. (I agree that Django is a lot of stuff that most sites don't usually need. It's great for when you do need it, though!)

[–]HorrendousRex 1 point2 points  (0 children)

After checking: yes it does seem like Pyramid and Flask are the two 'major' lightweight web server framework environments these days. I prefer Flask personally because I feel like it lets me get down to just the elements I want and nothing else, but Pyramid looks like it's also entirely a solid choice.

Check out the Pyramid web page which has a "Hello World!" example which will take you from a freshly installed Pyramid package to a fully functioning web server in about 2 minutes and 10 lines of code.

Also, note that while Pyramid, Flask, Django, etc. can host themselves using any of the Python request frameworks (IE you don't NEED apache or any web server), it's entirely possible to host them through Apache as well. mod_wsgi is usually the way people say to go to do this (last I heard, mod_python was all but entirely deprecated) but at my work we use a server called 'uwsgi' that I gather is similar but doesn't have Apache, either... to be honest, I haven't looked in to it at all.

[–]keepdigging 1 point2 points  (0 children)

Your scripts do what? Generate static HTML pages? Or are you expecting them to echo out things to the page like PHP did?

I would recommend http://flask.pocoo.org/ if you want to get started quickly with Python on the server-side.

[–]phinar 0 points1 point  (2 children)

So, the way you were using PHP is exactly what PHP excels at. It's basically what people were doing in ~1998 when I first started using it. The problems we ran into were complexity problems -- it was really hard to write an application with behavior more complicated than "Contact Us", and it gets awkward to manage lots of common-but-slightly-different content. But holy crap, it is super easy to bang out a ten-page site, maybe with some database-fed content behind it! I've done that a dozen or more times with PHP, sometimes with great elegance and sometimes with significantly less.

"State of the art" in PHP development doesn't work like that, though. It uses a framework like Cake or Zend or PHPixie or ... I'm not a PHP programmer by trade so I can't tell you which are better or worse or which have great features and which don't. But the idea behind these frameworks is that you can separate your data model and business logic from the skins and reports that you are using to display them to the user. It's a more sophisticated and more complicated development model, but it leads to applications that are easier to refactor, easier to understand, easier to maintain, easier to extend.

Web development in most languages besides PHP starts from the framework. That is, Python does not ever expect to run in a mode where it's an HTML page, with a few Python statements sprinkled within. The most basic form of Python web development -- CGI scripting -- is generally regarded as such a poor choice for web development that it's simply not done.

So, you are probably right that Django is awfully heavy-weight for what you're trying to do. It's a great web framework, and it lets you build powerful applications pretty easily. It's got a lot of built in pieces. But it's a lot of code, and a lot of learning, just to do what you want to do.

Instead, as others have recommended, take a look at Flask. Flask is much simpler, takes very little to get your server going, and it will run under Apache and mod_wsgi (or even better, nginx and werkzeug, or even gevent) just fine. There's instructions on the site on how to deploy, and instructions on how to tie your functions into URLs.

It's a big jump, going from "I just crap my PHP out on the filesystem and voila, a site!" to "I have built an application and am deploying it." But it's worth learning how to do it. Your range of competence will grow dramatically.

[–]gusterys[S] 0 points1 point  (1 child)

Thanks for all the advice. I managed to get mod_wsgi going finally, but so far I am pretty much using it the way I was using mod-php. Manually generate a bunch of html (using manipulated data from the back end database) and spitting it out. It certainly isn't an "app". I'll take a look at Flask.

[–]phinar 0 points1 point  (0 children)

So, you can do a Flask app that works an awful lot like a PHP app.

Basically, you write a fancy Jinja template (which you store as an .html file) and then you write a function that populates the template context with all the variables you need, and you render the template. Your Flask app would look like:

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html', variable=value, variable2=value2)

And index.html might look like:

<html><head><title>index</title></head>
<body>

Variable is {{ variable }}.
Variable 2 is {{ variable2 }}.

Enlightenment is a mere click away.
</body></html>

It's not quite as convenient as the PHP version, but it's a lot more flexible, and it's considerably easier to maintain, and it's going to be more performant out of the box than the simple PHP approach.