简体   繁体   English

使用uWSGI在Nginx上安装Django的正确方法

[英]Proper Way to Setup Django on Nginx using uWSGI

What I'm trying to do is to setup django with uwsgi on nginx 1.6.3 with http binding to port 8000 on centos7. 我想做的是在nginx 1.6.3上用uwsgi设置django,将http绑定到centos7上的8000端口。 I've looked at a blog 我看过一个博客

and a document 文件

The blog is pretty confusing and it does not work for my version of nginx; 该博客非常令人困惑,不适用于我的Nginx版本; but the document is decent and all the steps lead me to a successful execution using uwsgi but I'm having problems with nginx. 但是文档很不错,所有步骤都使我使用uwsgi成功执行,但是我在使用nginx时遇到了问题。

First of all, I can only run nginx as a sudoer (root) so I made a virtualenv for the root user. 首先,我只能以sudoer(root)身份运行nginx,因此我为root用户创建了virtualenv。 Then, I made my nginx config at: /etc/nginx/sites-enabled/mysite_nginx.conf and did a soft link: 然后,我在/etc/nginx/sites-enabled/mysite_nginx.conf中进行了Nginx配置,并进行了软链接:

sudo ln -s /etc/nginx/sites-enabled/mysite_nginx.conf /etc/nginx/mysite_nginx.conf 须藤ln -s /etc/nginx/sites-enabled/mysite_nginx.conf /etc/nginx/mysite_nginx.conf

but nginx does not pick it after nginx restart. 但是nginx重新启动后nginx不会选择它。 Does anyone know why? 有人知道为什么吗?

To continue, I decided to rename /etc/nginx/mysite_nginx.conf to /etc/nginx/nginx.conf to force it to get picked up. 为了继续,我决定将/etc/nginx/mysite_nginx.conf重命名为/etc/nginx/nginx.conf以强制使用它。 and my django app is at: /usr/share/nginx/firstsite owned by root and my nginx user is nginx. 我的django应用位于:/ usr / share / nginx / firstroot拥有,我的nginx用户是nginx。 Here is my nginx.conf: 这是我的nginx.conf:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    upstream django {
        server 127.0.0.1:8001;
    }

    server {
        listen       8000 default_server;
        listen       [::]:8000 default_server;
        server_name  _;
        root         /usr/share/nginx/firstsite;

        client_max_body_size 300M;   # adjust to taste

        location /media  {
            alias /usr/share/nginx/firstsite/media;
        }

        location /static {
            alias /usr/share/nginx/firstsite/static;
        }

        location / {
            uwsgi_pass  django;
            include     /usr/share/nginx/firstsite/uwsgi_params;
        }
    }
}

Also I do not understand how uwsgi is configured. 另外我不明白uwsgi是如何配置的。 I have a wusgi.ini that works. 我有一个wusgi.ini可以工作。 I just need to know where to put the uwsgi.ini file and whether it is possible to run nginx and uwsgi as a non-root user. 我只需要知道将uwsgi.ini文件放在哪里,以及是否可以以非root用户身份运行nginx和uwsgi。

I've run the nginx test and it says that my configuration file is ok. 我已经运行了nginx测试,它说我的配置文件正常。 Lastly, I get an error when trying to start nginx. 最后,尝试启动Nginx时出现错误。 It says it does not have permissions to bind to port 8000. so I read this post 它说它没有绑定到端口8000的权限。所以我读了这篇文章

then I tried: 然后我尝试:

yum -y install policycoreutils-python
semanage port -l | grep http_port_t
semanage port -a -t http_port_t  -p tcp 8000

and I get an error: 我得到一个错误:

ValueError: Port tcp/8000 already defined

I know there are several questions and problems here but the goal is pretty clear. 我知道这里有几个问题,但是目标很明确。

It could be... 它可能是...

a) the system firewall is block python/nginx/bash etc. from binding to port 80 or a)系统防火墙阻止python / nginx / bash等绑定到端口80或

b) you need to run it with sudo . b)您需要使用sudo运行它。 This is because Unix-like OSes require superuser privileges for an application to bind to a well-known port . 这是因为类Unix的操作系统需要超级用户特权才能使应用程序绑定到知名端口

nginx will usually run as root. nginx通常将以root身份运行。 uwsgi can also run as root or as a normal user, normally you'd have it run as root, and configure uwsgi apps to run as non-root users (you can specify that in the uwsgi config file for your app) - check out the link @doniyor posted for more details on how to set up Vassals. uwsgi也可以以root用户或普通用户身份运行,通常您需要以root用户身份运行它,并将uwsgi应用程序配置为以非root用户身份运行(您可以在应用程序的uwsgi配置文件中指定它)-签出链接@doniyor发布了有关如何设置Vassals的更多详细信息。

You can also set it up so that if you change a file (usually the wsgi file for the app), uwsgi will restart that app. 您也可以对其进行设置,以便在更改文件(通常是应用程序的wsgi文件)时,uwsgi将重新启动该应用程序。 You'd usually use the touch command on that file, and as that file is in a normal user account, you can easily restart your app without having to be root. 您通常会在该文件上使用touch命令,并且由于该文件位于普通用户帐户中,因此您无需root就可以轻松地重新启动应用程序。

As for reason nginx isn't picking your file up is that the file needs to be in /etc/nginx/sites-enabled , the ln command gets the source in the -s switch, so esentially you're linking files the other way around. 由于nginx不能提取文件的原因是该文件必须位于/etc/nginx/sites-enabled ,所以ln命令在-s开关中获取源代码,因此本质上您是用其他方式链接文件周围。

Permission to bind to unreserved ports shouldn't be a problem even for normal users. 即使对于普通用户,绑定到未保留端口的权限也不应该成为问题。 It is possible that something's already running on that port, try something different, like 8001. 可能已经有某些内容在该端口上运行,请尝试其他不同的内容,例如8001。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM