# 共享托管環境
共享的托管環境常常會對目錄結構以及配置文件有較多的限制。然而,在大多數情況下,你仍可以通過少量的修改以在共享托管環境下運行 Yii 2.0。
## 部署一個基礎應用模板
由于共享托管環境往往只有一個 webroot,如果可能,請優先使用基礎項目模板( basic project template )構建你的應用程序。參考?[安裝 Yii 章節](http://www.yiichina.com/doc/guide/2.0/start-installation)在本地安裝基礎項目模板。當你讓應用程序在本地正常運行后,我們將要做少量的修改以讓它可以在共享托管服務器運行。
### 重命名 webroot
用FTP或者其他的工具連接到你的托管服務器,你可能看到類似如下的目錄結構:
~~~
config
logs
www
~~~
在以上,`www`?是你的 web 服務器的 webroot 目錄。不同的托管環境下名稱可能各不相同,通常是類似:?`www`,?`htdocs`, 和`public_html`?之類的名稱。
對于我們的基礎項目模板而言,其 webroot 名為?`web`?。 在你上傳你的應用程序到 web 服務器上去之前,將你的本地 webroot 重命名以匹配服務器。 即: 從?`web`?改為?`www`,?`public_html`?或者其他你的托管環境的 webroot 名稱。
### FTP 根目錄可寫
如果你有 FTP 根目錄的寫權限,即,有?`config`,?`logs`?和?`www`?的根目錄,那么,如本地根目錄相同的結構上傳?`assets`,`commands`?等目錄。
### 增加 web 服務器的額外配置
如果你的 web 服務器是 Apache,你需要增加一個包含如下內容的?`.htaccess`?文件到你的?`web`?目錄(或者?`public_html`?根據實際情況而定,是你的?`index.php`?文件所在的目錄)。
~~~
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
~~~
對于nginx而言,你不需要額外的配置文件。
### 檢查環境要求
為了運行 Yii ,你的 web 服務器必須匹配它的環境要求。最低的要求必須是 PHP 5.4。為了檢查環境配置,將?`requirements.php`?從你的根目錄拷貝到 webroot 目錄,并通過瀏覽器輸入 URL?`http://example.com/requirements.php`?運行它。最后,檢查結束后別忘了刪除這個文件哦!
## 部署一個高級應用程序模板
部署一個高級應用程序到共享的托管環境比基礎應用要麻煩的原因在于它包含有兩個 webroot 目錄,而共享的托管環境不支持兩個。對于這種情況,我們需要調整目錄結構。
### 將入口文件移動到同一個 webroot
首先我們需要一個 webroot 目錄,如[重命名 webroot](http://www.yiichina.com/doc/guide/2.0/tutorial-shared-hosting#renaming-webroot)一節所述,創建一個新的跟你的托管環境 webroot 同名的目錄,如類似?`www`?或者`public_html`?的名字。創建如下的目錄結構,其中?`www`?目錄指代你剛剛創建的 webroot 目錄。
~~~
www
admin
backend
common
console
environments
frontend
...
~~~
`www`?目錄是我們的前臺目錄,所以將?`frontend/web`?里面的內容移到這個目錄。 將?`backend/web`?里面的內容移到?`www/admin`?目錄。對于每種情況下,你需要調整`index.php`?和?`index-test.php` 里面引用的目錄結構。
### 分離 Session 和 Cookie
通常情況下,backend 和 frontend 運行在不同的域下,當我們將其都移到同一個域時, frontend 和 backend 將會共享相同的 cookie,這樣會造成沖突。為了修復這個問題,如下調整 backend 的應用程序配置文件?`backend/config/main.php`:
~~~
'components' => [
'request' => [
'csrfParam' => '_backendCSRF',
'csrfCookie' => [
'httpOnly' => true,
'path' => '/admin',
],
],
'user' => [
'identityCookie' => [
'name' => '_backendIdentity',
'path' => '/admin',
'httpOnly' => true,
],
],
'session' => [
'name' => 'BACKENDSESSID',
'cookieParams' => [
'path' => '/admin',
],
],
],
~~~
- 介紹(Introduction)
- 關于 Yii(About Yii)
- 從 Yii 1.1 升級(Upgrading from Version 1.1)
- 入門(Getting Started)
- 安裝 Yii(Installing Yii)
- 運行應用(Running Applications)
- 第一次問候(Saying Hello)
- 使用 Forms(Working with Forms)
- 玩轉 Databases(Working with Databases)
- 用 Gii 生成代碼(Generating Code with Gii)
- 更上一層樓(Looking Ahead)
- 應用結構(Application Structure)
- 結構概述(Overview)
- 入口腳本(Entry Scripts)
- 應用(Applications)
- 應用組件(Application Components)
- 控制器(Controllers)
- 模型(Models)
- 視圖(Views)
- 模塊(Modules)
- 過濾器(Filters)
- 小部件(Widgets)
- 前端資源(Assets)
- 擴展(Extensions)
- 請求處理(Handling Requests)
- 運行概述(Overview)
- 引導(Bootstrapping)
- 路由引導與創建 URL(Routing and URL Creation)
- 請求(Requests)
- 響應(Responses)
- Sessions and Cookies
- 錯誤處理(Handling Errors)
- 日志(Logging)
- 關鍵概念(Key Concepts)
- 組件(Components)
- 屬性(Properties)
- 事件(Events)
- 行為(Behaviors)
- 配置(Configurations)
- 別名(Aliases)
- 類自動加載(Class Autoloading)
- 服務定位器(Service Locator)
- 依賴注入容器(Dependency Injection Container)
- 配合數據庫工作(Working with Databases)
- 數據庫訪問(Data Access Objects): 數據庫連接、基本查詢、事務和模式操作
- 查詢生成器(Query Builder): 使用簡單抽象層查詢數據庫
- 活動記錄(Active Record): 活動記錄對象關系映射(ORM),檢索和操作記錄、定義關聯關系
- 數據庫遷移(Migrations): 在團體開發中對你的數據庫使用版本控制
- Sphinx
- Redis
- MongoDB
- ElasticSearch
- 接收用戶數據(Getting Data from Users)
- 創建表單(Creating Forms)
- 輸入驗證(Validating Input)
- 文件上傳(Uploading Files)
- 收集列表輸入(Collecting Tabular Input)
- 多模型同時輸入(Getting Data for Multiple Models)
- 顯示數據(Displaying Data)
- 格式化輸出數據(Data Formatting)
- 分頁(Pagination)
- 排序(Sorting)
- 數據提供器(Data Providers)
- 數據小部件(Data Widgets)
- 操作客戶端腳本(Working with Client Scripts)
- 主題(Theming)
- 安全(Security)
- 認證(Authentication)
- 授權(Authorization)
- 處理密碼(Working with Passwords)
- 客戶端認證(Auth Clients)
- 安全領域的最佳實踐(Best Practices)
- 緩存(Caching)
- 概述(Overview)
- 數據緩存(Data Caching)
- 片段緩存(Fragment Caching)
- 分頁緩存(Page Caching)
- HTTP 緩存(HTTP Caching)
- RESTful Web 服務
- 快速入門(Quick Start)
- 資源(Resources)
- 控制器(Controllers)
- 路由(Routing)
- 格式化響應(Response Formatting)
- 授權驗證(Authentication)
- 速率限制(Rate Limiting)
- 版本化(Versioning)
- 錯誤處理(Error Handling)
- 開發工具(Development Tools)
- 調試工具欄和調試器(Debug Toolbar and Debugger)
- 使用 Gii 生成代碼(Generating Code using Gii)
- TBD 生成 API 文檔(Generating API Documentation)
- 測試(Testing)
- 概述(Overview)
- 搭建測試環境(Testing environment setup)
- 單元測試(Unit Tests)
- 功能測試(Functional Tests)
- 驗收測試(Acceptance Tests)
- 測試夾具(Fixtures)
- 高級專題(Special Topics)
- 高級應用模版(Advanced Project Template)
- 從頭構建自定義模版(Building Application from Scratch)
- 控制臺命令(Console Commands)
- 核心驗證器(Core Validators)
- 國際化(Internationalization)
- 收發郵件(Mailing)
- 性能優化(Performance Tuning)
- 共享主機環境(Shared Hosting Environment)
- 模板引擎(Template Engines)
- 集成第三方代碼(Working with Third-Party Code)
- 小部件(Widgets)
- Bootstrap 小部件(Bootstrap Widgets)
- jQuery UI 小部件(jQuery UI Widgets)
- 助手類(Helpers)
- 助手一覽(Overview)
- Array 助手(ArrayHelper)
- Html 助手(Html)
- Url 助手(Url)