详细步骤(下面步骤都是ROOT权限执行)
一.部署Django项目前的准备
1.1 更新系统软件包
yum update -y
备注: 这个操作一般会等待比较久的时间
1.2 安装软件管理包和可能使用的依赖
这些是django依赖的包
yum -y groupinstall "Development tools"
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel
1.3下载并安装python3
为了和我本地的python版本保持一致,此处我安装python 3.6版本的
cd /usr/local
wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
-- 解压
tar -zxvf Python-3.6.6.tgz
-- 进入 Python-3.6.6路径
cd Python-3.6.6
-- 编译安装到指定路径
./configure --prefix=/usr/local/python3
-- 注意:/usr/local/python3 路径可以自己指定,自己记着就行,下边要用到。
-- 安装python3
make
make install
安装完成之后 建立软链接 添加变量 方便在终端中直接使用python3
Python3安装完成之后pip3也一块安装完成,不需要再单独安装
同样给pip3建立软链接
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3
此时系统自带的python2和新安装的python3均可以使用了
[root@10-31-1-120 Python-3.6.6]# python -V
Python 2.7.5
[root@10-31-1-120 Python-3.6.6]# python3 -V
Python 3.6.6
[root@10-31-1-120 Python-3.6.6]#
1.4 查看Python3和pip3安装情况
[root@10-31-1-120 Python-3.6.6]# python3
Python 3.6.6 (default, Apr 7 2021, 15:11:22)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@10-31-1-120 Python-3.6.6]# pip3 -V
pip 10.0.1 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)
[root@10-31-1-120 Python-3.6.6]#
pip安装依赖模块的时候,会提示更新pip至最新的版本,可以进行升级
[root@10-31-1-120 Python-3.6.6]# pip3 install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/fe/ef/60d7ba03b5c442309ef42e7d69959f73aacccd0d86008362a681c4698e83/pip-21.0.1-py3-none-any.whl (1.5MB)
100% |████████████████████████████████| 1.5MB 3.2MB/s
Installing collected packages: pip
Found existing installation: pip 10.0.1
Uninstalling pip-10.0.1:
Successfully uninstalled pip-10.0.1
Successfully installed pip-21.0.1
[root@10-31-1-120 Python-3.6.6]#
[root@10-31-1-120 Python-3.6.6]# pip3 -V
pip 21.0.1 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)
[root@10-31-1-120 Python-3.6.6]#
1.5 安装virtualenv ,建议大家都安装一个virtualenv,方便不同版本项目管理
代码:
pip3 install virtualenv
-- 建立软链接
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
-- 安装成功在根目录下创建文件夹,用来存放项目
mkdir -p /data/env
测试记录:
[root@10-31-1-120 Python-3.6.6]# pip3 install virtualenv
Collecting virtualenv
pip3 install virtualenv Downloading https://files.pythonhosted.org/packages/91/fb/ca6c071f4231e06a9f0c3bd81c15c233bbacd4a7d9dbb7438d95fece8a1e/virtualenv-20.4.3-py2.py3-none-any.whl (7.2MB)
100% |████████████████████████████████| 7.2MB 4.1MB/s
Collecting six<2,>=1.9.0 (from virtualenv)
Downloading https://files.pythonhosted.org/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl
Collecting distlib<1,>=0.3.1 (from virtualenv)
Downloading https://files.pythonhosted.org/packages/f5/0a/490fa011d699bb5a5f3a0cf57de82237f52a6db9d40f33c53b2736c9a1f9/distlib-0.3.1-py2.py3-none-any.whl (335kB)
100% |████████████████████████████████| 337kB 18.2MB/s
Collecting importlib-metadata>=0.12; python_version < "3.8" (from virtualenv)
Downloading https://files.pythonhosted.org/packages/99/8f/b0ac918b2234848ec5bd2a887d2be7d6686355fcb22d7a0efe878d5c1555/importlib_metadata-3.10.0-py3-none-any.whl
Collecting importlib-resources>=1.0; python_version < "3.7" (from virtualenv)
Downloading https://files.pythonhosted.org/packages/f0/5e/69e6a0602c1f18d390952177de648468c4a380252858b0022affc3ce7811/importlib_resources-5.1.2-py3-none-any.whl
Collecting appdirs<2,>=1.4.3 (from virtualenv)
Downloading https://files.pythonhosted.org/packages/3b/00/2344469e2084fb287c2e0b57b72910309874c3245463acd6cf5e3db69324/appdirs-1.4.4-py2.py3-none-any.whl
Collecting filelock<4,>=3.0.0 (from virtualenv)
Downloading https://files.pythonhosted.org/packages/93/83/71a2ee6158bb9f39a90c0dea1637f81d5eef866e188e1971a1b1ab01a35a/filelock-3.0.12-py3-none-any.whl
Collecting typing-extensions>=3.6.4; python_version < "3.8" (from importlib-metadata>=0.12; python_version < "3.8"->virtualenv)
Downloading https://files.pythonhosted.org/packages/60/7a/e881b5abb54db0e6e671ab088d079c57ce54e8a01a3ca443f561ccadb37e/typing_extensions-3.7.4.3-py3-none-any.whl
Collecting zipp>=0.5 (from importlib-metadata>=0.12; python_version < "3.8"->virtualenv)
Downloading https://files.pythonhosted.org/packages/0f/8c/715c54e9e34c0c4820f616a913a7de3337d0cd79074dd1bed4dd840f16ae/zipp-3.4.1-py3-none-any.whl
Installing collected packages: six, distlib, typing-extensions, zipp, importlib-metadata, importlib-resources, appdirs, filelock, virtualenv
Successfully installed appdirs-1.4.4 distlib-0.3.1 filelock-3.0.12 importlib-metadata-3.10.0 importlib-resources-5.1.2 six-1.15.0 typing-extensions-3.7.4.3 virtualenv-20.4.3 zipp-3.4.1
You are using pip version 10.0.1, however version 21.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[root@10-31-1-120 Python-3.6.6]#
[root@10-31-1-120 Python-3.6.6]# pip3 install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/fe/ef/60d7ba03b5c442309ef42e7d69959f73aacccd0d86008362a681c4698e83/pip-21.0.1-py3-none-any.whl (1.5MB)
100% |████████████████████████████████| 1.5MB 3.2MB/s
Installing collected packages: pip
Found existing installation: pip 10.0.1
Uninstalling pip-10.0.1:
Successfully uninstalled pip-10.0.1
Successfully installed pip-21.0.1
[root@10-31-1-120 Python-3.6.6]#
[root@10-31-1-120 Python-3.6.6]# pip3 -V
pip 21.0.1 from /usr/local/python3/lib/python3.6/site-packages/pip (python 3.6)
[root@10-31-1-120 Python-3.6.6]#
1.6 切换到/data/env/下,创建指定版本的虚拟环境
这个地方只是模拟安装virtualenv ,后面我们在自己的项目下会重复类似操作
cd /data/env
virtualenv --python=/usr/bin/python3 pyweb
然后进入/data/env/pyweb/bin
启动虚拟环境:
cd /data/env/pyweb/bin
source activate
测试记录:
[root@10-31-1-120 env]# cd /data/env
[root@10-31-1-120 env]# virtualenv --python=/usr/bin/python3 pyweb
created virtual environment CPython3.6.6.final.0-64 in 188ms
creator CPython3Posix(dest=/data/env/pyweb, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
added seed packages: pip==21.0.1, setuptools==54.1.2, wheel==0.36.2
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
[root@10-31-1-120 env]#
[root@10-31-1-120 env]# cd /data/env/pyweb/bin
[root@10-31-1-120 bin]# source activate
(pyweb) [root@10-31-1-120 bin]#
1.7 虚拟环境里用pip3安django和uwsgi
pip3 install Django==3.1.7 (如果用于生产的话,则需要指定安装和你项目相同的版本)
pip3 install uwsgi
留意:uwsgi要安装两次,先在系统里安装一次,然后进入对应的虚拟环境安装一次。
第二步拷贝自己的项目的时候,需要再安装一次 uwsgi
给uwsgi建立软链接,方便使用
ln -s /usr/local/python3/bin/uwsgi /usr/bin/uwsgi
测试记录:
[root@10-31-1-120 my_learning_log]# pip3 install Django==3.1.7
Collecting Django==3.1.7
Downloading Django-3.1.7-py3-none-any.whl (7.8 MB)
|████████████████████████████████| 7.8 MB 411 kB/s
Requirement already satisfied: pytz in /usr/local/python3/lib/python3.6/site-packages (from Django==3.1.7) (2021.1)
Requirement already satisfied: sqlparse>=0.2.2 in /usr/local/python3/lib/python3.6/site-packages (from Django==3.1.7) (0.4.1)
Requirement already satisfied: asgiref<4,>=3.2.10 in /usr/local/python3/lib/python3.6/site-packages (from Django==3.1.7) (3.3.4)
Requirement already satisfied: typing-extensions in /usr/local/python3/lib/python3.6/site-packages (from asgiref<4,>=3.2.10->Django==3.1.7) (3.7.4.3)
Installing collected packages: Django
Successfully installed Django-3.1.7
[root@10-31-1-120 my_learning_log]#
[root@10-31-1-120 my_learning_log]#
[root@10-31-1-120 my_learning_log]# pip3 install uwsgi
Collecting uwsgi
Using cached uWSGI-2.0.19.1-cp36-cp36m-linux_x86_64.whl
Installing collected packages: uwsgi
Successfully installed uwsgi-2.0.19.1
[root@10-31-1-120 my_learning_log]#
二.本地项目搬迁到服务器
数据库使用的sqllite,直接在打包文件里,如使用mysql,需要进行数据迁移。
2.1 在项目目录下用下面的命令把当前的环境依赖包导出到requirements.txt文件
(ll_env) E:\python\my_learning_log>pip freeze > requirements.txt
(ll_env) E:\python\my_learning_log>
(ll_env) E:\python\my_learning_log>more requirements.txt
asgiref==3.3.1
Django==3.1.7
django-bootstrap3==14.2.0
importlib-metadata==1.7.0
pytz==2021.1
sqlparse==0.4.1
zipp==3.4.1
(ll_env) E:\python\my_learning_log>
安装所需的模块:
代码:
pip3 install asgiref==3.3.1
pip3 install Django==3.1.7
pip3 install django-bootstrap3==14.2.0
pip3 install importlib-metadata==1.7.0
pip3 install pytz==2021.1
pip3 install sqlparse==0.4.1
pip3 install zipp==3.4.1
测试记录:
[root@10-31-1-120 ~]# pip3 install asgiref==3.3.1
Collecting asgiref==3.3.1
Downloading asgiref-3.3.1-py3-none-any.whl (19 kB)
Installing collected packages: asgiref
Attempting uninstall: asgiref
Found existing installation: asgiref 3.3.4
Uninstalling asgiref-3.3.4:
Successfully uninstalled asgiref-3.3.4
Successfully installed asgiref-3.3.1
[root@10-31-1-120 ~]#
[root@10-31-1-120 ~]# pip3 install Django==3.1.7
Requirement already satisfied: Django==3.1.7 in /usr/local/python3/lib/python3.6/site-packages (3.1.7)
Requirement already satisfied: asgiref<4,>=3.2.10 in /usr/local/python3/lib/python3.6/site-packages (from Django==3.1.7) (3.3.1)
Requirement already satisfied: pytz in /usr/local/python3/lib/python3.6/site-packages (from Django==3.1.7) (2021.1)
Requirement already satisfied: sqlparse>=0.2.2 in /usr/local/python3/lib/python3.6/site-packages (from Django==3.1.7) (0.4.1)
[root@10-31-1-120 ~]#
[root@10-31-1-120 ~]# pip3 install django-bootstrap3==14.2.0
Collecting django-bootstrap3==14.2.0
Downloading django_bootstrap3-14.2.0-py3-none-any.whl (23 kB)
Collecting importlib-metadata<2.0.0,>=1.5.0
Downloading importlib_metadata-1.7.0-py2.py3-none-any.whl (31 kB)
Requirement already satisfied: django<4.0,>=2.2 in /usr/local/python3/lib/python3.6/site-packages (from django-bootstrap3==14.2.0) (3.1.7)
Requirement already satisfied: asgiref<4,>=3.2.10 in /usr/local/python3/lib/python3.6/site-packages (from django<4.0,>=2.2->django-bootstrap3==14.2.0) (3.3.1)
Requirement already satisfied: sqlparse>=0.2.2 in /usr/local/python3/lib/python3.6/site-packages (from django<4.0,>=2.2->django-bootstrap3==14.2.0) (0.4.1)
Requirement already satisfied: pytz in /usr/local/python3/lib/python3.6/site-packages (from django<4.0,>=2.2->django-bootstrap3==14.2.0) (2021.1)
Requirement already satisfied: zipp>=0.5 in /usr/local/python3/lib/python3.6/site-packages (from importlib-metadata<2.0.0,>=1.5.0->django-bootstrap3==14.2.0) (3.4.1)
Installing collected packages: importlib-metadata, django-bootstrap3
Attempting uninstall: importlib-metadata
Found existing installation: importlib-metadata 3.10.0
Uninstalling importlib-metadata-3.10.0:
Successfully uninstalled importlib-metadata-3.10.0
Successfully installed django-bootstrap3-14.2.0 importlib-metadata-1.7.0
[root@10-31-1-120 ~]#
[root@10-31-1-120 ~]# pip3 install importlib-metadata==1.7.0
Requirement already satisfied: importlib-metadata==1.7.0 in /usr/local/python3/lib/python3.6/site-packages (1.7.0)
Requirement already satisfied: zipp>=0.5 in /usr/local/python3/lib/python3.6/site-packages (from importlib-metadata==1.7.0) (3.4.1)
[root@10-31-1-120 ~]#
[root@10-31-1-120 ~]# pip3 install pytz==2021.1
Requirement already satisfied: pytz==2021.1 in /usr/local/python3/lib/python3.6/site-packages (2021.1)
[root@10-31-1-120 ~]#
[root@10-31-1-120 ~]# pip3 install sqlparse==0.4.1
Requirement already satisfied: sqlparse==0.4.1 in /usr/local/python3/lib/python3.6/site-packages (0.4.1)
[root@10-31-1-120 ~]#
[root@10-31-1-120 ~]# pip3 install zipp==3.4.1
Requirement already satisfied: zipp==3.4.1 in /usr/local/python3/lib/python3.6/site-packages (3.4.1)
[root@10-31-1-120 ~]#
[root@10-31-1-120 ~]#
2.2 打包项目源码并上传到服务器
将my_learning_log整个项目打包成.zip文件,上传到/data/env目录,并进行解压。
代码:
cd /data/env
unzip my_learning_log.zip
cd /data/env/my_learning_log/bin
virtualenv --python=/usr/bin/python3 my_learning_log
source activate
测试记录:
[root@10-31-1-120 my_learning_log]# cd /data/env
[root@10-31-1-120 env]# ls
my_learning_log my_learning_log.zip pyweb
[root@10-31-1-120 env]#
[root@10-31-1-120 env]#
[root@10-31-1-120 env]# cd /data/env/my_learning_log/bin
-bash: cd: /data/env/my_learning_log/bin: 没有那个文件或目录
[root@10-31-1-120 env]# virtualenv --python=/usr/bin/python3 my_learning_log
created virtual environment CPython3.6.6.final.0-64 in 214ms
creator CPython3Posix(dest=/data/env/my_learning_log, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
added seed packages: pip==21.0.1, setuptools==54.1.2, wheel==0.36.2
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
[root@10-31-1-120 env]#
[root@10-31-1-120 env]# cd /data/env/my_learning_log/bin
[root@10-31-1-120 bin]# source activate
(my_learning_log) [root@10-31-1-120 bin]#
(my_learning_log) [root@10-31-1-120 bin]#
进入虚拟环境,安装uwsgi
(my_learning_log) [root@10-31-1-120 bin]# pip3 install uwsgi
Collecting uwsgi
Using cached uWSGI-2.0.19.1-cp36-cp36m-linux_x86_64.whl
Installing collected packages: uwsgi
Successfully installed uwsgi-2.0.19.1
(my_learning_log) [root@10-31-1-120 bin]#
2.3 通过python3 manage.py runserver 运行一下项目
代码:
python3 manage.py runserver 10.31.1.120:8000
测试记录:
[root@10-31-1-120 my_learning_log]# python3 manage.py runserver 10.31.1.120:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
April 07, 2021 - 08:31:19
Django version 3.1.7, using settings 'learning_log.settings'
Starting development server at http://10.31.1.120:8000/
Quit the server with CONTROL-C.
注意:
一定要看日志,这个地方比较容易出问题,根据日志进行排查问题。
2.4 配置uwsgi
配置uwsgi有多种方式,这里我向大家介绍两种,我们使用的时候只需要使用其中一个就行了.
第二种:ini格式的配置文件
[root@10-31-1-120 my_learning_log]# cd /data/env/my_learning_log
[root@10-31-1-120 my_learning_log]#
[root@10-31-1-120 my_learning_log]# more uwsgi.ini
#添加配置选择
[uwsgi]
#配置和nginx连接的socket连接
socket=127.0.0.1:8997
#配置项目路径,项目的所在目录
chdir=/data/env/my_learning_log/
#配置wsgi接口模块文件路径,也就是wsgi.py这个文件所在的目录名
wsgi-file=learning_log/wsgi.py
#配置启动的进程数
processes=4
#配置每个进程的线程数
threads=2
#配置启动管理主进程
master=True
#配置存放主进程的进程号文件
pidfile=uwsgi.pid
#配置dump日志记录
daemonize=uwsgi.log
[root@10-31-1-120 my_learning_log]# pwd
/data/env/my_learning_log
[root@10-31-1-120 my_learning_log]#
通过下面的命令启动运行uwsgi
uwsgi --ini uwsgi.ini
测试记录:
[root@10-31-1-120 my_learning_log]# uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
[root@10-31-1-120 my_learning_log]#
ini配置文件其它相关命令:
#停止运行uwsgi,通过包含主进程编号的文件设置停止项目
uwsgi --stop uwsgi.pid
#重启uwsgi
uwsgi --reload uwsgi.pid
2.5 配置Nginx
进入home目录,执行下面命令
cd /home/
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -zxvf nginx-1.13.7.tar.gz
cd nginx-1.13.7
./configure
make
make install
nginx一般默认安装好的路径为/usr/local/nginx
在/usr/local/nginx/conf/中先备份一下nginx.conf文件,以防意外。
cd /usr/local/nginx/conf/
cp nginx.conf nginx.conf.bak
-- 不存在的目录先创建
mkdir -p /data/env/my_learning_log/static/
然后打开nginx.conf,把原来的内容删除,直接加入以下内容:
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
server_name 127.0.0.1:80; #改为自己的域名,没域名修改为127.0.0.1:80
charset utf-8;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8997; #端口要和uwsgi里配置的一样
uwsgi_param UWSGI_SCRIPT learning_log.wsgi; #wsgi.py所在的目录名+.wsgi
uwsgi_param UWSGI_CHDIR /data/env/my_learning_log/; #项目路径
}
location /static/ {
alias /data/env/my_learning_log/static/; #静态资源路径
}
}
}
进入/usr/local/nginx/sbin/目录
执行./nginx -t命令先检查配置文件是否有错,没有错就执行以下命令:
./nginx
测试记录:
[root@10-31-1-120 conf]# cd /usr/local/nginx/sbin/
[root@10-31-1-120 sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@10-31-1-120 sbin]# ./nginx
2.6 登陆web界面查看效果
浏览器输入:http://10.31.1.120:8000/
如下图所示,项目部署完成,太给力了。
参考:
1.https://www.django.cn/article/show-4.html
2.https://blog.csdn.net/lezeqe/article/details/83820621
3.https://blog.csdn.net/weixin_43336281/article/details/100055435