nginx+uwsgi部署django

1、安裝uwsgipython

直接使用pip安裝就能夠了nginx

#pip install uwsgi    

安裝過程當中若是出現c++

Exception: you need a C compiler to builduWSGIdjango

是由於服務器上沒有c編譯器,先安裝瀏覽器

#yum install -y gcc gcc-c++  

安裝完成測試是否安裝正常服務器

在你django項目下執行app

#uwsgi --http :8000 --module django_project.wsgi  
 django_project.wsgi  指向你的項目的wsgi.py文件

好比個人目錄結構爲django_project ->django_project ->wsgi.py,則那條語句在第一個django_project 下執行dom

而後在瀏覽器輸入127.0.0.1:8000,若是出現如下界面,則說明安裝正常socket

 

2、安裝 Nginx

# cd downloadfile
# wget http://nginx.org/download/nginx-1.10.3.tar.gz  
# tar xf nginx-1.10.3.tar.gz  
# cd nginx-1.10.3  
# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module  
#make && make install 

 

可能在./configure編譯這一步會出現一些錯誤:測試

一、./configure: error:the HTTP gzip module requires the zlib library.
You can either disable the module by using–without-http_gzip_module
option, or install the zlib library into thesystem, or build the zlib 
library  statically from the source with nginx by using–with-zlib=<path> option.

則須要先安裝zlib-devel,而後再從新編譯,這樣邊能夠解決了

yum install -y zlib-devel 

 

二、./configure:  error: the HTTP rewrite module requires the PCRE library.

安裝pcre-devel解決問題

yum -y install pcre-devel

三、錯誤提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.   You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.

解決辦法:

yum  -y install openssl openssl-devel

安裝完成測試是否正常,執行

#/usr/local/nginx/sbin/nginx  

不必定是這樣的命令,具體看安裝的路徑,按我這樣裝通常就是這條命令

而後在瀏覽器打開127.0.0.1:80若是出現如下界面,說明安裝正常

 

3、配置uwsgi

uwsgi支持ini、xml等多種配置方式,下面以ini爲例,在/etc/目錄下新建myuwsgi.ini,並添加如下配置:

[uwsgi]  
  
# Django-related settings  
# the base directory (full path)  
# chdir           = /path/to/your/project  
# Django's wsgi file  
# module          = project.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          = 127.0.0.1:9090  
# ... with appropriate permissions - may be needed  
# chmod-socket    = 664  
# clear environment on exit  
vacuum          = true  

 

4、配置nginx

找到nginx的安裝目錄(如:/usr/local/nginx-1.5.6/),打開conf/nginx.conf文件,修改server配置或添加server配置

server {     
# the port your site will be served on     
listen 80;      
# the domain name it will serve for 
   
server_name 127.0.0.1,bradball.xin; #這裏是填你的域名或ip,而後在瀏覽器經過這個訪問  

charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { alias /home/brad/blog_project; # your Django project's media files - amend as required } location /static/ { alias /home/brad/blog_project; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location /

{
uwsgi_pass
127.0.0.1:9090; #必定要跟uwsgi配置的同樣
include uwsgi_params;
# the uwsgi_params file you installed
uwsgi_param UWSGI_CHDIR /home/brad/blog_project; #你的項目的路徑,最好用完整路徑
uwsgi_param UWSGI_SCRIPT blog_project.wsgi; #指向wsgi.py,相對於項目的根目錄
 }
}

 而後執行:

sudo uwsgi --ini /etc/myuwsgi.ini & /usr/local/nginx/sbin/nginx

 從新加載 nginx

#/usr/local/nginx/sbin/nginx -s reload
相關文章
相關標籤/搜索