Linux系统上Nginx+Python的web.py与Django框架环境
1.编译nginx
在网上买了一本《实战nginx-取代Apache的高性能服务器》,写的比较浅,主要是些配置方面的东西,不过却正是目前我所需要的。由于需要支持https和rewrite,所以除了nginx的源码之外,又下载了 openssl-0.9.8r.tar.gz 和 pcre-8.12.tar.gz,把他们和nginx-1.0.4.tar.gz放到同一个目录。
为了方便编译,笔者写了一个脚本,代码如下:
#!/bin/bash #============================================================================= #脚本所在绝对目录 abs_path(){ local path=$1 local basename=$( basename $path ) local dirname=$( dirname $path ) cd $dirname if [ -h $basename ]; then path=$( readlink $basename ) abs_path $path else pwd fi } #============================================================================= #依赖的目录 src_base_dir=$( abs_path $0 ) src_openssl_dir=$src_base_dir'/openssl-0.9.8r' src_pcre_dir=$src_base_dir'/pcre-8.12' src_nginx_dir=$src_base_dir'/nginx-1.0.4' #============================================================================= #目标的目录 dest_base_dir=$src_base_dir'/release' dest_nginx_dir=$dest_base_dir'/nginx' #============================================================================= #把所有的tar.gz解压 find . -name "*.tar.gz" | xargs -IX tar zxvf X #============================================================================= #编译nginx cd $src_nginx_dir chmod u+x ./configure ./configure --with-http_stub_status_module --with-http_ssl_module --with-openssl=$src_openssl_dir --with-pcre=$src_pcre_dir --prefix=$dest_nginx_dir make && make install
2.配置nginx
在server配置项下增加
location / { #这两种方法都可以,只不过spawn-cgi启动的方法不同 #fastcgi_pass 127.0.0.1:9002; fastcgi_pass unix:webpy.sock; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param QUERY_STRING $query_string; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name; }
这里的3个location配置分别解决了,与python进程通信、django后台管理端样式存放、网站样式存放的问题。对照着apache的配置来看,就很容易明白了
WSGIPythonEggs /tmp <VirtualHost *> ServerName fuload.qq.com WSGIScriptAlias / /home/dantezhu/htdocs/fuload/conf/setting.wsgi <Directory /> Options FollowSymLinks AllowOverride Order allow,deny Allow from all </Directory> <Directory "/home/dantezhu/htdocs/fuload/mysite"> Order Deny,Allow Deny from all </Directory> Alias /admin_media "/usr/local/lib/python2.7/site-packages/django/contrib/admin/media" <Directory "/usr/local/lib/python2.7/site-packages/django/contrib/admin/media"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing </Directory> #AliasMatch /site_media/(.*\.(css|gif|png|jpg|jpeg)) /home/dantezhu/htdocs/fuload/media/$1 Alias /site_media /home/dantezhu/htdocs/fuload/media/ <Directory "/home/dantezhu/htdocs/fuload/media/"> Order allow,deny Options Indexes Allow from all IndexOptions FancyIndexing </Directory> </VirtualHost>
3.安装fastcgi依赖
需要到 http://trac.saddi.com/flup下载安装,之后fastcgi才能够正常启动。
4.启动django
创建django project的过程我们就不说了,只列出启动/停止的命令:
启动:
#python manage.py runfcgi daemonize=true pidfile=`pwd`/django.pid host=127.0.0.1 port=9001 maxrequests=1 & python manage.py runfcgi daemonize=true pidfile=`pwd`/django.pid socket=/home/dantezhu/nginx/sbin/django.sock maxrequests=1 &
停止:
kill -9 `cat django.pid`
启动nginx
启动:
./nginx -p /home/dantezhu/nginx/
停止:
kill -QUIT `cat ../logs/nginx.pid`
重新载入配置:
./nginx -t -c `pwd`/../conf/nginx.conf kill -HUP `cat ../logs/nginx.pid`
成功显示了django的后台界面:
PPPPPPPPPPPPPPPPPPPPP1
5.部署web.py版
安装依赖
spawn-cgi
flup
配置nginx
在server配置项下增加
location / { #这两种方法都可以,只不过spawn-cgi启动的方法不同 #fastcgi_pass 127.0.0.1:9002; fastcgi_pass unix:webpy.sock; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param QUERY_STRING $query_string; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name; }
一个简单的index.py
#!/usr/bin/python # -*- coding: utf-8 -*- import web urls = ("/.*", "hello") app = web.application(urls, globals()) class hello: def GET(self): return 'Hello, world!' if __name__ == "__main__": web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr) app.run()
并执行:
chmod +x index.py
.启动web.py
启动:
#spawn-fcgi -P `pwd`/webpy.pid -f /home/dantezhu/htdocs/ngx_web/index.py -a 127.0.0.1 -p 9002 & spawn-fcgi -P `pwd`/webpy.pid -f /home/dantezhu/htdocs/ngx_web/index.py -s /home/dantezhu/nginx/sbin/webpy.sock &
停止:
kill -9 `cat webpy.pid`
启动nginx
加入到rc.local中,自动启动
/home/dantezhu/nginx/sbin/start.sh sudo -u dantezhu /home/dantezhu/htdocs/ngx_django/mysite/start.sh sudo -u dantezhu /home/dantezhu/htdocs/ngx_web/start.sh
- Linux下安装Python3和django并配置mysql作为django默认服务器方法
- Linux下将Python的Django项目部署到Apache服务器
- 在Linux系统上部署Apache+Python+Django+MySQL环境
- 教你安装python Django(图文)
- linux环境下Django的安装配置详解
转载请注明出处:http://www.lishuoershouche.com/article/20230331/234840.html
随机推荐
-
在Linux系统上安装Linux内核头文件的教程
当你在编译一个设备驱动模块时,你需要在系统中安装内核头文件。内核头文件同样在你编译与内核直接链接的用户空间程序时需要。当你在这些情况下安装内核头文件时,你必须确保内核头文件精确地与你当前内核版本匹配(比如:3.13.0-24-generi...
-
移植新内核到Linux系统上的操作步骤
1、在ubuntu官网下载ubuntu16.04的镜像和对应ubuntu16.04的内核版本源代码,或者在镜像源上找 2、安装ubuntu16.04到PC主机上 接下来执行以下: 编译新的Linux内核给X86内核使用出现以下错误: sc...
-
Linux系统上用源码安装OpenSSL的方法
先下载openssl 1.0.1g版本,命令如下: 复制代码代码如下:#wget -c https://www.openssl.org/source/openssl-1.0.1g.tar.gz 再下载这个版本的md5校验包: 复制...
-
在Linux系统上通过uWSGI配置Nginx+Python环境的教程
1.安装ubuntu有uwsgi的ppa: add-apt-repository ppa:stevecrozz/ppa apt-get update apt-get install uwsgi 2. 用uwsgi代替mod_...
-
CUnit-2.1-3在Linux系统上的安装[引用]
文章出处: http://blog.csdn.net/bbwang8088/article/details/70139405?locationNum=9fps=1 1 下载CUnit安装包CUnit-2.1-3.tar.bz2保存至/ho...
-
在Linux系统上同时监控多个Oracle数据库表空间的方法
一,设计背景 由于所在公司ORACLE数据库较多,传统人工监控表空间的方式较耗时,且无法记录历史表空间数据,无法判断每日表空间增长量,在没有gridcontrol/cloudcontrol软件的情况下,笔者设计如下表空间监控方案,大家也...
-
Linux系统上实现定时重启Tomcat服务脚本介绍
目录一、创建Shell脚本二、修改文件权限三、建立定时任务四、定时任务描述语法1. 星号(*)2. 逗号(,)3. 斜杠(/)4. 横杠(-)五、其他因为使用阿里云部署服务器应用时,Tomcat服务经常由于内存不足出现OutOfMemor...
-
在Linux系统上部署Apache+Python+Django+MySQL环境
Linux+apache+mysql+python+mod_python+Django 说明:系统rhel 5.3,默认安装httpd、mysql,没有安装的,请下载安装RPM包,删除/etc/httpd/modules/mod_pyth...
-
在Linux系统上查看Apache服务器的错误日志
错误日志和访问日志文件为系统管理员提供了有用的信息,比如,为 Web 服务器排障,保护系统不受各种各样的恶意活动侵犯,或者只是进行各种各样的分析以监控 HTTP 服务器。根据你 Web 服务器配置的不同,其错误/访问日志可能放在你系统中不...
-
在Linux系统上安装Oracle数据库
前期准备:我用的是虚拟机上的CentOS 64位系统。所以需要设置网卡,时间,EPEL源,安装一些必备的软件。 1.1在虚拟机上安装好Linux系统后选择虚拟机设置—网络适配器—网络连接—桥接模式 1.2然后进入系统配置网卡。 永久...