AI智能
改变未来

Django项目windows上开发,虚拟机上调通打包,生产环境解压即用

linux上部署Django项目

首先创建一个简易的Django项目

[root@mcw1 /opt]$ lsjdk       jpype          mcw      mcwtest.zip  openssh-8.8p1         openssl         openssl-1.1.1l.tar.gz  tomcat_mcw1jieshiqi  machangwei.py  mcwtest  openssh      openssh-8.8p1.tar.gz  openssl-1.1.1l  Python-3.6.12.tgz[root@mcw1 /opt]$ tar xf Python-3.6.12.tgz[root@mcw1 /opt]$ lsjdk       jpype          mcw      mcwtest.zip  openssh-8.8p1         openssl         openssl-1.1.1l.tar.gz  Python-3.6.12.tgzjieshiqi  machangwei.py  mcwtest  openssh      openssh-8.8p1.tar.gz  openssl-1.1.1l  Python-3.6.12          tomcat_mcw1[root@mcw1 /opt]$ cd Python-3.6.12/[root@mcw1 /opt/Python-3.6.12]$ mkdir /opt/python3[root@mcw1 /opt/Python-3.6.12]$ lsaclocal.m4    config.sub  configure.ac  Grammar  install-sh  LICENSE  Makefile.pre.in  Modules  Parser  PCbuild   pyconfig.h.in  README.rst  Toolsconfig.guess  configure   Doc           Include  Lib         Mac      Misc             Objects  PC      Programs  Python         setup.py[root@mcw1 /opt/Python-3.6.12]$ ./configure --prefix=/opt/python3config.status: creating pyconfig.hcreating Modules/Setupcreating Modules/Setup.localcreating MakefileIf you want a release build with all stable optimizations active (PGO, etc),please run ./configure --enable-optimizations

编译部分过程已编译安装好并且能进入

将现有的site-package备份,用windows传上来的直接替换

