项目目录
路径:/opt/project_teacher
├—— teacher
│ ├—— __init__.py
│ ├—— settings.py
│ ├—— urls.py
│ └—— wsgi.py
├—— app01
│ ├—— urls.py
│ ├—— views.py
└—— script
│ ├—— uwsgi.ini # 该文件是uwsgi配置文件
└—— manage.py
一、Nginx
1. 安装$ sudo apt-get install nginx #安装
2. 检查nginx是否安装成功$ /etc/init.d/nginx start
[ ok ] Starting nginx (via systemctl): nginx.service.
检查nginx是否启动成功$ ps -ef|grep -i nginx
root 6961 1 0 03:56 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 6962 6961 0 03:56 ? 00:00:00 nginx: worker process
pala 6985 2090 0 03:57 pts/0 00:00:00 grep --color=auto -i nginx
然后打开浏览器,访问ip地址,出现如下页面即代表nginx安装完成且可以正常启动。
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
Nginx常用命令$ /etc/init.d/nginx start #启动
$ /etc/init.d/nginx stop #关闭
$ /etc/init.d/nginx restart #重启
$ killall nginx 杀死所有nginx
# 如果是生产环境的话Nginx正在运行,就不要直接stop start 或者 restart 直接reload就行了
# 对线上影响最低
$ /etc/init.d/nginx reload
二、Gunicorn
Gunicorn 一般用来管理多个进程,有进程挂了Gunicorn 可以把它拉起来,防止服务器长时间停止服务,还可以动态调整 worker 的数量,请求多的时候增加 worker 的数量,请求少的时候减少。
安装pip install gunicorn
快速启动gunicorn myproject.wsgi
# 实际启动
gunicorn teacher.wsgi
命令启动gunicorn -w 进程数量 -b ip:端口 项目名.wsgi:application
# 样例
gunicorn -w 2 -b 192.168.3.99:9998 teacher.wsgi:application
上述命令启动方法等同于下面文件配置启动# vim gunicorn.conf.py
# coding:utf-8
import multiprocessing
bind = "127.0.0.1:8000" #绑定的ip与端口
workers = multiprocessing.cpu_count() * 2 + 1 #进程数
errorlog = '/home/xxx/xxx/gunicorn.error.log' #发生错误时log的路径
accesslog = '/home/xxx/xxx/gunicorn.access.log' #正常时的log路径
backlog = 512 #监听队列
proc_name = 'gunicorn_project' #进程名
timeout = 120 # 设置超时时间120s,默认为30s。按自己的需求进行设置timeout = 120
worker_class = 'gevent' #使用gevent模式,还可以使用sync 模式,默认的是sync模式
threads = 2 #指定每个进程开启的线程数
loglevel = 'info' #日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置
#设置gunicorn访问日志格式,错误日志无法设置
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'
"""
其每个选项的含义如下:
h remote address
l '-'
u currently '-', may be user name in future releases
t date of the request
r status line (e.g. ``GET / HTTP/1.1``)
s status
b response length or '-'
f referer
a user agent
T request time in seconds
D request time in microseconds
L request time in decimal seconds
p process ID
"""
配置文件启动命令(使用该方法启动程序是没有任何反应,除非触发请求才有反应)gunicorn 项目名.wsgi:application -c /home/xxx/xxx/gunicorn.conf.py
样例
gunicorn teacher.wsgi:application -c /home/chenxinming/teacher/gunicorn.conf.py
gunicorn参考文献:
http://docs.gunicorn.org/en/latest/deploy.html
使用使用supervistor进程管理器
配置文件:[program:x508server]
command=gunicorn x508server.wsgi:application -b 0.0.0.0:8000 ; 被监控的进程路径
directory=/home/webserver/web/WebServer/ ; 执行前要不要先cd到目录$
autostart=true ; 随着supervisord的启动而启动
autorestart=true ; 自动重启。。当然要选上了
startretries=10 ; 启动失败时的最多重试次数
exitcodes=0 ; 正常退出代码
stopsignal=KILL ; 用来杀死进程的信号
stopwaitsecs=10 ; 发送SIGKILL前的等待时间
redirect_stderr=true ; 重定向stderr到stdout
stdout_logfile=/home/webserver/web/logfile.log ; 指定日志文件
; 默认为 false,如果设置为 true,当进程收到 stop 信号时,会自动将该信号发给该进$
stopasgroup=true ; send stop signal to the UNIX process
; 默认为 false,如果设置为 true,当进程收到 kill 信号时,会自动将该信号发给该进$
killasgroup=true ; SIGKILL the UNIX process group (def false)
nginx配置
打开nginx配置文件 建设为web.confvim /etc/nginx/conf.d/web.conf
配置信息server {
listen 9999;
server_name 192.168.3.99;
location / {
proxy_pass http://192.168.3.99:9998;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /static {
alias /home/chenxinming/teacher/static;
}
}
检查nginx语法问题$ nginx -t # 检查nginx语法问题
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重启nginx$ /etc/init.d/nginx restart #重启
三、django静态文件收集
1.setting.py设置DEBUG = False
STATIC_ROOT = os.path.join(BASE_DIR, 'statics')
2. 执行collectstatic命令:python manage.py collectstatic