本文使用XAMPP配置 Flask 运行环境。python解析使用mod_wsgi模块。理论上django也可以使用。
----------------------
2、安装mod_wsgi。官方需要自己编译。第三方地址:
http://www.lfd.uci.edu/
~gohlke/pythonlibs/#mod_wsgi 。解压找到so文件放在XAMPP/apache/conf/modules/mod_wsgi.so。
(
注意匹配自己的Python版本和Apache版本
)
3、配置Apache虚拟主机和端口
比如使用8000端口:
httpd.conf:
在LoadModule最后一行,添加 LoadModule wsgi_module modules/mod_wsgi.so
找到httpd-vhosts.conf一行,确认去掉头部的#,如果没有则自己添加。Include etc/extra/httpd-vhosts.conf
httpd-vhosts.conf
这个文件里面一般都是使用#注释掉了。
我们直接在最后这么写:
Apache 2.4版本
Listen 8000
NameVirtualHost *:8000
<VirtualHost *:8000>
ServerName test.com
ServerAlias www.test.com
ServerAdmin root@test.com
DocumentRoot "F:/web"
ErrorLog "F:/web/error.log"
WSGIScriptAlias / F:/web/run.wsgi
Alias /static F:/web/static
<Directory "F:/web">
#Options +ExecCGI
#AddHandler cgi-script .py
Options -Indexes +FollowSymLinks
Require all granted # ver 2.4
AllowOverride All
WSGIScriptReloading On
</Directory>
</VirtualHost>
NameVirtualHost *:8000
<VirtualHost *:8000>
ServerName test.com
ServerAlias www.test.com
ServerAdmin root@test.com
DocumentRoot "F:/web"
ErrorLog "F:/web/error.log"
WSGIScriptAlias / F:/web/run.wsgi
Alias /static F:/web/static
<Directory "F:/web">
#Options +ExecCGI
#AddHandler cgi-script .py
Options -Indexes +FollowSymLinks
Require all granted # ver 2.4
AllowOverride All
WSGIScriptReloading On
</Directory>
</VirtualHost>
Apache 2.2及以下版本
Listen 8000
NameVirtualHost *:8000
<VirtualHost *:8000>
ServerName test.com
ServerAlias www.test.com
ServerAdmin root@test.com
DocumentRoot "F:/web"
ErrorLog "F:/web/error.log"
WSGIScriptAlias / F:/web/run.wsgi
Alias /static F:/web/static
<Directory "F:/web">
#Options +ExecCGI
#AddHandler cgi-script .py
Options -Indexes +FollowSymLinks
Order deny,allow # ver 2.2
NameVirtualHost *:8000
<VirtualHost *:8000>
ServerName test.com
ServerAlias www.test.com
ServerAdmin root@test.com
DocumentRoot "F:/web"
ErrorLog "F:/web/error.log"
WSGIScriptAlias / F:/web/run.wsgi
Alias /static F:/web/static
<Directory "F:/web">
#Options +ExecCGI
#AddHandler cgi-script .py
Options -Indexes +FollowSymLinks
Order deny,allow # ver 2.2
Allow from all # ver 2.2
AllowOverride All
WSGIScriptReloading On
</Directory>
</VirtualHost>
AllowOverride All
WSGIScriptReloading On
</Directory>
</VirtualHost>
4、编写run.wsgi启动脚本
Flask: (已测试可用)
run.wsgi文件内容为:
from test import app as application
测试py文件为test.py:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return "Hello World!"
if __name__ == '__main__':
app.run(host='0.0.0.0',port=8000)
Django:(未做测试)
ru.wsgi文件内容:
import os
import sys
import django.core.handlers.wsgi
sys.path.append(r'F:/web')
#sys.path.append(os.path.dirname(os.path.dirname(__file__)))
os.environ['DJANGO_SETTINGS_MODULE'] = 'demo.settings'
os.environ['PYTHON_EGG_CACHE'] = 'F:/web/cache'
application = django.core.handlers.wsgi.WSGIHandler()
import sys
import django.core.handlers.wsgi
sys.path.append(r'F:/web')
#sys.path.append(os.path.dirname(os.path.dirname(__file__)))
os.environ['DJANGO_SETTINGS_MODULE'] = 'demo.settings'
os.environ['PYTHON_EGG_CACHE'] = 'F:/web/cache'
application = django.core.handlers.wsgi.WSGIHandler()
5、修改hosts文件:
为了使用方便,将虚拟网址映射为本地地址,所以需要修改hosts文件。如果没这个需求也可以不改,直接使用ip:port方式访问.
hosts文件地址:
C:\WINDOWS\system32\drivers\etc\hosts
在文件最后添加:(test.com改为你自己的测试地址)
127.0.0.1 test.com
至此,配置完成,打开XAMPP,启动Apache,若没有错误信息,可以看到:
如图,8000端口已经启动起来,输入http://test.com:8000 或者 http://127.0.0.1:8000 就可以看到 hello world! 了。
-------------常见问题集合(参考)-------------------
如果出问题,查看日志 F:/web/error.log 或默认的log文件。
apache AH01630: client denied by server configuration错误解决方法
出现这个错误的原因是,apache2.4 与 apache2.2 的虚拟主机配置写法不同导致。
apache2.2的写法:
<VirtualHost *:80>
ServerName fdipzone.demo.com
DocumentRoot "/home/fdipzone/sites/www"
DirectoryIndex index.html index.php
<Directory "/home/fdipzone/sites/www">
Options -Indexes +FollowSymlinks
AllowOverride All
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
如果在2.4中使用以上写法就会有apache AH01630: client denied by server configuration错误。
解决方法,apache2.4中
Order deny,allow
Allow from all
Allow from host ip
修改为
Require all granted
Require host ip
修改后的配置如下:
<VirtualHost *:80>
ServerName fdipzone.demo.com
DocumentRoot "/home/fdipzone/sites/www"
DirectoryIndex index.html index.php
<Directory "/home/fdipzone/sites/www">
Options -Indexes +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
----------------------------
一些隐藏的彩蛋:
在环境变量中添加,PYTHONHOME, 值为你的Python路径,比如C:/python27