dist文件部署到服务器,npm run build之后生成的dist如何扔到服务器运行(npm run build之后如何本地运行)...

运行npm run build之后,会生成一个dist文件夹,里面的目录结构大概是这样的:

124e61fbd9206deb631d9f4ecb1cf50c.png

生成完的文件我们怎么来运行呢?直接在本地打开inde.html是无法运行的,打包的时候有提示:

424e93e5957eb15ac55a6e0461c7c8f5.png

构建文件应该通过HTTP服务器提供服务。

所以我们要启动一个HTTP服务器才能够运行。选择一个HTTP服务器,下面用Apache Tomcat和Nginx为例,分别运行一下。

一、Apche Tomcat

1、下载Apche Tomcat

2、解压下载的Apche Tomcat

3、启动Apche Tomcat

打开解压后的文件,6dc4fd6c798f42dd2c2c0d3695bf71ec.png,进到bin目录,点击运行startup.bat,等待启动成功

439538fb7a1d1c442e2b548d7bc9b99a.png

4、打开http://localhost:8080/

可以看到服务器已经启动了

6c786bedea7730574fe7a9a24f03e14c.png

5、将dist内的文件丢到服务器内

因为Tomcat服务器默认运行解压目录下的webapps内ROOT文件夹内的index.jsp,所以你才看到了上面这个页面

ad18a79a46baef9ddadb3d91b59b7fd2.png

运行tomcat解压bin目录下的shutdown.bat停掉服务器

删掉ROOT目录下的所有文件

复制dist内的文件到ROOT目录下

1b0b587f4b3c07b539c7f0e9236bae09.png

然后startup.bat起服务器

访问http://localhost:8080/

可能会有以下报错:

3b0991cdb3a82d0c36845cbe7a439f87.png

报错的原因是找不到静态资源,也就是打包路径配错了。比如其中一个文件,访问的地址是:

http://localhost:8080/vueAdmin-template/static/css/app.cc4940008e7a1f8f5b3cef1ae12a5832.css

意思是到服务器上请求vueAdmin-template/static/css文件夹内的css文件

我们服务器上的文件只有static目录,没有vueAdmin-template这个目录,所以404找不到了

275d05f3a5fdd752ddce6f8e0c4ce69c.png

解决办法:

如果你知道是在哪配的,去改一下,和我一样是小白,全局搜索assetsPublicPath字段

比如我搜到的:assetsPublicPath: '/vueAdmin-template/',

改成:assetsPublicPath: '/',

重新npm run build

重复3,4,5步骤

打开http://localhost:8080/

你的项目就可以访问了

07a9d150623aebd0d1a604dfc2c1e1d2.png

二、Nginx服务器

和Tomcat一个原理,启动Nginx服务器,将dist文件丢到服务器里,运行访问

1、下载Nginx

2、解压Nginx

3、配置Nginx

nginx配置文件在nginx-1.15.0\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;

server {

listen 8099; #监听 8099 端口

server_name localhost; #本地

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

root C:/nginxRoot; #访问路径,相当于Tomcat的ROOT,这里自己配

index index.html index.htm; #访问index

}

#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;

# }

#}

}

我配置的是本地8099端口。

4、将dist文件丢到配置的路径,我这里是C:/nginxRoot,即丢到C盘下的nginxRoot文件夹内

5、启动服务器

在nginx.exe目录,打开命令行工具,用命令 启动/关闭/重启nginx

start nginx : 启动nginx

6、访问

http://localhost:8099/

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值