在Linux系统上怎么通过uWSGI配置Nginx+Python环境

在Linux系统上怎么通过uWSGI配置Nginx+Python环境,第1张

1.安装ubuntu有uwsgi的ppa:

add-apt-repository ppa:stevecrozz/ppa

apt-get update

apt-get install uwsgi

2. 用uwsgi代替mod_wsgi

Nginx 的整体配置这里不说了,假设已经明白 Nginx的基本配置,那么uwsgi就类似这么配置:

location / {

include uwsgi_params

uwsgi_pass 127.0.0.1:9090

}

再比如django就是:

.......

from django.core.handlers.wsgi import WSGIHandler

application = WSGIHandler()

然后运行uwsgi监听9090,其中-w后跟模块名,也就是刚才配置的myapp

uwsgi -s :9090 -w myapp

运行网站发现已经部署完成了。

3.uwsgi的参数

以上是单个project的最简单化部署,uwsgi还是有很多令人称赞的功能的,例如:

并发4个线程:

uwsgi -s :9090 -w myapp -p 4

主控制线程+4个线程:

uwsgi -s :9090 -w myapp -M -p 4

执行超过30秒的client直接放弃:

uwsgi -s :9090 -w myapp -M -p 4 -t 30

限制内存空间128M:

uwsgi -s :9090 -w myapp -M -p 4 -t 30 --limit-as 128

服务超过10000个req自动respawn:

uwsgi -s :9090 -w myapp -M -p 4 -t 30 --limit-as 128 -R 10000

后台运行等:

uwsgi -s :9090 -w myapp -M -p 4 -t 30 --limit-as 128 -R 10000 -d uwsgi.log

4.为uwsgi配置多个站点

为了让多个站点共享一个uwsgi服务,必须把uwsgi运行成虚拟站点:去掉“-w myapp”加上”–vhost”:

uwsgi -s :9090 -M -p 4 -t 30 --limit-as 128 -R 10000 -d uwsgi.log --vhost

然后必须配置virtualenv,virtualenv是Python的一个很有用的虚拟环境工具,这样安装:

apt-get install Python-setuptools

easy_install virtualenv

然后设置一个/多个app基准环境:

virtualenv /var/www/myenv

应用环境,在此环境下安装的软件仅在此环境下有效:

source /var/www/myenv/bin/activate

pip install django

pip install mako

...

最后配置nginx,注意每个站点必须单独占用一个server,同一server不同location定向到不同的应用不知为何总是失败,估计也 算是一个bug。

server {

listen 80

server_name app1.mydomain.com

location / {

include uwsgi_params

uwsgi_pass 127.0.0.1:9090

uwsgi_param UWSGI_PYHOME /var/www/myenv

uwsgi_param UWSGI_SCRIPT myapp1

uwsgi_param UWSGI_CHDIR /var/www/myappdir1

}

}

server {

listen 80

server_name app2.mydomain.com

location / {

include uwsgi_params

uwsgi_pass 127.0.0.1:9090

uwsgi_param UWSGI_PYHOME /var/www/myenv

uwsgi_param UWSGI_SCRIPT myapp2

uwsgi_param UWSGI_CHDIR /var/www/myappdir2

}

}

这样,重启nginx服务,两个站点就可以共用一个uwsgi服务了。

1.python和django的环境搭建

(1)下载anaconda3并安装

wget https://3230d63b5fc54e62148e-c95ac804525aac4b6dba79b00b39d1d3.ssl.cf1.rackcdn.com/Anaconda3-2.3.0-Linux-x86_64.sh

sh Anaconda3-2.3.0-Linux-x86_64.sh

一路enter键,然后提示是否加入到环境变量时,输入yes即可。

(2)安装django

直接pip install django

安装成功之后就可以新建项目

django-admin startproject demosite

cd demosite

python manage.py startapp blog

python manage.py migrate (要执行这个命令,让django生成可运行的app,否则后面使用uwsgi会报错)

(3)运行django

python manage.py runserver

curl 127.0.0.1:8000进行如果可以正常访问,就说明django安装成功。

2.安装uwsgi

(1)centOS

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

pip install uwsgi

uwsgi --version#查看 uwsgi 版本

(2)test.py

然后,Run uWSGI:

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

def application(env, start_response):

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

return ["Hello World"] # python2

#return [b"Hello World"] # python3

(3)ubuntu可以能会出现错误:

