配置Python、django、uwsgi

一、安装django、uwsgi

1.安装测试版本

# pip3 install django

# python3

>>> import django

>>> print(django.get_version())

2.2.6

>>>exit()

# python3 -c "import django; print(django.get_version())"

# ln -s /usr/local/python3/bin/django-admin /usr/bin/django-admin

2.安装uwsgi

WSGI是一种Web服务器网关接口、通信协议,uwsgi也是一种通信协议,uWSGI是一个Web服务器,可以实现上述两种协议。

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

https://blog.youkuaiyun.com/chenggong2dm/article/details/43937433

(1)下载最新版本源代码

# wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz

wget https://projects.unbit.it/downloads/uwsgi-2.0.19.1.tar.gz

# tar -zxvf uwsgi-latest.tar.gz

# cd uwsgi-2.0.18

(2)编译

# python3 uwsgiconfig.py --build

(3)拷贝程序

# cp uwsgi /usr/local/bin

# uwsgi --version (检测当前版本号)

# uwsgi --python-version (检测所属python版本号)

二、各种测试

(一)测试uwsgi

1.在你的同项目名目录中写一个test.py

执行:vim test.py

填写如下内容:

python3写法:(我用的是这个)

# test.py

def application(env, start_response):

    start_response('200 OK', [('Content-Type','text/html')])

    return [b"Hello World"]

2.然后执行shell命令:

请提前在云服务器控制台的防火墙设置中,添加规则允许相应端口通行。

# uwsgi --http :8000 --wsgi-file test.py

3.访问网页:

http://122.51.54.49:8000

看在网页上是否有Hello World

(二)测试django

1.创建项目:

# django-admin startproject mysite

2.创建app:

# cd mysite/

# django-admin startapp app01

3.在项目路径下创建模板文件目录(templates)和静态文件目录(static)

# mkdir templates static

4.配置setting.py文件

配置允许访问主机名、将APP加入到项目、添加模板搜索路径

# vim mysite/settings.py

修改代码如下:

import os

ALLOWED_HOSTS = ["*"]  #允许本机所有地址访问

INSTALLED_APPS = [

    'django.contrib.admin',

    'django.contrib.auth',

    'django.contrib.contenttypes',

    'django.contrib.sessions',

    'django.contrib.messages',

    'django.contrib.staticfiles',

    'app01',      #加入app名

]

