vue項目如何部署在伺服器
Ⅰ vue項目如何部署到伺服器
第一步配置 vue.config.js
第二步修改路由,改為 hash模式
第三步文件打包,執行以下,目錄中會出現一個dist文件夾,將文件拖到伺服器的 root 文件夾中
第四步可以通過域名進行訪問 http://www.linlin.run/my-project/index.html#/home
Ⅱ vue項目部署iis伺服器
一、將vue項目進行打包編譯後,根目錄生成dist的文件
當出現如上圖顯示時,說明打包編譯完成,已經生成dist文件
二、打開iis伺服器
打開iis伺服器後選中【網站】後右擊選【添加網站】,就可對網站進行配置,物理路徑選擇編譯後的dist文件夾後選確定就行。
三、右擊選擇【啟動】後出現如下報錯信息:
除非Windows Activation Service (WAS)和萬維網發布服務(W3SVC)均處於運行狀態,否則無法啟動網站
解決:
打開命令提示符中輸入services.msc ,打開服務。
找到windows install及word wide web發布服務選項,分別右擊啟動,並右鍵屬性改為啟動類型就可以。
這是重新啟動下,就可以訪問。
Ⅲ 前端vue與後端Thinkphp在伺服器的部署
vue在服務端部署時,我們都知道通過npm run build 指令打包好的dist文件,通過http指定是可以直接瀏覽的,Thinkphp通過域名指向index.php文件才可以瀏覽。要使前端正常調用後端數據,有兩種方法:1、前端跨域調用後端數據,2、前端打包文件部署在後端的伺服器文件夾下(同域)。
web伺服器: apache
一、跨域
在伺服器配置站點:
在路徑/home/www/ 下創建test項目文件夾,用來放項目文件。
找到httpd-vhosts.conf文件配置站點
前端站點:
ServerName test.test.com
DocumentRoot "/home/www/test/dist"
DirectoryIndex index.html
後端站點:
ServerName test.testphp.com
DocumentRoot "/home/www/test/php"
DirectoryIndex index.php
將前端打包好的dist文件放在/home/www/test/ 文件夾下,運行http://test.test.com可瀏覽,當路徑改變時,刷新會出現404錯誤。此時dist文件下創建一個.htaccess文件,當路徑不存在時,路徑指向http://test.test.com/index.html能解決此問題。
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
在/home/www/test文件夾下創建項目根目錄php文件夾,將thinkphp文件放在php下。TP5的入口文件在public文件下,在這將public下的入口文件index.php挪到php文件夾下(個人習慣將入口文件放在項目根目錄), 後端綁定Index模塊。
前端調用後端介面,存在跨域,跨域解決方法有好幾種,在這我將在後端php做配置,解決跨域問題,在公用控制器設置跨域配置:
class Common extends Controller
{
public $param;
// 設置跨域訪問
public function _initialize()
{
parent::_initialize();
isset($_SERVER['HTTP_ORIGIN']) ? header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']) : '';
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, authKey, sessionId");
$param = Request::instance()->param();
$this->param = $param;
}
}
前端調用登錄介面: this.axios.post('http://test.testphp.com/index.php/base/login', {user: '', password: ''})。
(可在webpack.base.conf.js文件下可定義介面:http://test.testphp.com/index.php/)
二、同域
後端配置同上,公共配置器中的header配置注釋。將前端的dist文件下的所有文件(包含.htaccess),放在php文件夾下。將後端index控制器的index方法的路徑重定向php下的index.html文件:
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
public function index() {
$this->redirect('/index.html');
}
}
前端調用登錄介面: this.axios.post('/index.php/base/login', {user: '', password: ''})
轉自:https://blog.csdn.net/qq_35465132/article/details/78986675