我的电脑环境:Win7 64位旗舰版,一直在用phpstudy写php脚本,所以电脑上装了phpstudy2016,这是个老版本了,一直没有更新到2017和2018,这不是重点;
C:\Users\Administrator>python --version
Python 3.5.3
C:\Users\Administrator>pip -V
pip 10.0.1 from D:\WinPython\python-3.5.3.amd64\lib\site-packages\pip (python 3.5)
2.安装Flup,这个现在已经不需要通过下载来安装了,直接通过pip就可以装:
C:\Users\Administrator>pip install flup
过程非常顺利:
Collecting flup
Downloading https://files.pythonhosted.org/packages/88/e5/17bcf4431e811ffaec213feea7609a6f003084006d2e210f53cee09095d9/flup-1.0.3-py3-none-any.whl (74kB)
100% |████████████████████████████████| 81kB 64kB/s
Installing collected packages: flup
Successfully installed flup-1.0.3
装完之后,验证一下是否成功,输入红色字的那行代码回车,看到最后的黑色粗体字那一行,就表示安装成功了:
C:\Users\Administrator>python
Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import flup;print(flup.__path__)
[‘D:\\WinPython\\python-3.5.3.amd64\\lib\\site-packages\\flup‘]
3.安装Nginx,由于我的机器上已经装了phpstudy,已经自带了Nginx,随意这一步就跳过了
Nginx官网下载地址:http://nginx.org/en/download.html
下载windows上的nginx最新版本,http://nginx.org/download/nginx-1.15.0.zip 解压即可;
运行nginx.exe后,本地浏览器打开 http://localhost 或者 http://127.0.0.1 将会看到欢迎页面,这证明nginx在本地运行良好,如果没有出现欢迎页面,请检查是否有其他的web服务器占用了80端口。
4. 安装Django,装完我回头看了下,好像这个也可以通过pip来安装,不过我是通过下载包来安装的,
官方地址:https://www.djangoproject.com/download/
最新版本当前是:https://www.djangoproject.com/m/releases/2.0/Django-2.0.6.tar.gz
PIP命令行方式安装:
pip install Django==2.0.6
我是下载回来安装的
下载完成后解压,例如解压到D:\MyPython\Django\Django-1.10.8\
运行cmd打开控制台窗口,切换到 D:\MyPython\Django\Django-1.10.8\
D:\MyPython\Django\Django-1.10.8>python setup.py install
一堆字符闪过之后,没有错误提示,
安装完成后,启动python,输入>>>import django;print(django.VERSION)
看到如下提示:(1, 10, 8, ‘final‘, 0) 表示Django安装成功了
然后把Django的路径加入到Windows的系统变量Path里,就可以直接在任意路径下执行相关脚本了:
D:\WinPython\python-3.5.3.amd64\Lib\site-packages\Django-1.10.8-py3.5.egg\django\bin> 是我的电脑上的Django安装好的所在文件夹
D:\WinPython\python-3.5.3.amd64\Lib\site-packages\Django-1.10.8-py3.5.egg\django\bin>python django-admin.py -h
Type ‘django-admin.py help <subcommand>‘ for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured
. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
5. 配置Nginx的nginx.conf文件
打开nginx.conf文件,加入了以下内容,然后关闭重启Nginx服务器即可,web监听3333端口,fastcgi监听8090端口,root是web根目录,我这里就继续使用phpstudy的web目录了:
server {
listen 3333;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root D:/phpStudy/WWW;
index index.html index.htm;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass 127.0.0.1:8090;
}
}
6.用任意文本编辑器编写一个HelloWorld.py的文件,放在web根目录下,我是放在 D:/phpStudy/WWW/ 下,注意一下python的严格缩进语法
#!/usr/bin/python
# encoding : utf-8
from flup.server.fcgi import WSGIServer
def myapp(environ, start_response):
start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/plain‘)])
return [‘Hello World!\n‘]
if __name__ == ‘__main__‘:
WSGIServer(myapp,bindAddress=(‘127.0.0.1‘,8090)).run()
执行下面命令启动helloWorld,D:\phpStudy\WWW\helloWorld.py
python D:\phpStudy\WWW\helloWorld.py 9001 fastcgi
出来一个黑窗口,不要关闭掉它,用你的浏览器输入 http://127.0.0.1:3333/ 因为我的电脑上80和8080都被占用了,所以我用是3333端口
就可以看到正确结果了。