项目实测环境:
- 虚拟机:VirtualBox
- Python:python3.6.6
- Django:2.4.0
- OS:CentOs7
- 虚拟机的IP:10.20.166.101(可以正常ping通主机)
- 架构:使用nginx访问静态文件,使用uwsgi与python应用程序通信
- Django项目:
BinglanProject/ ├── BinglanProject ├── binlan_app ├── manage.py ├── media # 媒体文件 存放mp4文件 └── static # 使用python3 manage.py collectstatic 产生的静态文件目录
Django项目
-
确保Django项目可以在centos下正常运行
Nginx环境设置
-
安装nginx
- 下载
wget http://nginx.org/download/nginx-1.14.0.tar.gz
- 解压
tar zxvf nginx-1.14.0.tar.gz
- 编译安装
cd nginx-1.14.0 ./configure --prefix=/usr/local/nginx make && make install
- 执行
/usr/local/nginx/sbin/nginx
- 打开浏览器访问 10.20.166.101 显示如下则nginx安装成功
- 打开浏览器访问 10.20.166.101 显示如下则nginx安装成功
- 下载
-
nginx访问静态文件
-
编写nginx配置文件,我们只需要复制一份原本nginx的配置文件,稍加修改即可
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/test.conf
-
修改配置文件
- vim /usr/local/nginx/conf/test.conf
listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location /media/ { alias /home/lanlan/BinglanProject/media/; } location / { root html; index index.html index.htm; }
Tips: 添加了media的location,/home/lanlan/BinglanProject/media/目录里面存放的是我的静态文件,两个MP4文件
-
运行nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/test.conf
,打开浏览器访问10.20.166.101(显示正常)
-
访问静态文件,10.20.166.101/media/1.mp4
111.PNG
-
查看nginx错误日志(Permission denied)
error.PNG
Tips: 错误日志显示 Permission denied,是因为没有足够的权限,所以需要配置nginx用户为系统用户,我使用root用户运行nginx则配置nginx以root用户运行。
-
更改nginx配置文件
vim /usr/local/nginx/conf/test.conf
user root; # 修改用户为root worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include 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"';
- 从新运行nginx
pkill nginx /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/test.conf
- 访问 10.20.166.101/media/1.mp4 (显示正常)
su.png -
到此,我们nginx基本配置完成
-
uWsgi配置
-
安装uWsgi
我们使用pip安装uwsgipip3 install uwsgi
-
测试uwsgi
- 新建 test.py 写入如下内容
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"]
- 运行
uwsgi uwsgi --http :8000 --wsgi-file test.py
- 浏览器访问 虚拟机IP 10.20.166.101:8000显示 Hello World 则uwsgi运行正常
-
uWsgi运行Django项目
- 与manage.py同级目录创建uwsgi.conf,写入如下内容
[uwsgi] chdir = /home/lanlan/BinglanProject ; 项目的根目录 master = true processes = 4 threads = 2 http = :8000 static-map = /static=/home/lanlan/BinglanProject/static vacuum = true home = /home/lanlan/BinglanProject/venv module = BinglanProject.wsgi
- 在项目根目录执行
uwsgi --ini uwsgi.conf
,浏览器访问 虚拟机IP 10.20.166.101:8000显示正常即可。
Django+Nginx+uWsgi部署
Tips: 我的项目目录
(venv) [lanlan@localhost ~]$ pwd
/home/lanlan
(venv) [lanlan@localhost ~]$ tree -L 1 BinglanProject/
BinglanProject/
├── BinglanProject
├── binlan_app
├── binlan_vue
├── manage.py
├── media
├── requirments.txt
├── static
├── test.py
├── uwsgi.ini
└── venv
- Nginx配置文件(存放在/usr/local/nginx/conf/binglan.conf)
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server unix:///home/lanlan/BinglanProject/uwsgi.sock
#server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location /media/ {
alias /home/lanlan/BinglanProject/media/;
}
location /static/ {
alias /home/lanlan/BinglanProject/static/;
}
location / {
uwsgi_pass django;
include /usr/local/nginx/conf/uwsgi_params;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
- uWsgi配置文件(存放在/home/lanlan/BinglanProject/uwsgi.conf)
[uwsgi]
chdir = /home/lanlan/BinglanProject
master = true
processes = 4
threads = 2
; static-map = /static=/home/lanlan/BinglanProject/static
vacuum = true
home = /home/lanlan/BinglanProject/venv
module = BinglanProject.wsgi
socket = /home/lanlan/BinglanProject/uwsgi.sock
daemonize = /home/lanlan/BinglanProject/uwsgi.log
到此,Django+uWsgi+Nginx部署完成
网友评论