时间:2023-04-18 15:06:01 | 来源:网站运营
时间:2023-04-18 15:06:01 来源:网站运营
Vue3+TypeScript+Django Rest Framework 搭建个人博客(五):网站部署:博客网站开发完成后,最后一棒是将博客网站部署到公网服务器上,提供域名访问,能通过搜索引擎搜索到的博客网站才是真正的博客网站。大家好,我是
落霞孤鹜
,经过几个星期的努力,我们已经完成了博客的开发。这一章我们开始开始部署博客网站,通过公网服务器和域名访问我们的博客网站。.cn
和 .com
都可以。Python
编写的,所以在服务器上部署的时候,需要安装Python
运行环境。python -Vpip -V
TypeScript
编写的前端,因此在部署之前,需要编译成原生 Javascript
代码。yarn build
执行完成后,会在代码路径下生成 dist
文件夹,里面是编译后的代码,文件结构如下图Home
目录下,创建一个文件夹blog
,在blog下创建文件夹frontend
。cd ~mkdir blogcd blogmkdir frontend
然后将本地dist
文件夹下的前端文件通过FTP工具上传到~/blog/frontend
路径下,前端代码发布完成。uwsgi
,因此需要先在服务器上安装依赖,执行如下命令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
,文件内容如下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
文件夹下,创建backend
文件夹cd ~/blogmkdir backend
通过 FTP 工具将后端代码上传到~/blog/backend
文件夹下,包括刚刚创建的uwsgi.ini
文件然后在backend文件夹下,创建logs
文件夹,用来记录日志mkdir logs
至此,后端代码已经部署到位。uwsgi --ini ~/blog/backend/uwsgi.ini
测试可以通过日志文件查看启动后的服务器日志tail -f ~/blog/backend/logs/uwsgi.log
停止命令如下:uwsgi --stop ~/blog/backend/uwsgi.pid
后期如果后端代码更新,则需要先停止uwsgi
服务后,再启动代码的更新。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
里面有很关键的两行配置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
压缩的原因是在前后端分离的场景下,网站首屏需要加载的内容很多,通过缩小网络传输过程中的文件大小,从而加快首屏渲染时的静态文件加载。# 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
自动转 Https
server { listen 80; server_name www.longair.cn; rewrite ^(.*)$ https://${server_name}$1 permanent; }
history
,这种模式的路由可读性强,但文章详情页面的路由如果通过新开页面的方式,会出现404的问题,因此我们需要对这种情况做处理,配置原理是,访问的路径如果不是文件结尾时,则自动重定向到首页,进入到index.html
后就可以通过路由变动进入到单页应用的路由,从而正确渲染详情页面。Vue-router 官网文档: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
数据库。Mysql
安装mysql -uroot -p
然后输入密码,回车,登录进入数据库。create database blog character set utf8mb4;
create user 'blog'@'%' identified by '12345678';
grant all privileges on blog.* to 'blog'@'%';flush privileges;
sqlite
数据库,调试方便,访问速度快,而生产环境上,使用 Mysql
会更安全。所以我们希望开发和生产环境使用不同的数据库配置。Github
等仓库,可以会泄露自己的数据库密码,因此我们这里通过单独的配置文件来记录数据库账号信息,然后在Python中通过读取数据库账号配置文件来获取信息,从而实现数据库密码保护。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') # 这个文件的内容在下一步会创建,至于路径是哪里都可以,只要下一步创建的文件是在这里写的路径下即可 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': cf.get('db', 'database'), 'USER': cf.get('db', 'user'), # 从配置文件中获取数据库用户名 'PASSWORD': cf.get('db', 'password'), # 从配置文件中获取数据库密码 'HOST': 'localhost', 'PORT': '3306' } } DEBUG = False # 生产环境下关闭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
,然后增加如下内容:[db]database = bloguser = blogpassword = 12345678default-character-set = utf8
数据库、账号、密码和上面的数据库信息保持一致。关键词:部署