失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Django uWSGI 和 nginx 搭建服务器python应用环境

Django uWSGI 和 nginx 搭建服务器python应用环境

时间:2019-09-23 09:08:52

相关推荐

Django   uWSGI 和 nginx 搭建服务器python应用环境

1、给服务创建一个非root的账号

参考这个

2、用非root账号登陆Ubuntu

建一个虚拟的python环境,并进入

virtualenv uwsgi-tutorialcd uwsgi-tutorialsource bin/activate

在这个环境中安装Django框架,并创建一个名为mysite的project

pip install Djangodjango-admin.py startproject mysitecd mysite

安装uwsgi,这个是服务器和python应用的胶水程序

pip install uwsgi

安装Nginx,启动它

sudo apt-get install nginxsudo /etc/init.d/nginx start # start nginx

配置Nginx

cd /etc/nginx/sites-available/

创建一个文件

mysite_nginx.conf,内容如下

# configuration of the serverserver {# the port your site will be served onlisten8000;# the domain name it will serve forserver_name 42.193.46.15; # substitute your machine's IP address or FQDNcharsetutf-8;# max upload sizeclient_max_body_size 75M; # adjust to taste# Django medialocation /media {alias /home/ubuntu/code/mysite/media; # your Django project's media files - amend as required}location /static {alias /home/ubuntu/code/mysite/static; # your Django project's static files - amend as required}# Finally, send all non-media requests to the Django server.location / {uwsgi_pass unix:///home/ubuntu/code/mysite/mysite.sock;include/etc/nginx/uwsgi_params;#/home/ubuntu/venvs/mysite/uwsgi_params; # the uwsgi_params file you installed}}

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

Deploying static files

Before running nginx, you have to collect all Django static files in the static folder. First of all you have to edit mysite/settings.py adding:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

and then run

python manage.py collectstatic

Basic nginx test

Restart nginx:

sudo /etc/init.d/nginx restart

uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # (very permissive)

这时候nginx监听mysite.sock的信息,如果是/就直接发送mysite.sock交给上游uwsgi ,uwsgi 不做任何处理,直接交给test.py 程序

# test.pydef application(env, start_response):start_response('200 OK', [('Content-Type','text/html')])return [b"Hello World"] # python3#return ["Hello World"] # python2

If that doesn’t work

Check your nginx error log(/var/log/nginx/error.log). If you see something like:

connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permissiondenied)

解决方案看这个

把测试test.py换成Django application

创建一个参数文件mysite_uwsgi.ini

# mysite_uwsgi.ini file[uwsgi]# Django-related settings# the base directory (full path)chdir = /home/ubuntu/code/mysite# Django's wsgi filemodule= mysite.wsgi# the virtualenv (full path)home = /home/ubuntu/venvs/venv# process-related settings# mastermaster= true# maximum number of worker processesprocesses = 10# the socket (use the full path to be safesocket= /home/ubuntu/code/mysite/mysite.sock# ... with appropriate permissions - may be neededchmod-socket = 664# clear environment on exitvacuum= true

启动

uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file

访问http://42.193.46.15:8000/

访问到了django自带的界面,想访问自己的界面.

创建一个名叫polls的 app module

$ python manage.py startapp polls

That’ll create a directorypolls, which is laid out like this:

polls/__init__.pyadmin.pyapps.pymigrations/__init__.pymodels.pytests.pyviews.py

写一个自己的view,Open the filepolls/views.pyand put the following Python code in it:

polls/views.py¶

from django.http import HttpResponsedef index(request):return HttpResponse("Hello, world. You're at the polls index.")

The next step is to point the root URLconf at thepolls.urlsmodule.

Inmysite/urls.py, add an import fordjango.urls.includeand insert aninclude()in theurlpatternslist, so you have:

mysite/urls.py¶

from django.contrib import adminfrom django.urls import include, pathurlpatterns = [path('polls/', include('polls.urls')),path('admin/', admin.site.urls),]

访问新写的界面,成功!

参考官方教程

https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

/en/3.2/intro/tutorial01/

如果觉得《Django uWSGI 和 nginx 搭建服务器python应用环境》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。