[root@mcw1 /opt]$ python3/bin/python3Python 3.6.12 (default, Dec 17 2021, 07:14:00)[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linuxType \"help\", \"copyright\", \"credits\" or \"license\" for more information.>>>[root@mcw1 /opt]$ mv python3/lib/python3.6/site-packages python3/lib/python3.6/site-packages_bak[root@mcw1 /opt]$ cp -a jieshiqi/mcwtest/Include/            Lib/                pip-selfcheck.json  pyvenv.cfg          Scripts/[root@mcw1 /opt]$ cp -a jieshiqi/mcwtest/Lib/site-packages   python3/lib/python3.6/[root@mcw1 /opt]$ python3/bin/python3Python 3.6.12 (default, Dec 17 2021, 07:14:00)[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linuxType \"help\", \"copyright\", \"credits\" or \"license\" for more information.>>>[root@mcw1 /opt]$

现在有Djongo了,但是还报错

[root@mcw1 /opt/mcwtest]$ /opt/python3/bin/python3  manage.py runserverWatching for file changes with StatReloaderException in thread django-main-thread:Traceback (most recent call last):File \"/opt/python3/lib/python3.6/threading.py\", line 916, in _bootstrap_innerFile \"/opt/python3/lib/python3.6/sqlite3/__init__.py\", line 23, in <module>from sqlite3.dbapi2 import *File \"/opt/python3/lib/python3.6/sqlite3/dbapi2.py\", line 27, in <module>from _sqlite3 import *ModuleNotFoundError: No module named \'_

从这里下载一个sqlite源码包:https://www.sqlite.org/download.html

编译安装到sqlite3目录下

[root@mcw1 /opt]$ ls sqlite
sqlite3/ sqlite-autoconf-3370000/ sqlite-autoconf-3370000.tar.gz
[root@mcw1 /opt]$ ls sqlite

备份并修改python源码包中的setup文件

[root@mcw1 /opt]$ ls Python-3.6.6aclocal.m4    config.status  Doc         Lib              Makefile         Modules  PCbuild         pyconfig.h.in  python-config.py  setup.pybakbuild         config.sub     Grammar     libpython3.6m.a  Makefile.pre     Objects  Programs        python         python-gdb.py     Toolsconfig.guess  configure      Include     LICENSE          Makefile.pre.in  Parser   pybuilddir.txt  Python         README.rstconfig.log    configure.ac   install-sh  Mac              Misc             PC       pyconfig.h      python-config  setup.py[root@mcw1 /opt]$ ls Python-3.6.6/setup.pyPython-3.6.6/setup.py

[root@mcw1 /opt]$ grep sqlite -C 5 Python-3.6.6/setup.py
if db_setup_debug: print(\”db: no appropriate library found\”)

在setup文件中添加sqlite编译安装的目录文件信息。然后重新编译安装python3.6

重新编译安装python3.6之后,继续运行任务

报错:

[root@mcw1 /opt]$ python3/bin/python3 mcwtest/manage.py runserverTraceback (most recent call last):File \"mcwtest/manage.py\", line 11, in mainfrom django.core.management import execute_from_command_lineModuleNotFoundError: No module named \'django\'The above exception was the direct cause of the following exception:Traceback (most recent call last):File \"mcwtest/manage.py\", line 22, in <module>main()File \"mcwtest/manage.py\", line 17, in main) from excImportError: Couldn\'t import Django. Are you sure it\'s installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?[root@mcw1 /opt]$

解决方法:将刚刚编译安装好的python3.6.6的site-package目录备份,然后用windows的pycharm项目使用的解释器中的site-package目录,直接复制过去替代原来的

[root@mcw1 /opt]$ mv python3/lib/python3.6/site-packages/ python3/lib/python3.6/site-packages_bak[root@mcw1 /opt]$ cp -a jieshiqi/mcwtest/Lib/site-packages/ python3/lib/python3.6/site-packages[root@mcw1 /opt]$ python3/bin/python3 mcwtest/manage.py runserverWatching for file changes with StatReloaderPerforming system checks...System check identified no issues (0 silenced).December 17, 2021 - 01:46:37Django version 3.2.10, using settings \'mcwtest.settings\'Starting development server at http://127.0.0.1:8000/Quit the server with CONTROL-C.^C[root@mcw1 /opt]$

运行程序

访问页面

Invalid HTTP_HOST header: \'10.0.0.131:8000\'. You may need to add \'10.0.0.131\' to ALLOWED_HOSTS.

安装上面的报错信息提示,修改添加项目配置,允许本机ip访问

然后再次运行项目

至少能访问到页面了

将编译安装在/opt/下的python环境,sqlite3,以及我们的项目目录都传到mcw02主机上。看复制文件是否就能实现djonge项目在其它主机运行。

将项目复制到其它主机,修改主机ip配置,但是无法运行起项目来

[root@mcw2 ~]# ls /opt/[root@mcw2 ~]# cd /opt/[root@mcw2 opt]# lsmcwtest  python3  sqlite3[root@mcw2 opt]# hostname -I10.0.0.132 172.16.1.132[root@mcw2 opt]# vim mcwtest/mcwtest/settings.py[root@mcw2 opt]# grep -i host mcwtest/mcwtest/settings.pyALLOWED_HOSTS = [\"10.0.0.132\"][root@mcw2 opt]# python3/bin/python3 mcwtest/manage.py runserver 0.0.0.0:8000Watching for file changes with StatReloaderException in thread django-main-thread:.......File \"/opt/python3/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py\", line 73, in <module>check_sqlite_version()File \"/opt/python3/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py\", line 69, in check_sqlite_version\'SQLite 3.9.0 or later is required (found %s).\' % Database.sqlite_versiondjango.core.exceptions.ImproperlyConfigured: SQLite 3.9.0 or later is required (found 3.7.17).

sqlie数据库,貌似是不用起进程的,这里编译安装好就行,连接的库也不是这里。而且项目已经连接的数据库,是从pycharm那里运行项目时自动生成的那个sqlite3数据库。还在项目的根目录下,并且能正常使用。

之前上面那个问题解决方法:备份源码,并修改源码,支持当前版本sqlite

查看到报错位置有个检查版本的函数,原来是3,9,0比较。而我的sqlite版本是3.7的,所以我就改了版本比较数为3,3,0.这样就不会因为版本低而无法运行项目了。或许你可以去重新编译安装sqlite版本为3.9以上,但是不只是花时间,而且我自己也没有在官网找到这个版本的包,我3.7已经是下载的最新版本sqlite了。

def check_sqlite_version():if Database.sqlite_version_info < (3, 3, 0):raise ImproperlyConfigured(\'SQLite 3.3.0 or later is required (found %s).\' % Database.sqlite_version)

修改以后,在mcw02主机上重新运行项目,正常运行

然后页面访问迁移到mcw02主机的项目,成功访问。至此,完成了将项目从windows上移到linux上,又从linux上移到到其它linux上。至于为什么要再从linux上移到其它linux上,这是因为生产环境我们尽量不弄乱它的环境。而这里我们可用在windows上开发,在虚拟机上调试好三个目录,能正常运行,然后可以打包直接去生产上解压运行,只是占三个目录,并不会影响到其它目录或者部署的东西到其它目录下。互不干扰。如果python是用虚拟环境的方式,那么需要使用命令创建虚拟环境,暂时对这个没深入研究,害怕创建虚拟环境影响到其它程序使用python,或者自己弄自己的虚拟环境,不小心把其它的虚拟环境也弄坏了,而这个编译安装的python,独自有自己的目录,修改啥的也对其它程序影响小很多;如果sqlite使用的是mysql,那么需要起一个mysql进程,这样会占用资源比较多,而sqlie却没有起进程,就在那个目录下,对其它程序影响小

编译安装的是可以迁移其它目录和主机的

如果是编译安装的python和 sqlite等,编译安装指定某个目录后,直接打包目录,不只是能跨主机,也是能跨目录的,不是非要在编译安装的目录才能使用

比如下面这个,python3和sqlite都是编译安装到/opt目录下的,但是直接打包放到别的目录或者别的主机,照样可以运行程序

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » Django项目windows上开发,虚拟机上调通打包,生产环境解压即用