Installing (pypy + Flask) dependent on nginx

Installing (pypy + Flask) dependent on nginx


Nginx is an open source web server, with a strong focus on high performance. When you need to enhance performance for website, choice nginx is better.
  • Flask dependent on nginx need through flup library.
  • Run pypy in the virtualenv.
Why choose pyp, because pypy is JIT compiler, it’s performance quickly for CPython.

Requirements
  • pypy-1.9 (stable version)
  • nginx-1.2.6

Install or update nginx

Nginx can’t use 0.6.* version, because we need a parameter “fastcgi_split_path_info” in nginx, if your nginx no support, please update.

Update nginx we can use apt-get to install nginx:
# apt-get update
# apt-get install nginx

After installed the nginx conf in the /etc/nginx. take nginx service status command as below:

# /etc/init.d/nginx {start|stop|reload|restart}

However, if wants install nginx use tarball source, download .tar.gz in official site, and install command as below:
Before make install please to rename or delete /usr/sbin/nginx

# mv /usr/sbin/nginx /usr/sbin/nginx.old
# tar zxvf nginx-1.2.6.tar.gz
# cd nginx-1.2.6
# ./configure
# make && make install

Notice: When you during configure will check the depends libraries, so if there have message for some packages not install, please just use apt-get to install it.
After make install, the installed path is in the /usr/local/nginx/conf
In my case after installed, I’ll move /etc/nginx/nginx.conf to here, and sites-available, sites-enabled and so on.
However, modify the nginx configure like below:

    server {
        listen 80;
        server_name dev.localhost.com;
        location = / { rewrite ^ // last; }
        location / {
        include fastcgi_params;
        fastcgi_split_path_info ^(/)(.*)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_pass 127.0.0.1:5000;
        }
    }

Install pypy

download pypy-1.9-linux.tar.bz2 from official site then uncompress.

# tar jxvf pypy-1.9-linux.tar.bz2
# cd pypy-1.9
# bin/pypy

Python 2.7.2 (341e1e3821ff, Jun 07 2012, 15:40:31)
[PyPy 1.9.0 with GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``<arigato> no, normal work is such
much less tiring than vacations''
>>>>

Press Ctrl+Z to exit the pypy console.
And before install Flask we need to install distribute and pip packages.

# wget http://python-distribute.org/distribute_setup.py
# wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py
# bin/pypy distribute_setup.py
# bin/pypy get-pip.py

Install Virtualenv

# bin/pip install virtualenv
# mkdir /home/myweb
# cd /home/myweb
# virtualenv myenv
# source myenv/bin/activate

Notice: virtualenv is in the pypy-1.9/bin/ directory.

Install Flask

(dev) # bin/pip install Flask
(… here need a few time…)

If you also need install another like SQLAlchemy for Flask, just also use pip install command.

(dev) # bin/pip install Flask-SQLAlchemy
(dev) # bin/pip install mysql-python

When you first install mysql-python, during the install, sometime will show mysql_config not found, which can be installed from:

(dev) # apt-get install libmysqlclient-dev

Simple test code, filename: web.py

#!/home/myweb/myenv/bin/pypy
#-*- coding: utf-8 -*-

from flup.server.fcgi import WSGIServer
from flask import Flask, url_for, render_template
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://xxx:xxx@localhost/DBNAME'
db = SQLAlchemy(app)
c={}
class city(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    COUNTRY = db.Column(db.String(50))
    def __init__(self,COUNTRY):
        self.COUNTRY = COUNTRY

@app.route('/')
def index():
    c['countrys'] = city.query.group_by(city.COUNTRY).all()
    return render_template('index_en-us.html',c=c)

if __name__ == '__main__':
    WSGIServer(app, multithreaded=True, bindAddress=('127.0.0.1', 5000)).run()


(dev) # pypy web.py

Finally, we can open the browser http://dev.localhost.com to see results, here http://url is your website address, while you setup in nginx config.

Comments

Popular Posts