Install web.py on CentOS 6.2

July 6, 2013 - 2 minute read -

Recently I am working on a project using web.py, and I am going to host the website on a linux virtual machine (from Aliyun 阿里云) with Cent OS 6.2 installed.
This is the installation note of what I’ve done yesterday.

This project is using MySQL-python (sometimes it is called MySQLdb). It is found that, in order to install some Python packages, we have to install the development version of both Python and MySQL.
To install web.py and some other packages, pip is a very handy tool. The package name for pip in yum is python-pip.

sudo yum install python, python-devel, mysql, mysql-devel, python-pip

Now we can install the Python packages used in over project with command python-pip install packagename.
In my project, the following packages are used.

  • web.py
  • PIL
  • mysql-python
  • Jinja2

The installations go well unless I got error message The _imagingft C module is not installed when I try to draw text with PIL. According to this answer in Stackoverflow, the error disappear after I install libfreetype with command yum install freetype.

Run the app

To run the website app after logging off from virtual machine, we can use command

nohup python app.py &

Please do not forget the & character at the end.
To stop the server, we can use command kill pid. The pid of the process can be found by ps -ef | grep app.py.

Using Apache proxy

Because I host some other websites in that server, I uses Apache to bind a domain to the Python app.
To do this, simply add the following text to Apache’s httpd.conf file. (Assuming your app is using port 8080.)

<VirtualHost *:80>
    ServerName www.yourdomain.com
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
    <Location />
        ProxyPass http://localhost:8080/
        ProxyPassReverse http://localhost:8080/
    </Location>
</VirtualHost>

MINE type issue

There is one more interesting issus I found yesterday. The html rendered by web.py can be displayed by browsers correctly when I use port 8080, but can only be displayed as pure text by port 80.
Later I found the DefaultType is set to be text/plain in httpd.conf, so I changed it to text/html. Now everything works like magic ;)