怎样搭建Django服务器环境?

本人Django小白一枚,想在云主机上搭建一个Django服务器环境,可是最后总是报错,如何解决?
具体环境配置如下:centos 7.2/python 2.7/django 1.11.3/uWSG I2.0/Nginx 1.12
django项目路径:/root/mysite
uwsgi配置文件:/etc/uwsgi9090.ini
Nginx配置文件:/usr/local/nginx/conf/nginx.conf
配置步骤完全按照Django菜鸟教程“Django Nginx+uwsgi 安装配置”来做的,可是最后在浏览器中验证的时候总是报“Internal Server Error”,不知道哪里出错了。请高人帮助解决一下。

阅读 8.4k
9 个回答

1、先看看python及django的环境是否正常

python manage.py runserver 0.0.0.0:9090

2、运行uwsgi,同时查看uwsgi日志是否有报错

uwsgi --ini /etc/uwsgi9090.ini

3、再运行nginx,同时查看nginx日志是否有报错

nginx -c /usr/local/nginx/conf/nginx.conf

如还不能解决,把你的配置文件贴出来

既然是小白完全没必要弄这么复杂的服务器环境, 又是nginx 又是 uwsgi 的, 我跟你说, 不用这么这么麻烦:

django-admin.py startproject learn_django

进入learn_django目录运行命令:

python manage.py runserver 0.0.0.0:8000

就可以在浏览器 http://localhost:8000 就能看到了.
参考链接https://www.hongweipeng.com/i...

settings.pyALLOWED_HOSTS = ['*']

uwsgi配置文件/etc/uwsgi9090.ini内容如下:
[uwsgi]
socket = 127.0.0.1:9090
master = true //主进程
vhost = true //多站模式
no-site = true //多站模式时不设置入口模块和文件
workers = 2 //子进程数
reload-mercy = 10
vacuum = true //退出、重启时清理文件
max-requests = 1000
limit-as = 512
buffer-size = 30000
pidfile = /var/run/uwsgi9090.pid //pid文件,用于下面的脚本启动、停止该进程
daemonize = /website/uwsgi9090.log


nginx配置文件/usr/local/nginx/conf/nginx.conf内容如下:(斜体字为新加部分,其他部分未改动)

    location / {
        *include  uwsgi_params;
        uwsgi_pass  127.0.0.1:9090;
        uwsgi_param UWSGI_SCRIPT mysite.wsgi;
        uwsgi_param UWSGI_CHDIR /root/mysite;
        client_max_body_size 35m;*

        root   html;
        index  index.html index.htm;
    }

  

如果觉得阅读不方便 http://www.chenxm.cc/post/275...
本文采用uwsgi+nginx来部署Django。

这种方式是将nginx作为服务器前端,将接受web所有的请求,统一管理。Nginx把所有的静态请求自己处理(静态文件处理是ngInx强项),然后把所有非静态请求通过uwsgi传递给Django,由Django来处理,从而完成一次web请求。

一、uWSGI

  1. 安装

pip install uwsgi

  1. 测试uWSGI是否安装成功

        在终端中输入以下命令查看uwsgi的版本号,如果输出正常,说明uswgi已安装成功
    

$ uwsgi --version
2.0.15

  1. 编写一个简单的wsgi应用测试uwsgi是否能正常使用

        首先创建一个test.py文件(命令:vim test.py)
    

test.py

def application(env, start_response):

start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2

4.运行uwsgi:

uwsgi --http :8000 --wsgi-file test.py
参数解释:

http :8000表示使用http协议,端口号为8000,

wigi-file则表示要运行的wsgi应用程序文件。

uwsgi运行后打开浏览器,访问http://127.0.0.1:8000/ ,或者是相应服务器地址的8000端口,就可以看到hello world 页面了。

运行截图:

1.png

如果想要运行项目来测试

uwsgi --http :8000 --chdir 项目路径 -w 项目.wsg --static-map=/static=static

uwsgi --http :8000 --chdir /home/teacher/ -w teacher.wsgi --static-map=/static=static
uWSGI常用命令:

uwsgi --chdir=/path/to/your/project \

--module=mysite.wsgi:application \
--env DJANGO_SETTINGS_MODULE=mysite.settings \
--master --pidfile=/tmp/project-master.pid \
--socket=127.0.0.1:49152 \      # 可以ip地址,也可以是文件 
--processes=5 \                 # 进程数量
--uid=1000 --gid=2000 \         # 如果是root用户,uwsgi可以有删除权限
--harakiri=20 \                 # 一个请求超时时间
--max-requests=5000 \           # 一个工作进程最大请求数
--vacuum \                      # 退出时清楚环境
--home=/path/to/virtual/env \   # virtualenv的路径
-- static                       # 做一个映射,指定静态文件
--http                          # 这个就和runserver一样指定IP 端口
--daemonize=/var/log/uwsgi/yourproject.log      # 日志 
  1. 创建uwsgi配置文件

    在项目统计目录下创建文件夹script,(比如项目目录路径/home/project_teacher/teacher,那么scrip文件存放在/home/project/teacher/script)
    

