使用centos主机搭建django使用nginx服务器运行环境。

名词解释

nginx:Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 服务器。
uwsgi:WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx)与应用服务器(如uWSGI服务器)通信的一种规范。uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。

搭建步骤

安装python

  1. 下载:https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz
  2. 安装:
    [root@carlton download]# tar -xf Python-3.4.3.tgz 
    [root@carlton download]# cd Python-3.4.3
    [root@carlton Python-3.4.3]# ./configure --prefix=/usr/local/python3.4
    显示:Successfully installed pip-6.0.8 setuptools-12.0.5 安装成功。
    
  3. 检测:
    [root@carlton ~]# cd /usr/local/python3.4/bin/
    [root@carlton bin]# ./python3
    Python 3.4.3 (default, May 25 2015, 11:40:38) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
  4. 安装virtualenv虚拟环境(python通过包管理的,为了不把以后用到的python的所有功能包含到基础python环境里,我们可以通过virtualenv来区分不同功能的不同python运行环境,比如django,我们可以建立一个虚拟环境只包含django运行相关的包,这样能够保证python环境是干净的):
    [root@carlton bin]# ./easy_install-3.4 virtualenv
  5. 使用一个django运行的虚拟环境:
    # 创建一个虚拟环境:
    [root@carlton bin]# ./virtualenv /home/python-virtual/django
    # 激活虚拟环境:
    [root@carlton bin]# source /home/python-virtual/django/bin/activate
    # 退出在虚拟环境中使用:
    (django)[root@carlton bin]# deactivate

注意:以下关于python包的安装都需要激活虚拟环境,这样才能保证安装的功能是只针对这个虚拟环境的(激活后命令行前面会有一个标志,如下面的红色(django))。

安装uwsgi

# 开始安装
(django)[root@carlton bin]# pip install uwsgi 
#安装成功: Successfully built uwsgi Installing collected #packages: uwsgi Successfully installed uwsgi-2.0.10

安装django

(django)[root@carlton bin]# pip install django
Collecting django
  Downloading Django-1.8.2-py2.py3-none-any.whl (6.2MB)
    100% |████████████████████████████████| 6.2MB 46kB/s 
Installing collected packages: django
Successfully installed django-1.8.2

安装nginx

  1. 下载
    (django)[root@carlton download]# wget http://nginx.org/download/nginx-1.9.0.tar.gz
  2. 安装
    (django)[root@carlton download]# tar -zxvf nginx-1.9.0.tar.gz 	
    (django)[root@carlton download]# cd nginx-1.9.0
    (django)[root@carlton nginx-1.9.0]# ./configure —prefix=/usr/local/nginx-1.9.0
    # 如果出现下面的情况则需要安装PCRE,如果没有出现直接忽略PCRE的安装,直接跳转到继续安装nginx:
    checking for PCRE library ... not found
    checking for PCRE library in /usr/local/ ... not found
    checking for PCRE library in /usr/include/pcre/ ... not found
    checking for PCRE library in /usr/pkg/ ... not found
    checking for PCRE library in /opt/local/ ... not found
    ./configure: error: the HTTP rewrite module requires the PCRE library.
    You can either disable the module by using --without-http_rewrite_module
    option, or install the PCRE library into the system, or build the PCRE library
    statically from the source with nginx by using --with-pcre=<path> option.
    # 那么你需要安装PCRE:
    # 下载PCRE和安装
    (django)[root@carlton download]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz
    (django)[root@carlton download]# tar -zxvf pcre-8.36.tar.gz 
    (django)[root@carlton download]# cd pcre-8.36
    (django)[root@carlton pcre-8.36]# ./configure
    # 如果报错:需要c++:
    yum -y install gcc-c++
    # 然后:
    (django)[root@carlton pcre-8.36]# make && make install
    #继续安装nginx:
    (django)[root@carlton pcre-8.36]# ./configure —prefix=/usr/local/nginx-1.9.0
    #配置成功:
    Configuration summary
      + using system PCRE library
      + OpenSSL library is not used
      + md5: using system crypto library
      + sha1: using system crypto library
      + using system zlib library
    
      nginx path prefix: "/usr/local/nginx-1.9.0"
      nginx binary file: "/usr/local/nginx-1.9.0/sbin/nginx"
      nginx configuration prefix: "/usr/local/nginx-1.9.0/conf"
      nginx configuration file: "/usr/local/nginx-1.9.0/conf/nginx.conf"
      nginx pid file: "/usr/local/nginx-1.9.0/logs/nginx.pid"
      nginx error log file: "/usr/local/nginx-1.9.0/logs/error.log"
      nginx http access log file: "/usr/local/nginx-1.9.0/logs/access.log"
      nginx http client request body temporary files: "client_body_temp"
      nginx http proxy temporary files: "proxy_temp"
      nginx http fastcgi temporary files: "fastcgi_temp"
      nginx http uwsgi temporary files: "uwsgi_temp"
      nginx http scgi temporary files: “scgi_temp"
    # 编译和安装
    (django)[root@carlton nginx-1.9.0]# make && make install
  3. 检测
    #安装完成后如果没有报错,则尝试开启服务:
    (django)[root@carlton lib]# /usr/local/nginx-1.9.0/sbin/nginx 
    #如果发现报错:
    ./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
    # 1、确认已经安装PCRE: 
    [root@carlton lib]$ cd /lib  
    [root@carlton lib]$ ls *pcre*  
    libpcre.so.0  libpcre.so.0.0.1   
    # 2、添加软链接:  
    [root@carlton lib]$ ln -s /lib/libpcre.so.0.0.1 /lib/libpcre.so.1  
    注: 在有的操作系统上面,安装pcre后,安装的位置为/usr/local/lib/*pcre*
    在redhat 64位机器之上有这样的情况.
    在redhat 64位机器上, nginx可能读取的pcre文件为/lib64/libpcre.so.1文件.
    所以在改用下面的软连接:
    [root@carlton ~]$ ln -s /usr/local/lib/libpcre.so.1 /lib64/

至此全部软件已经安装完成,uwsgi和nginx的配置可以参考下面的思路,详细的配置,空了再弄:

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

很多时候我们并不用需要使用第三方推送来给app添加推送,我们可以通过openfire来自己搭建推送服务器,openfire不但可以实现推送,而且可以用来实现聊天程序的即时响应。这里纯粹的介绍在android客户端怎么去跟openfire服务器交互。

继续阅读