项目开发完毕后,需要将代码放到服务器上,这样用户才能访问。接下来我们一步一步来进行一波部署操作。
1. 简单粗暴
项目开发完毕,在部署之前需要再配置文件中将 ALLOWED_HOSTS配置设置为:当前服务器IP或*,如:
1
|
ALLOWED_HOSTS = [ "*" ,] |
示例源码:猛击下载
然后将源码上传至服务器指定目录,如:/data/ ,然后执行命令来运行:
注: 上传命令: scp /Users/wupeiqi/PycharmProjects/oldboy-1.zip root@192.168.10.33:/data/
解压,进入目录并执行以下命令:
1
|
python3 mange.py runserver 0.0 . 0.0 : 8000 |
2. uwsgi
Django框架运行依赖wsgi(本质提供socket服务端),众多模块实现了wsgi规范,而django框架中默认使用wsigiref模块来实现,他由于性能比较低,所以用于本地开发和测试,而线上部署时需要使用uwsgi来代替。
a. 在服务器上安装uwsgi
1
|
pip3 install uwsgi |
需要先安装:yum install python-devel
b. 单文件使用uwsgi
在服务器上编写一个Python文件:
1
2
3
|
def application(env, start_response): start_response( '200 OK' , [( 'Content-Type' , 'text/html' )]) return [b "Hello World" ] |
在服务器上执行命令启动Web服务器:
1
2
3
|
uwsgi - - http : 9001 - - wsgi - file app.py # 或 uwsgi - - http : 9002 - - wsgi - file foobar.py - - master - - processes 4 |
PS: flask需要再加 --callable app
访问查看即可:
c. django程序使用uwsgi
将开发好的django程序拷贝到服务器目录【同示例一】,即:/data/oldboy ,执行命令启动:
1
|
uwsgi - - http : 9005 - - chdir / data / oldboy / - - wsgi - file oldboy / wsgi.py - - master - - processes 4 |
1. 创建配置文件 oldboy.ini [uwsgi] http = 0.0.0.0:9005 chdir = /data/oldboy/ wsgi-file = oldboy/wsgi.py processes = 4 static-map = /static=/data/oldboy/allstatic 2. 根据配置文件启动uwsgi uwsigi --ini oldboy.ini
PS: 如果有虚拟环境可以添加上虚拟环境路径配置: virtualenv
=
/env
/oldboy
_venv
此时访问时,会出现找不到静态文件的错误。
想要uwsgi处理静态文件,需要先将django的静态文件收集到制定目录,然后再设置对应关系。
- 收集django静态文件
- 在django的配置文件中添加:STATIC_ROOT = os.path.join(BASE_DIR,"allstatic")
- 执行 python3 manage.py collectstatic 命令,至此django项目所有相关静态文件都会收集到制定目录。
- 设置uwsgi静态文件对应关系
- uwsgi --http :9005 --chdir /data/oldboy/ --wsgi-file oldboy/wsgi.py --master --processes 4 --static-map /static=/data/oldboy/allstatic
访问地址,即可看到你的女朋友们了....
3. nginx
利用Nginx做反向代理、负载均衡以及处理静态文件。
a. 安装Nginx
1
|
yum install nginx |
b. 配置nginx
user root; worker_processes 4; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { 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; tcp_nodelay on; keepalive_timeout 65; include /etc/nginx/mime.types; default_type application/octet-stream; upstream django { server 127.0.0.1:8001; # server 192.1.123.1; } server { listen 80; charset utf-8; # max upload size client_max_body_size 75M; location /static { alias /data/oldboy/allstatic; } location / { uwsgi_pass django; include uwsgi_params; } } }
确保nginx配置文件目录下有uwsgi_params文件(默认)
c. 配置uwsgi
为了确保让所有请求均通过80端口来访问网站,将uwsgi的配置文件修改为:
1
2
3
4
5
6
|
[uwsgi] socket = 127.0 . 0.1 : 9005 chdir = / data / oldboy / wsgi - file = oldboy / wsgi.py processes = 4 logto = / tmp / oldboy.log |
d. 启动uwsgi和nginx
1
2
3
|
uwsgi --ini /data/oldboy/oldboy .ini & /etc/init .d /nginx start |
3. supervisor
supervisor是一个对进程管理的软件,可以帮助我们启动uwsgi并维护(uwsgi进程关闭时,自动将其启动起来)。
a. 安装
1
|
yum install supervisor |
b. 配置 vim /etc/supervisor.conf
[supervisord] http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server) ;http_port=127.0.0.1:9001 ; (alternately, ip_address:port specifies AF_INET) ;sockchmod=0700 ; AF_UNIX socketmode (AF_INET ignore, default 0700) ;sockchown=nobody.nogroup ; AF_UNIX socket uid.gid owner (AF_INET ignores) ;umask=022 ; (process file creation umask;default 022) logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB) logfile_backups=10 ; (num of main logfile rotation backups;default 10) loglevel=info ; (logging level;default info; others: debug,warn) pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) nodaemon=false ; (start in foreground if true;default false) minfds=1024 ; (min. avail startup file descriptors;default 1024) minprocs=200 ; (min. avail process descriptors;default 200) ;nocleanup=true ; (don't clean up tempfiles at start;default false) ;http_username=user ; (default is no username (open system)) ;http_password=123 ; (default is no password (open system)) ;childlogdir=/tmp ; ('AUTO' child log dir, default $TEMP) ;user=chrism ; (default is current user, required if root) ;directory=/tmp ; (default is not to cd during start) ;environment=KEY=value ; (key value pairs to add to environment) [supervisorctl] serverurl=unix:///var/tmp/supervisor.sock ; use a unix:// URL for a unix socket ;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket ;username=chris ; should be same as http_username if set ;password=123 ; should be same as http_password if set ;prompt=mysupervisor ; cmd line prompt (default "supervisor") ; The below sample program section shows all possible program subsection values, ; create one or more 'real' program: sections to be able to control them under ; supervisor. ;[program:theprogramname] ;command=/bin/cat ; the program (relative uses PATH, can take args) ;priority=999 ; the relative start priority (default 999) ;autostart=true ; start at supervisord start (default: true) ;autorestart=true ; retstart at unexpected quit (default: true) ;startsecs=10 ; number of secs prog must stay running (def. 10) ;startretries=3 ; max # of serial start failures (default 3) ;exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) ;stopsignal=QUIT ; signal used to kill process (default TERM) ;stopwaitsecs=10 ; max num secs to wait before SIGKILL (default 10) ;user=chrism ; setuid to this UNIX account to run the program ;log_stdout=true ; if true, log program stdout (default true) ;log_stderr=true ; if true, log program stderr (def false) ;logfile=/var/log/cat.log ; child log path, use NONE for none; default AUTO ;logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) ;logfile_backups=10 ; # of logfile backups (default 10) [program:oldboy] command=/usr/local/bin/uwsgi /data/oldboy/oldboy.ini ;命令 priority=999 ; 优先级(越小越优先) autostart=true ; supervisord启动时,该程序也启动 autorestart=true ; 异常退出时,自动启动 startsecs=10 ; 启动后持续10s后未发生异常,才表示启动成功 startretries=3 ; 异常后,自动重启次数 exitcodes=0,2 ; exit异常抛出的是0、2时才认为是异常 stopsignal=QUIT ; 杀进程的信号 stopwaitsecs=10 ; 向进程发出stopsignal后等待OS向supervisord返回SIGCHILD 的时间。若超时则supervisord将使用SIGKILL杀进程 user=chrism ; 设置启动该程序的用户 log_stdout=true ; 如果为True,则记录程序日志 log_stderr=false ; 如果为True,则记录程序错误日志 logfile=/var/log/cat.log ; 程序日志路径 logfile_maxbytes=1MB ; 日志文件最大大小 logfile_backups=10 ; 日志文件最大数量
c. 启动
1
2
3
|
supervisord /etc/supervisor .conf 或 /etc/init .d /supervisor start |
来源:https://www.cnblogs.com/qinghuani/p/9289743.html