TEMPLATES = [

    {

        'BACKEND': 'django.template.backends.django.DjangoTemplates',

        'DIRS': [os.path.join(BASE_DIR,'templates')],  #加入模板搜索路径

......

]

5.在views.py文件中创建视图函数

# vim app01/views.py

复制代码如下:

from django.shortcuts import render

def index(request):

    return render(request,"app01/index.html")

6.在templates目录中新建测试模板文件

# mkdir -p templates/app01

# vim templates/app01/index.html

复制代码如下:

<h1>test django server</h1>

7.在urls.py文件中建立路由

# vim mysite/urls.py

添加以下内容:

from django.contrib import admin

from django.urls import path,re_path  #添加

from app01 import views  #添加

urlpatterns = [

    path('admin/', admin.site.urls),

    re_path('^$',views.index,name='index'),  #添加的路由

]

8.检查运行代码

# python3 manage.py check

# python3 manage.py migrate

9.测试django

# python3 manage.py runserver 0:8000

10.测试uwsgi与django协作

# uwsgi --http :8000 --chdir /home/wwwftp/www/mysite/ --wsgi-file mysite/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

或者

# uwsgi --http :8000 --chdir /home/wwwftp/www/mysite/ --module mysite.wsgi --master --processes 4 --threads 2 --stats 127.0.0.1:9191

三、设计ini模式运行

1.编辑uwsgi.ini文件

对于uWSGI服务器的配置,如上命令加上很多参数非常麻烦,可以写成配置文件的方式,创建一个配置文件uwsgi.ini。

其中http参数用于以上测试,而与Nginx交互需要使用socket参数,即使用TCP协议,WSGI和uwsgi协议都在TCP协议之上。socket参数也可以配置为网络地址,如socket=127.0.0.1:7070,但如果Nginx和uWSGI同在一个服务器上,可以使用socket文件的形式。chmod-socket是为了动态配置socket文件的权限,因为socket文件会在每次uWSGI启动时被重新创建。

# vim /home/wwwftp/www/mysite/uwsgi.ini

1.对应第一条语句

[uwsgi]

http = :8000

socket = /run/uwsgi.sock

chdir = /home/wwwftp/www/mysite

wsgi-file = mysite/wsgi.py

processes = 4

threads = 2

stats = 127.0.0.1:9191

daemonize = /home/wwwftp/www/mysite/uwsgi.log                                           

2.对应第二条语句

[uwsgi]

http = :8000

socket = /run/uwsgi.sock

chdir = /home/wwwftp/www/mysite

module = mysite.wsgi

processes = 4

threads = 2

stats = 127.0.0.1:9191

daemonize = /home/wwwftp/www/mysite/uwsgi.log

3.测试运行

# uwsgi --ini uwsgi.ini

http://122.51.54.49:8000

四、与Nginx结合

1.对接nginx复制一下代码

# vim /home/wwwftp/www/mysite/uwsgi.ini

[uwsgi]

# http = :8000

socket = /run/uwsgi.sock

chdir = /home/wwwftp/www/mysite

wsgi-file = mysite/wsgi.py

processes = 4

threads = 2

stats = 127.0.0.1:9191

vacuum= true

daemonize = /home/wwwftp/www/mysite/uwsgi.log

pidfile = /home/wwwftp/www/mysite/uwsgi.pid

以下为解释

#当服务器退出的时候自动清理环境,删除unix socket文件和pid文件

vacuum= true

#使进程在后台运行,并将日志打到指定的日志文件或者udp服务器

daemonize = /home/wwwftp/www/mysite/uwsgi.log

#指定pid文件

pidfile = /home/wwwftp/www/mysite/uwsgi.pid  

# socket = 127.0.0.1:8000

# http=0.0.0.0:8000

# chdir = /home/wwwftp/www/mysite

# chmod-socket=664

# module = mysite.wsgi    

# home=/usr/local/bin

# master = true

# processes = 2

# threads = 2

# max-requests = 2000

# vacuum = true

# stats = 127.0.0.1:9001

# post-buffering = 65535

# buffer-size = 65535

# harakiri-verbose = true

# harakiri = 300

# uid = nginx

2.编辑启动文件

# vim /usr/lib/systemd/system/uwsgi.service

复制以下代码

[Unit]

Description=uwsgi service

After=network.target

[Service]

Type=forking

PIDFile=/home/wwwftp/www/mysite/uwsgi.pid

ExecStartPre=/usr/bin/rm -f /home/wwwftp/www/mysite/uwsgi.pid

ExecStart=/usr/local/bin/uwsgi --ini /home/wwwftp/www/mysite/uwsgi.ini

ExecReload=/bin/kill -s HUP $MAINPID

KillSignal=SIGQUIT

TimeoutStopSec=5

KillMode=process

PrivateTmp=true

[Install]

WantedBy=multi-user.target

3.运行

# systemctl daemon-reload

# systemctl start uwsgi.service

4.修改nginx的配置文件

# vim /etc/nginx/conf.d/default.conf

location /static {

       alias /home/wwwftp/www/mysite/static;  #指定django的静态文件

    }

    location /python/ {

            include     /etc/nginx/uwsgi_params;  #加载uwsgi模块

            uwsgi_pass  unix:///run/uwsgi.sock;

    }

以下修改实现php与python同时支持

# cat /etc/nginx/conf.d/default.conf

server {

    listen       80;

    server_name  localhost;

    #charset koi8-r;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {

        root   /home/wwwftp/www;

        index  index.php index.html index.htm;

    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html

    #

    error_page   500 502 503 504  /50x.html;

    location = /50x.html {

        root   /home/wwwftp/www;

    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80

    #

    #location ~ \.php$ {

    #    proxy_pass   http://127.0.0.1;

    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    #

    location ~ \.php$ {

        root           /home/wwwftp/www;

        fastcgi_pass   127.0.0.1:9000;

        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

        include        fastcgi_params;

    }

    # deny access to .htaccess files, if Apache's document root

    # concurs with nginx's one

    #

    #location ~ /\.ht {

    #    deny  all;

    #}

}

server {

    listen       8080;

    server_name  localhost:8080;

    #charset koi8-r;

    #access_log  /var/log/nginx/host.access.log  main;

    location /static {

       alias /home/wwwftp/www/mysite/static;  #指定django的静态文件

    }

    location / {

            include     /etc/nginx/uwsgi_params;  #加载uwsgi模块

            uwsgi_pass  unix:///run/uwsgi.sock;

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值