主要內容:ubuntu下 Django + MySQL + nginx + uwsgi
操作系統環境:ubuntu 14.04
一、相關軟件安裝:
首先升級apt-get : sudo apt-get update
Linux系統自帶python 主要使用的是python 2.7.6版本
python的相關目錄:/usr/bin/python 2.7.6 或 /usr/bin/python3.4
install MySQL: sudo apt-get install mysql-server mysql-client
installMySQLdb: sudo apt-get install python-mysqldb
install Nginx: sudo apt-get install nginx
install python-dev:sudo apt-get install python-dev
install uwsgi: pip install uwsgi
install Django:
sudo apt-get install python-pip
pip install django == 1.xx(我用的是1.8)
二、其原理爲:
the web client<-> the web server(nginx)<-> the socket<-> uwsgi <-> Django
1.便捷的配置:在項目的個目錄下建立mysite_nginx.conf配置文件
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///path/to/yourproject/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 80;
# the domain name it will serve for
server_name yourweb.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/yourproject/media; # your Django project's media files - amend as required
}
location /static {
alias /path/to/yourproject/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/yourproject/uwsgi_params; # the uwsgi_params file you installed
}
}
其中uwsgi_params文件,你需要從/etc/nginx/uwsgi_params拷貝到你項目的根目錄下。在mysite/yourproject/settings.py中的APPS中添加創建的app應用,配置DATABASES數據庫,
在靜態文件部分添加靜態文件的根目錄:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
在manage.py目錄下使用python manage.py collectstatic收集靜態文件
創建media目錄用於存放媒體文件。
爲了防止權限不夠,無法訪問相關文件,我在nginx.conf中更改用戶權限:
user root;
然後我們繼續在項目的根目錄下創建uwsgi.ini文件:
#uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /path/to/yourproject/
# Django's wsgi file
module = mysite.wsgi
# the virtualenv (full path)
#home = /path/to/virtualenv
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
threads = 2
# the socket (use the full path to be safe
socket = /path/to/yourproject/mysite.sock
# ... with appropriate permissions - may be needed
#chmod-socket = 666
# clear environment on exit
vacuum = true
將mysite_nginx.conf文件在/etc/nginx/sites-enabled/下建立連接文件
ln -s /path/to/yourproject/mysite_nginx.conf /etc/nginx/sites-enabled/
順便刪除/etc/nginx/sites-enabled下面的default文件,如果你想再次導入default文件
可以在/etc/nginx/sites-available/文件下找到。
隨後運行:
uwsgi --ini uwsgi.ini
成功後編輯/etc/rc.local文件,可以使uwsgi在開機時自動啓動
/usr/local/bin/uwsgi --socket /path/to/mysite.sock --module /path/to/mysite.wsgi
<span style="font-family:Microsoft YaHei;">#</span>我沒用<span style="font-family:Microsoft YaHei;">該權限</span>命令--chmod-socket=666
若以上有問題參考以下博客
主要參考網站:
http://blog.csdn.net/yjdlailin/article/details/50879449