Python uwsgi初始配置操作几个问题
我是新建的vagrant虚拟机,新的utuntu 12.04系统,除了update
和upgrade
外没动过,然后刚刚安装完 uwsgi 和 nginx。我在
/etc/nginx/sites-available/
/etc/nginx/sites-enabled/
/etc/uwsgi/apps-available/
/etc/uwsgi/apps-enabled/
这三个目录下面添加了一些文件,nginx的sites-available和sites-enabled下面文件是link到一起的,uwsgi的apps-available下面则放了一个xml文件,和apps-enabled里面的文件link到一起。
下面是我执行uwsgi
命令的结果:
$ uwsgi
*** Starting uWSGI 1.0.3-debian (64bit) on [Thu Jan 29 12:43:23 2015] ***
compiled with version: 4.6.3 on 17 July 2012 02:26:54
current working directory: /home/vagrant/mypy-proj.com
detected binary path: /usr/bin/uwsgi-core
*** WARNING: you are running uWSGI without its master process manager ***
your memory page size is 4096 bytes
The -s/--socket option is missing and stdin is not a socket.
我想知道:
1. 这些输出是什么意思?
2. 上面那三个文件夹是安装完nginx
和uwsgi
之后就有的吗?还是我用vim
新建其中文件的时候,同时创建出来的?
3. 怎么让我在这三个文件夹里放的配置文件生效?貌似它们所在的路径是有权限限制的,那么我让它们生效的时候岂不是必须要用 sudo 了?
(但是我用 sudo 执行uwsgi --xml [xml配置文件名]
的话,随后从客户端访问这个 vagrant 虚拟服务器,会出现 502 错误,查看日志会发现是因为没有权限而无法访问 socket 文件。)
最好通过python pip安装uwsgi.
$sudo apt-get install python-dev
$sudo apt-get install python-pip
$sudo pip install pip --upgrade
$sudo apt-get install libpcre3 libpcre3-dev
$sudo apt-get install zlib1g-dev
$sudo apt-get install nginx-full
如果安装版本错误,先卸载:
$pip uninstall uwsgi
$sudo apt-get remove uwsgi
python 版本最好是python 2.7.*
pip的版本应该是最新版本。
查看pip 版本:
$pip --version
pip 6.0.7 from /usr/local/lib/python2.7/dist-packages (python 2.7)
接下来安装uwsgi。
$sudo pip install uwsgi
输出配置:
################# uWSGI configuration #################
pcre = True
kernel = Linux
malloc = libc
execinfo = False
ifaddrs = True
ssl = False
zlib = True
locking = pthread_mutex
plugin_dir = .
timer = timerfd
yaml = embedded
json = False
filemonitor = inotify
routing = True
debug = False
capabilities = False
xml = expat
event = epoll
############## end of uWSGI configuration #############
安装成功后看成uwsgi版本:
$uwsgi --version
2.0.9
这样就确保uwsgi的版本是最新版本了。
举例:
django进行配置:
$django-admin startproject hello
$python manage.py syncdb
$python manage.py runserver 0.0.0.0:8000
如果能够正常访问,那么可以测试uwsgi.
这里要通过django的wsgi启动,wsgi.py 在hello目录下面。
python
import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hello.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
$uwsgi --http :8000 --module hello.wsgi
显示出itworks!
配置django static目录,
在django settings,
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
然后运行
$python manage.py collectstatic
接下来进行配置nginx。
启动nginx:
$sudo /etc/init.d/nginx start
首先要确保nginx配置路径下面有uwsgi_params
地址:https://github.com/nginx/nginx/blob/master/conf/uwsgi_params
$cat /etc/nginx/uwsgi_params
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param UWSGI_SCHEME $scheme;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
在工程目录下新建一个mysite.conf.
nginx配置文件:
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name localhost; # substitute your machine's IP address or FQDN
charset utf-8;
access_log /root/project/hello/access_log;
error_log /root/project/hello/error_log;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
#location /media {
# alias /to/your/mysite/media; # your Django project's media files - amend as required
#}
location /static {
alias /root/project/hello/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass 127.0.0.1:3400;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
注意,由于django settings 里面会配置STATIC_URL
这样在nginx里面只能设置成,
location /static {
alias /root/project/hello/static; # your Django project's static files - amend as required
}
TIPS:
如果静态文件目录用户权限是root
drwsr-xr-x 3 root root 4096 2月 1 00:13 static/
则需要更改nginx.conf,添加
user root;
通过软连接:
$sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/mysite_nginx.conf
重启nginx
$sudo /etc/init.d/nginx restart
接下来配置uwsgi
新建一个hello_uwsgi.ini文件。
ini
# mysite_uwsgi.ini file [uwsgi] socket = 127.0.0.1:3400 # Django-related settings # the django project directory (full path) chdir = /root/project/hello # Django's wsgi file module = hello.wsgi # process-related settings # master master = true # maximum number of worker processes processes = 2 threads = 2 max-requests = 6000 # ... with appropriate permissions - may be needed chmod-socket = 664 # clear environment on exit vacuum = true
启动uwsgi
uwsgi --ini hello_uwsgi.ini
正常输出:
[uWSGI] getting INI configuration from hello_uwsgi.ini
*** Starting uWSGI 2.0.9 (64bit) on [Sat Jan 31 19:27:23 2015] ***
compiled with version: 4.8.2 on 31 January 2015 19:21:57
os: Linux-2.6.32-042stab092.2 #1 SMP Tue Jul 8 10:35:55 MSK 2014
nodename: localhost.localdomain
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 2
current working directory: /root/project/hello
detected binary path: /usr/local/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /root/project/hello
your processes number limit is 175957
your memory page size is 4096 bytes
detected max file descriptor number: 4096
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 127.0.0.1:3400 fd 3
Python version: 2.7.6 (default, Mar 22 2014, 23:03:41) [GCC 4.8.2]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1dc6ef0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 218280 bytes (213 KB) for 2 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x1dc6ef0 pid: 21844 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 21844)
spawned uWSGI worker 1 (pid: 21845, cores: 1)
spawned uWSGI worker 2 (pid: 21846, cores: 1)
如何设置uwsgi后台运行:
需要在mysite_uwsgi.ini
配置文件中添加
daemonize = /root/project/hello/uwsgi.log
这样就会吧日志打印到uwsgi.log中。
通过查 nginx
的access_log 和 error_log 进行调试错误。
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd19030.html
相关文章 Recommend
- • 2019年3月最新消息: Python 3.4.10 现已推出
- • [上海]招Python量化系统开发工程师
- • 优集品网络科技有限公司招Python中/高级工程师
- • 爱因互动科技发展有限公司招募Python开发攻城狮
- • mozio招聘Python/Django工程师
- • Kavout金融科技公司招Python研发工程师
- • Python数组逆向输出,编程练习题实例四十
- • Python数组插入排序,编程练习题实例三十九
- • Python矩阵for循环应用,编程练习题实例三十八
- • Python操作Redis数据库方面的问题
- • 请python高手帮我看看这段python代码中函数setter的
- • Python什么方法可以快速将两个队列变成字典
必知PYTHON教程 Must Know PYTHON Tutorials
- • python 解释器
- • python idle
- • python dir函数
- • python 数据类型
- • python type函数
- • python 字符串
- • python 整型数字
- • python 列表
- • python 元组
- • python 字典
- • python 集合
- • python 变量
- • python print
- • python 函数
- • python 类定义
- • python import
- • python help
- • python open
- • python 异常处理
- • python 注释
- • python continue
- • python pass
- • python return
- • python global
- • python assert
- • python if语句
- • python break
- • python for循环
- • python while循环
- • python else/elif
- • lambda匿名函数
必知PYTHON模块 Must Know PYTHON Modules
- • os 模块
- • sys 模块
- • re 正则表达式
- • time 日期时间
- • pickle 持久化
- • random 随机
- • csv 模块
- • logging 日志
- • socket网络通信
- • json模块
- • urlparse 解析URL
- • urllib 模块
- • urllib2 模块
- • robotparser 解析
- • Cookie 模块
- • smtplib 邮件
- • Base64 编码
- • xmlrpclib客户端
- • string 文本
- • Queue 线程安全
- • math数学计算
- • linecache缓存
- • threading多线程
- • sqlite3数据库
- • gzip压缩解压
最新内容 NEWS
- • django app提供pv信息的方法是什么
- • Django项目版本升级如何操作?
- • django较多数据传递如何优雅的呈现
- • django1.7获取参数问题求助
- • Django1.7使用内置comment遇到问题
- • python mysql数据库做insert操作时报_mysql_ex
- • 关于python mysql的duplicate insert机制的疑问
- • pymongo使用insert函数批量插入被中断要怎么
- • Python程序员解决棘手问题的常用库
- • 求助关于restfull api接口几个问题
图文精华 RECOMMEND
-
django1.7获取参数问题求助
-
Python程序员解决棘手问题的常用库
-
求问str()同__str__原理上有什么不同
-
scrapy框架里面用link extractor怎么能
-
python {}.fromkeys创建字典append添加操
-
python3 类型Type str doesn't support th
热点文章 HOT
- 学习Python有什么好的书籍推荐?
- Python匿名函数 Lambda表达式作用
- Python与Java、C、Ruby、PHP等编程语言有什么
- Python 正则中文网页字符串提取问题
- 如何为实时性应用存取经纬度?django my
- 想用python做个客户端,在二维码登录这个地
- 有让IDE可识别Python函数参数类型的方法吗
- Python字符串转换成列表正则疑问