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

Python029

在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安装包(对应自己电脑系统位数),window7系统.

版本:python-2.7.13(python-2.7.13.amd64.msi)

1. 下载和自己本机匹配的python版本,注意自己的电脑是64位的还是32的!

2.下载好python安装包后双击python安装包,选择install  just for me,(当然,也可以选择install for all users)点击下一步。

3.选择python要安装的盘符,(记住自己选择的安装位置,方便添加环境变量,比如我安装在C://Python27)点击next下一步。

4.选择python索要安装的文件,默认全部安装,可以不选择,直接点击next。

5.稍等一小会儿会就会安装成功,然后点击finsh安装完成。

6.添加环境变量:

右键计算机(我的电脑), 选择属性。

选择高级系统配置,点击环境变量,选择path路径,然后双击打开Path, 把刚刚安装python时选择的路径放在path路径里面,比如我的C://Python27,就要在原来的Python后面添加C://Python27 注意这个时候每个路径后面都有一个分号‘;’。

7.然后在cmd命令行下键入python,若出现python的版本信息,即为安装完成!

win7系统还是win8系统,其安装方法都是一样的,希望可以帮助到你!

转自 Xiongpq

http://www.cnblogs.com/xiongpq/p/3381069.html

略有补充(可能出现错误及解决办法)

环境:

CentOS X64 6.4

nginx 1.5.6

Python 2.7.5

正文:

一:安装需要的类库及Python2.7.5

安装必要的开发包

yum groupinstall "Development tools"

yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

CentOS 自带Python2.6.6,但我们可以再安装Python2.7.5:

cd ~

wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2

tar xvf Python-2.7.5.tar.bz2

cd Python-2.7.5

./configure --prefix=/usr/local

make && make altinstall

安装完毕后,可是使用”python2.7”命令进入python2.7的环境。

二:安装Python包管理

easy_install包 https://pypi.python.org/pypi/distribute

方便安装Python的开发包

cd ~

wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gz

tar xf distribute-0.6.49.tar.gz

cd distribute-0.6.49python2.7 setup.py install

easy_install --version

红色部分必须是“python2.7”,否则将安装到默认的2.6环境内。

pip包 https://pypi.python.org/pypi/pip

安装pip的好处是可以pip list、pip uninstall 管理Python包, easy_install没有这个功能,只有uninstall

easy_install pip

pip --version

三:安装uwsgi

uwsgi: https://pypi.python.org/pypi/uWSGI

uwsgi参数详解: http://uwsgi-docs.readthedocs.org/en/latest/Options.html

pip install uwsgi

uwsgi --version

测试uwsgi是否正常:

新建test.py文件,内容如下:

def application(env, start_response):

start_response('200 OK', [('Content-Type','text/html')])

return "Hello World"

然后在终端运行:

uwsgi --http :8001 --wsgi-file test.py

在浏览器内输入: http://你购买的云主机公网ip:8001 ,看是否有“Hello World”输出,若没有输出,请检查你的安装过程。

四:安装django

pip install django

测试django是否正常,运行:

django-admin.py startproject demosite

cd demositepython2.7 manage.py runserver 0.0.0.0:8002

在浏览器内输入: http://1 你购买的云主机公网 :8002,检查django是否运行正常。

五:安装nginx

cd ~

wget http://nginx.org/download/nginx-1.5.6.tar.gz

tar xf nginx-1.5.6.tar.gz

cd nginx-1.5.6

./configure --prefix=/usr/local/nginx-1.5.6 \

--with-http_stub_status_module \

--with-http_gzip_static_module

make && make install

安装Nginx时报错

错误提示:

./configure: error: the HTTP rewrite module requires the PCRE library.

安装pcre-devel与openssl-devel解决问题

yum -y install pcre-devel openssl openssl-devel

./configure --prefix=/usr/local/nginx

make

make install

六:配置uwsgi

uwsgi支持ini、xml等多种配置方式,但个人感觉ini更方便:

在/ect/目录下新建uwsgi9090.ini,添加如下配置:

[uwsgi]

socket =

master = true         //主进程

vhost = true          //多站模式

no-stie = true        //多站模式时不设置入口模块和文件

workers = 2           //子进程数

reload-mercy = 10

vacuum = true         //退出、重启时清理文件

max-requests = 1000

limit-as = 512

buffer-sizi = 30000

pidfile = /var/run/uwsgi9090.pid    //pid文件,用于下面的脚本启动、停止该进程

daemonize = /website/uwsgi9090.log

设置uwsgi开机启动,在/etc/init.d/目录下新建uwsgi9090文件,内容如下:

uwsgi9090

#! /bin/sh

# chkconfig: 2345 55 25

# Description: Startup script for uwsgi webserver on Debian. Place in /etc/init.d and

# run 'update-rc.d -f uwsgi defaults', or use the appropriate command on your

# distro. For CentOS/Redhat run: 'chkconfig --add uwsgi'

### BEGIN INIT INFO

# Provides:          uwsgi

# Required-Start:    $all

# Required-Stop:     $all

# Default-Start:     2 3 4 5

# Default-Stop:      0 1 6

# Short-Description: starts the uwsgi web server

# Description:       starts uwsgi using start-stop-daemon

### END INIT INFO

# Author:   licess

# website:  http://lnmp.org

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DESC="uwsgi daemon"

NAME=uwsgi9090

DAEMON=/usr/local/bin/uwsgi

CONFIGFILE=/etc/$NAME.ini

PIDFILE=/var/run/$NAME.pid

SCRIPTNAME=/etc/init.d/$NAME

set -e

[ -x "$DAEMON" ] || exit 0

do_start() {

$DAEMON $CONFIGFILE || echo -n "uwsgi already running"

}

do_stop() {

$DAEMON --stop $PIDFILE || echo -n "uwsgi not running"

rm -f $PIDFILE

echo "$DAEMON STOPED."

}

do_reload() {

$DAEMON --reload $PIDFILE || echo -n "uwsgi can't reload"

}

do_status() {

ps aux|grep $DAEMON

}

case "$1" in

status)