如果出现错误,!!! no internal routing support, rebuild with pcre support !!!

sudo apt-get install libpcre3 libpcre3-dev

sudo pip uninstall uwsgi

sudo apt-get remove uwsgi

sudo pip install uwsgi

(4)测试

1) 打开下面url,浏览器上应该显示hello world

curl http://127.0.0.1:8000 如果安装httpie模块的话使用http http://127.0.0.1:8000

如果显示正确是Hello World,说明上面的环节是畅通的

2) 测试django

默认使用django新建工程会在app下面生成一个wsgi.py的文件

uwsgi --http :8000 --wsgi-file wsgi.py 直接这样也会报错

uwsgi --http :8000 --wsgi-file appname/wsgi.py

打开浏览器输入http://127.0.0.1:8000 如果现实正确说明web client <-->uwsgi <--->django是畅通的

3. 安装配置nginx

(1)安装

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

tar xf nginx-1.9.5.tar.gz

cd nginx-1.9.5

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module

make &&make install

或者参考

http://cantgis.blog.51cto.com/5788192/1540004

(2)配置文件

vi /usr/local/nginx/conf/nginx.conf

一般来说加入个server就OK了

参考配置如下

user root

worker_processes 1

#error_log logs/error.log

#error_log logs/error.log notice

#error_log logs/error.log info

pidlogs/nginx.pid

events {

use epoll

worker_connections 65535

}

http {

include mime.types

default_type application/octet-stream

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '

# '$status $body_bytes_sent "$http_referer" '

# '"$http_user_agent" "$http_x_forwarded_for"'

#access_log logs/access.log main

sendfileon

#tcp_nopush on

#keepalive_timeout 0

keepalive_timeout 65

#gzip on

server {

listen 8099

server_name 10.117.52.157 ##对外访问的IP和端口号

access_log /tmp/cms/access.log

error_log /tmp/cms/error.log

#charset koi8-r

#access_log logs/host.access.log main

location / {

includeuwsgi_params

uwsgi_pass 127.0.0.1:8088

uwsgi_read_timeout 300

}

#error_page 404 /404.html

# redirect server error pages to the static page /50x.html

#

#error_page 500 502 503 504 /registration/500.html

#location = /registration/500.html {

#root html

#}

location /static/ {

alias /root/cms/cms/static/

index index.html index.htm

}

location /media/ {

alias /root/cms/cms/media/

}

}

}

(3)运行and 重启

/usr/local/nginx/sbin/nginx

启动: nginx start

重启: nginx -s reload

4. 使用uwsgi的配置文件运行django

在确保nginx运行之后,就可以通过uwsgi来运行django了。nginx 在最外层接收请求,静态的自己处理,动态的通过socket端口交给uwsgi来处理。

配置文件内容如下

[uwsgi]

socket=:8088 #要和nginx对应的IP和端口号一致

chdir=/root/cms/cms #APP的目录

module=cms.wsgi#wsgi.py文件位置

touch-reload=/root/cms/cms/reload #重启只要输入命令touch reload文件即可

processes=4

threads=2

daemonize=/tmp/cms/wsgi.log #日志文件位置

放在APP的上一级目录

直接运行uwsgi --ini uwsgi.ini 即可

uwsgi安装

ubuntu安装uwsgi遇到的问题

Command "/root/myenv/bin/python3.4 -c "import setuptools, tokenize__file__='/tmp/pip-build-7cr2or3v/uwsgi/setup.py'exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-omoei_c8-record/install-record.txt --single-version-externally-managed --compile --install-headers /root/myenv/include/site/python3.4/uwsgi" failed with error code 1 in /tmp/pip-build-7cr2or3v/uwsgi

(myenv)root@iZ94oziy87wZ:~# 12

上面的问题不知道原因,也没有给我太多错误提示。

python-dev已经安装。

索性直接安装源码。

直接 下载源码安装

*** uWSGI compiling embedded plugins ***

[x86_64-linux-gnu-gcc -pthread] plugins/python/python_plugin.o

In file included from plugins/python/python_plugin.c:1:0:

plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory

#include <Python.h>

^123456

这回算是知道问题在哪里了,Python.h

find /usr -name Pythond.h

只在Python2.7下面找到这个。

顿悟

apt-get install python3-dev

以上所有 *** 作均在virtualenv下面完成。


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

原文地址:https://54852.com/yw/7297008.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存