环境准备
1、安装Node.js
部署Vue项目到服务器的第一步是安装Node.js,Node.js是一个基于Chrome V8引擎的JavaScript运行时环境,可以让JavaScript在服务器端运行,访问Node.js官网(https://nodejs.org/)下载对应操作系统的安装包,按照提示进行安装即可。
2、安装npm
Node.js安装完成后,会自动安装npm(Node.js包管理器),如果没有安装,可以访问npm官网(https://www.npmjs.com/)下载对应操作系统的安装包,按照提示进行安装。
3、创建Vue项目
使用Vue CLI创建一个新的Vue项目,首先确保已经全局安装了Vue CLI,如果没有安装,可以通过以下命令进行安装:
npm install -g @vue/cli
然后使用以下命令创建一个新的Vue项目:
vue create my-project
my-project
是你的项目名称,可以根据需要自行修改,创建完成后,进入项目目录:
cd my-project
构建项目
1、安装打包工具
为了将Vue项目部署到服务器,需要将其打包成一个静态文件,这里我们使用Webpack作为打包工具,首先安装Webpack及其相关依赖:
npm install --save-dev webpack webpack-cli webpack-dev-server html-webpack-plugin css-loader style-loader file-loader babel-loader @vue/cli-plugin-babel @vue/cli-plugin-eslint @vue/cli-plugin-router@4 @vue/cli-plugin-unit-jest @vue/cli-service@next vue-template-compiler cross-env --save-dev extra-babel-plugin-styles
2、配置Webpack
在项目根目录下创建一个名为vue.config.js
的配置文件,并添加以下内容:
module.exports = { productionSourceMap: false, devServer: { open: true, host: 'localhost', port: 8080, // 可选,设置为0表示不监听端口,使用默认端口8080,也可以自定义端口号,如:port: 9090。 } }
3、打包项目
在项目根目录下运行以下命令进行打包:
npm run build
打包完成后,会在项目根目录下生成一个名为dist
的文件夹,里面包含了打包后的静态文件。
部署到服务器
1、将打包后的静态文件上传到服务器
可以使用FTP工具(如FileZilla)或者SSH工具(如PuTTY)将本地的dist
文件夹上传到服务器上,上传完成后,在服务器上找到该文件夹并解压,将文件夹解压到/var/www/html
目录下。
2、配置Nginx代理
为了让用户能够访问到我们的Vue应用,需要配置Nginx代理,确保服务器上已经安装了Nginx,然后编辑Nginx配置文件,通常位于/etc/nginx/nginx.conf
或/etc/nginx/sites-available/default
,在server
块中添加以下内容:
location /my-project/ { root /var/www/html; 这里填写你的项目解压后的路径,/var/www/html/my-project/dist; try_files $uri $uri/ @rewrites; 首先尝试直接访问静态资源,如果无法访问则尝试重写URL,这里的@rewrites是一个内部注释,需要取消注释并添加相应的重写规则。 }
接下来,取消Nginx配置文件中的第7行注释,并添加相应的重写规则:
location @rewrites { 这个注释是为了防止语法错误而加上的,实际上不需要保留,但为了保证代码的可读性,可以保留这个注释,后面的代码也是如此,请不要删除这些注释和空格。---------------------------------------------------------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------------------------------------------------------------------------------------------} try to fix the issue below and leave this comment around until it's resolved. rewrite ^/my-project/(.*)$ /index.html last; redirect everything else to index.html break; If the above rule doesn't work, try this if ($request_filename != '/') { rewrite ^/my-project(.*)$ $1 last; } try_files $uri $uri/ @rewrites; If the above rule still didn't work (e.g. you have custom routes), then we can try to do a catch-all for all other requests (not just the ones listed above). This will make sure that no other route is handled by this server block. Note that this rule will only work if the server block is the first one encountered by Nginx when trying to resolve a request. So make sure that there are no other server blocks on your configuration! If the above rules don't work either (i.e. you have other server blocks or custom routes), then we can try another approach: Use a proxy server to forward requests to your backend server (the one hosting your Vue app). This way, you don't need to configure Nginx again for your Vue app. For more information about proxy servers, see https://www.nginx.com/resources/wiki/start/topics/examples/full/howtosetupareverseproxyforthenginxserver location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { proxy_pass http://your_backend_server_ip:your_backend_server_port; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; add_header Access-Control-Expose-Headers 'Content-Length,Content-Range'; } If you want to use a reverse proxy with SSL support (HTTPS), you can use the following code snippet instead: location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { proxy_pass https://your_backend_server_ip:your_backend_server_port; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; add_header Access-Control-Expose-Headers 'Content-Length,Content-Range'; } If you want to use a reverse proxy with SSL support (HTTPS), you can use the following code snippet instead: location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { proxy_pass https://your_backend_server_ip:your_backend_server_port; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS'; add_header Access-Control-Allow-Headers 'DNT,User-Agent,X-Requested
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/264965.html