Django開發個人部落格網站教程之伺服器centos7安裝uwsgi,centos7uwsgi
nginx+uwsgi+django是我們常用的django部署方式。nginx作為最前端的伺服器,他負責接收所有的用戶端請求,對於請求的靜態檔案,由nginx伺服器自己完成,因為它具有很好處理靜態檔案的能力,效能進行過最佳化,支援高並發量;uWSGI伺服器作為支援伺服器,是用來服務nginx的,nginx將請求的動態檔案交給uWSGI進行處理。uWSGI實現了uwsgi、wsgi和http協議,uwsgi協議是uWSGI自訂的協議,定義的是架構(django)和伺服器對接的介面。
1、安裝uwsgi
pip install uwsgi
2、建立設定檔
安裝完成後,將我們windows中的專案檔blog通過winscp傳遞到linux中,我們這裡直接放在root目錄下。
然後在/root/myblog/目錄下建立一個檔案夾conf
mkdir conf
然後建立uwsgi的設定檔:
vim uwsgi.ini
寫入
# mysite_uwsgi.ini file[uwsgi]# Django-related settings# the base directory (full path)chdir = /root/blog# Django's wsgi filemodule = blog.wsgi# the virtualenv (full path)# process-related settings# mastermaster = true# maximum number of worker processesprocesses = 5# the socket (use the full path to be safesocket = 127.0.0.1:8000# ... with appropriate permissions - may be needed# chmod-socket = 664# clear environment on exitvacuum = truevirtualenv = /root/.virtualenvs/blog_env_py3
3、對blog項目變更
我們在windows中是在開發環境下進行的,現在需要將其改成生產環境
setting.py
DEBUG = FalseALLOWED_HOSTS = ['*']# STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)STATIC_ROOT = os.path.join(BASE_DIR, 'static')
urls.py
from blog.settings import STATIC_ROOT#添加靜態檔案的訪問處理函數url(r'^static/(?P.*)/$', serve, {'document_root': STATIC_ROOT}),
更改haystack中的源碼
進入Virtualenv\blog_env_py3\lib\python3.6\site-packages\haystack\utils\highlighting.py
添加上
if len(self.text_block) < self.max_length: return self.text_block[:start_offset] + highlighted_chunk
4、配置nginx
在conf檔案夾下建立nginx設定檔案
vim uc_ngnix.conf
寫入
# the upstream component nginx needs to connect toupstream django {# server unix:///path/to/your/mysite/mysite.sock; # for a file socketserver 127.0.0.1:8000; # for a web port socket (we'll use this first)}# configuration of the serverserver {# the port your site will be served onlisten 80;# the domain name it will serve forserver_name 20.20.20.20 www.geerniya.cn ; # substitute your machine's IP address or FQDNcharset utf-8;# max upload sizeclient_max_body_size 75M; # adjust to taste# Django medialocation /media { alias /root/blog/media/; # 指向django的media目錄}location /static { alias /root/blog/static/; # 指向django的static目錄}# Finally, send all non-media requests to the Django server.location / { uwsgi_pass django; include uwsgi_params; # the uwsgi_params file you installed}}
server_name為你的公網ip地址和備案後的網域名稱
將該設定檔加入到nginx的啟動設定檔中
ln -s /root/myblog/conf/uc_nginx.conf /etc/nginx/conf.d/
運行命令, 搜集所有靜態檔案到static下,包括admin中的
python manage.py collectstatic
最後通過navicat將資料庫匯入到伺服器中,配置即可完成。