在服务器上部署一个Web项目,可以使用Apache,nginx等开源服务器。而本文将使用openresty部署一个vue3+vite项目。
01 在云服务器上安装openresty
openresty官网上有详细的安装教程openresty.org/cn/installa…
一些常见的Linux发行版本(如:ubuntu、debian等)有官方的预编译包,官方推荐优先使用这种方式进行安装 openresty.org/cn/linux-pa…
02 项目打包
vue项目打包命令:
npm run build
执行打包命令后会生成dist文件夹。
03 将dist文件夹上传至服务器
文件传输有多种不同的工具,如FTP,SCP和rsync。rsync即"远程同步"(remote sync),与其他文件传输工具(如 FTP 或 scp)不同,rsync 的最大特点是会检查发送方和接收方已有的文件,仅传输有变动的部分(默认规则是文件大小或修改时间有变动)。本文选用rsync进行文件上传。
chmod 600 ./etc/rsync.pass
如果是使用腾讯云,则还可以使用OrcaTerm登录Linux实例,使用OrcaTerm的SFTP文件管理工具来进行文件上传。
04 配置openresty
- 修改nginx.conf配置文件
sudo vim /usr/local/openresty/nginx/conf/nginx.conf
# /usr/local/openresty/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
location / {
root /home/ubuntu/dist;
index index.html;
}
}
- 重新加载配置文件
sudo /usr/local/openresty/nginx/sbin/nginx -t
sudo /usr/local/openresty/nginx/sbin/nginx -s reload
05 验证
在浏览器中输入服务器的服务器的公网IP地址或者域名,页面正常加载则说明项目部署成功。