资讯专栏INFORMATION COLUMN

测试格式

y1chuan / 2384人阅读

摘要:首先要安装因为系统已经装了所以接下来直接装虚拟环境建立一个独立于系统的虚拟环境不会跟系统环境混淆运行环境退出环境运行虚拟环境,在环境中安装新建项目新建不用也可以进行接下来的操作官网安装在虚拟环境中检测是否正常工作在与同目录下写一个模拟

1. virtualvenv 2. django 3. uWSGI 4. nginx 1. virtualvenv

virtualvenv install

首先要安装python3

因为系统已经装了 python3.6 所以接下来直接装虚拟环境 virtualvenv

mkdir yourwebproject folder
cd yourwebproject
/usr/python3.6/bin/python3.6 -m venv venv   #建立一个独立于系统的虚拟环境 不会跟系统环境混淆
source venv/bin/activate                    #运行环境
deactivate                                  #退出环境
2. django

运行虚拟环境,在环境中安装django

pip install django

新建项目

   1). django-admin startproject proName        
   2). cd proName
   3). python manage.py runserver    # run

新建app

不用也可以进行接下来的操作

3. uWSGI

官网: https://uwsgi-docs.readthedoc...

安装

在虚拟环境中

pip install uwsgi
检测 uwsgi 是否正常工作

在与venv同目录下写一个模拟站点文件 test.py

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

运行

uwsgi --http :8080 --wsgi-file test.py  # 假设访问端口号8080

浏览器访问

http://serverIP:8080

若显示站点文件中的输出,说明uwsgi生效

路径 web client <-> uwsgi <-> python

部署 django

首先运行django确保django能正常工作

python manage.py runserver 0.0.0.0:8000

在manage.py同级目录下运行

uwsgi --http :8080 --module djangoProName.wsgi

--module djangoProName.wsgi 代表 djangoProName 目录下的 wsgi.py 文件

浏览器访问

http://serverIP:8080
4. nginx

官网: https://www.nginx.com/resourc...

不在虚拟环境中安装
由于服务器上已经安装了 nginx 所以安装步骤省略,只需要在 nginx.conf 中添加配置即可。

配置 nginx

nginx.conf 结构:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http {
   # server{ ... }
}

添加 server

   server {
        listen       98 default_server;   # 访问时输入的端口 本地和外部浏览器后面都要加这个端口号
        server_name  10.41.95.85;          # 自己网站的域名

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        
        location / {                      # location : 文件系统配置 去应答一些要服务器资源的请求
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:9898;    # 与ini文件对接端口 与上面的 93 端口没有关系
        }
    }

配置完了之后记得重启nginx

配置 uwsgi

将配置项全部写入ini文件
在venv同目录下自己新建uwsgi的ini文件

[uwsgi]
socket = 127.0.0.1:9898                               ; 与 nginx 对接 IP
; django pro dir
chdir = /root/Odin/TrackManagement/TrackManagement/   ; django project dir
wsgi-file = TrackManagement/wsgi.py                   ; 代表 TrackManagement 目录下的 wsgi.py 文件
; module = TrackManagement.wsgi                       ; 有上面的wsgi配置这个就不用写了

processes = 2                                         ; 进程
threads = 1                                           ; 线程
stats = 127.0.0.1:9696                                ; 内部配置访问ip 与socket区别开

浏览器访问

http://10.41.95.85:98

若显示django画面 则 uwsgi+nginx生效

路径 web client <-> nginx <-> uwsgi <-> django


接下来是过程中踩的坑


uwsgi: unrecognized option "--http:8089"

uwsgi: unrecognized option "--http:8089"
getopt_long() error


原因:

参数格式不对 :8089前面要加空格 uwsgi还在开启


uwsgi: unrecognized option "--http"

(venv) [root@localhost TrackManagement]# uwsgi --http:8089 --module TrackManagement.wsgi
uwsgi: unrecognized option "--http"
getopt_long() error


原因:

uwsgi还在开启 先杀了进程再重启

uwsgi trkMngm_uwsgi.ini -> invalid request block size: 21573 (max 4096)...skip
启动了之后每次访问

*** Stats server enabled on 127.0.0.1:9295 fd: 12 ***
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip
invalid request block size: 21573 (max 4096)...skip


原因:

trkMngm_uwsgi.ini 文件中有设置nginx的socket

如果这时候nginx没有对应的配置或者配置了但是nginx没有重启
就会产生这个错误


ModuleNotFoundError: No module named "TrackManagement/TrackManagement/wsgi"

