博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
django环境部署-nginx环境
阅读量:5131 次
发布时间:2019-06-13

本文共 11096 字,大约阅读时间需要 36 分钟。

一、使用nginx+mod_wsgi

参考:http://moodylee.me/2011/10/13/python_web_programming_1/

1、下载软件

mkdir software
# cd software/
http://nginx.org/en/download.html
# wget http://nginx.org/download/nginx-1.2.5.tar.gz
错误:这个是用在apache上面的,不是用在nginx上面的
# wget http://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz
或者
#hg clone https://code.google.com/p/modwsgi/
也就是mod_wsgi-3.2-1.el6.i686包
# rpm -qa|grep mod_wsgi
mod_wsgi-3.2-1.el6.i686

正确为

hg工具需要安装mercurial-hgk
# yum -y install mercurial-hgk
# hg clone http://bitbucket.org/lifeeth/mod_wsgi/

2、安装

1)基本环境
./configure: error: the WSGI addon requires Python library.
# yum -y install python-devel
./configure: error: the HTTP rewrite module requires the PCRE library.
# yum -y install pcre-devel
2)安装nginx+mod_wsgi
#tar -zxvf nginx-1.2.5.tar.gz
#cd nginx-1.2.5
# ./configure --add-module=/root/software/mod_wsgi
config通过
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/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"
# make && make install
3、配置mod_wsgi
1)基本bin和配置文件
# cd /root/software/mod_wsgi
安装好后,先执行mod_wsgi中的配置程序setup.py。它会帮你把一些文件拷贝到nginx配置
mod_wsgi本身带了一些示例,全部放在代码的examples目录中。配置文件也有一个示例在这个目录中就叫做nginx.conf。
如果编译安装nginx不是默认配置,指定了安装目录,则运行本脚本需要指定目录
#python setup.py --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf --sbin-path=/usr/local/nginx/sbin
如果是默认,则无需
# python setup.py
using
prefix: /usr/local/nginx
sbin-path: /usr/local/nginx/sbin
conf-path: /usr/local/nginx/conf
done
增加
sbin
# ll /usr/local/nginx/sbin/
total 2908
-rwxr-xr-x 1 root root 2956970 Nov 30 12:09 nginx #原来
-rwxr-xr-x 1 root root 4584 Nov 30 12:18 ngx_wsgi.py
-rwxr-xr-x 1 root root 5322 Nov 30 12:18 ngx_wsgiref.py
-rwxr-xr-x 1 root root 3905 Nov 30 12:18 ngx_wsgitest.py
配置
/usr/local/nginx/conf/wsgi_vars
2)ngxin配置文件

# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf_bak

# cp /root/software/mod_wsgi/examples/nginx.conf /usr/local/nginx/conf/nginx.conf
vim /usr/local/nginx/conf/nginx.conf
worker_processes 2;
daemon off;
master_process on;

error_log logs/error.log info;

pid logs/nginx.pid;

events {

worker_connections 1024;
}

env HOME;

#env PYTHONPATH=/some/path; #修改 项目路径,可不填写
env PYTHON_EGG_CACHE=/tmp; #增加 #解决python执行egg包的权限问题

http {

#include conf/mime.types; #修改 路径问题
include mime.types;
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local] $request '

# '"$status" $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;

#tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65;

gzip off;

#ssl on;
#ssl_certificate conf/server.crt;
#ssl_certificate_key conf/server.key;

wsgi_python_optimize 0;

#wsgi_python_executable /usr/bin/python;
#wsgi_python_home /usr/;
wsgi_enable_subinterpreters on;

server {

listen 1234;
server_name localhost;

#include conf/wsgi_vars; # 修改 路径问题
include wsgi_vars;

#wsgi_middleware wsgiref.validate validator;

#wsgi_middleware paste.lint;

location / {

#client_body_buffer_size 50;
wsgi_pass /usr/local/nginx/nginx.py; #默认没有这个文件,需要拷贝,用于测试

wsgi_pass_authorization off;

wsgi_script_reloading on;
wsgi_use_main_interpreter on;
}
}
}
}
125 #gzip_types text/html text/plain; 需要注释
或者下面全部删除
3)测试脚本
# cp mod_wsgi/examples/nginx.py /usr/local/nginx/nginx.py
启动后还报错
没有nginx_module这模版
注释 #import nginx_module
4、启动
# /usr/local/nginx/sbin/nginx
http://172.16.9.252:1234/
会显示一些信息信息
5、部署自己的项目
1) 404 500页面
vim /usr/local/nginx/html/404
# cat /usr/local/nginx/html/404.html
<h1>Oh Noes!</h1>
# cat /usr/local/nginx/html/500.html
<h1>Oh Bad!</h1>
注意确保
#vim /usr/lib/python2.6/site-packages/rsync-api/myproject/settings.py
TEMPLATE_DIRS = (
'/usr/local/nginx/html/',
)
2)自己程序
rpm -vih rsync-api-0.0.1-1.noarch.rpm
2) 入口程序
(1)入口脚本
#vim /usr/lib/python2.6/site-packages/rsync-api/main.wsgi
# -*- coding: utf-8 -*-
import os, sys
sys.path.append('/usr/lib/python2.6/site-packages/rsync-api')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

