DjangoHelper 文档
项目部署文档
Windows本地运行
Linux服务器部署
RabbitMQ服务部署
-
+
首页
Linux服务器部署
<div style="text-align: center;"> [](https://gitee.com/django-helper/djangohelper) [](https://gitee.com/django-helper/dhapp) </div> + ## 一、获取项目 ### 1.1 Git克隆 > 把项目拉取到 `/opt`目录,因为部署配置写的是这里,可以按需修改配置文件 `djangohelper_supervisor.conf` `djangohelper_uwsgi.ini` `djangohelper.conf` ``` cd /opt git clone https://gitee.com/django-helper/djangohelper.git ``` + ## 二、环境搭建 ### 2.1 安装虚拟环境 ``` pip3 install virtualenv pip3 install virtualenvwrapper mkvirtualenv -p python3 djangohelper ``` ### 2.2 导入相关依赖 ``` cd /opt/djangohelper pip3 install -i https://pypi.doubanio.com/simple -r requirements.txt ``` ### 2.3 导入mysqlclient `pip install mysqlclient==2.1.0` > linux环境安装下mysqlclient可以直接安装 如果出现`mysql_config: not found`,则先执行`apt-get install libmysqlclient-dev` #### 2.3.1 异常 > d 因为环境问题出现`subprocess-exited-with-error`可以尝试`sudo apt-get install python-dev python3-dev build-essential libssl-dev libffi-dev libxml2-dev libxslt1-dev zlib1g-dev` + ## 三、数据库 ### 3.1 创建数据库 #### 3.1.1 终端指令创建 ``` mysql -u root -p # 进入数据库 CREATE SCHEMA `dhdb` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ; # 创建数据库 exit; # 退出 ``` #### 3.1.2 使用Navicat等软件 也可以直接使用Navicat等软件创建名为`dhdb` 编码为 `utf8mb4` & `utf8mb4_general_ci`的数据库 #### 3.1.3 在setting.py文件内配置 修改 `setting.py` 中的数据库配置 `DATABASES`相关的数据库名称和账号密码 ### 3.2 映射数据结构 ``` python manage.py makemigrations python manage.py migrate ``` ### 3.3 创建超级用户 `python manage.py createsuperuser` > 或使用项目中的`dhdb-clean.sql`文件导入测试数据 测试数据内的默认用户,账号 `admin` 密码 `admin123..` + ## 四、测试运行 ### 4.1 终端指令 `python manage.py runserver 0.0.0.0:8000` > 然后即可在`http://自己的服务器IP:8000/`查看项目,测试后使用`Ctrl` + `C` 结束运行 ### 4.2 端口开放 + 使用云服务器需要在在后台防火墙开放对应端口 + 如果对应端口开放后依旧无效、可以尝试使用`iptables -I INPUT -p tcp --dport 8000 -j ACCEPT` + ## 五、部署运行 ### 5.1 Uwsgi #### 5.1.1 安装uwsgi uwsgi是一个python编写的应用服务器,非静态文件的网络请求就必须通过他完成,也可以充当静态文件服务器。 `pip3 install uwsgi` #### 5.1.2 修改配置 修改`djangohelper_uwsgi.ini`配置文件中相关的数据 #### 5.1.3 测试 进入项目根目录,使用如下命令。如果能够在浏览器中访问到这个页面,说明uwsgi可以加载项目了。 `uwsgi --http :8000 --ini djangohelper_uwsgi.ini` > 测试后挂载进程有可能会占用端口影响后续操作,可以通过`top` + `kill -9 进程` 处理进程,如果自信也可以跳过测试 ### 5.2 Nginx #### 5.2.1 安装nginx #### 5.2.2 修改配置 修改`djangohelper.conf`配置文件中相关的数据 #### 5.2.3 转移Nginx配置文件 把配置文件移到`nginx`下的`conf.d`文件夹内 ##### 5.2.3.1 linux指令 `cp -r /opt/djangohelper/djangohelper.conf /etc/nginx/conf.d` 然后编辑配置文件`vi /etc/nginx/conf.d/djangohelper.conf`修改对应的域名IP ##### 5.2.3.2 或使用WinSCP 在本地修改后使用WinSCP移入`/etc/nginx/conf.d` #### 5.2.4 代理转发 如果需要使用其他端口或者解析的域名地址,可以到修改文件`vi /etc/nginx/nginx.conf` ``` server { listen 80;# 监听端口 server_name www.baidu.com;# 监听地址/域名 location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://公网IP:8000/;# djangohelper.conf 中配置的端口 } } ``` #### 5.2.5 测试nginx配置正确性 `service nginx configtest` #### 5.2.6 重启nginx服务器 `service nginx restart` ### 5.3 Supervisor #### 5.3.1 安装Supervisor 使用supervisor管理uwsgi进程,在系统级别的python环境下执行安装命令 `pip3 install supervisor` > 如果在虚拟环境内安装,则该进程守护无法使用sh脚本配置开机自启,如果不需要则问题不大 #### 5.3.2 修改配置 修改`djangohelper_supervisor.conf` 配置文件中的相关信息 内含redis端口配置,服务器需要安装redis,且需要和别的端口区分避免占用 #### 5.3.3 在项目目录下创建log文件夹 `mkdir log` #### 5.3.4 测试运行 `supervisord -c djangohelper_supervisor.conf` #### 5.3.5 uwsgi管理 以后如果想要启动uwsgi,就可以通过如下命令进入到管理控制台,然后可以执行相关的命令进行管理 ``` supervisorctl -c djangohelper_supervisor.conf status # 查看状态 start djangohelper # 启动程序 restart djangohelper # 重新启动程序 stop djangohelper # 关闭程序 reload # 重新加载配置文件 quit # 退出控制台 ``` #### 5.3.6 停止supervisor进程 `supervisorctl -c djangohelper_supervisor.conf shutdown` + ## 六、设置开机自启 ### 6.1 建立rc-local.service文件,如果存在的话可以不用创建 `sudo vim /etc/systemd/system/rc-local.service` ### 6.2 将下列内容复制进rc-local.service文件 ``` [Unit] Description=/etc/rc.local Compatibility ConditionPathExists=/etc/rc.local [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 StandardOutput=tty RemainAfterExit=yes SysVStartPriority=99 [Install] WantedBy=multi-user.target ``` ### 6.3 创建文件rc.local `sudo vim /etc/rc.local` ### 6.4 将下列内容复制进rc.local文件 ``` #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. /root/.virtualenvs/djangohelper/bin/supervisord -c /opt/djangohelper/djangohelper_supervisor.conf ``` ### 6.5 给rc.local加上权限 `sudo chmod +x /etc/rc.local` ### 6.6 启用服务 `sudo systemctl enable rc-local` ### 6.7 启动服务并检查状态 `sudo systemctl start rc-local.service` `sudo systemctl status rc-local.service` ### 6.8 重启系统测试 `reboot` + ## 七、代码生成说明 经过测试发现,在服务器上执行生成代码操作后,需要重启进程才会生效 ``` /root/.virtualenvs/djangohelper/bin/supervisorctl -c /opt/djangohelper/djangohelper_supervisor.conf restart djangohelper # 重新启动程序 quit # 退出控制台 ``` > 配置了开机自启的情况下,非开发人员可以选择直接重启服务器
Samle
2022年12月14日 14:06
分享文档
收藏文档
上一篇
下一篇
微信扫一扫
复制链接
手机扫一扫进行分享
复制链接
关于 MrDoc
伊起知库MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果伊起知库给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护伊起知库,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码