- Ubuntu18.04系统、
- python3.9.5、
- Django3.2.3框架、
- MySQL5.7数据库
选择操作系统为 Ubuntu18.04
二、服务器安装环境 1.进入控制台,重置密码为abc.123.
2. ssh远程连接到服务器
登录云服务器的用户名和密码:
•Linux操作系统用户名:root
如忘记登录密码,可通过控制台提供的“ 重置密码 ” 功能设置新密码。
ssh root@公网ip
也可以各种shell客户端
若出现错误Host key verification failed.
3. 安装python3.9.5
输入python -V 查看服务器python版本。
若不是你的django项目的python版本。选择安装新版本python。
3.1. 下载python3源码# 在开始安装Python之前,请在系统上安装一些必需的软件包。登录到您的Ubuntu系统并执行以下命令: sudo apt update sudo apt install software-properties-common # 使用Apt-Get安装Python 3.9 # Apt软件包管理器提供了在Ubuntu系统上安装Python 3.9的简单方法。请按照以下步骤操作: # 打开系统上的终端,然后为系统配置Deadsnakes PPA。 sudo add-apt-repository ppa:deadsnakes/ppa # 在Ubuntu系统上添加ppa后,更新apt缓存并在Ubuntu上安装Python 3.9。 sudo apt update sudo apt install python3.9 # 等待安装完成。通过执行以下命令检查Python版本: python3.9 -V # 至此,Python 3.9已安装在Ubuntu系统上并可以使用。
3.2. 创建软连接
# 备份python2的软连接 sudo cp /usr/bin/python /usr/bin/python_bak # 删除原来的python2软连接 sudo rm /usr/bin/python # 重新指定python为python3的软连接 sudo ln -s /usr/bin/python3.9 /usr/bin/python3.3. 检查是否成功
python -V
apt-get update apt-get upgrade
(华为云其实不需要,其已经将源修改了)
若apt-get指令无法下载。更新系统下载源
更新系统下载源
root模式下进入sources.list文件
sudo vim /etc/apt/sources.list
将文件中默认的ubuntu源内容替换成下面源命令模式下dd可以删除光标所在行
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
# 安装pip3 sudo apt-get install python3-pip # 检查 pip 是否安装成功 pip3 -v # 更新pip pip3 install --upgrade pip
4.2 修改pip下载源
修改配置文件 :
根目录创建.pip文件:
mkdir ~/.pip cd ~/.pip #编辑pip.conf文件 sudo vim pip.conf
在pip.conf文件中添加清华大学的pypi镜像,要是想用其他的镜像,替换成相应的地址即可。
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple trusted-host = pypi.tuna.tsinghua.edu.cn
其他国内镜像源:
https://mirrors.aliyun.com/pypi/simple https://pypi.mirrors.ustc.edu.cn/simple/ http://pypi.hustunique.com/ http://pypi.sdutlinux.org/ http://pypi.douban.com/simple/
4.3 创建软连接
cd /usr/bin rm pip3 ln -s pip3.6 pip3
重新登录
root@hecs-x-medium-2-linux-20200704093014:~# pip3 -V pip 20.1.1 from /usr/local/lib/python3.6/dist-packages/pip (python 3.6) root@hecs-x-medium-2-linux-20200704093014:~# pip3.6 -V pip 20.1.1 from /usr/local/lib/python3.6/dist-packages/pip (python 3.6) root@hecs-x-medium-2-linux-20200704093014:~# pip -V pip 20.1.1 from /usr/local/lib/python3.6/dist-packages/pip (python 3.6)
5. 安装虚拟环境 (若当前服务器有多项目)
pip install virtualenv
创建虚拟环境
cd ~ cd /home virtualenv py39_django cd ./py39_django source bin/activate 激活虚拟环境
6. 安装mysql service
cd /home wget https://dev.mysql.com/get/mysql-apt-config_0.8.15-1_all.deb dpkg -i mysql-apt-config_0.8.15-1_all.deb
选5.7,
遇到 package configuration 下面有ok键但无法点击的时候。按住键盘上下键选中ok,然后按下enter
apt-get update apt-get install mysql-server
ubuntu 启动/停止mysql
sudo service mysql status # 查看状态 service mysql start service mysql stop service mysql status
先启动mysql服务再登录进入mysql,上面active就代表启动了
mysql -u root -p
输入quit退出
如果配置出错,可选择ubuntu的mysql卸载然后重装
# 删除mysql的数据文件 sudo rm /var/lib/mysql/ -R # 删除mysql的配置文件 sudo rm /etc/mysql/ -R # 自动卸载mysql(包括server和client) sudo apt-get autoremove mysql* --purge sudo apt-get remove apparmor # 然后在终端中查看MySQL的依赖项: dpkg --list|grep mysql # 有时候自动卸载并没有卸载完成。则需要再执行 sudo apt-get remove name手动卸载。 # 清除残留数据: dpkg -l|grep ^rc|awk '{print$2}'|sudo xargs dpkg -P # 最后再执行一次查看依赖。 dpkg --list|grep mysql
7. 安装mysql client
cd /home # 若下载提示失败,可以通过复制网址,浏览器打开下载,然后传到服务器 wget https://dev.mysql.com/get/Downloads/MySQL-8.0/libmysqlclient-dev_5.7.34-1ubuntu18.04_amd64.deb dpkg -i libmysqlclient-dev_5.7.34-1ubuntu18.04_amd64.deb apt-get install libmysqlclient-dev
若报错
Reading package lists... Done Building dependency tree Reading state information... Done You might want to run 'apt-get -f install' to correct these: The following packages have unmet dependencies: libmysqlclient-dev : Depends: libmysqlclient21 (= 8.0.20-1ubuntu18.04) but it is not going to be installed E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution). apt-get -f install apt-get install libmysqlclient-dev
三、操作实践示例 1. 克隆项目代码
cd /home # 替换成你自己的项目git网址,如git clone https://codehub-cn-south-1.devcloud.huaweicloud.com/cgqh00002/visit-tsinghua.git git clone https://xxxxx
注:如果代码的托管仓库是华为,使用https,不要使用ssh(ssh只是针对开发者的,需要密钥)
更新到本地项目
# proj_lab为git之后自动创建的项目文件夹,如cd visit_tsinghua cd proj_lab git fetch --all && git reset --hard origin/master && git pull
2. 导入数据库 2.1新建项目数据库 visit_tsinghua
mysql -u root -p mysql> create database visit_tsinghua default charset=utf8mb4 default collate utf8mb4_unicode_ci; Query OK, 1 row affected (0.67 sec) # 分配新用户(不要使用root用户管理) mysql> create user 'shijianfeng'@'localhost' identified by 'root'; 新建用户shijianfeng Query OK, 0 rows affected (0.60 sec) mysql> grant all privileges on visit_tsinghua .* to 'shijianfeng'@'localhost'; 赋予管理权限 Query OK, 0 rows affected (0.58 sec) mysql> flush privileges; # 刷新权限 Query OK, 0 rows affected (0.13 sec) mysql> quit;
2.2. 重新登录查看是否正确设置
D:\MyDatabase\mysql\mysql-8.0.20-winx64\bin>mysql -h localhost -u shijianfeng -p Enter password: ******** ... mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | visit_tsingua | +--------------------+ 2 rows in set (0.00 sec)
2.3 导出本地数据库上传到服务器
Microsoft Windows [版本 10.0.18362.900] (c) 2019 Microsoft Corporation。保留所有权利。 C:\WINDOWS\system32>mysqldump -u root -p visit_tsinghua >visit_tsinghua.sql Enter password: ******** C:\WINDOWS\system32>
若安装了phpmyadmin,也可以使用phpmyadmin导出
导出时发现,我们在models.py中,数据表的名称是驼峰,有大写,如:BookingRecord。
可是在windows的mysql中,都变成了小写,如bookingrecord,这是因为windows不区分大小写。
但是,unbuntu区分大小写!所以,我们需要手动将数据库表名都修改为驼峰,再导出。
修改windows数据库表之前,应该先设置windows mysql是区分大小写的
请参考:https://blog.csdn.net/qq_35476299/article/details/79991344
若使用的是phpmyadmin
若提示,不能把user修改为User,可以先修改为User1,再由User1修改为User
大小写都修改完之后,再导出visit_tsinghua.sql
复制visit_tsinghua.sql到项目文件夹。使用git,scp, ftp等方法上传
scp 本地文件地址 目的主机:目的主机存储地址
git打开本地存放数据库数据的文件夹
2.4 服务器导入数据库
cd 'visit_tsinghua.sql文件所在的文件夹' mysql -u shijianfeng -p password: mysql>use visit_tsinghua; mysql>source visit_tsinghua.sql; 导入数据库 mysql> show tables;
3. 修改settings.py
DEBUG = False
ALLOWED_HOSTS = ['*']
DEBUG为True,则bug时页面会显示错误信息
*表示允许所有ip访问
4. 根据requirement.txt安装依赖
# 开发机上导出 pip3 freeze > requirements.txt # 查看依赖 cat requirenment.txt Django==3. django-ckeditor==5.9.0 django-js-asset==1.2.2 isort==4.3.21 lazy-object-proxy==1.4.3 mccabe==0.6.1 mysqlclient==1.3.14 Pillow==7.1.2 pycodestyle==2.5.0 pylint==2.4.4 pytz==2020.1 six==1.14.0 typed-ast==1.4.1 wincertstore==0.2 wrapt==1.11.2 # 上传服务器,然后在服务器安装依赖 pip install -r requirements.txt
一定要确保进入了虚拟环境再安装
ERROR
/usr/bin/ld: cannot find -lssl /usr/bin/ld: cannot find -lcrypto collect2: error: ld returned 1 exit status error: command 'x86_64-linux-gnu-gcc' failed with exit status 1 apt-get install openssl apt-get install libssl # 无法安装libssl可以尝试安装libssl-dev root@hecs-x-medium-2-linux-20200704093014:/home/proj_lab# apt-get install libssl Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package libssl dpkg --add-architecture i386 apt-get update apt-get install libssl-dev:i386 pip install -r requirenment.txt # 安装依赖 pip list # 查看已安装的库
若提示
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='pypi.tuna.tsinghua.edu.cn
可以重试
pip install -r requirements.txt -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
5. 启动
python manage.py runserver 0.0.0.0:80
0.0.0.0/0表示所有公网ip地址
端口号可以在服务器的安全组中看到
(py39_django) root@hecs-x-medium-2-linux-20200704093014:/home/proj_lab# python manage.py runserver 0.0.0.0:80 Performing system checks... System check identified no issues (0 silenced). July 04, 2020 - 17:00:14 Django version 2.0, using settings 'proj_lab.settings' Starting development server at http://0.0.0.0:80/ Quit the server with CONTROL-C.
输入服务器的公网ip和端口号访问:http://121.37.130.65:80/
若报错,ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH
则,检查
- pip install -r requirements.txt 安装依赖时是否在虚拟环境下安装
- python manage.py runserver是否在虚拟环境下启动
参考
https://support.huaweicloud.com/usermanual-deployman/deployman_hlp_1014.html
https://mp.weixin.qq.com/s/g4_aJ_kz1Ney50ltFSuOyA
https://www.cnblogs.com/a5idc/p/13850650.html