部署项目之前的准备:
1.购买一台服务器,这里我用的是腾讯云服务器,新用户8元/月,还是非常实惠的.链接:https://cloud.tencent.com/?fromSource=gwzcw.234976.234976.234976
买的时候注意选一下系统,我用的是ubuntu16.04,买完以后在控制台修改一下密码(要先将服务器关机),然后打开终端,用ssh命令远程连接服务器
ssh 服务器的用户名@服务器公网IP
ssh ubuntu@94.191.98.70
然后输入服务器密码即可,要注意的是刚改完密码可能会报错
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
07:36:8e:d0:72:88:38:f7:21:10:c3:12:d6:35:ad:55.
Please contact your system administrator.
Add correct host key in /Users/watsy/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/watsy/.ssh/known_hosts:1
RSA host key for 192.168.2.108 has changed and you have requested strict checking.
Host key verification failed.
解决办法:
rm -rf ~/.ssh/known_hosts
登录上去以后先更新一下应用,安装虚拟环境,mysql数据库,pip3,nginx 以下是具体的命令,如果不想用MySQL,也可以用自带的sqlite,记得要迁移即可
sudo apt update
sudo apt upgrade
sudo apt instatll python3-pip
sudo apt install mysql-server mysql-client (注意安装数据库时一定要记得设置用户名和密码,密码最好和本地的一样)
sudo apt-get install nginx
sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper
sudo vi .bashrc 贴入下面三行代码:
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
2.进入项目下的settings.py将本地的项目Debuge改为False,ALLOWED_HOSTS = ['*'] ,并将静态url注掉
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
改为:
STATIC_ROOT = os.path.join(BASE_DIR,'static')
将本地项目压缩,导出本地的虚拟环境和数据库数据,然后依次通过scp命令上传到服务器:
scp 压缩文件 用户名@ip地址:home/ubuntu
例如: scp Desktop/Desktop.zip ubuntu@94.191.98.70:/home/ubuntu
创建虚拟环境并进入,将上传的虚拟环境文件导入 ,安装uwsgi
sudo pip install uwsgi
进入服务器的MySQL数据库,建一个和本地一样的数据库,并导入上传的数据库数据
3.解压上传的项目压缩包,在项目下新建一个conf文件夹,进入配置nginx和uwsgi,直接上代码:(修改外网IP和静态路径)
vi test_nginx
贴入以下代码并保存退出 shift+z+z
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
server_name 你的外网ip ; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/ubuntu/test1/static/media; # 指向django的media目录
}
location /static {
alias /home/ubuntu/test1/static; # 指向django的static目录
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include uwsgi_params; # the uwsgi_params file you installed
}
}
建立一条软连接
sudo ln -s /home/ubuntu/项目名字/conf/test_nginx.conf /etc/nginx/conf.d
重启nginx:
sudo service nginx restart
拉取所有需要的static file到同一个目录,
cd ..
python manage.py collectstatic
再次进入conf配置uwsgi
vi uwsgi.ini
贴入以下代码,按照提示修改自己的项目名,应用名及虚拟环境名即可
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/ubuntu/项目名
# Django's wsgi file
module = 应用名.wsgi
# the virtualenv (full path)
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = 127.0.0.1:8000
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
virtualenv = /home/ubuntu/.virtualenvs/虚拟环境名称
#虚拟环境路径
#logto = /tmp/mylog.log
最后 拉起项目
uwsgi uwsgi.ini
再浏览器即可访问你的服务器IP,看到发布的项目
网友评论