折騰了兩天的django+nginx+uWSGI+ubuntu環境搭建,終于把它解決了,由于網上這方面的資料很少,出了問題在google和stackoverflow也沒找到答案,所以希望我的搭建過程對你有所幫助!少遇坑!
介紹
- Nginx是一個網頁服務器,它能反向代理HTTP, HTTPS, SMTP, POP3, IMAP的協議鏈接,以及一個負載均衡器和一個HTTP緩存
- uWSGI是一個web服務器,它同時實現了uwsgi協議和WSGI協議
以下圖片很好的說明它們的關系:
準備
- http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html (官方文檔 PS:不要覺得自己英文爛就不去看,其實官方的文檔才是最好的)
- 相關環境的版本:django-1.9.8, nginx 1.4.6, ubuntu14.04.1 LTS
配置(盡量按照官網的順序)
- 安裝環境
Django安裝
pip install Django
django-admin.py startproject mysite
cd mysite
uwsgi安裝
pip install uwsgi
創建測試文件test.py
# test.py
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
#return [b"Hello World"] # python3
return ["Hello World"] # python2
運行uwsgi
uwsgi --http :8000 --wsgi-file test.py
**訪問你的網站或者在瀏覽器輸入127.0.0.1:8000,如果看到Hello World則說明下面這三者是通的 **
the web client <-> uWSGI <-> Python
運行你的Django程序
python manage.py runserver 0.0.0.0:8000
訪問你的網站或者在瀏覽器輸入127.0.0.1:8000,如果看到It works,說明你的Django也是通的
在項目目錄下輸入以下命令,并且運行(其中mysite.wsgi是mysite/mysite/wsgi.py文件):
uwsgi --http :8000 --module mysite.wsgi
說明你的uwsgi到django也是通的:
the web client <-> uWSGI <-> Django
安裝nginx
sudo apt-get install nginx
sudo /etc/init.d/nginx start
訪問127.0.0.1或者你的網站,如果出現nginx則說明你的nginx可用(默認是是80端口)
the web client <-> the web server
下面步驟用來告訴nginx你的網站在哪里
首先從copy下面文件到你的項目
cp /etc/nginx/uwsgi_params ~/mysite
創建文件mysite_nginx.conf并寫入以下內容:
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /path/to/your/mysite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
創建一個軟鏈接到nginx,意思是告訴nginx你的網站配置信息
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
配置你的Django的setting.py
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
同步靜態文件
python manage.py collectstatic
重啟nginx
sudo /etc/init.d/nginx restart
在mysite/media目錄下放入一張圖片,media.png,并訪問 http://ip:8000/media/media.png或者http://127.0.0.1:8000/media/media.png則可以看到你的圖片
接下來打通nginx、uWSGI和test.py
uwsgi --socket :8001 --wsgi-file test.py
訪問http://ip:8000或者127.0.0.1:8000,如果正常,則說明以下是通的
the web client <-> the web server <-> the socket <-> uWSGI <-> Python
使用Unix sockets代替你的端口
在mysite_nginx.conf文件中修改:
server unix:///path/to/your/mysite/mysite.sock; # for a file socket# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
運行測試,在瀏覽器輸入 http://ip:8000/或http://127.0.0.1:8000/是否成功
uwsgi --socket mysite.sock --wsgi-file test.py
如果出現502錯誤或者在/var/log/nginx/error.log發現
connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permissiondenied)
則使用以下方法解決權限問題,本人使用第一種成功了~
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # (very permissive)
or:
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664 # (more sensible)```
##調通你的django、nginx和uwsgi
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
>在瀏覽器輸入 http://ip:8000/或http://127.0.0.1:8000/在網頁出現It works這說明成功
##創建一個uWSGI的初始化文件mysite_uwsgi.ini
mysite_uwsgi.ini file
[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 = 10
the socket (use the full path to be safe
socket = /path/to/your/project/mysite.sock
... with appropriate permissions - may be needed
chmod-socket = 664
clear environment on exit
vacuum = true
##運行
uwsgi --ini mysite_uwsgi.ini
##將uwsgi配置為Emperor模式(可不做)
create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
symlink from the default config directory to your config file
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/
run the emperor
uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
###(PS:在實際過程中,最后一句 我使用的是uwsgi --emperor /etc/uwsgi/vassals)
##將uwsgi配置為開機啟動,編輯/etc/rc.local
/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data --daemonize /var/log/uwsgi-emperor.log