部署Django專案Nginx uwsgi

2021-08-21 14:54:04 字數 3015 閱讀 7283

部署方式:nginx**+uwsgi應用服務

作業系統:centos7

django專案:為例:

1. 安裝 python3.6

獲取安裝包

wget

tar -xzvf python-3.6.2.tgz -c /tmp

cd /tmp/python-3.6.2/

安裝到 /usr/local 目錄

./configure --prefix=/usr/local

make

make altinstall

更改/usr/bin/python鏈結

ln -s /usr/local/bin/python3.6 /usr/bin/python3

2. mariadb

安裝 sudo yum install mariadb-server

啟動 sudo systemctl start mariadb

設定bind-ip

vim /etc/my.cnf 在 [mysqld]下新增

bind-address = 0.0.0.0

設定訪問許可權

mysql命令直接進入mysql互動環境,執行下面命令:

grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;

grant all privileges on *.* to 'root'@'localhost' identified by '123456' with grant option;

flush privileges;

yum install python-setuptools python-devel

在.bashrc檔案中,新增

export workon_home=$home/.virtualenvs

source ~/.bashrc

新建虛擬環境 mkvirtualenv mysite-env

進入虛擬環境 workon mysite-env

安裝django專案依賴

在開發環境通過 pip freeze > requirements.txt 將mysite的虛擬環境安裝包匯出來

在部署伺服器的虛擬環境中執行:pip install -r requirements.txt

5. uwsgi

安裝pip install uwsgi

測試uwsgi

安裝配置新建mysite_nginx.conf 

# the upstream component nginx needs to connect to

upstream django

# configuration of the server

server

location /static

# finally, send all non-media requests to the django server.

location /

}

將該配置檔案加入到nginx的啟動配置檔案中

sudo ln -s /root/mysite/mysite_nginx.conf /etc/nginx/conf.d/

7. 拉取所有需要的static file 到同乙個目錄

在django的setting檔案中,新增下面一行內容:

static_root = os.path.join(base_dir, "static/")

執行命令 python manage.py collectstatic

8. 重啟nginx

如果systemctl restart nginx 有許可權問題,則直接用nginx命令啟動 sudo /usr/sbin/nginx

9. 配置並啟動uwsgi

新建uwsgi.ini 配置檔案, 內容如下:

# mysite_uwsgi.ini file

[uwsgi]

# django-related settings

# the base directory (full path)

chdir = /root/mysite

# django's wsgi file

module = mysite.wsgi

# the virtualenv (full path)

virtualenv = /root/.virtualenvs/mysite-env

# 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

# chmod-socket = 664

# clear environment on exit

vacuum = true

logto = /tmp/mylog.log

注:

chdir: 表示需要操作的目錄,也就是專案的目錄

module: wsgi檔案的路徑

processes: 程序數

virtualenv:虛擬環境的目錄

執行uwsgi uwsgi -i /root/mysite/uwsgi.ini &

10.訪問

q:安裝mysqlclient出問題

centos 7: yum install python-devel mariadb-devel -y

ubuntu: sudo apt-get install libmysqlclient-dev

然後: pip install mysqlclient

部署django專案

在你專案的根目錄中建立mysite.xml 名字無所謂 或者建立mysite.ini,輸入以下內容 uwsgi socket 127.0.0.1 8000 socket chdir data project1 chdir module project1.wsgi module processes 4...

nginx部署專案

yum install y nginx 設定開機啟動 systemctl start nginx.service systemctl enable nginx.service nginx預設使用埠 80,ecs例項沒有開啟埠80,預設只有 22 和 3389 將自己的vue專案 執行如下命令打包 c...

nginx基於uwsgi部署django專案

1.安裝nginx yum install y nginx 需要epel源 2.安裝環境 可以考慮使用虛擬化環境,本處不再使用3.安裝uwsgi yum groupinstall development tools yum install zlib devel bzip2 devel pcre de...