ubuntu12.04 python2.7.3+uwsgi+nginx

經過本身的不斷堅持的努力,終於找到了這個解決方案 php

由於個人是ubuntu12.04 css

因此python的版本默認是2.7.3 html

nginx是經過sudo  apt-get isntall nginx安裝的 python

uwsgi也是經過sudo apt-get install uwsgi安裝的 nginx

django的版本是1.4的,是經過sudo easy-install django安裝的 web

若是常常使用ubuntu的TX確定知道,nginx的配置文件在哪裏,作虛擬配置的文件在哪裏 shell

通常的是在 django

/etc/nginx/sites-avaliable ubuntu

/etc/nginx/sites-enabled(這個目錄必須有/etc/nginx/site-avaliable這個目錄下你要使用的軟連接,不知道爲何ubuntu爲何會這麼作,不過用習慣了,我是感受挺好的) 瀏覽器

個人配置文件目錄結果是這樣的

/etc/nginx/sites-avaliable/local.pythonweb.com
/etc/nginx/sites-enabled/local.pythonweb.com #這個是軟連接,作配置編輯的時候只要修改/etc/nginx/sites-avaliable/local.pythonweb.com這個文件就能夠了

1,nginx的配置

/etc/nginx/sites-avaliable/local.pythonweb.com的內容是首先複製了一下/etc/nginx/sites-avaliable/default文件直接修改的

修改後的內容爲

# You may add here your
# server {
#	...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
	listen   80; ## listen for ipv4; this line is default and implied
	#listen   [::]:80 default ipv6only=on; ## listen for ipv6

	#root /usr/share/nginx/www/pythonweb/mysite;
	#index index.html index.htm;

	# Make site accessible from http://localhost/
	server_name local.pythonweb.com;

	access_log    /var/log/nginx/local.pythonweb.com.access.log;
	error_log    /var/log/nginx/local.pythonweb.com.error.log;

	#location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to index.html
		#try_files $uri $uri/ /index.html;
		# Uncomment to enable naxsi on this location
		# include /etc/nginx/naxsi.rules
	#}
	location / {
		include uwsgi_params;   
		uwsgi_pass 127.0.0.1:9090; root /usr/share/nginx/www/pythonweb/mysite; #web site的根目錄
		#root   /usr/share/nginx/www/pythonweb/mysite/mysite;  
		#index  index.html index.htm;
		#sudo uwsgi -s 127.0.0.1:9001 -w code
		#uwsgi -s :9090 -w uws_app -p10 -d uws.error
	}

	location /static/ {
		root	/usr/share/nginx/www/pythonweb/mysite;
		access_log  off;
	}

	location /static/admin/ {
		root	/usr/share/nginx/www/pythonweb/mysite/static;
		access_log  off;
	}

	location ~ ^.+\.(gif|jpg|png|ico|jpeg)$ {
		expires 3d;
	}

	location ~ ^.+\.(css|js)$ {
	   expires 12h;
	}

	#location /doc/ {
	#	alias /usr/share/doc/;
	#	autoindex on;
	#	allow 127.0.0.1;
	#	deny all;
	#}

	# Only for nginx-naxsi : process denied requests
	#location /RequestDenied {
		# For example, return an error code
		#return 418;
	#}

	#error_page 404 /404.html;

	# redirect server error pages to the static page /50x.html
	#
	#error_page 500 502 503 504 /50x.html;
	#location = /50x.html {
	#	root /usr/share/nginx/www;
	#}

	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
	#
	#location ~ \.php$ {
	#	fastcgi_split_path_info ^(.+\.php)(/.+)$;
	#	# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
	#
	#	# With php5-cgi alone:
	#	fastcgi_pass 127.0.0.1:9000;
	#	# With php5-fpm:
	#	fastcgi_pass unix:/var/run/php5-fpm.sock;
	#	fastcgi_index index.php;
	#	include fastcgi_params;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#	listen 8000;
#	listen somename:8080;
#	server_name somename alias another.alias;
#	root html;
#	index index.html index.htm;
#
#	location / {
#		try_files $uri $uri/ /index.html;
#	}
#}


# HTTPS server
#
#server {
#	listen 443;
#	server_name localhost;
#
#	root html;
#	index index.html index.htm;
#
#	ssl on;
#	ssl_certificate cert.pem;
#	ssl_certificate_key cert.key;
#
#	ssl_session_timeout 5m;
#
#	ssl_protocols SSLv3 TLSv1;
#	ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
#	ssl_prefer_server_ciphers on;
#
#	location / {
#		try_files $uri $uri/ /index.html;
#	}
#}
上面 紅色的部分是建立的pyhton的項目目錄

=============================

這裏要說一下,我在查找資料的時候

不知道爲何有目錄文件結構跟個人同樣,也許是版本的問題,因此我這裏強調的是django我用的是1.4版本的

最初建立後的目錄結構是

-/usr/share/nginx/www/pythonweb/mysite/

-/usr/share/nginx/www/pythonweb/mysite/manage.py

-/usr/share/nginx/www/pythonweb/mysite/mysite/

-/usr/share/nginx/www/pythonweb/mysite/mysite/__init__.py

-/usr/share/nginx/www/pythonweb/mysite/mysite/settings.py

