首页 > uwsgi初始配置问题

uwsgi初始配置问题

我是新建的vagrant虚拟机,新的utuntu 12.04系统,除了updateupgrade外没动过,然后刚刚安装完 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. 上面那三个文件夹是安装完nginxuwsgi之后就有的吗?还是我用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目录下面。

pythonimport 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 进行调试错误。

【热门文章】
【热门文章】