nignx部署django

操作系统:Linux wiki 2.6.32-131.0.15.el6.x86_64

nginx版本: nginx-1.5.7

uwsgi版本:uwsgi-2.0.8

大致流程参考:http://www.linuxyw.com/353.html

(官方文档:http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html)

下面主要介绍安装过程中出现的几个问题

1. uwsgi安装中,由于openssl 源码升级过导致的问题

ssl.c:(.text+0x4a4): undefined reference to `EC_KEY_new_by_curve_name‘
ssl.c:(.text+0x4e6): undefined reference to `EC_KEY_free‘
collect2: error: ld returned 1 exit status
*** error linking uWSGI ***
make: *** [all] Error 1

常叔说是由于找到了头文件,但是没有找到头文件的函数的定义导致的,google一把,果然如此:

From the error shown it looks like you have new openssl headers but still old libssl.so library in the library path. Make sure to remove old openssl library (and install new library if there are no new libssl.so available).  http://trac.nginx.org/nginx/ticket/169

下面问题似乎明朗了,由于我之前给openssl升级过,升级命令如下:

./config --prefix=/usr/ --openssldir=/usr/local/openssl
make
make test
make install

再看下系统现在rpm包的安装情况和调用的动态链接库的情况:

[email protected]192.168.100.252:/data/installs/uwsgi-2.0.8# rpm -qa | grep openssl
openssl-1.0.0-10.el6.x86_64
openssl-devel-1.0.0-10.el6.x86_64
openssl098e-0.9.8e-17.el6.x86_64
[email protected]192.168.100.252:/data/installs/uwsgi-2.0.8# ll /usr/lib64/ | grep ssl
lrwxrwxrwx.  1 root root       27 Aug 20 01:15 libnss_compat_ossl.so.0 -> libnss_compat_ossl.so.0.0.0
-rwxr-xr-x.  1 root root    84672 Mar 17  2010 libnss_compat_ossl.so.0.0.0
-rwxr-xr-x.  1 root root   215792 Apr 22  2011 libssl3.so
-rw-r--r--   1 root root   676230 Jan 22 12:52 libssl.a
lrwxrwxrwx.  1 root root       21 Aug 20 01:17 libsslcommon.so.5 -> libsslcommon.so.5.0.0
-rwxr-xr-x.  1 root root   209904 Apr  6  2011 libsslcommon.so.5.0.0
lrwxrwxrwx.  1 root root       15 Aug 20 01:15 libssl.so -> libssl.so.1.0.0
-rwxr-xr-x.  1 root root   324680 Apr 21  2010 libssl.so.0.9.8e
lrwxrwxrwx.  1 root root       15 Aug 20 01:14 libssl.so.10 -> libssl.so.1.0.0
-rwxr-xr-x.  1 root root   374128 Feb 10  2011 libssl.so.1.0.0
lrwxrwxrwx.  1 root root       16 Aug 20 01:19 libssl.so.6 -> libssl.so.0.9.8e
drwxr-xr-x.  3 root root     4096 Aug 20 01:14 openssl
drwxr-xr-x.  3 root root     4096 Aug 20 01:19 openssl098e

稍等,那个1098e的rpm包应该可以先删掉了。。rpm -e openssl098e-0.9.8e-17.el6.x86_64

不行啊,这样还是找不到问题的根源,先找出我的虚拟机244,在上面装一把看看,内网服务器别搞崩溃了

244的小机器也是一样的openssl升级方法,安装uwsgi完全没报错,这下真迷惑了,继续google

http://www.111cn.net/sys/CentOS/61326.htm 大牛说需要加一个参数 shared zlib-dynamic

再次make clean ; ./config --prefix=/usr/ --openssldir=/usr/local/openssl shared zlib-dynamic ; make 还是报错