echo -en "Status $NAME: \n"

do_status

start)

echo -en "Starting $NAME: \n"

do_start

stop)

echo -en "Stopping $NAME: \n"

do_stop

reload|graceful)

echo -en "Reloading $NAME: \n"

do_reload

*)

echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2

exit 3

esac

exit 0

uwsgi9090

然后在终端执行:

-- 添加服务

chkconfig --add uwsgi9090

-- 设置开机启动

chkconfig uwsgi9090 on

七:设置nginx

找到nginx的安装目录/usr/local/nginx,打开conf/nginx.conf文件,修改server配置

server {

listen       80

server_name  localhost

location / {

include  uwsgi_params

uwsgi_pass                //必须和uwsgi中的设置一致

index  index.html index.htm

client_max_body_size 35m

}

}

注意 :安装django这步中startproject的demosite路径,需要和nginx.conf中 uwsgi_param UWSGI_CHDIR /demosite对应上。

设置nginx开机启动,在/etc/init.d/目录下新建nginx文件,内容如下:

nginx

#!/bin/sh

#

# nginx - this script starts and stops the nginx daemon

#

# chkconfig:   - 85 15

# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \

#               proxy and IMAP/POP3 proxy server

# processname: nginx

# config:      /usr/local/nginx/conf/nginx.conf

# pidfile:     /var/run/nginx.pid

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ "$NETWORKING" = "no" ] && exit 0

nginx="/opt/nginx-1.5.6/sbin/nginx"

prog=$(basename $nginx)

NGINX_CONF_FILE="/opt/nginx-1.5.6/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

