Use Apache + mod_wsgi to deploy Python programs such as webpy
Webpy is a lightweight Web framework on python. Like many other Python web frameworks, webpy supports wsgi deployment, at present, the best wsgi deployment solution is Apache + mod_wsgi, which provides a very simple and excellent deployment method, making it no longer difficult to deploy Python programs, this article describes how to deploy mod_wsgi on Apache and try to use mod_wsgi to support webpy programs.
First, assume that Apache has been installed and configured on our server. The installation path is/usr/local/Apache, the VM configuration file is in/usr/local/Apache/CONF/vhost, so we can start installing mod_wsgi.
Refer to the official website of mod_wsgi
Before compilation, we need to check whether the current environment supports mod_wsgi. To compile it, we need a complete GCC compiling environment. At the same time, we must ensure that the python and Python-dev packages are installed, if you are not sure whether python is supported, you can directly install apt-Get On Debian.
1apt-get installpython python-dev
If the prompt is not installed, install
Compile mod_wsgi
./configure --with-apxs=/usr/local/apache/bin/apxs --with-python=/usr/bin/python2make3make install
If there are no errors, congratulations, mod_wsgi has been compiled and installed successfully.
Edit the/usr/local/Apache/CONF/httpd. conf file.
Add
LoadModule wsgi_module modules/mod_wsgi.soAddType text/html .py
To enable Python + wsgi support on a virtual host, you only need to add
WSGIScriptAlias / /var/www/main.py/
/Var/www/Main. py is the main program that provides wsgi.
Of course, General programs support static files. Therefore, you may need to skip the processing of files in a directory, as shown below:
Alias /static /var/www/static/
In this case, add the directory settings. For the above example, the complete virtual host configuration is as follows:
01 ServerAdmin admin@xxx.com02 DocumentRoot /var/www03 ServerName xxx.com04 ServerAlias www.xxx.com05 WSGIScriptAlias / /var/www/main.py/06 Alias /static /var/www/static/07 AddType text/html .py08 ErrorLog /var/log/httpd/xxx_error.log09 CustomLog "|/usr/bin/cronolog /var/log/httpd/xxx_access_%Y%m%d.log" custom110 11 12 Options Indexes FollowSymLinks13 AllowOverride All14 Order allow,deny15 Allow from all
In this way, the mod_wsgi support of this site is configured. How can this configuration work be used for python programs? The following uses webpy as an example to deploy a test program.
For webpy, the official wsgi configuration example, specific reference: http://webpy.org/cookbook/mod_wsgi-apache/zh-cn
The actual main. py content is as follows:
01 #!/usr/bin/env python02 import os03 import sys04 path =os.path.dirname(os.path.realpath(__file__))05 sys.path.append(path)06 07 import web08 09 urls =(10 '/','index'11 )12 13 class index:14 defGET(self):15 return"Hello, I am Leven."16 17 app =web.application(urls,globals())18 curdir =os.path.dirname(__file__)19 session =web.session.Session(app, web.session.DiskStore(curdir+ '/' + 'sessions'),)20 21 def session_hook():22 web.ctx.session=session23 24 app.add_processor(web.loadhook(session_hook))25 application =app.wsgifunc()
For a brief explanation, because we are considering deploying a virtual host, we have not installed webpy on the server. We put webpy under the web directory. Therefore, when using it, you must add the current directory to the path to load the webpy module. Add the current directory to the path before starting all files.
Then, because the session is often used in web programs, we add the session to the program. The specific storage path is under the sessions directory.
Upload the webpy directory and Main. py to the web root directory, and then access it. If the access result is as follows, congratulations, your configuration has been successful.