$ pwd
/home/project_teacher

$ mkdir script
$ vim uwsgi.ini

配置信息如下:

[uwsgi]

项目目录

chdir=/opt/project_teacher/teacher/

指定项目的application

module=teacher.wsgi:application

进程个数

workers=5
pidfile=/opt/project_teacher/script/uwsgi.pid

指定IP端口

http=192.168.31.123:8080

指定静态文件

static-map=/static=/opt/test_project/teacher/static

启动uwsgi的用户名和用户组

uid=root
gid=root

启用主进程

master=true

自动移除unix Socket和pid文件当服务停止的时候

vacuum=true

序列化接受的内容,如果可能的话

thunder-lock=true

启用线程

enable-threads=true

设置自中断时间

harakiri=30

设置缓冲

post-buffering=4096

设置日志目录

daemonize=/opt/project_teacher/script/uwsgi.log

指定sock的文件路径

socket=/opt/project_teacher/script/uwsgi.sock

  1. 启动配置

$ uwsgi --ini uwsgi.ini # 启动uwsgi配置
[uwsgi-static] added mapping for /static => /home/trunk/static # 启动成功

$ uwsgi --stop uwsgi.pid # 关闭uwsgi
signal_pidfile()/kill(): Operation not permitted [core/uwsgi.c line 1659]

二、Nginx

  1. 安装

$ sudo apt-get install nginx #安装

  1. 检查nginx是否安装成功

$ /etc/init.d/nginx start

[ ok ] Starting nginx (via systemctl): nginx.service.
检查nginx是否启动成功

$ ps -ef |grep -i nginx
root 6961 1 0 03:56 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 6962 6961 0 03:56 ? 00:00:00 nginx: worker process
pala 6985 2090 0 03:57 pts/0 00:00:00 grep --color=auto -i nginx
然后打开浏览器,访问ip地址,出现如下页面即代表nginx安装完成且可以正常启动。

2.png

Nginx常用命令

$ /etc/init.d/nginx start #启动
$ /etc/init.d/nginx stop #关闭
$ /etc/init.d/nginx restart #重启
三、Django + uWSGI +Nginx

创建一个xxx.conf配置文件(nginx的默认配置目录为/etc/nginx/conf.d)

$ vim /etc/nginx/conf.d/xxx.conf
配置文件信息如下:

server { # 这个server标识我要配置了

    listen 80;  # 我要监听那个端口
    server_name 10.129.205.183 ;  # 你访问的路径前面的url名称
    access_log  /var/log/nginx/access.log  main;  # Nginx日志配置
    charset  utf-8; # Nginx编码
    gzip on;  # 启用压缩,这个的作用就是给用户一个网页,比如3M压缩后1M这样传输速度就会提高很多
    gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;  # 支持压缩的类型

    error_page  404           /404.html;  # 错误页面
    error_page   500 502 503 504  /50x.html;  # 错误页面

    # 指定项目路径uwsgi
    location / {        # 这个location就和咱们Django的url(r'^admin/', admin.site.urls),
        include uwsgi_params;  # 导入一个Nginx模块他是用来和uWSGI进行通讯的
        uwsgi_connect_timeout 30;  # 设置连接uWSGI超时时间
        uwsgi_pass unix:/opt/project_teacher/script/uwsgi.sock;  # 指定uwsgi的sock文件所有动态请求就会直接丢给他
    }

    # 指定静态文件路径
    location /static/ {
        alias  /opt/project_teacher/teacher/static/;
        index  index.html index.htm;
    }
3. 重启nginx

$ /etc/init.d/nginx restart #重启

如果是Internal server error,可以查看日志找到哪里出错。

另外,推荐一个Django项目生成器cookiecutter-django

用这个项目生成一个Django项目,就可以直接用了,支持普通模式和Docker

新手上路,请多包涵

我估计是你启动了非调试模式,然后ALLOWED_HOSTS忘记设置了吧,可以设置*或者你具体用于访问该web的host,如果不是这个原因,你就是开调试模式,用runserver命令只系

我之前也用过nginx,发现还是用apache2配置简单点,成功率也挺高的。
这下面是我写的笔记,可以参考一下。
https://segmentfault.com/n/13...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题