import django.core.handlers.wsgi

_application = django.core.handlers.wsgi.WSGIHandler()

DEBUG = True

def application(environ, start_response):
environ['PATH_INFO'] = environ['SCRIPT_NAME'] + environ['PATH_INFO']
return _application(environ, start_response)
(2)nginx配置修改
vim /usr/local/nginx/conf/nginx.conf
location / {
#client_body_buffer_size 50;
#wsgi_pass /usr/local/nginx/nginx.py;修改为程序脚本
wsgi_pass /usr/lib/python2.6/site-packages/rsync-api/main.wsgi;

wsgi_pass_authorization off;

wsgi_script_reloading on;
wsgi_use_main_interpreter on;
}
4)测试
测试访问不存在的页面
测试访问引起500
http://172.16.9.252:1234/rsync/list
500
打开DEBUG可以看到是权限问题,
默认是nobody运行
nobody 18982 0.0 0.2 8024 2188 pts/2 S+ 18:01 0:00 nginx: worker process
nobody 18983 0.0 0.2 8024 2256 pts/2 S+ 18:01 0:00 nginx: worker process
无法对/etc/rsyncd.conf操作
# ll /etc/rsyncd.conf
-r-------- 1 root root 5901 Nov 30 14:14 /etc/rsyncd.conf
配置修改为
# vim /usr/local/nginx/conf/nginx.conf
user root;

二、使用nginx+uWSGI方式

http://projects.unbit.it/uwsgi/
http://projects.unbit.it/uwsgi/wiki/Install
http://projects.unbit.it/uwsgi/wiki/Quickstart
http://www.oschina.net/question/54100_30386
http://wiki.nginx.org/HttpUwsgiModuleMultipleDynamicApplications
http://projects.unbit.it/uwsgi/wiki/Example
1、下载
# cd software/
http://nginx.org/en/download.html
# wget http://nginx.org/download/nginx-1.2.5.tar.gz
http://projects.unbit.it/uwsgi/
# wget http://projects.unbit.it/downloads/uwsgi-1.4.2.tar.gz
2、安装
1) uwsgi
# yum -y install libxml2-dev
# tar -zxvf uwsgi-1.4.2.tar.gz
# cd uwsgi-1.4.2
#####
# make -f Makefile.Py26 #指定你python的版本,如果你的python是2.7 就应该是 make -f Makefile.Py27
cp uwsgi /usr/sbin/uwsgi
####这种方式老版本方式了
# python setup.py --help
# python setup.py build
running build
running build_py
creating build/lib
# python setup.py install
############## end of uWSGI configuration #############
*** uWSGI is ready, launch it with /usr/bin/uwsgi ***
running build
running build_py
running install_lib
copying build/lib/uwsgidecorators.py -> /usr/lib/python2.6/site-packages
byte-compiling /usr/lib/python2.6/site-packages/uwsgidecorators.py to uwsgidecorators.pyc
running install_egg_info
running egg_info
creating uWSGI.egg-info
writing uWSGI.egg-info/PKG-INFO
writing top-level names to uWSGI.egg-info/top_level.txt
writing dependency_links to uWSGI.egg-info/dependency_links.txt
writing manifest file 'uWSGI.egg-info/SOURCES.txt'
reading manifest file 'uWSGI.egg-info/SOURCES.txt'
writing manifest file 'uWSGI.egg-info/SOURCES.txt'
Copying uWSGI.egg-info to /usr/lib/python2.6/site-packages/uWSGI-1.4.2-py2.6.egg-info
running install_scripts
另外一种安装方式
# yum -y install python-pip.noarch
# pip-python install pip
未成功
python uwsgiconfig.py --build

cp uwsgi /usr/sbin/uwsgi

至此 uWSGI 就算是安装完成了,下一步安装 Nginx > 0.8 的版本,因为只有Nginx > 0.8 的版本才支持wsgi

2)、nginx

yum -y install nginx

3、配置nginx

如果没有
# cp ./nginx/uwsgi_params /etc/nginx/
#vi /etc/nginx/conf.d/test-uwsgi.conf
server {
listen 12345;
server_name test.skylog.com;
access_log /data/logs/sitelogs/test.skylog.com-access.log ;
error_log /data/logs/sitelogs/test.skylog.com-error.log ;

location / {

uwsgi_pass 127.0.0.1:8630;
include uwsgi_params;
}
}
全部
/etc/nginx/nginx.conf
user root;
worker_processes 2;
daemon off;
master_process on;

#error_log logs/error.log info;

pid /data/logs/nginx.pid;

events {

use epoll;
worker_connections 1024;
}

#env HOME;

#env PYTHONPATH=/var/www/rsync;
#env PYTHON_EGG_CACHE=/tmp;

