赞
踩
部署简介
本项目是一个前后端分离项目,前端使用vue
和elementUI
,后端是springBoot
集成redis
、mysql
。项目中的静态资源直接上传到服务器磁盘,使用nginx做动静分离。
部署实战
1:服务器项目环境搭建
2:前后端项目的资源打包上传
后端项目打包:这里要注意几个问题,
打包上传
npm run build
命令之后,会生成一个dist目录,这个就是打包之后的文件。然后使用xftp把文件上传到服务器。你也可以使用其他方式上传。
3:nginx的动静分离及反向代理的配置
[root@iZwz9hv1phm24s3jicy8x1Z ~]# whereis nginx
nginx: /usr/local/nginx
[root@iZwz9hv1phm24s3jicy8x1Z ~]# cd /usr/local/nginx/conf
[root@iZwz9hv1phm24s3jicy8x1Z conf]# cat 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 dongmu {
server 127.0.0.1:8080;#这里填写自己的外网IP39.128176.250:后端服务的端口
}
server {
listen 80;#这里填写nginx监听的端口
server_name localhost;
root /home/crm/dist;#这里是饿哦们刚才上传的前端项目的路径
#这样写了之后我们访问外网ip直接就会跳到前端项目入口
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
root /home/crm/dist;
index index.html index.htm;
proxy_set_header Host $host;#保留代理之前的host
proxy_set_header X-Real-IP $remote_addr;#保留代理之前的真实客户端ip
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header HTTP_X_FORWARDED_FOR $remote_addr;#在多级代理的情况下,记录每次代理之前的客户端真实ip
}
location /crmImage {
root /home/data;
autoindex on; #列出当前目录的内容
proxy_set_header Host $host;#保留代理之前的host
proxy_set_header X-Real-IP $remote_addr;#保留代理之前的真实客户端ip
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header HTTP_X_FORWARDED_FOR $remote_addr;#在多级代理的情况下,记录每次代理之前的客户端真实ip
}
location /api {
#这个本来是可以用作负载均衡的,但是我的前端项目9999端口跳转不到,直接给我跳转到了80,
#我也不知道为什么,由于我项目的路径是/api我就设置这个反向代理到本机的8080端口
proxy_pass http://本机的外网ip:8080;
proxy_redirect default;
proxy_set_header Host $host;#保留代理之前的host
proxy_set_header X-Real-IP $remote_addr;#保留代理之前的真实客户端ip
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header HTTP_X_FORWARDED_FOR $remote_addr;#在多级代理的情况下,记录每次代理之前的客户端真实ip
}
#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;
# }
#}
}
4:启动项目及外网访问测试
nohup java -jar Project_CRM-0.0.1-SNAPSHOT.jar &
nohup:不挂断地运行命令,输出都将附加到当前目录的 nohup.out 文件中。如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中
[root@iZwz9hv1phm24s3jicy8x1Z conf]# ps -ef|grep java
root 878777 878582 0 09:59 pts/0 00:00:25 java -jar Project_CRM-0.0.1-SNAPSHOT.jar
root 882333 881377 0 10:53 pts/2 00:00:00 grep --color=auto java
#这里可以看到项目启动成功
停止项目
[root@iZwz9hv1phm24s3jicy8x1Z crm]# ps -aux|grep jar
root 878777 0.0 14.3 2734536 289296 ? Sl Apr20 7:26 java -jar Project_CRM-0.0.1-SNAPSHOT.jar
root 1401743 0.0 0.0 221460 792 pts/0 S+ 23:41 0:00 grep --color=auto jar
[root@iZwz9hv1phm24s3jicy8x1Z crm]# kill -9 878777
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。