-/usr/share/nginx/www/pythonweb/mysite/mysite/urls.py

-/usr/share/nginx/www/pythonweb/mysite/mysite/wsgi.py

2,django和uwsgi的配置

我使用是經過配置xml文件的形式啓動uwsgi的一些配置的

sudo mkdir -p /home/uwsgi
sudo touch /home/uwsgi/uwsgi.xml

uwsgi.xml的內容以下

<uwsgi>  

  <socket>127.0.0.1:9090</socket>  

  <listen>200</listen>  

  <master>true</master>  

  <pidfile>/usr/share/nginx/uwsgi.pid</pidfile>  

  <processes>8</processes>  

  <pythonpath>/usr/share/nginx/www/pythonweb/mysite</pythonpath>  

  <pythonpath>/usr/share/nginx/www/pythonweb/mysite</pythonpath>  

  <module>django_wsgi</module>  

  <profiler>true</profiler>  

  <memory-report>true</memory-report>  

  <enable-threads>true</enable-threads>  

  <logdate>true</logdate>  

  <limit-as>6048</limit-as>  

  <daemonize>/var/www/logs/django.log</daemonize>

</uwsgi>
接下來是
touch /usr/share/nginx/www/pythonweb/mysite/django_wsgi.py
sudo gedit /usr/share/nginx/www/pythonweb/mysite/django_wsgi.py
django_wsgi.py的內容爲
import os
import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
application = django.core.handlers.wsgi.WSGIHandler()
那麼最後個人項目的目錄結構爲

-/usr/share/nginx/www/pythonweb/mysite/

-/usr/share/nginx/www/pythonweb/mysite/manage.py

-/usr/share/nginx/www/pythonweb/mysite/mysite/

-/usr/share/nginx/www/pythonweb/mysite/mysite/__init__.py

-/usr/share/nginx/www/pythonweb/mysite/mysite/settings.py

-/usr/share/nginx/www/pythonweb/mysite/mysite/urls.py

-/usr/share/nginx/www/pythonweb/mysite/mysite/wsgi.py

-/usr/share/nginx/www/pythonweb/mysite/mysite/django_wsgi.py

啓動服務:

sudo uwsgi -x /home/uwsgi/uwsgi.xml
sudo /etc/init.d/nginx restart

這樣部署完成了

輸入http://local.pythonweb.com/會有python的默認提示成功的界面

ubuntu server 12.04 安裝nginx+uwsgi+django環境

另外一種方式的我沒有試過,有時間試試 


檔案補充:

不知道什麼緣由,尤其同仁遇到了這樣的問題:

2012/11/07 13:58:36 [error] 9116#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:9090", host: "localhost"

我從新通過測試,確實遇到了問題,個人解決方案是:

uwsgi的啓動方式改成ini方式,即

sudo uwsgi --ini ./uwsgi.ini
下面我來詳細講解一下:

項目的目錄結構爲

-/usr/share/nginx/www/pythonweb/mysite/

-/usr/share/nginx/www/pythonweb/mysite/manage.py

-/usr/share/nginx/www/pythonweb/mysite/mysite/

-/usr/share/nginx/www/pythonweb/mysite/mysite/__init__.py

-/usr/share/nginx/www/pythonweb/mysite/mysite/settings.py

-/usr/share/nginx/www/pythonweb/mysite/mysite/urls.py

-/usr/share/nginx/www/pythonweb/mysite/mysite/wsgi.py

-/usr/share/nginx/www/pythonweb/mysite/mysite/django_wsgi.py

------------------------------------------------------------

這裏咱們無論django_wsgi.py,只是將裏面的內容複製一份給wsgi.py(經過此次加深理解,其實我知道對於uwsgi的配置,django已經作好了),即-/usr/share/nginx/www/pythonweb/mysite/mysite/wsgi.py這個文件。

接下來編寫uwsgi.ini文件,這個文件能夠放在/home/uwsgi/uwsgi.ini,內容爲

[uwsgi]
socket = /tmp/mysite.socket
#http=127.0.0.1:9090
chdir=/usr/share/nginx/www/pythonweb/mysite
module=mysite.wsgi
master=True
pidfile=/tmp/uwsgi.pid
vacuum=True
max-requests=5000
daemonize=/var/log/uwsgi/uwsgi.log
同時修改:nginx相關的配置文件,即 /etc/nginx/sites-avaliable/local.pythonweb.com

找到

location / {
		include uwsgi_params;   
		uwsgi_pass 127.0.0.1:9090; root /usr/share/nginx/www/pythonweb/mysite; #web site的根目錄
		#root   /usr/share/nginx/www/pythonweb/mysite/mysite;  
		#index  index.html index.htm;
		#sudo uwsgi -s 127.0.0.1:9001 -w code
		#uwsgi -s :9090 -w uws_app -p10 -d uws.error
	}
修改成

location / {
		include uwsgi_params;   
		uwsgi_pass unix:///tmp/mysite.socket;
	}

到這裏應該配置的文件都已經結束了

能夠從新啓動nginx和uwsgi啦

#從新啓動nginx
sudo nginx -s reload
#從新啓動uwsgi
sudo uwsgi --ini /home/uwsgi/uwsgi.ini
這樣你再打開瀏覽器,就應該有結果了,呵呵
相關文章
相關標籤/搜索