http {

include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_port - $remote_user [$time_local] $request '

'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;

#tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65;

gzip off;

#ssl on;
#ssl_certificate conf/server.crt;
#ssl_certificate_key conf/server.key;

##wsgi_python_optimize 2;

##wsgi_python_executable /usr/bin/python;
#wsgi_python_home /usr;
#wsgi_enable_subinterpreters on;
server {
listen 12345;
server_name test.skylog.com;
access_log /data/logs/sitelogs/test.skylog.com-access.log ;
error_log /data/logs/sitelogs/test.skylog.com-error.log ;

location / {

uwsgi_pass 127.0.0.1:8630;
include uwsgi_params;
}
}

}

4、配置uwsgi

socket方式,比http方式好
1)配置文件
# vim /usr/lib/python2.6/site-packages/rsync-api/myproject.xml
<uwsgi>
<socket>127.0.0.1:8630</socket>
<listen>20</listen>
<master>true</master>
<pidfile>/tmp/uwsgi.pid</pidfile>
<processes>2</processes>
<module>wsgi</module> #就是wsgi.py
<pythonpath>/usr/lib/python2.6/site-packages/rsync-api/</pythonpath> #项目目录
<profiler>true</profiler>
<memory-report>true</memory-report>
<enable-threads>true</enable-threads>
<logdate>true</logdate>
<limit-as>6048</limit-as>
</uwsgi>
2)加载脚本
作用通过加载项目加载到项目和应用
也就是上面的<module>wsgi</module>
# vi /usr/lib/python2.6/site-packages/rsync-api/wsgi.py
import os,sys

if not os.path.dirname(__file__) in sys.path[:1]:

sys.path.insert(0, os.path.dirname(__file__))
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

from django.core.handlers.wsgi import WSGIHandler

application = WSGIHandler()

说明:

os.path.dirname(__file__):本文件所在目录
sys.path系统路径
3)启动
#uwsgi -x /usr/lib/python2.6/site-packages/rsync-api/myproject.xml
不行
成功的提示
Sat Dec 1 13:00:01 2012 - WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x9596c78 pid: 21343 (default app)
未成功的提示
Sat Dec 1 13:00:44 2012 - unable to load app 0 (mountpoint='') (callable not found or import error)
Sat Dec 1 13:00:44 2012 - *** no app loaded. going in full dynamic mode **

问题原因<module>jin</module>不能项目名相同,不能设置为<module>myproject</module>,也就是加载的脚本也不能设置为myproject.py

也不能叫uwsgi
myproject.xml 可以和项目相同

规范:

加载脚本:uwsgi 不能是uwsgi或者项目名,不然无法加载
完整xml:myproject.xml

测试nginx是root这种方式无法读取/etc/rsyncd.conf文件

快速启动方式

uwsgi --socket 127.0.0.1:8630 --pythonpath /usr/lib/python2.6/site-packages/rsync-api --module wsgi --processes 2
启动之后杀进程关不掉
# kill -9 `cat /tmp/uwsgi.pid`
5、启动
# /etc/init.d/nginx start
6、测试
http://192.168.78.250:12345/
7、也可以使用unix socket模式
socket = /tmp/i_am_a_unix_socket.sock
#vim /usr/lib/python2.6/site-packages/rsync-api/myproject1.xml
# cp /usr/lib/python2.6/site-packages/rsync-api/wsgi.py /usr/lib/python2.6/site-packages/rsync-api/rsync.py
uwsgi -x /usr/lib/python2.6/site-packages/rsync-api/myproject1.xml
uwsgi --socket /tmp/i_am_a_unix_socket.sock --pythonpath /usr/lib/python2.6/site-packages/rsync-api --module rsync --processes 2
module是其中一个app名称也报错
# uwsgi --socket /tmp/uwsgi.sock --pythonpath /usr/lib/python2.6/site-packages/rsync-api --module wsgi --processes 2
# vim /etc/nginx/nginx.conf
uwsgi_pass unix:///tmp/uwsgi.sock;

转载于:https://www.cnblogs.com/diege/archive/2012/12/01/2797308.html

你可能感兴趣的文章
【Java】synchronized与lock的区别
查看>>
django高级应用(分页功能)
查看>>
【转】Linux之printf命令
查看>>
关于PHP会话:session和cookie
查看>>
STM32F10x_RTC秒中断
查看>>
display:none和visiblity:hidden区别
查看>>
C#double转化成字符串 保留小数位数, 不以科学计数法的形式出现。
查看>>
牛的障碍Cow Steeplechase
查看>>
Zookeeper选举算法原理
查看>>
3月29日AM
查看>>
利用IP地址查询接口来查询IP归属地
查看>>
HTML元素定义 ID,Class,Style的优先级
查看>>
构造者模式
查看>>
http和https的区别
查看>>
Hbuild在线云ios打包失败,提示BuildConfigure Failed 31013 App Store 图标 未找到 解决方法...
查看>>
找到树中指定id的所有父节点
查看>>
今天新开通了博客
查看>>
AS3优化性能笔记二
查看>>
ElasticSearch(站内搜索)
查看>>
4----COM:a Generative Model for group recommendation(组推荐的一种生成模型)
查看>>