Django項目部署

部署項目之前的準備:
1.購買一臺服務(wù)器,這里我用的是騰訊云服務(wù)器,新用戶8元/月,還是非常實惠的.鏈接:https://cloud.tencent.com/?fromSource=gwzcw.234976.234976.234976
買的時候注意選一下系統(tǒng),我用的是ubuntu16.04,買完以后在控制臺修改一下密碼(要先將服務(wù)器關(guān)機),然后打開終端,用ssh命令遠程連接服務(wù)器

ssh 服務(wù)器的用戶名@服務(wù)器公網(wǎng)IP

ssh ubuntu@94.191.98.70

然后輸入服務(wù)器密碼即可,要注意的是剛改完密碼可能會報錯

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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

登錄上去以后先更新一下應(yīng)用,安裝虛擬環(huán)境,mysql數(shù)據(jù)庫,pip3,nginx 以下是具體的命令,如果不想用MySQL,也可以用自帶的sqlite,記得要遷移即可

sudo apt update
sudo apt upgrade
sudo apt instatll python3-pip
sudo apt install mysql-server mysql-client     (注意安裝數(shù)據(jù)庫時一定要記得設(shè)置用戶名和密碼,密碼最好和本地的一樣)
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 = ['*'] ,并將靜態(tài)url注掉
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
改為:

STATIC_ROOT = os.path.join(BASE_DIR,'static')

將本地項目壓縮,導(dǎo)出本地的虛擬環(huán)境和數(shù)據(jù)庫數(shù)據(jù),然后依次通過scp命令上傳到服務(wù)器:

scp 壓縮文件 用戶名@ip地址:home/ubuntu
例如: scp Desktop/Desktop.zip ubuntu@94.191.98.70:/home/ubuntu

創(chuàng)建虛擬環(huán)境并進入,將上傳的虛擬環(huán)境文件導(dǎo)入 ,安裝uwsgi

sudo pip install uwsgi

進入服務(wù)器的MySQL數(shù)據(jù)庫,建一個和本地一樣的數(shù)據(jù)庫,并導(dǎo)入上傳的數(shù)據(jù)庫數(shù)據(jù)
3.解壓上傳的項目壓縮包,在項目下新建一個conf文件夾,進入配置nginx和uwsgi,直接上代碼:(修改外網(wǎng)IP和靜態(tài)路徑)

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 你的外網(wǎng)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

貼入以下代碼,按照提示修改自己的項目名,應(yīng)用名及虛擬環(huán)境名即可

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir = /home/ubuntu/項目名
# Django's wsgi file
module = 應(yīng)用名.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/虛擬環(huán)境名稱
#虛擬環(huán)境路徑
#logto = /tmp/mylog.log

最后 拉起項目

uwsgi uwsgi.ini

再瀏覽器即可訪問你的服務(wù)器IP,看到發(fā)布的項目

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容