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

参考这个

2、用非root账号登陆Ubuntu

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

virtualenv uwsgi-tutorial
cd uwsgi-tutorial
source bin/activate

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

pip install Django
django-admin.py startproject mysite
cd mysite

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

pip install uwsgi

安装Nginx,启动它

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

配置Nginx

cd /etc/nginx/sites-available/

创建一个文件

mysite_nginx.conf,内容如下
# configuration of the server
server {# the port your site will be served onlisten      8000;# the domain name it will serve forserver_name 42.193.46.15; # substitute your machine's IP address or FQDNcharset     utf-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.py
def 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: Permission
denied)

解决方案看这个

把测试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 file
module          = mysite.wsgi
# the virtualenv (full path)
home            = /home/ubuntu/venvs/venv# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /home/ubuntu/code/mysite/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 664
# clear environment on exit
vacuum          = 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 directory polls, which is laid out like this:

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

写一个自己的view,Open the file polls/views.py and 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 the polls.urls module.

In mysite/urls.py, add an import for django.urls.include and insert an include() in the urlpatterns list, so you have:

mysite/urls.py¶

from django.contrib import admin
from 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

https://docs.djangoproject.com/en/3.2/intro/tutorial01/

Django 、 uWSGI 和 nginx 搭建服务器python应用环境相关推荐

  1. Nginx搭建服务器

    第一步:下载安装包 Nginx下载地址 第二步:解压到合适的位置,修改配置 我这边是解压到D:\Program Files (x86)\nginx-1.21.6目录下,然后打开conf文件夹找到ngi ...

  2. 手动搭建服务器—Python

    目录 1.HTTP协议 2.HTTP请求头 3.IP地址的绑定 4.根据不同的请求返回不同的内容 5.面向对象的服务器封装 6.WSGI服务器 6.1 WSGI接口 6.2 WSGI不同路径返回不同内 ...

  3. Docker部署Django由浅入深系列(中): 双容器部署Django + Uwsgi + Nginx

    本文是使用Docker部署Django由浅入深系列的中篇,我们将构建两个容器,一个容器放Django + Uwsgi,另一个容器放Nginx.我们将了解不同容器间是如何通信的,并学会正确配置uwsgi ...

  4. python搭建内网网站,python搭建服务器

    python搭建服务器 python搭建服务器以快捷著称,实际上,我们也可以使用python搭建简易的服务器. 1.环境配置 当然首先得搭建python的开发环境,对于mac和linux的用户来说,一 ...

  5. 使用 Nginx 搭建图片服务器

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 作者 | ITDragon龙 链接 | cnblogs.com/i ...

  6. Nginx 搭建图片服务器

    本章内容通过Nginx 和 FTP 搭建图片服务器.在学习本章内容前,请确保您的Linux 系统已经安装了Nginx和Vsftpd. Nginx 安装 http://www.cnblogs.com/i ...

  7. Python 3 怎么快速搭建服务器

    Python 3 怎么快速搭建服务器 python 3 环境,我就不多说了 1.新建一个文件夹 2.放文件夹里一个 index.html 文件用来默认访问,不然会炸的 3.进入文件目录 4.执行以下命 ...

  8. python3 supervisor_python3-django+uwsgi+supervisor+nginx跳坑指南(记录)

    首先运行django项目:在项目目录内: python manage.py runserver 0.0.0.0:8000 外部服务器访问:http://www.xxx.com:8000/ 可以正常运行 ...

  9. python服务器搭建nginx_python服务器环境搭建Flask,uwsgi和nginx

    python服务器环境搭建Flask,uwsgi和nginx 环境配置 服务器配置 [部署系统环境Ubuntu] 使用python的Flask框架搭建好网页后台后,便要开始将网站部署到服务器平台了.为 ...

  10. nginx+uWSGI+django+virtualenv+supervisor发布web服务器

    导论 WSGI是Web服务器网关接口.它是一个规范,描述了Web服务器如何与Web应用程序通信,以及Web应用程序如何链接在一起以处理一个请求,(接收请求,处理请求,响应请求) 基于wsgi运行的框架 ...

最新文章

  1. Chemistry.AI | 基于图卷积神经网络(GCN)预测分子性质
  2. Hadoop-2.4.1学习之edits和fsimage查看器
  3. FFmpeg使用遇到问题记录
  4. 详解MTK系统中字符转换问题
  5. 2020云栖大会编程限时抢答赛 - 早中晚3场题解
  6. Laravel神奇的服务容器
  7. submit和button的区别
  8. 【数据分析】基于matlab GUI学生成绩管理系统【含Matlab源码 601期】
  9. 什么是脏数据,缓存中是否可能产生脏数据,如果出现脏数据该怎么处理?
  10. python123平台第四周作业答案_python123答案
  11. 计算机病毒怎么取消,电脑病毒恶意软件无法删除。怎么办?
  12. 微信公众号怎么集赞服务器,微信公众号分享集赞吸粉方案,人人可复制
  13. sqlserver2008使用设置sa用户登录步骤
  14. 比吸烟还可怕的九大“爱好”
  15. win7系统屏幕不休眠,怎么设置
  16. android 正则句子按照标点符号断句,正则Pattern;
  17. 企业邮箱外发被退信的处理过程
  18. Bosun RabbitMQ数据收集
  19. 常用EMAIL的pop和smtp服务器地址
  20. java解包_Java的自动封包和解包(Autoboxing和AutoUnboxing)

热门文章

  1. BZOJ5232[Lydsy2017省队十连测] 好题
  2. git revert 之后怎么撤销_Git撤销回滚操作(git reset 和 get revert)
  3. jq获取验证码成功之后弹出的提示框_验证码填写错误,请重新填写。。。
  4. java中的servlet_关于JavaWeb中Servlet的总结
  5. 怎么向前撤回_延迟复工,工资到底怎么算?
  6. python数据处理太慢_使用Python将数据写入LMDB非常慢
  7. 英文c语言试题,C语言今日练习试题(主要练习英文阅读能力)
  8. mac 二进制安装mysql_教程方法;在mac下安装mysql二进制分发版的方法(不是dmg的)电脑技巧-琪琪词资源网...
  9. php分配变量,php之smarty分配变量
  10. ccf-csp认证历年真题(持续更新)