Home > python, ubuntu > Use Python like PHP (in public_html with wsgi)

Use Python like PHP (in public_html with wsgi)

You want to set up a LAMP (Linux, Apache, MySQL, Python) environment at home for testing purposes.

Since development on mod_python has stopped, the recommended way to go is mod_wsgi.

In addition, we want to create a public_html directory in our HOME directory and we want to put our Python web scripts in there.

I suppose Apache2 is installed and works correctly. If you visit http://localhost/, you should see the text “It works!”.

Now, create the directory ~/public_html and put in this folder a basic index.html file with some greetings context (like “<h1>hello from index.html<h1>”). Enable the user directories and restart the web server:

$ sudo a2enmod userdir
$ sudo service apache2 restart

Now visit http://localhost/~%5Busername%5D/index.html. You should see the greetings.

Install and enable WSGI in user directories

Create the file ~/public_html/hello.wsgi:

def application(environ, start_response):
    """"Simplest possible application object"""
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain')]
    start_response(status, response_headers)
    return ['Hello, World!\n']

Point your browser to this file. It’s very likely that your browser will try to download this file, which means that WSGI is not yet configured.

Install the WSGI module and enable it in Apache2:

$ sudo apt-get install libapache2-mod-wsgi
$ sudo a2enmod wsgi

Open /etc/apache2/apache2.conf and add this line to the end:

WSGIRestrictEmbedded On

For each wsgi user on the system, add the following lines to the end of /etc/apache2/apache2.conf:

WSGIDaemonProcess [user name] user=[user name] home=/home/[user name]/public_html
<Directory /home/[user name]/public_html>
    WSGIProcessGroup [user name]

Add the following lines to /etc/apache2/sites-available/default above the closing </Virtualhost>:

<Directory /home/*/public_html>
    Options Indexes FollowSymLinks MultiViews ExecCGI
    AddHandler wsgi-script .wsgi
    Order allow,deny
    Allow from all

Restart Apache2 with “sudo service apache2 restart” and point your browser to hello.wsgi. It should work fine.

Get Flask work with Apache2 + mod_wsgi
Put in the ~/public_html/hello2.wsgi file the following:

from flask import Flask
app = Flask(__name__)
application = app    # The trick is HERE! Add this extra line!

def hello():
    return "Hello Flask!"

if __name__ == "__main__":


About these ads
Categories: python, ubuntu Tags: , , ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 73 other followers

%d bloggers like this: