【linux】部署django项目

1.安装python解释器跳转

2.安装mysql跳转

3.创建数据库并导入数据

# 导出:
mysqldump -u root -p   test >  test .sql
# 导入:
mysql -uroot -p  test <  test .sql 

4.创建虚拟环境:跳转

5.上传django项目代码到linux服务器

6.解决项目运行时需要的模块依赖

导出windows系统下解释器的所有模块信息,通过命令一键安装所有的模块

在windows系统下导出解释器的模块信息

pip3 freeze >  requirements.txt

在linux系统下通过命令安装

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple   -r requirements.txt  

7.安装uwsgi

pip3 install -i https://pypi.douban.com/simple uwsgi 

8.创建uwsgi的配置文件

cd /opt/rikkac
touch uwsgi.ini  

[uwsgi]
# Django-related settings
# the base directory (full path)
# 填写项目的完整绝对路径,第一层
chdir           = /opt/rikkac 
# Django's wsgi file
#指定django的wsgi文件路径,填写相对路径
module          = rikkac.wsgi  
# the virtualenv (full path)
#填写虚拟环境的绝对路径 可以通过cdvirtualenv切换到虚拟环境路径
home            = /opt/venv/rikkac
# process-related settings
# master
master          = true
# maximum number of worker processes
#定义程序的多进程数量的,以cpu核数的2倍+1数量填写   2n+1 数量 
processes       = 9
# the socket (use the full path to be safe
#把uwsgi启动在socket协议上,的8000端口
socket          = 0.0.0.0:8000
#指定http协议的话,用户是可以直接访问到的,不安全的,建议使用socket协议
#http =0.0.0.0:8000

# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true

9.启动uwsgi

uwsgi --ini wgsgi.ini

10.安装nginx链接

11.配置nginx

    server {
        listen       80;   
        server_name  _;
        location / {
        uwsgi_pass  127.0.0.1:8000;
        include uwsgi_params;
        }
    }

12.处理django项目静态文件

# 修改django的settings.py配置文件,添加如下配置
STATIC_URL = '/static/'
STATIC_ROOT='/opt/rikkacstatic'

13.使用命令收集项目的所有静态文件

python3  manage.py collectstatic

14.配置nginx处理静态文件请求

location  /static {
	# 别名,将请求中到/static替换
	alias  /opt/rikkacstatic/;
}

15.重启uwsgi

16.重启nginx

17.使用supervisor管理uwsgi的后台

安装

 yum install supervisor

生成配置文件

echo_supervisord_conf >  /etc/supervisor.conf

添加配置文件,加载django项目

[program:rikkac]  ;任务名称
command=/opt/python367/bin/uwsgi --ini  /opt/rikkac/uwsgi.ini     ; 程序启动命令 ,uwsgi命令的绝对路径和配置文件的绝对路径
autostart=true       ; 在supervisord启动的时候也自动启动
stopasgroup=true     ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括>子进程
killasgroup=true     ;默认为false,向进程组发送kill信号,包括子进程

启动supervisord服务端

# 启动前确保uwsgi已关闭
supervisord -c  /etc/supervisord.conf

进入supervisor的管理交互式终端,管理任务

supervisorctl -c /etc/supervisord.conf 
# supervisor的管理命令
status #查看状态
start all #启动所有任务
restart all #重启所有任务
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章