部署图书管理项目须要如下软件html
上传图书管理系统项目到linux服务器上python
在/opt/目录下传输项目文件,数据库文件一样存放在这个文件夹下mysql
经过virtualenvwrapper工具的配置,解决虚拟环境问题linux
建立虚拟环境mkvirtualenv book_manage_envnginx
在centos7本体下安装配置mariadb数据库,且建立数据库数据,迁移导入图书管理系统数据sql
-测试使用linux的python解释器去运行项目 切换到 项目中运行(注意要解决解释器的模块问题,才能正常运转项目)数据库
完成uWSGI命令学习,使用uWSGI启动图书管理系统项目,支持多进程django
(1)使用pip安装uwsgi模块vim
pip3 install uwsgi
(2)建立一个测试文件testuwsgi.py, 运行简单的uWSGI站点windows
第一步vim /opt/book_homework/testuwsgi.py 写入如下文件
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"]
第二步 使用uwsgi命令启动此文件
uwsgi --http :9000 --file testuwsgi.py
第三步 在浏览器经过IP加端口进行访问
(3)使用uwsgi.ini文件,简单的命令就能够将uwsgi启动起来
cd /opt/book_manage
vim uwsgi.ini
写入配置文件
[uwsgi] # Django-related settings # the base directory (full path) # 写上项目的绝对路径 chdir = /opt/book_manage # Django's wsgi file # 填写找到django的wsgi文件,填写相对路径,以chdir参数为相对路径 module = book_manage.wsgi # the virtualenv (full path) # 填写虚拟环境的绝对路径 home = /root/Envs/book_manage_env/ # process-related settings # master #启动uwsgi主进程 master = true # maximum number of worker processes processes = 1 # the socket (use the full path to be safe #若是你使用了nginx,作反向代理,必须填写socket连接,而不是http参数 socket = 0.0.0.0:8000 #若是你不用nginx,直接使用uwsgi,运行一个http服务端,就用这个http参数 http = 0.0.0.0:8000 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true
使用uwsgi命令指定配置文件启动,单个项目是能够这么启动
uwsgi -ini /opt/book_homework/uwsgi.ini
收集django静态文件
vim /opt/book_homework/book_homework/settings.py
加入一行配置,静态文件
STATIC_ROOT='/opt/static'
回到django项目主目录下(有manage.py文件的目录), 输入命令进行收集静态文件
python3 manage.py collectstatic
下图为/opt/nginx112/conf/uwsgi_params文件
添加如下配置
location / { # nginx自带ngx_http_uwsgi_module模块,起到nginx和uwsgi交互做用 # 经过uwsgi_pass设置服务器地址和协议,将动态请求转发给uwsgi处理 include /opt/nginx112/conf/uwsgi_params; uwsgi_pass 0.0.0.0:8000; root html; index index.html index.htm; } ************************************************************************************************************* location /static { alias /opt/static; }
如图配置
nginx.conf所有配置文件以下
worker_processes 1; error_log logs/error.log; 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; keepalive_timeout 65; # nginx反向代理uwsgi server { listen 80; server_name qishijd.com; location / { # nginx自带ngx_http_uwsgi_module模块,起到nginx和uwsgi交互做用 # 经过uwsgi_pass设置服务器地址和协议,将动态请求转发给uwsgi处理 include /opt/nginx112/conf/uwsgi_params; uwsgi_pass 0.0.0.0:8000; root html; index index.html index.htm; } # nginx处理静态页面资源 # 当用户请求是qishijd.com/static/的时候, 就会进入这个location匹配 # 经过alias参数进行路径别名,让nginx去/opt/static底下去找静态资源 location /static { alias /opt/static; } # nginx处理媒体资源 location /media{ alias /opt/nginx112/media; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
/opt/nginx112/sbin/nginx -s reload
这个主要是用于守护进程
/root/Envs/book_manage/bin/uwsgi --ini /opt/book_manage/uwsgi.ini
ps -ef | grep uwsgi
在浏览器访问http://192.168.1.44已经能够正常访问,静态文件也能够正常加载了,修改数据也没有问题, 说明数据库链接正常
supervisor 是基于 python 的任务管理工具,用来自动运行各类后台任务,固然你也能直接利用 nohup 命令使任务自动后台运行,但若是要重启任务,每次都本身手动 kill 掉任务进程,这样很繁琐,并且一旦程序错误致使进程退出的话,系统也没法自动重载任务。
这里咱们要配置基于virtualenv的supervisor,可是请注意:因为supervisor在python3下没法使用,所以只能用python2去下载!!!!!!
安装suprvisor
easy_install supervisor # 这个是python2下面的安装模块命令,等同于python3下面的pip
若是没有easy_install这个命令,就须要安装setuptools工具
yum install python-setuptools
echo_supervisord_conf > /etc/supervisord.conf
[program:book_manage] command=/root/Envs/book_manage_env/bin/uwsgi /opt/book_manage/uwsgi.ini stopasgroup=true # 若是发现关闭supervisor进程后,结束uwsgi进 killasgroup=true # 程无效,就须要加上这两个参数
如图所示
supervisord -c /etc/supervisord.conf
# 任务管理命令以下:有两种,一个是参数形式, 一个是交互式 # 参数形式 supervisorctl -c /etc/supervisor.conf stop/start/restart all supervisorctl -c /etc/supervisor.conf start crm_knight # 交互式形式 supervisorctl -c /etc/supervisor.conf
1、添加好配置文件后 2、更新新的配置到supervisord supervisorctl update 3、从新启动配置中的全部程序 supervisorctl reload 4、启动某个进程(program_name=你配置中写的程序名称) supervisorctl start program_name 5、查看正在守候的进程 supervisorctl 6、中止某一进程 (program_name=你配置中写的程序名称) pervisorctl stop program_name 7、重启某一进程 (program_name=你配置中写的程序名称) supervisorctl restart program_name 8、中止所有进程 supervisorctl stop all 注意:显示用stop中止掉的进程,用reload或者update都不会自动重启。
a