一、前言
之前在本地主机上写了一个django项目,最近又买了阿里云的学生服务器,就想着把这个django项目部署到服务器上。如今完成部署,发现自己踩过好多坑,就写了这篇文章来记录一下自己踩的坑,希望对你有帮助。第一次写文章,如有出错,请多多包涵与指正。
二、部署前的准备
- 有一台可以连接公网的服务器(我的系统是ubuntu 18.04.5)
- 可以运行的django项目
- 了解git与github(用于给服务器上传django项目)
- 了解一些基础的linux操作指令
- 一个可用的域名(如果没有也没关系,服务器公网ip也可以登录网站)
- 新手最好有一个可视化的SFTP工具(如WinSCP,可以方便操作)
三、开始部署
3.1搭建环境
如果你的服务器是“崭新出厂”的,请创建一个普通用户并赋予他超级权限。因为在root下部署代码不是很安全,且有可能出错。
创建一个拥有超级权限的用户步骤如下:
#创建新用户wss
root@VM-0-7-ubuntu:~# useradd -m -s /bin/bash wss
#新创建的用户加入超级权限组
root@VM-0-7-ubuntu:~# usermod -a -G sudo wss
#为新用户设置密码
root@VM-0-7-ubuntu:~# passwd wss
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
#切换到创建的新用户
root@VM-0-7-ubuntu:~# su - wss
#切换成功,@符号前面已经是新用户名而不是 root 了
wss@VM-0-7-ubuntu:~#
同时,记得更新一下系统,避免版本太老带来的一些困惑。(因为某些原因,我下面使用的还是root,没有用wss用户)
root@VM-0-7-ubuntu:~# sudo apt-get update
root@VM-0-7-ubuntu:~# sudo apt-get upgrade
安装一些必备的软件(挨个复制)
root@VM-0-7-ubuntu:~# sudo apt-get install nginx
root@VM-0-7-ubuntu:~# sudo apt-get install git python3 python3-pip
root@VM-0-7-ubuntu:~# sudo pip3 install virtualenv
#安装mysql
root@VM-0-7-ubuntu:~# sudo apt-get install mysql-server
root@VM-0-7-ubuntu:~# apt-get isntall mysql-client
#安装过程中会提示设置密码什么的,注意设置了不要忘了,安装完成之后可以使用如下命令来检查是否安装成功:
root@VM-0-7-ubuntu:~# sudo netstat -tap | grep mysql
(No info could be read for "-p": geteuid()=1000 but you should be root.)
tcp 0 0 localhost:mysql *:* LISTEN
#登陆mysql数据库可以通过如下命令
root@VM-0-7-ubuntu:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.7.27-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#查看当前的数据库
mysql> show databases;
+--------------------+
| Database |
+---------------