start() {

[ -x $nginx ] || exit 5

[ -f $NGINX_CONF_FILE ] || exit 6

echo -n $"Starting $prog: "

daemon $nginx -c $NGINX_CONF_FILE

retval=$?

echo

[ $retval -eq 0 ] && touch $lockfile

return $retval

}

stop() {

echo -n $"Stopping $prog: "

killproc $prog -QUIT

retval=$?

echo

[ $retval -eq 0 ] && rm -f $lockfile

return $retval

}

restart() {

configtest || return $?

stop

sleep 1

start

}

reload() {

configtest || return $?

echo -n $"Reloading $prog: "

killproc $nginx -HUP

RETVAL=$?

echo

}

force_reload() {

restart

}

configtest() {

$nginx -t -c $NGINX_CONF_FILE

}

rh_status() {

status $prog

}

rh_status_q() {

rh_status >/dev/null 2>&1

}

case "$1" in

start)

rh_status_q && exit 0

$1

stop)

rh_status_q || exit 0

$1

restart|configtest)

$1

reload)

rh_status_q || exit 7

$1

force-reload)

force_reload

status)

rh_status

condrestart|try-restart)

rh_status_q || exit 0

*)

echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

exit 2

esac

nginx

然后在终端执行:

-- 添加服务

chkconfig --add nginx

-- 设置开机启动

chkconfig nginx on

八:测试

OK,一切配置完毕,在终端运行

service uwsgi9090 start

service nginx start

错误1:

[root@VM_23_251_centos nginx-1.5.6]# service uwsgi9090 start

env: /etc/init.d/uwsgi9090: Permission denied

[root@VM_23_251_centos nginx-1.5.6]# service nginx start

env: /etc/init.d/nginx: Permission denied

解决办法1:把可执行勾选上

错误2:

[root@VM_23_251_centos ~]# sudo service nginx start

env: /etc/init.d/nginx: No such file or directory

解决办法1:

Be careful what text editors you use on your local Windows computer. Some create Windows-style line endings (\r\n, CR-LF), which cause problems like this on Unix-like systems such as Linux or Mac OS X.

If you use such an editor, adjust its settings so that it creates files with Unix-style line endings. If the editor has no such setting, you should not be using it at all.

文本格式由windows 改为unix版

错误3:

*** WARNING: Can't find section "uwsgi" in INI configuration file /etc/uwsgi9090.ini ***

解决办法3:

在浏览器输入: http:// 云主机公网ip,恭喜你可以看到django的“It work”了~

九:多站配置

我采用运行多个uwsgi服务的方法来实现多个站点。

重复第六步,创建uwsgi9091.ini,并相应修改文件中的

socket = 127.0.0.1:9091

pidfile = /var/run/uwsgi9091.pid

daemonize = /website/uwsgi9091.log

并创建服务uwsgi9091,设置开机启动。

然后修改nginx的配置文件为:

nginx

然后我们就可以通过 http://127.0.0.1:1300 来访问新的网站了。

十:其他配置

防火墙设置

CentOS默认关闭外部对80、3306等端口的访问,所以要在其他计算机访问这台服务器,就必须修改防火墙配置,打开/etc/sysconfig/iptables

在“-A INPUT –m state --state NEW –m tcp –p –dport 22 –j ACCEPT”,下添加:

-A INPUT -m state --state NEW -m tcp -p -dport 80 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p -dport 3306 -j ACCEPT

然后保存,并关闭该文件,在终端内运行下面的命令,刷新防火墙配置:

service iptables restart

安装Mysqldb

yum -y install mysql-develeasy_install-2.7 MySQL-python

注意红色部分,easy_install-2.7,否则它将默认安装到Python2.6环境内。

------------------------------------------------------------------------------------------------------------------

2014年12月02日添加:

CentOS 7中默认使用Firewalld做防火墙,所以修改iptables后,在重启系统后,根本不管用。

Firewalld中添加端口方法如下:

firewall-cmd --zone=public --add-port=3306/tcp --permanent

firewall-cmd --reload