部署項目之前的準備:
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,看到發布的項目