在Windows系统上搭建Nginx+Python+MySQL环境的教程

在Windows系统上搭建Nginx+Python+MySQL环境的教程,第1张

1 安装nginx

下载windows上的nginx最新版本,/en/download.html。

解压后即可。

运行nginx.exe后本地打开localhost,将会看到欢迎页面,这证明nginx在本地运行良好,如果没有出现欢迎页面,请检查是否有进程占用了80端口。

2 安装flup

下载对应版本的flup,这里下载flup3.x版本,适合python3.2,下载地址:https:///chxanders/flup3

解压(比如解压到D:flup)

安装(进入到python的安装路径,然后执行下面的命令

1 python setup.py install )

!注意,如果提示缺少setuptools,安装distribute-0.6.49.tar.gz,安装方法和flup安装一样,下载地址:https:///pypi/distribute/0.6.49

3 安装Mysql

在这里我使用的是5.1版本。在win系统上双击安装文件,下一步下一步完成。下载地址:/downloads/

4 安装数据库驱动

下载用于win上对应py版本的的python-mysql驱动,双击安装即可。下面的下载地址是3.2:

/wangqc/distribute-0.6.49.zip

5 配置服务器

首先需要修改nginx的配置文件nginx.conf。

找到:

1 2 3 4 location / { root htmlindex index.html index.htm} 在里面加上:

1 2 3 4 5 6 7 8 9 10 11 12 # host and port to fastcgi server fastcgi_pass 127.0.0.1:55880fastcgi_param PATH_INFO $fastcgi_script_namefastcgi_param REQUEST_METHOD $request_methodfastcgi_param QUERY_STRING $query_stringfastcgi_param SERVER_NAME $server_namefastcgi_param SERVER_PORT $server_portfastcgi_param SERVER_PROTOCOL $server_protocolfastcgi_param CONTENT_TYPE $content_typefastcgi_param CONTENT_LENGTH $content_lengthfastcgi_pass_header Authorizationfastcgi_intercept_errors off然后测试该配置文件是否正确,在cmd中切换到nginx安装目录里,输入

1 nginx.exe -t 即可开始对配置文件测试,如果提示成功,说明配置正确,

这是可以结束掉任务管理器中所有的nginx.exe进程,重新运行ngin.exe重启nginx服务。

6 运行Server.py

在cmd下切换到项目目录,输入命令

1 python Server.py runfcgi method=threaded host=127.0.0.1 port=55880 注意,

这条命令只能用来启动项目,如果出现错误并不会给出提示。下面是正确的情况.

用浏览器打开127.0.0.1:8080 测试一下,项目是不是已经跑起来了(第一次运行等待的时间稍长,请耐心等待)。

7 一些注意事项

(1)、首先应该改的地方,这个大家应该都知道:

1 DEBUG = TEMPLATE_DEBUG = False#将debug设置为False (2)、改一下ALLOWED_HOSTS,我死死的就将这个给忘了,我的`配置:

1 2 3 4 if DEBUG:#根据DEBUG来确定 ALLOWED_HOSTS = [""] else: ALLOWED_HOSTS = ["localhost","127.0.0.1"] (3)、重头戏来了,配置nginx:

这里相关的代码网上很多,但是几乎都没有说明一件事:

配置location ~ ^/static/ 的位置,一定要在location ~ ^/的前面,而且不能单纯的使用 location /static/ 或者location /static ,不然,static文件夹中的静态文件都不能加载!!!

ps:提起这个,慢慢的泪啊,就错在这里了。

嫌麻烦可以写成批处理,运行的时候双击一下就好了。

使用Python对Mysql进行 *** 作,前提是安装了python-Mysql的安装包,安装的方法有多种,可以使用easy_install或者pip 或者是源码进行安装。

下面介绍下如何使用Python对Mysql进行 *** 作,下面是一些简单的例子:使用Python对Mysql数据库进行 *** 作

(1).使用Python连接MySQL:

1 import MySQLdb2 3 try:4 conn = MySQLdb.connect(host="localhost",,user='root',passwd="sina.com",db="mysql",port=3307)5 except MySQLdb.OperationalError,e:6 print "the error msg is :",e

(2).使用Python查询MySQL数据:

1 import MySQLdb 2 3 try: 4 conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test',port=3306) 5 cur=conn.cursor() 6 cur.execute('select * from user') 7 cur.close() 8 conn.close() 9 except MySQLdb.Error,e:10 print "Mysql Error %d: %s" % (e.args[0], e.args[1])

(3).使用Python添加和更新MySQL数据:

1 import MySQLdb 2 3 try: 4 conn=MySQLdb.connect(host='localhost',user='root',passwd='root',port=3306) 5 cur=conn.cursor() 6 7 cur.execute('create database if not exists python') 8 conn.select_db('python') 9 cur.execute('create table test(id int,info varchar(20))')10 11 value=[1,'hi rollen']12 cur.execute('insert into test values(%s,%s)',value)13 14 values=[]15 for i in range(20):16 values.append((i,'hi rollen'+str(i)))17 18 cur.executemany('insert into test values(%s,%s)',values)19 20 cur.execute('update test set info="I am rollen" where id=3')21 22 conn.commit()23 cur.close()24 conn.close()25 26 except MySQLdb.Error,e:27 print "Mysql Error %d: %s" % (e.args[0], e.args[1])

import MySQLdb try:

conn=MySQLdb.connect(host='localhost',user='root',passwd='root',port=3306)

cur=conn.cursor()

conn.select_db('python')

count=cur.execute('select * from test')

print 'there has %s rows record' % count

result=cur.fetchone()

print result

print 'ID: %s info %s' % result

results=cur.fetchmany(5)

for r in results:

print r

print '=='*10cur.scroll(0,mode='absolute')

results=cur.fetchall()

for r in results:

print r[1]

conn.commit()

cur.close()

conn.close() except MySQLdb.Error,e:

print "Mysql Error %d: %s" % (e.args[0], e.args[1])

(4).经常使用的一些API方法:

1 commit() 提交

2 rollback() 回滚

3 cursor用来执行命令的方法:

4 callproc(self, procname, args):用来执行存储过程,接收的参数为存储过程名和参数列表,返回值为受影响的行数

5 execute(self, query, args):执行单条sql语句,接收的参数为sql语句本身和使用的参数列表,返回值为受影响的行数

6 executemany(self, query, args):执行单挑sql语句,但是重复执行参数列表里的参数,返回值为受影响的行数

7 nextset(self):移动到下一个结果集

8 cursor用来接收返回值的方法:

9 fetchall(self):接收全部的返回结果行.

10 fetchmany(self, size=None):接收size条返回结果行.如果size的值大于返回的结果行的数量,则会返回cursor.arraysize条数据.

13 fetchone(self):返回一条结果行.

14 scroll(self, value, mode='relative'):移动指针到某一行.如果mode='relative',则表示从当前所在行移动value条,如果 mode='absolute',则表示从结果集的第一行移动value条。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/6714173.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存