首先介绍一下环境:centos7.6,python3.7.8,django2.1.2
1、安装python3.7.8
先把依赖安装了:
[root@nginx ~]# yum install -y gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
python的下载地址:https://www.python.org/downloads/source/
下载完后解压安装:
[root@nginx soft]# tar -zxvf Python-3.7.8
[root@nginx soft]#
[root@nginx soft]# ls
Python-3.7.8 Python-3.7.8.tgz
[root@nginx soft]# cd Python-3.7.8/
[root@nginx Python-3.7.8]# ./configure
[root@nginx Python-3.7.8]# make && make install
[root@nginx Python-3.7.8]# cd
[root@nginx ~]# python3 -V
Python 3.7.8
[root@nginx ~]#
python3.7.8安装完成,默认会自动添加到环境变量的,如果没有的话可以手动添加一下。
2、安装虚拟环境
2.1安装virtualenv虚拟模块
[root@nginx ~]# pip3 install virtualenv
如果安装过程中出现tls/ssl的错误,类似这样的:
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
解决办法重新安装python3,在编译的的时候加参数:
1、安装 openssl openssl-devel
yum -y install openssl openssl-devel
2、编译安装python,添加如下选项
./configure --with-ssl
2.2安装virtualenvwrapper虚拟模块管理工具
[root@nginx ~]# pip3 install virtualenvwrapper
2.3配置Linux用户的环境变量
[root@nginx ~]# vim ~/.bashrc
# 添加以下内容
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
保存退出重载一下配置文件
[root@nginx ~]# source .bashrc
2.4创建一个虚拟环境:
[root@nginx ~]# mkvirtualenv v1
(v1) [root@nginx ~]#
创建后之后会自动进入到创建好的虚拟化环境中,我这里创建的虚拟环境名字叫v1
虚拟环境的常用操作命令:
workon:查看所有虚拟环境,后面接上虚拟环境名称进入该虚拟环境
mkvirtualenv [–python==python解释器路径] 虚拟环境名:指定python版本并创建虚拟环境
deactivate:退出当前虚拟环境
lsvirtualenv:列出虚拟环境,同workon
rmvirtualenv:删除虚拟环境
cdvirtualenv:进入虚拟环境所在目录
2.5可移植性: freeze:将当前环境的包导出到文件,在新环境中导入,确保环境一致性,具体用法如下:
(v1) [root@nginx ~]# pip3 freeze > requirements.txt
(v1) [root@nginx ~]# ls
requirements.txt
(v1) [root@nginx ~]# cat requirements.txt
asgiref==3.3.1
Django==2.1.2
pytz==2020.5
sqlparse==0.4.1
(v1) [root@nginx ~]#
新环境导入方法:
[root@nginx ~]# pip3 install -r requirements.txt
3、虚拟环境安装django
因为当前的python版本是3.7.8,即可以使用django2.x版本也可以使用3.x版本,本次演示换进使用django2.1.2版本,django和python的版本对应关系如下:免得掉坑
安装指定版本的django2.1.2
(v1) [root@nginx ~]# pip3 install django==2.1.2
Collecting django==2.1.2
Downloading Django-2.1.2-py3-none-any.whl (7.3 MB)
|████████████████████████████████| 7.3 MB 212 kB/s
Requirement already satisfied: pytz in /root/.virtualenvs/v1/lib/python3.7/site-packages (from django==2.1.2) (2020.5)
Installing collected packages: django
Successfully installed django-2.1.2
WARNING: You are using pip version 20.3.1; however, version 20.3.3 is available.
You should consider upgrading via the '/root/.virtualenvs/v1/bin/python -m pip install --upgrade pip' command.
(v1) [root@nginx python]#
如上提示我们可以对pip升级,使用它提示的命令对pip升级:
(v1) [root@nginx python]# /root/.virtualenvs/v1/bin/python -m pip install --upgrade pip
Requirement already satisfied: pip in /root/.virtualenvs/v1/lib/python3.7/site-packages (20.3.1)
Collecting pip
Using cached pip-20.3.3-py2.py3-none-any.whl (1.5 MB)
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 20.3.1
Uninstalling pip-20.3.1:
Successfully uninstalled pip-20.3.1
Successfully installed pip-20.3.3
(v1) [root@nginx python]#
创建项目,项目名称blog
(v1) [root@nginx test]# pwd
/data/test
(v1) [root@nginx test]# django-admin startproject blog
在/data/test/目录下创建的项目blog,同时目录下会自动创建blog目录
(v1) [root@nginx test]# ls
blog
(v1) [root@nginx test]# cd blog/
(v1) [root@nginx blog]# ls
blog manage.py
(v1) [root@nginx blog]# cd blog/
(v1) [root@nginx blog]# ls
__init__.py settings.py urls.py wsgi.py
可以看到blog目录下还会生成一个blog目录和manager.py的文件,修改一下配置,默认是只允许本地访问,修改blog/setting.py文件,如下:
(v1) [root@nginx blog]# vim settings.py
ALLOWED_HOSTS = ['*']
启动服务,并监听本地8000端口:
(v1) [root@nginx blog]# python3 manage.py runserver 0.0.0.0:8080
Performing system checks...
System check identified no issues (0 silenced).
You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 02, 2021 - 08:04:05
Django version 2.1.2, using settings 'blog.settings'
Starting development server at http://0.0.0.0:8080/
Quit the server with CONTROL-C.
通过浏览器访问服务器的8000端口:
如上图所示服务已经正常运行了。