時間:2023-04-18 15:06:01 | 來源:網(wǎng)站運營
時間:2023-04-18 15:06:01 來源:網(wǎng)站運營
Vue3+TypeScript+Django Rest Framework 搭建個人博客(五):網(wǎng)站部署:博客網(wǎng)站開發(fā)完成后,最后一棒是將博客網(wǎng)站部署到公網(wǎng)服務(wù)器上,提供域名訪問,能通過搜索引擎搜索到的博客網(wǎng)站才是真正的博客網(wǎng)站。大家好,我是
落霞孤鶩
,經(jīng)過幾個星期的努力,我們已經(jīng)完成了博客的開發(fā)。這一章我們開始開始部署博客網(wǎng)站,通過公網(wǎng)服務(wù)器和域名訪問我們的博客網(wǎng)站。.cn
和 .com
都可以。Python
編寫的,所以在服務(wù)器上部署的時候,需要安裝Python
運行環(huán)境。python -Vpip -V
TypeScript
編寫的前端,因此在部署之前,需要編譯成原生 Javascript
代碼。yarn build
執(zhí)行完成后,會在代碼路徑下生成 dist
文件夾,里面是編譯后的代碼,文件結(jié)構(gòu)如下圖Home
目錄下,創(chuàng)建一個文件夾blog
,在blog下創(chuàng)建文件夾frontend
。cd ~mkdir blogcd blogmkdir frontend
然后將本地dist
文件夾下的前端文件通過FTP工具上傳到~/blog/frontend
路徑下,前端代碼發(fā)布完成。uwsgi
,因此需要先在服務(wù)器上安裝依賴,執(zhí)行如下命令pip install uwsgi
安裝完成后,通過命令驗證是否安裝成功uwsgi --version
uwsgi
配置文件uwsgi.ini
,我們通過socket的方式連接后端接口,配置信息如下:[uwsgi]socket = 127.0.0.1:8000# 可以理解為此文件的絕對路徑chdir = ~/blog/backend/# wsgi與chdir的相對路徑wsgi-file = project/wsgi.pyprocesses = 4# 日志daemonize = ~/blog/backend/logs/uwsgi.logpidfile = ~/blog/backend/uwsgi.pidmaster = True
在后端代碼項目路徑下新建文件uwsgi.param
,文件內(nèi)容如下uwsgi_param QUERY_STRING $query_string;uwsgi_param REQUEST_METHOD $request_method;uwsgi_param CONTENT_TYPE $content_type;uwsgi_param CONTENT_LENGTH $content_length;uwsgi_param REQUEST_URI $request_uri;uwsgi_param PATH_INFO $document_uri;uwsgi_param DOCUMENT_ROOT $document_root;uwsgi_param SERVER_PROTOCOL $server_protocol;uwsgi_param REQUEST_SCHEME $scheme;uwsgi_param HTTPS $https if_not_empty;uwsgi_param REMOTE_ADDR $remote_addr;uwsgi_param REMOTE_PORT $remote_port;uwsgi_param SERVER_PORT $server_port;uwsgi_param SERVER_NAME $server_name;
~/blog
文件夾下,創(chuàng)建backend
文件夾cd ~/blogmkdir backend
通過 FTP 工具將后端代碼上傳到~/blog/backend
文件夾下,包括剛剛創(chuàng)建的uwsgi.ini
文件然后在backend文件夾下,創(chuàng)建logs
文件夾,用來記錄日志mkdir logs
至此,后端代碼已經(jīng)部署到位。uwsgi --ini ~/blog/backend/uwsgi.ini
測試可以通過日志文件查看啟動后的服務(wù)器日志tail -f ~/blog/backend/logs/uwsgi.log
停止命令如下:uwsgi --stop ~/blog/backend/uwsgi.pid
后期如果后端代碼更新,則需要先停止uwsgi
服務(wù)后,再啟動代碼的更新。conf
文件下。conf
文件夾中,名稱是 nginx.conf
。location / { root ~/blog/frontend/dist; index index.html index.htm; if (!-e $request_filename) { rewrite ^/(.*) /index.html last; break; }}location /upload/ { root ~/blog/backend/; expires 24h;}
location
里面有很關(guān)鍵的兩行配置uwsgi_pass blog
和include ~/blog/backend/uwsgi.param
,明確的是通過uwsgi
的方式代理 Python 的接口# server upstream blog{ server 127.0.0.1:8000; }location /api/ { uwsgi_pass blog; include ~/blog/backend/uwsgi.param; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_set_header accept-encodeing 'gzip, deflate'; proxy_set_header content-type 'application/json'; proxy_set_header X-Real-IP $remote_addr; proxy_set_header authorization $http_authorization; proxy_set_header accept '*/*'; proxy_set_header x-bce-date $http_x_bce_date; }
Gzip
壓縮Gzip
壓縮的原因是在前后端分離的場景下,網(wǎng)站首屏需要加載的內(nèi)容很多,通過縮小網(wǎng)絡(luò)傳輸過程中的文件大小,從而加快首屏渲染時的靜態(tài)文件加載。# Gzip gzip on; gzip_min_length 1k; gzip_comp_level 3; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml; gzip_vary on; gzip_disable "MSIE [1-6]/."; gzip_buffers 32 4k; gzip_http_version 1.0;
server { listen 80; server_name www.longair.cn; rewrite ^(.*)$ https://${server_name}$1 permanent; }server { listen 443 ssl; server_name www.longair.cn; root html; index index.html index.htm;}
Https
證書文件配置ssl_certificate /etc/nginx/longair.cn_nginx/server.crt;ssl_certificate_key /etc/nginx/longair.cn_nginx/server.key;ssl_session_timeout 5m;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_prefer_server_ciphers on;
Http
自動轉(zhuǎn) Https
server { listen 80; server_name www.longair.cn; rewrite ^(.*)$ https://${server_name}$1 permanent; }
history
,這種模式的路由可讀性強,但文章詳情頁面的路由如果通過新開頁面的方式,會出現(xiàn)404的問題,因此我們需要對這種情況做處理,配置原理是,訪問的路徑如果不是文件結(jié)尾時,則自動重定向到首頁,進(jìn)入到index.html
后就可以通過路由變動進(jìn)入到單頁應(yīng)用的路由,從而正確渲染詳情頁面。Vue-router 官網(wǎng)文檔:HTML5 History 模式 | Vue Router (vuejs.org)location / { root ~/blog/frontend/dist; index index.html index.htm; if (!-e $request_filename) { rewrite ^/(.*) /index.html last; break; } }
user root;worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; keepalive_timeout 65; client_max_body_size 200M; # Gzip gzip on; gzip_min_length 1k; gzip_comp_level 3; gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml; gzip_vary on; gzip_disable "MSIE [1-6]/."; gzip_buffers 32 4k; gzip_http_version 1.0; # server upstream blog{ server 127.0.0.1:8000; } # blog http server { listen 80; server_name www.longair.cn; rewrite ^(.*)$ https://${server_name}$1 permanent; } server { listen *:80; listen *:443 ssl; listen [::]:80; listen [::]:443 ssl; server_name longair.cn; ssl_certificate /etc/nginx/longair.cn_nginx/server.crt; ssl_certificate_key /etc/nginx/longair.cn_nginx/server.key; return 301 https://www.longair.cn$request_uri; } # blog https server { listen 443 ssl; server_name www.longair.cn; root html; index index.html index.htm; ssl_certificate /etc/nginx/longair.cn_nginx/server.crt; ssl_certificate_key /etc/nginx/longair.cn_nginx/server.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { root ~/blog/frontend/dist; index index.html index.htm; if (!-e $request_filename) { rewrite ^/(.*) /index.html last; break; } } location /upload/ { root /blog/backend/; expires 24h; } location /api/ { uwsgi_pass blog; include ~/blog/backend/uwsgi.param; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_set_header accept-encodeing 'gzip, deflate'; proxy_set_header content-type 'application/json'; proxy_set_header X-Real-IP $remote_addr; proxy_set_header authorization $http_authorization; proxy_set_header accept '*/*'; proxy_set_header x-bce-date $http_x_bce_date; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location = /50x.html { root ~/blog/frontend/dist; } }}
/usr/local/nginx/sbin/nginx -s start
/usr/local/nginx/sbin/nginx -s stop
/etc/nginx/sbin/nginx -s reload
tail -f /etc/nginx/logs/access.logtail -f /etc/nginx/logs/error.log
uwsgi --ini ~/blog/backend/uwsgi.ini
uwsgi --stop ~/blog/backend/uwsgi.pid
uwsgi --stop ~/blog/backend/uwsgi.pid
Mysql
數(shù)據(jù)庫。Mysql
安裝mysql -uroot -p
然后輸入密碼,回車,登錄進(jìn)入數(shù)據(jù)庫。create database blog character set utf8mb4;
create user 'blog'@'%' identified by '12345678';
grant all privileges on blog.* to 'blog'@'%';flush privileges;
sqlite
數(shù)據(jù)庫,調(diào)試方便,訪問速度快,而生產(chǎn)環(huán)境上,使用 Mysql
會更安全。所以我們希望開發(fā)和生產(chǎn)環(huán)境使用不同的數(shù)據(jù)庫配置。Github
等倉庫,可以會泄露自己的數(shù)據(jù)庫密碼,因此我們這里通過單獨的配置文件來記錄數(shù)據(jù)庫賬號信息,然后在Python中通過讀取數(shù)據(jù)庫賬號配置文件來獲取信息,從而實現(xiàn)數(shù)據(jù)庫密碼保護(hù)。project/settings.py
ALLOWED_HOSTS = ['www.longair.cn', '127.0.0.1', 'localhost', ]ENV_PROFILE = os.getenv("PYTHON_ENV")if ENV_PROFILE == "production": import configparser cf = configparser.ConfigParser() cf.read('/blog/db.cnf') # 這個文件的內(nèi)容在下一步會創(chuàng)建,至于路徑是哪里都可以,只要下一步創(chuàng)建的文件是在這里寫的路徑下即可 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': cf.get('db', 'database'), 'USER': cf.get('db', 'user'), # 從配置文件中獲取數(shù)據(jù)庫用戶名 'PASSWORD': cf.get('db', 'password'), # 從配置文件中獲取數(shù)據(jù)庫密碼 'HOST': 'localhost', 'PORT': '3306' } } DEBUG = False # 生產(chǎn)環(huán)境下關(guān)閉debug模式else: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'data/db.sqlite3'), } } DEBUG = True
vi ~/.bashrc
,在里面添加一行:export PYTHON_ENV=production
,然后按 esc
,輸入::wq
保存source ~/.bashrc
~/blog
下新增文件vi db.cnf
,然后增加如下內(nèi)容:[db]database = bloguser = blogpassword = 12345678default-character-set = utf8
數(shù)據(jù)庫、賬號、密碼和上面的數(shù)據(jù)庫信息保持一致。關(guān)鍵詞:部署
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。