[email protected]:/data/installs/openssl-1.0.0m# ./config --prefix=/usr/ --openssldir=/usr/local/openssl shared zlib-dynamic[email protected]:/data/installs/openssl-1.0.0m# make/usr/bin/ranlib ../../libcrypto.a || echo Never mind.
make[2]: Leaving directory `/data/installs/openssl-1.0.0m/crypto/ts‘
if [ -n "libcrypto.so.1.0.0 libssl.so.1.0.0" ]; then                 (cd ..; make libcrypto.so.1.0.0);         fi
make[2]: Entering directory `/data/installs/openssl-1.0.0m‘
make[3]: Entering directory `/data/installs/openssl-1.0.0m‘
make[4]: Entering directory `/data/installs/openssl-1.0.0m‘
/usr/bin/ld: libcrypto.a(e_4758cca.o): relocation R_X86_64_32 against `.data‘ can not be used when making a shared object; recompile with -fPIC
libcrypto.a(e_4758cca.o): could not read symbols: Bad value
collect2: error: ld returned 1 exit status
make[4]: *** [link_a.gnu] Error 1
make[4]: Leaving directory `/data/installs/openssl-1.0.0m‘
make[3]: *** [do_linux-shared] Error 2
make[3]: Leaving directory `/data/installs/openssl-1.0.0m‘
make[2]: *** [libcrypto.so.1.0.0] Error 2
make[2]: Leaving directory `/data/installs/openssl-1.0.0m‘
make[1]: *** [shared] Error 2
make[1]: Leaving directory `/data/installs/openssl-1.0.0m/crypto‘
make: *** [build_crypto] Error 1

好熟悉的感觉,可能是源码被污染了,删掉重来。。

################# uWSGI configuration #################

pcre = True
kernel = Linux
malloc = libc
execinfo = False
ifaddrs = True
ssl = True
zlib = True
locking = pthread_mutex
plugin_dir = .
timer = timerfd
yaml = embedded
json = False
filemonitor = inotify
routing = True
debug = False
capabilities = False
xml = libxml2
event = epoll

############## end of uWSGI configuration #############
total build time: 6 seconds
*** uWSGI is ready, launch it with ./uwsgi ***

done

2. nginx安装,这个比较简单,就不介绍了

cd /data/var/nginx-1.5.7
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre=/data/var/pcre-8.11 --with-openssl=/data/installs/openssl-1.0.0m/
make
make install

3. uwsgi配置

3.1 参照官方文档,先写一个test.py测试一下uwsgi运行情况

[email protected]192.168.100.252:/usr/local/nginx# cat /data/forilen/Kikyou/Kikyou_web/test.py
def application(env, start_response):
    start_response(‘200 OK‘, [(‘Content-Type‘,‘text/html‘)])
    #return [b"Hello World"] # python3
    return ["Hello World"] # python2

启动uwsgi

[email protected]192.168.100.252:/data/forilen/Kikyou/Kikyou_web# uwsgi --http :8090 --wsgi-file test.py
*** Starting uWSGI 2.0.8 (64bit) on [Thu Jan 22 18:07:28 2015] ***
compiled with version: 4.8.2 on 22 January 2015 15:33:07
os: Linux-2.6.32-131.0.15.el6.x86_64 #1 SMP Tue May 10 15:42:40 EDT 2011
nodename: wiki
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /data/forilen/Kikyou/Kikyou_web
detected binary path: /usr/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 1024
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 :8090 fd 4
spawned uWSGI http 1 (pid: 31133)
uwsgi socket 0 bound to TCP address 127.0.0.1:56481 (port auto-assigned) fd 3
Python version: 2.6.6 (r266:84292, Apr 11 2011, 15:50:32)  [GCC 4.4.4 20100726 (Red Hat 4.4.4-13)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1aad390
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72768 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint=‘‘) ready in 0 seconds on interpreter 0x1aad390 pid: 31132 (default app)
*** uWSGI is running in multiple interpreter mode ***

浏览器拉取页面:

spawned uWSGI worker 1 (and the only) (pid: 31132, cores: 1)
[pid: 31132|app: 0|req: 1/1] 192.168.100.30 () {36 vars in 847 bytes} [Thu Jan 22 18:08:14 2015] GET / => generated 11 bytes in 0 msecs (HTTP/1.1 200) 1 headers in 44 bytes (1 switches on core 0)

3.2 再用uwsgi重新启动项目Kikyou_web,下面的Kikyou_web.wsgi不用存在

[email protected]192.168.100.252:/data/forilen/Kikyou/Kikyou_web# uwsgi --http :8090 --module Kikyou_web.wsgi
*** Starting uWSGI 2.0.8 (64bit) on [Thu Jan 22 18:14:04 2015] ***
compiled with version: 4.8.2 on 22 January 2015 15:33:07
os: Linux-2.6.32-131.0.15.el6.x86_64 #1 SMP Tue May 10 15:42:40 EDT 2011
nodename: wiki

浏览器访问

3.3 配置/usr/local/nginx/conf/nginx.conf,添加django资源处理配置部分

    server {
        # the port your site will be served on
        listen 8090;
        server_name localhost;
        charset     utf-8;
        # max upload size
        client_max_body_size 75M;   # adjust to taste

        # Django media
        location /media  {
            alias /data/forilen/Kikyou/Kikyou_web/media;  # your Django project‘s media files - amend as required
        }
        location /static {
            alias /data/forilen/Kikyou/Kikyou_web/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     /usr/local/nginx/conf/uwsgi_params; # the uwsgi_params file you installed
        }
    }

reload nginx配置,浏览器访问请求资源

3.4 修改nginx.conf

    upstream django {
        server unix:///usr/local/nginx/uwsgi.sock; # for a file socket
        #server 127.0.0.1:8001; # for a web port socket (we‘ll use this first)
    }

启动项目:[email protected]:/usr/local/nginx# uwsgi --socket /usr/local/nginx/uwsgi.sock --module Kikyou_web.wsgi --chmod-socket=664

此时只有静态页面可以访问 >_<

3.5 添加文件/usr/local/nginx/conf/uwsgi.ini

# uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /data/forilen/Kikyou/Kikyou_web
# Django‘s wsgi file
module          = Kikyou_web.wsgi
# the virtualenv (full path)
# home            = /path/to/virtualenv

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 2
# the socket (use the full path to be safe
socket          = /usr/local/nginx/uwsgi.sock
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true

[email protected]:/usr/local/nginx# uwsgi --ini /usr/local/nginx/conf/uwsgi.ini

reload nginx,再次访问,此时静态页面和动态页面都能正常访问。。^_^

时间: 2024-10-13 04:54:48

nignx部署django的相关文章

nginx基于uwsgi部署Django (单机搭建)

nginx基于uwsgi部署Django (单机搭建) 参考链接: https://blog.51cto.com/wangfeng7399 https://blog.51cto.com/wangfeng7399/2341281 https://blog.csdn.net/shylonegirl/article/details/83030024 安装nignx yum -y install nginx (需要epel源) 安装依赖包 yum groupinstall "Development to

linux中部署django项目

通过Nginx部署Django(基于ubuntu) Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求.nginx把所有静态请求自己来处理(这是NGINX的强项).然后,NGINX将所有非静态请求通过uwsgi传递给Django,由Django来进行处理,从而完成一次WEB请求. 可见,uwsgi的作用就类似一个桥接器.起到桥梁的作用. Linux的强项

跨过Nginx上基于uWSGI部署Django项目的坑

先说说他们的关系,Nginx和uWSGI都是Web服务器,Nginx负责静态内容,uWSGI负责Python这样的动态内容,二者配合共同提供Web服务以实现提高效率和负载均衡等目的.uWSGI实现了多个协议,如WSGI,HTTP协议,还有它自己的uwsgi协议,想了解更多关于uWSGI和uwsgi协议内容可以查阅这里.这样和fastcgi类似,请求和响应的流程如下: Request > Nginx > uWSGI > Django > uWSGI > Nginx > R

Ubuntu apache2 wsgi 部署django

在ubuntu上部署django其实还算简单直观,最重要的问题就是路径设置正确. 一,安装python 和django.这个很简单,django用pip安装就可以.就不解释了. 二,安装apache2 和 wsgi. 这里用的是apt-get. sudo apt-get insall apache2 sudo apt-get install libapache2-mod-wsgi 需要注意的是,如果之前安装配置过apache2的并且配置很乱,无法修复,建议还是完全卸载之后在安装.完全卸载的命令:

Nginx+Gunicorn+virtualenv+supervisord+Postgresql部署Django应用

关于Django应用部署 Django是一个高效.多功能和动态地进化的Web应用开发框架.目前比较流行的部署.运行Django应用方式是基于Apache的mod_wsgi模块,但更加高效.弹性,同时又更加复杂的方式是使用以下工具来部署实施:Nginx.Gunicorn.virtualenv.supervisord.Postgresql.以下详细介绍如何结合这些工具来部署Django应用到Linux上. 准备工作 需要有一台拥有root权限的Linux服务器,这是部署应用的平台.本文采用CentO

生产环境使用Nginx+uwsgi部署Django

在本地运行django应用相对来说还是挺方便的,使用自带的runserver启动即可.如果在生产环境部署django,就要多考虑一些问题了.比如静态文件处理,安全,效率等等 在网上找到了不错的部署的教程,讲解的还是很详细的,我就不重新再整理了. 五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(上) – Django中国社区五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(下) – Django中国社区 但是作为记录,我还是要记录下部署中遇

Apache2.4部署django出现403 Forbidden错误解决办法

前言:Apache部署django出现403 Forbidden错误最好要结合apache中的错误日志来观察出现何种错误导致出现403错误 下午百度了一下午没找到解决办法,试了n种方法,简直坑爹! 比如网页出现最多的解决办法是: <Directory E:/wamp/Apache24/www(你的工程路径)>           Order allow,deny           Allow from all       </Directory> 可惜这样改了后还是报403,最后

Apache部署django

1.环境:windows7 2.版本:Python 2.7.8 Django 1.6.5 Apache 2.2     http://httpd.apache.org/(download-binaries-win32下) mod_wsgi.so   http://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi 3.下载并安装python,Django,Apache 注意事项:1.请全部安装32位的,因为Apache官方没有64位版本,你也别想找民间的64

基于nginx和uWSGI在Ubuntu系统上部署Django项目

1. nginx1.1 安装sudo apt-get install nginx1.2启动.停止和重启sudo /etc/init.d/nginx startsudo /etc/init.d/nginx stopsudo /etc/init.d/nginx restart或者sudo service nginx startsudo service nginx stopsudo service nginx restart2. uWSGI安装用python的pip安装最简单:apt-get inst