centos7+nginx+python3+django+uwsgi配置Django 项目部署

 

1.租的服务器(选择centos)的话,需要在阿里云后台控制台开放几个端口,克隆一下已开放的端口,tcp自定义就行,mysql(3306),nginx(8000以上都行)。(都切换到root用户操作)

2.安装python3

3.安装nginx

4.安装mysql(这一步如果暂时用不上数据库也可以不操作)

5.确定2,3两步安装成功了,接下来就用pip3 安装django 和uwsgi,

在进行下一步之前建议你看一下这篇博客:http://blog.csdn.net/c465869935/article/details/53242126

还有官方文档:https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html(直接搜uwsgi官方文档(找到Setting up Django and your web server with uWSGI and nginx)).

6.测试uwsgi能否正常运行:

  随便找个干净的目录下vim  test.py,新建一个py文件,在里面写上:

 def application(env, start_response):
   start_response(‘200 OK‘, [(‘Content-Type‘,‘text/html‘)])
  return "Hello World".encode()

  并保存。然后在当前目录下执行:uwsgi --http :8000 --wsgi-file test.py。

  访问 http://127.0.0.1:8000,能显示Hello World说明uwsgi没问题。

7.随便找个干净的目录 新建一个django项目

  进入项目里面 编辑settings.py : vim settings.py ,在里面加上:

  ALLOWED_HOSTS = [‘test.xq.com‘,‘localhost‘, ‘127.0.0.1‘,‘虚拟机自己的ip‘,]

  #test.xq.com为你自己买的域名,暂时可以不用,用虚拟机自己的ip或者服务器的ip就行

同时加上:

  STATIC_ROOT = os.path.join(BASE_DIR, ‘static‘)

并保存 执行 cd ..切换到pyDemo项目的根目录,能看到manage.py即可,然后执行:python3 manage.py collectstatic.

在同一目录下新建uwsgi的配置文件: vim uwsgi.ini ,在里面写上:

[uwsgi]

socket = 127.0.0.1:8001

chdir=/py36_projects/pyDemo

module=pyDemo.wsgi

master = true

processes=2

threads=2

max-requests=2000

chmod-socket=664

vacuum=true

daemonize = /py36_projects/pyDemo/uwsgi.log

