vue 项目部署到nginx 服务器

一 vue 项目打包

1 本地环境

npm run build

2 打包完成生成一个dist 文件夹,将其放到服务器指定的文件夹,此文件夹可以在nginx 配置文件中配置

二 nginx

1 根据对应的系统搜索安装命令

sudo yum install nginx

2 查看conf位置

如果不知道的话

nginx -t

复制代码
[admin@iZf8zfzio8ppow8wx1a15lZ ~]$ sudo nginx -t
nginx: the configuration file /www/server/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /www/server/nginx/conf/nginx.conf test is successful

举例一些代码

复制代码
# 启动nginx
nginx
# 启动时权限不够就sudo
sudo nginx
 
# 查看nginx占用端口
ps -ef|grep nginx
# 查看端口占用情况
lsof -i:8080
 
# 查看nginx安装目录、编译参数、配置文件、日志文件的位置等信息
nginx -V
# 查看conf位置
nginx -t
 
# 优雅停止
nginx -s quit
# 立即停止
nginx -s stop
 
# 重载配置文件
nginx -s reload
# 重新打开日志文件
nginx -s reopen

配置文件

在配置文件中,找到 server 块,修改为以下内容,然后进行保存

复制代码
server {
    listen 80;
    server_name your_domain.com;  # 替换为你的域名或服务器 IP

    root /var/www/html/dist;  # 指向 dist 文件夹
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;  # 支持 Vue 的路由模式
    }

    # 如果需要禁止访问某些文件
    location ~* \.(htaccess|htpasswd|git|env) {
        deny all;
    }

    # 如果需要配置静态文件缓存
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
        expires 1y;
        add_header Cache-Control "public, no-transform";
    }
}

启动nginx 之前,先进行测试

sudo nginx -t

如果发现绑定端口号有占用 ,使用如下命令查看占用

sudo netstat -tulnp | grep :80

然后杀死进程号

sudo kill -9 1234

然后进行重启服务器

sudo service nginx restart

最后在浏览器访问域名,即可成功

三 上传到文件到服务器

使用scp 命令

scp -r dist/ user@your_server_ip:/var/www/html/

dist 的文件夹

四 安装 SSL 证书

如果需要启用 HTTPS,可以使用 Let's Encrypt 免费 SSL 证书。

1. 安装 Certbot

sudo yum install certbot python3-certbot-nginx

2. 获取 SSL 证书

后面的路径是nginx 配置文件的路径

sudo certbot --nginx --nginx-server-root /www/server/nginx/conf certonly

记得最后验证一下,一定要输入https:// + 你的域名

相关推荐
阿方索10 小时前
虚拟化技术实践指南:KVM 与 VMware ESXi 部署全流程
linux·运维·服务器
野犬寒鸦10 小时前
从零起步学习MySQL || 第八章:索引深入理解及高级运用(结合常见优化问题讲解)
java·服务器·数据库·后端·mysql
Onlyᝰ11 小时前
前端tree树
javascript·vue.js·elementui
安卓开发者11 小时前
Docker与Nginx:现代Web部署的完美二重奏
前端·nginx·docker
葡萄城技术团队11 小时前
Vue 生态下前端 Excel 导入导出终极方案:SpreadJS 实战指南
vue.js
哟哟耶耶11 小时前
Starting again company 03
前端·javascript·vue.js
妹妹够啦12 小时前
宝塔部署-Nginx配置
运维·nginx·junit
Shelley种了一只橙子12 小时前
第一章部署 chrony服务器
运维·服务器
Full Stack Developme12 小时前
Linux 有哪些功能相似的命令
linux·运维·服务器
低音钢琴12 小时前
【从零开始构建性能测试体系-08】如何诊断性能瓶颈:从服务器到数据库的全方位分析
服务器·数据库·php