top:環境MacBook
1、通過composer 安裝yii2 【yii2需要php的PDO和pdo_mysql擴展,需要確認已安裝】
a. 首先需要配置composer:
我使用的是阿里雲的鏡像:https://developer.aliyun.com/composer
具體配置參照阿里雲的文檔,具體使用全局composer命令百度 or Google
b. 安裝yii2:
yii2china.com上的文檔通過composer安裝yii2的命令是:composer create-project --prefer-dist yiisoft/yii2-app-basic basic
進入目錄:cd /data0/www/ 執行:composer create-project --prefer-dist yiisoft/yii2-app-basic basic
成功的話會在www目錄建立一個basic目錄,里面為代碼
安裝時可能遇到的問題有:
可能會遇到的問題1:
Failed to decode response: zlib_decode(): data error
Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info
這個問題其實我不確定是否解決了,因為我前一天遇到了,第二天百度下執行了 composer self-update之后,再composer就沒了。
可能會遇到的問題2:
Your requirements could not be resolved to an installable set of packages.
Problem 1
- The requested package bower-asset/jquery could not be found in any version, there may be a typo in the package name.
Problem 2
- The requested package bower-asset/inputmask could not be found in any version, there may be a typo in the package name.
Problem 3
- The requested package bower-asset/punycode could not be found in any version, there may be a typo in the package name.
Problem 4
- The requested package bower-asset/yii2-pjax could not be found in any version, there may be a typo in the package name.
Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details.
- It's a private package and you forgot to add a custom repository to find it
Read <https://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.
問題2解析:Yii依靠 Bower 和/或 NPM 軟件包來安裝 asset(CSS 和 JavaScript)庫。 它使用Composer來獲取這些庫,允許 PHP 和 CSS/JavaScript 包版本同時解析。
問題2是我第一次create-project的時候,執行了這個錯誤的命令:composer create-project --prefer-dist yiisoft/yii2 ,遇到的。
解決方式為:composer安裝composer bower-asset的幾個包
查看包版本:composer show -all fxp/composer-asset-plugin 全局安裝該包[這里我選了最新版]:composer global require "fxp/composer-asset-plugin:~1.4.6" 此時再安裝composer.json中的包就可以成功了: composer install
可能會遇到的問題3:
安裝的時候如果提示需要提供Token(hidden): 這里是需要你提供github的token
獲取token方式為:
登錄github->點擊頭像->Settings->左欄最下Developer settings->左欄Personal access tokens->Generate new token
然后創建一個tab,選項全勾選,生成token,之后拿這個token復制到Token(hidden): 那里即可繼續安裝。
之后即可安裝成功,框架入口文件為web/index.php。
2、配置啟動
a. 使用yii2提供的命令行啟動測試服務器:
yii2提供了yii2 serve 可以本地啟動服務作為測試使用
命令為:php72 yii serve --docroot='/data0/www/basic/web' --port=8888
瀏覽器訪問http://localhost:8888/ 即可看到恭喜頁面。
說一下--docroot這個參數項:該參數為指定web根目錄
起初按照文檔執行 php yii serve --port=8888時,報錯為:Document root "/data0/www/yii2/console/web" does not exist.
百度加google之后,報錯原因為:
直接執行 php yii serve,不指定docroot時,yii2無法知道你的項目根目錄要的是哪個目錄,是前台目錄,還是后台目錄?所以得手動指定下web目錄。
b. nginx配置項目:
根據官網的nginx配置文件走即可:
另外官網的nginx服務建議項還有:
使用該配置時,你還應該在 php.ini 文件中設置 cgi.fix_pathinfo=0 , 能避免掉很多不必要的 stat() 系統調用。
還要注意當運行一個 HTTPS 服務器時,需要添加 fastcgi_param HTTPS on; 一行, 這樣 Yii 才能正確地判斷連接是否安全。
server { charset utf-8; client_max_body_size 128M; listen 80; ## listen for ipv4 #listen [::]:80 default_server ipv6only=on; ## listen for ipv6 server_name mysite.test; root /path/to/basic/web; index index.php; access_log /path/to/basic/log/access.log; error_log /path/to/basic/log/error.log; location / { # Redirect everything that isn't a real file to index.php try_files $uri $uri/ /index.php$is_args$args; } # uncomment to avoid processing of calls to non-existing static files by Yii #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ { # try_files $uri =404; #} #error_page 404 /404.html; # deny accessing php files for the /assets directory location ~ ^/assets/.*\.php$ { deny all; } location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; #fastcgi_pass unix:/var/run/php5-fpm.sock; try_files $uri =404; } location ~* /\. { deny all; } }