1. 程式人生 > 實用技巧 >使用django+nginx搭建網站

使用django+nginx搭建網站

https://blog.csdn.net/qq_42327424/article/details/109012658

本地環境

使用windows 10 & python3.8
windows10 中,在python3.8的環境下,找到django-admin,重新命名為django-admin38,並加入環境變數,這樣能在pyhon38的環境下執行。

搭建django工程

終端輸入

django-admin38 startproject blog_website
  • 1

初始化django工程
輸入python manage.py runserver開啟本地測試localhost:8000
說明安裝成功

伺服器配置

我購買的是華為雲耀伺服器
使用xshell登入伺服器,埠號22
更新apt-get

apt-get update
apt-get upgrade
  • 1
  • 2

上傳程式碼

將程式碼放在根目錄以外的目錄中,這裡上傳到/home/workplace中,配合pycharm專業版,可以快速上傳(方法待更新)

安裝nginx

伺服器系統是Ubuntu 18.04server,輸入

apt-get install libpcre3 libpcre3-dev
apt-get install nginx
  • 1
  • 2

輸入

/etc/init.d/nginx start
  • 1

開啟nginx服務,訪問ip地址,可以看到
注:需要將雲伺服器的安全組策略中指定的埠開放

配置python環境

/home資料夾下下載miniconda

cd /home
wget -c https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
  • 1
  • 2

安裝Mini-conda

sh Miniconda3-latest-Linux-x86_64.sh
  • 1

沒有許可權的增加一下許可權
安裝路徑選擇/home/miniconda3,conda不進行初始化
使用vim編輯bashrc檔案

vim ~/.bashrc
  • 1

在檔案最後新增

export  PATH="/home/miniconda3/bin:"$PATH
  • 1

最後啟用

source ~/.bashrc
  • 1

安裝python3.8

conda create -n python38 python=3.8
  • 1

安裝必要python庫

安裝django

source activate
conda activate python38
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ django
  • 1
  • 2
  • 3

安裝uWSGI

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ uwsgi
  • 1

這裡記錄一個插曲,gcc編譯錯誤,我這邊是gcc版本過高,安裝gcc4.8 刪除原有軟連線,建立新軟連結,然後重新安裝

sudo apt-get  install gcc-4.8
sudo rm /usr/bin/gcc
sudo ln -s /usr/bin/gcc-4.8 /usr/bin/gcc 
  • 1
  • 2
  • 3

配置nginx&uwsgi

在專案根目錄下建立uwsgi.ini檔案

[uwsgi]
uid = www
gid = www
socket = :9001
chdir           = /home/www/blog_website
module          = blog_website.wsgi
master          = true
processes       = 2
vacuum          = true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

/etc/nginx/sites-available/default51行後新增

include /etc/nginx/uwsgi_params;
uwsgi_pass     127.0.0.1:9001;
  • 1
  • 2

重啟nginx

/etc/init.d/nginx restart
  • 1

settings.py檔案中將欄位ALLOWED_HOSTS = []改成ALLOWED_HOSTS = ['*']
增加使用者和組

/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
  • 1
  • 2

uwsgi.ini同目錄下執行

uwsgi  --ini uwsgi.ini
  • 1

可看到與本地除錯一樣的執行結果

靜態部署(每次釋出任務都要執行)

1、修改/etc/nginx/sites-available/default

註釋掉try_files $uri $uri/ =404;
新增:

location /static/ {
	alias /home/www/blog_website/static/;
}
  • 1
  • 2
  • 3
2、建立靜態目錄static

3、執行
python manage.py collectstatic
  • 1

修改settings.pydebug=False

4、重啟nginx & uwsgi
/etc/init.d/nginx restart
uwsgi  --ini uwsgi.ini
  • 1
  • 2

可以訪問"ip/admin"來測試是否成功

安裝MySQL

執行命令

sudo apt-get install mysql-server
  • 1

配置資料庫

建立應用使用者

mysql
create user '使用者名稱'@'local' identified by '密碼';  # 建立使用者
grant all on 資料庫名.* to '使用者名稱'@'local';  # 授權
#REVOKE all on 資料庫名.* to '使用者名稱'@'local';  # 取消授權
flush privileges;  # 重新整理許可權
  • 1
  • 2
  • 3
  • 4
  • 5

檢視mysql埠號(一般是3306)

netstat -nultp
  • 1

settings.py中配置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '資料庫名',
        'USER': '使用者名稱',
        'PASSWORD': '密碼',
        'HOST': '127.0.0.1',
        "PORT": '3306',
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

執行

python manage.py migrate
  • 1

新增後臺管理員

python manage.py createsuperuser
  • 1

即可訪問域名或者ip登入

服務式啟動

在路徑/etc/systemd/system/WEBSITENAME.service建立檔案

[Unit]
Description=PROJ's Django server by uWSGI
After=syslog.target

[Service]
ExecStart=python38下uwsgi路徑 --ini 工程中uwsgi.ini路徑
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

輸入

systemctl enable webserver #生效服務
systemctl status webserver #檢視狀態
systemctl start webserver #啟動服務
#systemctl stop webserver #關閉服務
  • 1
  • 2
  • 3
  • 4

至此,配置工作完成,配合git託管程式碼

git init
git add *
git commit
git remote add origin yourrepository
git push origin master