django学习笔记2-----环境搭建

  venv搭建和django环境配置

建议使用venv来学习django,可以在多个不同版本之间切换。

环境信息:

CentOS Linux release 7.5.1804 (Core),Ubuntu 14.04.5 LTS 都可以

python: 3.5.2, python3.6.5安装readline模块有问题。

django: 2.0 , PS: pip install django==2.0,2.1后面的django不支持5.5的mysql

mysql : 5.5.59-0ubuntu0.14.04.1

openssl: 使用ssl模块需要

zlib: virtualenv需要

1、 环境安装

1. 1安装python3.5.2,源码安装,Python-3.5.2.tgz

  # 使用enable-shared编译参数解决安装mod_wsgi时如下的问题

/usr/bin/ld: /usr/local/lib/libpython3.5m.a(abstract.o): relocation R_X86_64_32S against `_PyObject_NextNotImplemented' can not be used when making a shared object; recompile with -fPIC

/usr/local/lib/libpython3.5m.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
apxs:Error: Command failed with rc=65536
make: * [src/server/mod_wsgi.la] Error 1

  ./configure --prefix=/usr/local/Python-3.5.2 --enable-shared

  make && make install

1.2. 安装apache,源码安装。httpd-2.4.34.tar.gz

./configure --prefix=/usr/local/http-2.4.34 --enable-load-all-modules --enable-log-debug --enable-lua  -enable-luajit --enable-proxy  --enable-proxy-connect --enable-proxy-http --enable-proxy-fcgi --enable-proxy-scgi --enable-proxy-uwsgi --enable-proxy-fdpass --enable-ssl--with-apr=/usr/local/apr-1.5.2 --with-apr-util=/usr/local/apr-util-1.5.4

make && make install

 

1.3. 安装mod_wsgi mod_wsgi-4.6.4.tar.gz

编译安装之前,添加lib路径,解决下面的问题。

python3: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file:

echo "/usr/local/Python-3.5.2/lib" >>  /etc/ld.so.conf.d/python3.conf && ldconfig

编译安装

./configure --prefix=/usr/local/mod_wsgi --with-apxs=/usr/local/http-2.4.34/bin/apxs --with-python=/usr/local/Python-3.5.2/bin/python3

make && make install

1.4. 修改apache的配置文件 httpd.conf

添加一行下面的内容,并重启apache

LoadModule wsgi_module modules/mod_wsgi.so

 重启apache在error.log里应该有如下类似的内容Apache/2.4.34 (Unix) mod_wsgi/4.6.4 Python/3.5 OpenSSL/1.0.    1e-fips configured -- resuming normal operations,表示配置生效

 

2、 配置apache和django对接

 https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/modwsgi/

2.1. 修改apache配置文件httpd.conf

添加如下内容

WSGIScriptAlias /  /home/workstation/venvs/env3/repopri/djangos/ciblog/ciblog/wsgi.py    # /polls需要处理的根url, 项目的wsgi.py路径,告诉apache所有的/polls开头的url使用这个wsgi
WSGIPythonHome / /home/workstation/venvs/env3  # 项目的python解释器,这里使用的virtualenv,该路径通过  python -c 'import sys; print(sys.prefix)' 命令获取
WSGIPythonPath /home/workstation/venvs/env3/repopri/djangos/ciblog   # 项目的文件件,可以导入下面的模块的路径。

Alias /robots.txt /home/workstation/venvs/env3/repopri/djangos/ciblog/polls/static/polls/robots.txt
Alias /favicon.ico /home/workstation/venvs/env3/repopri/djangos/ciblog/polls/static/polls/favicon.ico

Alias /media/ /home/workstation/venvs/env3/repopri/djangos/ciblog/polls/media/
Alias /static/ /home/workstation/venvs/env3/repopri/djangos/ciblog/polls/static/

<Directory /home/workstation/venvs/env3/repopri/djangos/ciblog/polls/static/>
Require all granted
</Directory>

<Directory /home/workstation/venvs/env3/repopri/djangos/ciblog/polls/media/>
Require all granted
</Directory>

<Directory /home/workstation/venvs/env3/repopri/djangos/ciblog/ciblog>

<Files wsgi.py>
Require all granted
</Files>
</Directory>

 

 2.2 重启apache使配置生效

 

 现在即可不用python manage.py runserver即可访问,即使用apache作为web接口。

 

 2.3 配置鉴权

https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/apache-auth/

 

3、 配置nginx和django对接

https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/uwsgi/

https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

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

 其通信模型如下:

A Web Server Gateway Interface - WSGI - does this job. WSGI is a Python standard.

the web client <-> the web server <-> the socket <-> uwsgi <-> Django

 

3.1 安装nginx

./configure --prefix=/usr/local/nginx --with-http_ssl_module

3.2  安装uwsgi

   a)  pip install uswgi

   b) 测试是否正常工作

    b.1) 创建一个test_uwsgi.py文件

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

       b.2) 运行 uwsgi --http :8000 --wsgi-file test_uwsgi.py

    b.3)  访问 http://IP:8000返回 Hello,World表示正常,即如下流程拉通

the web client <-> uWSGI <-> Python

   c) 测试和django直接是否正常

  c.1) 在django的project(ciblog)为例,在ciblog目录下执行如下命令

uwsgi --http :8000 --module ciblog.wsgi

 

   c.2)  访问对应的界面,如果正常,则表示如下流程拉通

the web client <-> uWSGI <-> Django

 

 3.3 配置nginx

   a) 安装配置完成之后,配置启动nginx,拉通如下流程

the web client <-> the web server

  b) 将nginx的conf目录下的uwsgi_params 拷贝到django的工程目录下

    c)  修改nginx配置文件 nginx.conf,在http{}中添加如下一行内容

   include       sites-enabled/*.conf;

  d) 在nginx/conf 目录中创建文件夹 sites-enabled/*.conf,创建一个 ciblog.conf,添加如下内容

# the upstream component nginx needs to connect to
upstream 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 0.0.0.0; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/workstation/venvs/env3/repopri/djangos/ciblog/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/workstation/venvs/env3/repopri/djangos/ciblog/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     /home/workstation/venvs/env3/repopri/djangos/ciblog/uwsgi_params; # the uwsgi_params file you installed
    }
}

  e) 修改django工程里面的settings.py,添加如下一行内容,指定static目录

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

  f) 在django目录中创建步骤d中对应的media和static目录,并随便添加一个text文件作为测试

  g) 重启nginx,并测试访问 http://IP:8000/static/xxxxx  或者 http://IP/media/xxxxxx,此时可以正常访问则表示配置正常。

  h) 启动uwsgi测试nginx,uwsgi,和test_uwsgi.py

uwsgi --socket :8001 --wsgi-file test_uwsgi.py

  i) 访问 http://IP:8000,返回Hello,world表示如下流程正常

the web client <-> the web server <-> the socket <-> uWSGI <-> Python

  j) 启用django,nginx,uwsgi的

uwsgi --socket :8001 --wsgi-file ciblog/wsgi.py --chmod-socket=666
uwsgi --socket :8001 --module ciblog.wsgi --chmod-socket=666 # 这两种方式都可以,一般建议使用这种

  k) 使用8000端口访问django,如果有问题,查看nginx的errorlog

  此时django,nginx,uwsgi已经正常工作

the web client <-> the web server <-> the socket <-> uWSGI <-> Python

 

  l)此时使用的是tcp 8000端口,So far we have used a TCP port socket, because it’s simpler, but in fact it’s better to use Unix sockets than ports -修改nginx/conf/sites-enabled/ciblog.conf

upstream django {
     server unix:///home/workstation/venvs/env3/repopri/djangos/ciblog/ciblog.sock; # for a file socket,使用这个配置,注释下面的配置
    #server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

  m) 重启nginx

  n) 启动django的代理

uwsgi --socket ciblog.sock --wsgi-file ciblog/wsgi.py --chmod-socket=666
uwsgi --socket ciblog.sock --module ciblog.wsgi --chmod-socket=666     # 这两种方式都可以,一般建议使用这种

  o) 访问django,此时仍然能够正常工作

 

 3.4 使用 .ini文件启动 

 这里有问题,后面再说吧

*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /home/workstation/venvs/env3/repopri/djangos/ciblog
your processes number limit is 31545
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: enabled

 

 

nginx

General configuration of nginx is not within the scope of this tutorial though you’ll probably want it to listen on port 80, not 8000, for a production website.

You should also configure a separate nginx location block for serving non-Django files. For example, it’s inefficient to serve static files via uWSGI. Instead, serve them directly from Nginx and completely bypass uWSG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4、 配置mysql和django对接

4.1 安装pymysql

python3 不支持MySQLDB模块了,使用pymysql就行

4.2 修改配置文件

修改 django项目的settings.py文件,database连接信息。修改下面的name,user,pwd,host,port为真实环境信息即可

import pymysql         # 一定要添加这两行!通过pip install pymysql!
pymysql.install_as_MySQLdb()

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'databasename', 'USER': 'user', 'PASSWORD': 'pwd', 'HOST': 'IP', 'PORT': '3306', },

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

---------------------------             end line     -------------------------------

 

 

 

和mod_wsgi

posted @ 2023-06-05 14:20  lvxiaobo616  阅读(96)  评论(0)    收藏  举报