二、项目启动和部署
1. 项目简单技术介绍:此项目为个人博客网站,前台博客网站用nuxt.js开发,后台博客管理系统及接口用Koa开发,模板引擎使用的EJS,对数据库的连接及增删改查使用了mongoose。
2. 安装两个项目依赖,命令npm install,如果npm速度很慢,我们可以使用cnpm,安装命令npm install -g cnpm --registry=https://registry.npm.taobao.org
3. 将我们的原有的数据库的数据导入到云服务器的数据库中,打开控制台,导入命令C:\MongoDB\bin>mongorestore -h localhost:27017 -d LifeRecord C:\LifeRecordData,LifeRecord是数据库名称,C:\LifeRecordData是备份数据的路径
数据导出命令为C:\MongoDB\bin>mongodump -d localhost:27017 -d <数据库名> -o <备份数据导出路径>
4. 运行koa项目,注意只需监听3000端口号,不需要添加localhost或者1270.0.1
5. 运行nuxt.js项目,修改项目运行的主机和端口号,在nuxt.config.js文件中添加代码server:{ port:8080,host:'0.0.0.0' },axios请求代理的域名还是http://localhost:3000/api/
开发环境运行命令npm run dev
生产环境先npm run build,再npm run start
6. 进入阿里云控制台,【安全】-【防火墙】-【添加规则】-应用类型为自定义、协议为TCP、端口范围为8080,再添加一条端口范围为3000
打开浏览器在地址栏输入35.105.38.16:8080或者zhuyinghui.shop:8080或者manage.zhuyinghui.shop就能打开前台博客网站,35.105.38.16:3000或者zhuyinghui.shop:3000或者manage.zhuyinghui.shop:3000就能打开后台管理系统
那么到目前为止,我们就已经启动了前后台项目,并且通过域名/IP地址加端口号就能访问到我们的项目,接下来我们就要利用nginx,使得域名zhuyinghui.shop访问博客前台,域名manage.zhuyinghui.shop访问博客后台
7. 配置nginx,打开nginx/conf/nginx.conf文件,此文件为配置文件,文件内容如下
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream frontend {
server 127.0.0.1:8080;
}
upstream rearend {
server 127.0.0.1:3000;
}
server {
listen 80;
server_name zhuyinghui.shop;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#设置主机头和客户端真实地址,以便服务器获取客户端真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#禁用缓存
proxy_buffering off;
#反向代理的地址
proxy_pass http://frontend;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 80;
server_name manage.zhuyinghui.shop;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#设置主机头和客户端真实地址,以便服务器获取客户端真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#禁用缓存
proxy_buffering off;
#反向代理的地址
proxy_pass http://rearend;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
8. 打开任务管理器,结束所有nginx后台进程,再双击nginx.exe重新启动
最后,我们便能成功使用zhuyinghui.shop进入博客前台,使用manage.zhuyinghui.shop进入后台管理系统了。