Centos7 使用uwsgi+nginx部署django
阿新 • • 發佈:2020-11-25
Centos7 使用uwsgi+nginx部署django
- 安裝wget用於下載python原始碼包
yum install wget
- 安裝python需要的元件
yum -y install zlib-devel bzip2-devel openssl-devel libffi-devel ncurses-devel sqlite-sevel readline-devel tk-devel gcc make
- 下載python原始碼包
""" 1. 下載python原始碼包 wget url 2.解壓原始碼包 3.進入python目錄,執行自檢命令 ./configure --prefix=/usr/local/python37 # --prefix後面跟的是安裝路徑 4. 安裝 make && make install 5.建立快捷命令(軟連線) ln -s /usr/local/python37/bin/python3 /usr/bin/python3 6.安裝虛擬環境 yum install python-virtualenv 7.建立虛擬環境 virtualenv -p /usr/local/python37/bin/python3 ./venv/python3 # python所在的路徑 虛擬環境建立的路徑 進入虛擬環境:source python3/bin/activate 退出虛擬環境:deactivate
- 安裝uwsgi
pip3 install uwsgi -i https://pypi.doubanio.com/simple
- uwsgi配置檔案
[uwsgi] # 對外提供 http 服務的埠 http = 0.0.0.0:8000 #the local unix socket file than commnuincate to Nginx 用於和 nginx 進行資料互動的埠 socket = 127.0.0.1:8001 # the base directory (full path) django 程式的主目錄 chdir =../zhanhong_erp # 指定專案的application
uwsgi配置啟動:uwsgi --ini uwsgi.ini 停止:uwsgi --stop uwsgi.pid 重啟:uwsgi --reload uwsgi.pid
- 安裝nginx
yum install nginx -y
# =====================================全域性配置==================================================== vi /etc/nginx/nginx.conf user nginx; # 程序數量 worker_processes 4; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { # 最大連線數量 worker_connections 4096; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
nginx全域性配置# ===================================== 單專案配置 ================================== vi /etc/nginx/conf.d/專案名.conf # ============================= 正常配置 ====================================== server { listen 80; server_name 127.0.0.1; access_log /var/log/nginx/access.log main; charset utf-8; gzip on; gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; error-page 404 /404.html; error_page 500 502 503 504 /50x.html; # 指定專案路徑 location / { include uwsgi_params; uwsgi_connect_timeout 30; uwsgi_pass unix;/opt/project_zhanhong/script/uwsgi.sock; } #指定靜態檔案路徑 location /static/{ alias /opt/project_teacher/teacher/static/; index index.html index.htm; } }
單專案配置# ===================================== 單專案配置 ================================== vi /etc/nginx/conf.d/專案名.conf # ============================= 負載均衡配置 ======================================== upstream mywebserver { # 這裡寫伺服器叢集 server 127.0.0.1:8000; } server { listen 80; server_name 0.0.0.0; charset UTF-8; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; client_max_body_size 75M; location / { # 解決下載大檔案超時的問題 proxy_read_timeout 6000s proxy_pass http://mywebserver; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
使用負載均衡的配置啟動:systemctl start nginx.service 停止:systemctl stop nginx.service 重啟:systemctl restart nginx.service
- 遇到的錯誤1
""" 系統啟動Nginx後,報 [emerg] bind() to 0.0.0.0:XXXX failed (13: Permission denied)錯誤的處理方式,分為兩種: 第一種:埠小於1024的情況: 原因是1024以下埠啟動時需要root許可權,所以sudo nginx即可。 第二種:埠大於1024的情況: 這種情況,需要如下操作: 首先,檢視http允許訪問的埠: semanage port -l | grep http_port_t http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000 其次,將要啟動的埠加入到如上埠列表中 semanage port -a -t http_port_t -p tcp 8090 CentOS安裝semanage的方法 yum install semanage 如果提示:No package semanage available.使用下面方法 yum provides semanage 執行成功之後 (提示:Filename : /usr/sbin/semanage) yum -y install policycoreutils-python.x86_64 安裝成功 """
- 遇到的錯誤2
""" connect() to 192.168.1.118:8888 failed (13: Permission denied) 檢視selinux是否開啟,如果開啟需要關閉 [root@localhost ~]# getenforce Enforcing [root@localhost ~]# setenforce 0 [root@localhost ~]# getenforce Permissive [root@localhost ~]# sed -i 's/\(^SELINUX=\).*/\SELINUX=disabled/' /etc/selinux/config 網上還有一種說法,在此記錄下。執行下面的命令 setsebool -P httpd_can_network_connect 1 """
- django 靜態檔案問題
# 在settings中新增 STATIC_ROOT = os.path.join(BASE_DIR, "static") # STATICFILES_DIRS必須為空否者會報錯 然後執行 python manage.py collectstatic 這時靜態檔案被提取到專案根目錄的 static下
然後配置路由from django.views import static
from django.conf import settings# 新增這個路由
re_path(r'^static/(?P<path>.*)$', static.serve,
{'document_root': settings.STATIC_ROOT}, name='static')