前阵子在腾讯云花5元租了一个月的云服务器+一年的域名,想着将Django项目部署到云服务器上,捣鼓了半天,才实现了部分云端环境的配置。
目录
python3的安装
yum的配置
MySQL数据库的安装
新建django项目
Django项目中配置mysql
本地查看云服务器上启动的Django项目
CentOS 7.6自带的是python 2.7.5版本,
[root@VM_0_3_centos ~]# python -V
Python 2.7.5
首先安装python3,
[root@VM_0_3_centos ~]# yum install python3
查看python3版本
[root@VM_0_3_centos ~]# python3 -V
Python 3.6.8
设置软连接(类似windows系统的快捷方式)将python连接到python3,首先查看现有关于python的软连接
[root@VM_0_3_centos ~]# ll /bin/python*
lrwxrwxrwx 1 root root 7 Nov 5 23:09 /bin/python -> python2
lrwxrwxrwx 1 root root 9 Nov 5 23:09 /bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7216 Aug 7 2019 /bin/python2.7
lrwxrwxrwx 1 root root 9 Apr 7 10:07 /bin/python3 -> python3.6
-rwxr-xr-x 2 root root 11408 Aug 8 2019 /bin/python3.6
-rwxr-xr-x 2 root root 11408 Aug 8 2019 /bin/python3.6m
删除原有python的软连接,并设置对python3.6的连接
[root@VM_0_3_centos ~]# rm /bin/python
rm: remove symbolic link ‘/bin/python’? y
[root@VM_0_3_centos ~]# ln -s /bin/python3.6 /bin/python
再次查看可以发现python已经软连接到python3.6
[root@VM_0_3_centos ~]# ll /bin/python
lrwxrwxrwx 1 root root 14 Apr 7 10:17 /bin/python -> /bin/python3.6
同样的方法可以设置pip到pip3的软连接
[root@VM_0_3_centos ~]# ln -s /bin/pip3 /bin/pip
[root@VM_0_3_centos ~]# ll /bin/pip
lrwxrwxrwx 1 root root 9 Apr 7 10:33 /bin/pip -> /bin/pip3
在安装完python3并设置好软连接后,就碰到了第一个坑,发现yum无法使用了
[root@VM_0_3_centos ~]# yum
File "/usr/bin/yum", line 30
except KeyboardInterrupt, e:
^
SyntaxError: invalid syntax
原来是yum默认使用的是python2.7的版本,在更改完python的软连接后,导致yum使用的是python3.6版本,因此通过vi编辑器改回去即可,具体方法是在第一行中原有的python后添加 “2”,然后保存退出
#!/usr/bin/python2
import sys
try:
import yum
except ImportError:
print >> sys.stderr, """\
...
...
...
同时对 File "/usr/libexec/urlgrabber-ext-down" 也需要进行同样的修改
#! /usr/bin/python2
# A very simple external downloader
# Copyright 2011-2012 Zdenek Pavlas
...
...
...
yum从软件库中下载安装软件包,查看现有的软件库
[root@VM_0_3_centos ~]# ll /etc/yum.repos.d/
total 8
-rw-r--r-- 1 root root 614 Apr 7 13:27 CentOS-Base.repo
-rw-r--r-- 1 root root 230 Apr 7 13:27 CentOS-Epel.repo
首先需要添加 MySQL Yum Repository(可以查看官网教程)
[root@VM_0_3_centos ~]# rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
再次查看软件库,发现多了两项
[root@VM_0_3_centos ~]# ll /etc/yum.repos.d/
total 16
-rw-r--r-- 1 root root 614 Apr 7 13:45 CentOS-Base.repo
-rw-r--r-- 1 root root 230 Apr 7 13:45 CentOS-Epel.repo
-rw-r--r-- 1 root root 2076 Apr 25 2019 mysql-community.repo
-rw-r--r-- 1 root root 2108 Apr 25 2019 mysql-community-source.repo
同时可以通过rpm -qa ...查看所有已经安装的有关mysql的软件包,结果显示目前只有一个
[root@VM_0_3_centos HelloWorld]# rpm -qa mysql*
mysql80-community-release-el7-3.noarch
现在可以通过 yum install mysql-community-server进行安装,该命令不仅安装了mysql server 还安装了其他必须的软件包
[root@VM_0_3_centos ~]# yum install mysql-community-server
安装结束后,利用rpm -qa mysql*查看,发现新安装了5各软件包
[root@VM_0_3_centos ~]# rpm -qa mysql*
mysql80-community-release-el7-3.noarch
mysql-community-libs-8.0.19-1.el7.x86_64
mysql-community-libs-compat-8.0.19-1.el7.x86_64
mysql-community-common-8.0.19-1.el7.x86_64
mysql-community-client-8.0.19-1.el7.x86_64
mysql-community-server-8.0.19-1.el7.x86_64
开启mysql
[root@VM_0_3_centos HelloWorld]# systemctl start mysqld.service
之后在mysqld.log里会生成一个root用户的初始密码,
[root@VM_0_3_centos ~]# cat /var/log/mysqld.log | grep password
2020-04-07T07:00:22.584068Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: IbHxyweP/2cx
登陆mysql,输入对应的密码
[root@VM_0_3_centos ~]# mysql -uroot -p
在进行其他操作时,提示需要先更改密码
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
按照提示进行更改,'Tianwan3!'表示修改后的密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Tianwan3!';
Query OK, 0 rows affected (0.01 sec)
新建数据库HelloWorld,留作Django项目备用。
mysql> CTEATE DATABASE HelloWorld;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CTEATE DATABASE HelloWorld' at line 1
mysql> CREATE DATABASE HelloWorld;
Query OK, 1 row affected (0.02 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| HelloWorld |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
首先pip安装django
[root@VM_0_3_centos ~]# pip install django
然后cd到/home目录下,新建HelloWorld项目
[root@VM_0_3_centos ~]# cd /home/
[root@VM_0_3_centos home]# django-admin startproject HelloWorld
输入tree查看项目结构(没有tree,可以先yum install tree安装)
[root@VM_0_3_centos home]# tree
.
`-- HelloWorld
|-- HelloWorld
| |-- asgi.py
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| `-- wsgi.py
`-- manage.py
2 directories, 6 files
cd到HelloWorld项目中,输入python manange.py runserver启动
[root@VM_0_3_centos HelloWorld]# python manage.py runserver
...
...
...
django.core.exceptions.ImproperlyConfigured: SQLite 3.8.3 or later is required (found 3.7.17).
结果显示报错了,原因是sqlite版本较低(sqlite是django项目默认使用的一款轻量型数据库),CentOS 7.6自带的sqlite是3.7.17
[root@VM_0_3_centos HelloWorld]# rpm -qa sqlite*
sqlite-3.7.17-8.el7.x86_64
解决方法之一可以在File "/usr/local/lib64/python3.6/site-packages/django/db/backends/sqlite3/base.py"中将有关代码注释掉
...
...
...
#def check_sqlite_version():
# if Database.sqlite_version_info < (3, 8, 3):
# raise ImproperlyConfigured('SQLite 3.8.3 or later is required (found %s).' % Database.sqlite_version)
#check_sqlite_version()
...
...
...
再次输入python manage.py runserver,成功启动!
[root@VM_0_3_centos HelloWorld]# python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 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.
April 07, 2020 - 06:48:35
Django version 3.0.5, using settings 'HelloWorld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
首先需要安装第三方库pymysql
[root@VM_0_3_centos HelloWorld]# pip install pymysql
在项目的子目录HelloWorld中的__init__.py中导入pymysql
import pymysql
pymysql.install_as_MySQLdb()
在子目录HelloWorld中的settings.py里修改数据库,初始设置如下:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
修改后如下:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'HelloWorld',
'USER': 'root',
'PASSWORD': 'Tianwan3!',
'PORT': '3306',
'HOST': '',
}
}
启动项目,报错,可以发现错误问题类似于先前sqlite版本问题
[root@VM_0_3_centos HelloWorld]# python manage.py runserver
...
...
...
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
打开文件/usr/local/lib64/python3.6/site-packages/django/db/backends/mysql/base.py ,注释掉如下两行
#if version < (1, 3, 13):
# raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
再次启动,成功!
[root@VM_0_3_centos HelloWorld]# python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, ses
sions.
Run 'python manage.py migrate' to apply them.
April 07, 2020 - 07:33:01
Django version 3.0.5, using settings 'HelloWorld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
云服务器上启动项目
[root@VM_0_3_centos HelloWorld]# python manage.py runserver 0.0.0.0:8000
本地浏览器中输入62.234.83.169:8000(云服务器的公网ip+端口号),结果报错,提示需要将‘62.234.83.169’添加到ALLOWED_HOSTS
云服务器端l利用vi打开项目中 HelloWorld子目录里的settings.py,进行ip的添加
ALLOWED_HOSTS = ['62.234.83.169']
云服务器端再次启动项目,本地浏览器刷新,成功!