(venv) [root@localhost TrackManagement]# uwsgi --http :8089 --module TrackManagement/TrackManagement/wsgi.py
*** Starting uWSGI 2.0.18 (64bit) on [Tue Feb 19 11:33:20 2019] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-36) on 18 February 2019 05:28:03
os: Linux-3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018
nodename: localhost.localdomain
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /root/Odin/TrackManagement
detected binary path: /root/Odin/TrackManagement/venv/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 63229
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8089 fd 4
spawned uWSGI http 1 (pid: 33181)
uwsgi socket 0 bound to TCP address 127.0.0.1:33454 (port auto-assigned) fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 3.6.4 (default, Mar  6 2018, 13:19:57)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x12acbf0
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
ModuleNotFoundError: No module named "TrackManagement/TrackManagement/wsgi"
unable to load app 0 (mountpoint="") (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 33180, cores: 1)


原因: 应该在TrackManagement项目里面运行 即这个目录下面
(venv) [root@localhost TrackManagement]# ls
db.sqlite3  manage.py  testUwsgi.py  TrackManagement


runserver运行django error : Bad Request

(venv) [root@localhost TrackManagement]# python manage.py runserver 0.0.0.0:8080
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run "python manage.py migrate" to apply them.

February 18, 2019 - 07:47:25
Django version 2.1.7, using settings "TrackManagement.settings"
Starting development server at http://0.0.0.0:8080/
Quit the server with CONTROL-C.
Invalid HTTP_HOST header: "10.41.95.85:8080". You may need to add "10.41.95.85" to ALLOWED_HOSTS.
Bad Request: /
[18/Feb/2019 07:47:51] "GET / HTTP/1.1" 400 60826
Invalid HTTP_HOST header: "10.41.95.85:8080". You may need to add "10.41.95.85" to ALLOWED_HOSTS.
Bad Request: /favicon.ico
[18/Feb/2019 07:47:54] "GET /favicon.ico HTTP/1.1" 400 60906


solution

django setting.py

ALLOWED_HOSTS = ["*"] -> ALLOWED_HOSTS = ["*"]


manage.py语法错误

SyntaxError: invalid syntax
[root@localhost TrackManagement]# python manage.py runserver 0.0.0.0:8080
  File "manage.py", line 14
    ) from exc


solution

没有运行虚拟环境 外面的环境是python 虚拟环境才是python3 所以会有语法错误

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/40339.html

相关文章

  • 什么是接口测试?接口测试基础、案例及Json格式详解

    摘要:什么是接口测试全称接口是一个位于复杂系统之上能简化任务,像中间人一样不需要你了解详细的所有细节。接口测试与性能测试之间存在接口性能测试,主要通过来进行压测。 很多小...

    WalkerXu 评论0 收藏0
  • vue+element tree(树形控件数据格式)组件(1)

    摘要:树形控件数据格式组件最近做了第一个组内可以使用的组件,虽然是最简版,也废了不少力。让我来记录这个树形组件的编写过程和期间用到的知识点。 vue+element tree(树形控件数据格式)组件(1), 最近做了第一个组内可以使用的组件,虽然是最简版,也废了不少力。各位前辈帮我解决问题,才勉强搞定。让我来记录这个树形组件的编写过程和期间用到的知识点。 首先说说需求,就是点击出现弹窗+蒙板...

    Pines_Cheng 评论0 收藏0
  • vue+element tree(树形控件数据格式)组件(1)

    摘要:树形控件数据格式组件最近做了第一个组内可以使用的组件,虽然是最简版,也废了不少力。让我来记录这个树形组件的编写过程和期间用到的知识点。 vue+element tree(树形控件数据格式)组件(1), 最近做了第一个组内可以使用的组件,虽然是最简版,也废了不少力。各位前辈帮我解决问题,才勉强搞定。让我来记录这个树形组件的编写过程和期间用到的知识点。 首先说说需求,就是点击出现弹窗+蒙板...

    wangdai 评论0 收藏0
  • API管理平台XXL-API

    摘要:只需要填写测试的参数值,点击下方运行按钮,即可发起一次接口请求,请求结果将会在下方显示出来保存历史在接口测试界面,在进行接口测试后点击下方保存按钮将会把本次测试数据接口,测试参数等信息保存下来。 《API管理平台XXL-API》 一、简介 1.1 概述 XXL-API是一个简洁易用API管理平台,提供API的管理、文档、Mock和测试等功能。现已开放源代码,开箱即用。 1.2 特性 ...

    SmallBoyO 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<