8.剩下的只有配置nginx的配置文件,

  vim /etc/nginx/nginx.conf,进入nginx.conf配置文件看看有没有下面这句代码:

  include /etc/nginx/conf.d/*.conf;(意思是导入/etc/nginx/conf.d/下的所有配置文件)

于是我们只要在/etc/nginx/conf.d/目录下:

  cd /etc/nginx/conf.d

新建一个conf就行:vim pyDemo.conf(名字随便取),里面写上:

stream django {

    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket

    server 127.0.0.1:8001; # for a web port socket (we‘ll use this first)

}

# configuration of the server

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;

    # max upload size

    client_max_body_size 75M;   # adjust to taste

    location /static {

        alias /py36_projects/pyDemo/static; # your Django project‘s static files - amend as required

    }

    # Finally, send all non-media requests to the Django server.

    location / {

        uwsgi_pass  django;

        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed

    }

}

一些说明:

listen      8000; 阿里云服务器的话为上面提到的阿里云后台控制台添加的端口(对外的端口)

server_name localhost; localhost可以替换你购买的域名(如前面django的配置文件里的test.xq.com)

/py36_projects/pyDemo/ 是我的项目路径

uwsgi_pass  django; 里面的 server 127.0.0.1:8001;和上面的uwsgi.ini配置文件的 socket = 127.0.0.1:8001 的端口一致,这个端口8000以上把(不要和nginx配置的端口相同就行)

include     /etc/nginx/uwsgi_params;引入/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  REQUEST_SCHEME     $scheme;

uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;

uwsgi_param  REMOTE_PORT        $remote_port;

uwsgi_param  SERVER_PORT        $server_port;

uwsgi_param  SERVER_NAME        $server_name;

9.以上基本配置好了,验证的准备:

先执行(一步一步来):

pkill -9 uwsgi

pkill -9 nginx

然后切换到项目根目录:

cd /py36_projects/pyDemo 执行:

uwsgi --ini uwsgi.ini

最后执行:

systemctl start nginx 或者 nginx -c /etc/nginx/nginx.conf(都是启动nginx的)

10.开始验证:

打开浏览器:输入你虚拟机或者服务器的 ip:8000,这样就配置成功了

http://blog.csdn.net/c465869935/article/details/53242126

还有官方文档:https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html(直接搜uwsgi官方文档(找到Setting up Django and your web server with uWSGI and nginx)).

浏览这两篇文章很重要,对你的理解有帮助

 
 

阿里云 centos7 django + uWSGI+Nginx + python3 部署攻略的更多相关文章

  1. Centos7 django+uwsgi+nginx+python3.6.8部署

    安装依赖 yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-d ...

  2. 阿里云centos7搭建php+nginx环境

    阿里云Centos搭建lnmp(php7.1+nginx+mysql5.7) https://jingyan.baidu.com/article/215817f7a10bfb1eda14238b.ht ...

  3. Django部署,Django+uWSGI+nginx+Centos部署

    说明:系统是在windows上开发的,使用django1.11.4+python3.6.3开发,需要部署在centos6.4服务器上. 第一步:在Centos6.4上安装Python3.6.2 安装请 ...

  4. 阿里云 centOS7.4新装nginx 不能访问

    反复装了几遍ngxin,什么防火墙,nginx.conf改了好几次都不能访问外网的ip, 原因是阿里云这货新的服务器根本就没开通443,80端口,真是坑人啊 点击配置规则,增加端口就行了 添加安全规则 ...

  5. centos7 nginx配置httpsCenos(6.6/7.1)下从源码安装Python+Django+uwsgi+nginx环境部署(二)

     1.yum安装nginx 下载对应当前系统版本的nginx包(package) # wget  http://nginx.org/packages/centos/7/noarch/RPMS/ngin ...

  6. docker简单使用+django+uwsgi+nginx项目部署

    使用docker 搭建 centos7 环境: 主机环境:windows 10专业版 一.安装docker Hub.docker.com官网下载 docker for windows 安装完成后,任务 ...

  7. django+uwsgi+nginx的部署

    1.下载与项目对应的django版本pip3 install django==1.11.16 -i https://pypi.douban.com/simple/2.用django内置的wsgi模块测 ...

  8. django+uwsgi+nginx+sqlite3部署+screen

    note:可通过该命令查找文件未知 sudo find / -name filename 一:项目(github) ssh root@server ip         #  连接你的服务器 git ...

  9. Django+Uwsgi+Nginx项目部署文档

    一.基本环境搭建 1)查看服务器 [root@Myjumpserver ~]# cat /etc/sysconfig/selinux SELINUX=disabled SELINUXTYPE=targ ...

随机推荐

  1. 内存管理-slab[原理]

    前言 主要讲解原理,基于2.6.32版本内核源码.本文整体思路:先由简单内存模型逐渐演进到当下通用服务器面对的内存模型,讨论每一个内存模型下slab设计需要解决的问题. 历史简介 linux内核运行需 ...

  2. Python模块——subprocess

    subprocess模块 通过Python去执行一条系统命令或脚本. 三种执行命令的方法 subprocess.run(*popenargs, input=None, timeout=None, ch ...

  3. python买卖股票的最佳时机--贪心/蛮力算法简介

    开始刷leetcode算法题 今天做的是“买卖股票的最佳时机” 题目要求 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更 ...

  4. 把本地项目放进新建的仓库(idea)

    1,获取仓库地址 比如: git@bitbucket.org:360717118/springboot_servlet-filter-listener-file.git,   2,idea 设置:

  5. Spring Boot 集成 Swagger2 与配置 OAuth2.0 授权

    Spring Boot 集成 Swagger2 很简单,由于接口采用了OAuth2.0 & JWT 协议做了安全验证,使用过程中也遇到了很多小的问题,多次尝试下述配置可以正常使用. Maven ...

  6. android 代码混淆示例

    参考其它资料为项目代码做了一下混淆 项目中使用了 slidingmenu   actionbarsherlock   fastjson  volley   httpclient 等第三方库, 并使用了 ...

  7. 2018.4.25-ml笔记(梯度下降)

  8. java对文件的检索

    先说下思路吧. 1.先把xls等文件转换为静态html文件:可以用很多插件,类似我的博客地址: http://www.cnblogs.com/Alandre/p/3221006.html 2.再对转的 ...

  9. 理解极大似然估计(MLE)

    极大似然估计学习时总会觉得有点不可思议,为什么可以这么做,什么情况才可以用极大似然估计.本文旨在通俗理解MLE(Maximum Likelihood Estimate). 一.极大似然估计的思想与举例 ...

  10. 【PyTorch深度学习60分钟快速入门 】Part2:Autograd自动化微分

      在PyTorch中,集中于所有神经网络的是autograd包.首先,我们简要地看一下此工具包,然后我们将训练第一个神经网络. autograd包为张量的所有操作提供了自动微分.它是一个运行式定义的 ...