阿里云 centos7 django + uWSGI+Nginx + python3 部署攻略

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)).

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

原文地址:https://www.cnblogs.com/xinghuaikang/p/8576580.html

时间: 2024-08-03 07:30:09

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

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

安装依赖 yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make 下载python安装包 https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz 安装niginx yum源 rpm -Uvh http://nginx.org/packages/centos/7/noarch/RP

django+uwsgi+nginx+sqlite3部署

一:项目(github) ssh [email protected] ip         #  连接你的服务器 git clone -b https://mygithub.com     #  -b 指定分支 安装virtualenv及配置环境变量     详细见:https://www.cnblogs.com/tangpg/p/8458233.html mkvirtualenv envname -p python3 # 创建项目使用的python版本的虚拟环境,创建成功默认回进入该虚拟环境

Django+uwsgi+Nginx安装部署

安装 安装Nginx Nginx是最流行的高性能HTTP服务器. wget http://nginx.org/download/nginx-1.10.1.tar.gz tar -zxvf nginx-1.10.1.tar.gz cd nginx-1.10.1 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module make && make install 安装MySQL-python MySQL-python

阿里云centos7下的nginx配置

1.添加资源 添加CentOS 7 Nginx yum资源库,打开终端,使用以下命令(没有换行): # sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 2.安装Nginx 在你的CentOS 7 服务器中使用yum命令从Nginx源服务器中获取来安装Nginx: # yum install -y nginx 3.启动Nginx 运行Ng

阿里云 asp.net core nginx 单机部署

1. dotnet core 安装 https://www.microsoft.com/net/download#core 安装之前要安装依赖:yum install libunwind libicu 2.nginx 安装 yum install nginx systemctl start nginx 3. dotnet 后台运行 startup 文件增加: app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeader

Django + Uwsgi + Nginx 实现生产环境部署

一.如何在生产上部署Django? Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 二.uwsgi介绍 uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换. 要注意 WSGI / uwsgi / uWSGI 这三个概念的区分. WSGI是一种Web服务器网关接口.它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Flask

[linux]centos7.4部署django+Uwsgi+Nginx

前言:我已经写了几个接口用来部署在服务器上的,首先选择django+Uwsgi+Nginx因为配置简单,比较符合python的简单操作功能强大的特点 然后对于django的一些版本在之前的文章写了 参考:https://www.cnblogs.com/Jack-cx/p/9351633.html  根据版本,服务器pip3 install xxx 安装下 我的django项目大概结构: TestWebApi ----TestWebApi ----Apiso(创建的应用) ----manage.p

Django+uWSGI+Nginx 部署网站

Django 1.11设置 保证Django在本地调试没有问题: 当然这是前提^_^ 收集静态文件至指定文件夹 Django静态文件设置具体参考:https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/ 在myproject/settings.py中设置静态文件STATIC_URL和STATIC_ROOT设置: 如: STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE

django+uwsgi+nginx部署(非常详细)

django+uwsgi+nginx部署 1.介绍: 在网上看了很多教程,但自己部署了很久都没有成功,这篇博文记录自己所踩过得坑. 2.环境: 1 Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-130-generic x86_64) 3.下载uwsgi以及nginx 1 apt-get install uwsgi 2 apt-get install nginx (注:如果下载异常出现权限问题,在命令前添加sudo) 4.创建Django项目 例如:项目名为mysite