Django專案部署到web服務器
- 前言
- 準備作業
- 安裝python3
- 安裝django和uwsgi
- 測驗是否安裝成功
- 配置自己的python專案
- 配置settings.py
- 連接資料庫
- 撰寫uwsgi.ini檔案
- 配置nginx
- 同步靜態檔案到nginx設定的目錄下
- 啟動專案
- 啟動uwsgi
- 啟動nginx
前言
自己寫的Django專案 一直沒試過部署上線 結果今天部署了一天才搞好 踩了不少坑 但是最后還是弄好了!
準備作業
LNMP詳細部署可以查看我另一篇博客
LNMP部署
- Python專案
- LNMP環境(Linux Nginx Mysql Python)
- uwsgi
- Django
安裝python3
yum install -y epel-release # 安裝EPEL擴展源
yum install -y python36 python36-pip python36-devel sqlite-devel supervisor # 安裝Python3.6及其工具組件
[root@maomao scripts]# python3
Python 3.6.8 (default, Nov 16 2020, 16:55:22)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello world')
hello world
pip3 install --upgrade pip # 升級pip版本
echo "alias python='/usr/bin/python3.6'" >/etc/profile.d/python.sh
# 添加Python 3.6為系統執行的默認Python
source /etc/profile # 使系統配置生效
安裝django和uwsgi
這里需要注意的點 你專案的django版本 需要和服務器安裝的django版本一樣
pip3 list 可以查看版本
比如我是django2.2 因此安裝django的時候這樣寫
pip3 install django==2.2
pip3 install uwsgi
測驗是否安裝成功
vim test.py
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b'Hello World']
uwsgi --http :8000 --wsgi-file test.py
在瀏覽器中輸入 網址:8000,出現 “Hello World”表示安裝成功,

配置自己的python專案

我的專案已經打包傳上了服務器
[root@maomao home]# ls
studentsSystem.zip test.py
[root@maomao home]# unzip studentsSystem.zip
[root@maomao home]# cd studentsSystem
配置settings.py
將資料庫和路徑都要改成當前環境的
運行所有ip訪問
ALLOWED_HOSTS = ['*']
修改資料庫資訊
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'studentssystem',
'USER': 'root',
'PASSWORD': '123',
'HOST': '47.108.76.25',
'PORT': '3306',
'OPTIONS':{
'init_command': "SET sql_mode='STRICT_TRANS_TABLES';" # 初始化資料庫的命令
}
}
}
連接資料庫
這里需要注意阿里云的安全組 需要手動配置打開3306埠 不然無法連接資料庫
如果還是連接不上可以授權
grant all on *.* to 'root'@'%' identified by '123';
grant all on *.* to 'root'@'localhost' identified by '123';
flush privileges;
pip install pymysql
創建自己專案的庫
mysql> create database studentssystem;
執行sql
[root@maomao studentsSystem]# pwd
/home/studentsSystem
python3 manage.py migrate 創建資料表
python manage.py createsuperuser 創建admin用戶

查看資料庫里自己的專案表
mysql> select * from studentssystem;
ERROR 1146 (42S02): Table 'studentssystem.studentssystem' doesn't exist
mysql> show tables;
+----------------------------+
| Tables_in_studentssystem |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| course |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
| student |
| studentinformation |
+----------------------------+
13 rows in set (0.00 sec)
撰寫uwsgi.ini檔案
[uwsgi]
socket =127.0.0.1:8000
chdir = /home/studentsSystem
module = studentsSystem.wsgi
master = true
processes = 4
vacuum = true
- socket: 指定專案執行的埠號
- chdir: 專案的目錄,
- module: module = studentsSystem.wsgi 意思是對于 uwsgi.ini 檔案而言,與它同級目錄有一個 studentsSystem 目錄,這個目錄下有一個 wsgi.py 檔案,
通過 uwsgi 命令讀取 uwsgi.ini 檔案啟動專案
uwsgi --ini uwsgi.ini
配置nginx
vim /usr/local/nginx/conf/nignx.conf
events {
worker_connections 4096;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
upstream django {
server 127.0.0.1:8000;
}
server {
listen 8080;
server_name localhost;
charset utf-8;
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass django;
}
location /static/ {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /home/studentsSystem/static/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
檢查語法
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
同步靜態檔案到nginx設定的目錄下
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = (
('css',os.path.join(MEDIA_ROOT,'css').replace('\\','/') ),
('js',os.path.join(MEDIA_ROOT,'js').replace('\\','/') ),
('images',os.path.join(MEDIA_ROOT,'images').replace('\\','/') ),
('upload',os.path.join(MEDIA_ROOT,'upload').replace('\\','/') ),
)
# 設定圖片等靜態檔案的路徑(電腦系統檔案夾路徑)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, '/home/studentsSystem/static/').replace('\\', '/'),
)
在django專案setting.py最下面添加
STATIC_ROOT = '/home/studentsSystem/static/'
命令列輸入
python manage.py collectstatic
自動將所有靜態檔案復制到nginx的索引目錄
啟動專案
啟動uwsgi
uwsgi --ini uwsgi.ini
啟動nginx
systemctl start nginx



添加學生資訊之后我們去資料查看一下


專案部署成功
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/273625.html
標籤:其他
