vagrant的學習 之 Yii2
本文根據慕課網的視頻教程練習,感謝慕課網!
慕課視頻學習地址:https://www.imooc.com/video/14218。
慕課的參考文檔地址:https://github.com/apanly/mooc/tree/master/va
Yii2的中文官網地址:https://www.yiichina.com/
第一步,需要安裝composer:
curl -sS https://getcomposer.org/installer | php
返回失敗:
All settings correct for using Composer Downloading... Failed to decode zlib stream
再次執行該命令,返回成功:
All settings correct for using Composer Downloading... Composer (version 1.7.2) successfully installed to: /home/www/yii2/composer.phar Use it: php composer.phar
然后把composer修改為全局變量,執行:
mv composer.phar /usr/local/bin/composer
第二步,安裝Yii:
composer create-project --prefer-dist yiisoft/yii2-app-basic basic
報錯:
Cannot create cache directory /home/vagrant/.composer/cache/repo/https---repo.packagist.org/,
or directory is not writable. Proceeding without cache
給目錄增加權限:
sudo chmod -R 777 /home/vagrant/.composer/cache/
再次執行,還是報錯:
The "https://repo.packagist.org/packages.json" file could not be downloaded: SSL: crypto enabling timeout Failed to enable crypto failed to open stream: operation failed https://repo.packagist.org could not be fully loaded, package information was loaded from the local cache and may be out of date [Composer\Downloader\TransportException] Content-Length mismatch, received 19839 bytes out of the expected 1080215
超時了,搜索后嘗試修改composer的國內鏡像下載地址,實現加速試一試:
composer config -g repo.packagist composer https://packagist.phpcomposer.com
結果顯示無權限:
[ErrorException] touch(): Unable to create file /home/vagrant/.composer/config.json because Permission denied
給文件增加權限:
sudo chmod -R 777 /home/vagrant/.composer/
然后再次執行修改composer的下載地址:
composer config -g repo.packagist composer https://packagist.phpcomposer.com
這次沒有報錯,查看配置文件已經修改:
cat /home/vagrant/.composer/config.json
顯示:
{ "config": {}, "repositories": { "packagist": { "type": "composer", "url": "https://packagist.phpcomposer.com" } } }
然后再次嘗試下載yii框架:
composer create-project --prefer-dist yiisoft/yii2-app-basic basic
下載成功,發現目錄下多了個basic目錄
重命名項目名字:
mv basic yii2
第三步,配置nginx:
//進入nginx的配置目錄 cd /etc/nginx/conf.d/ //常見yii的配置文件 sudo touch yii2.conf //編輯配置文件 sudo vim yii2.conf //文件內容 server{ server_name study.yii2.com; root /home/www/yii2/web; index index.php index.html; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php${ include fastcgi_params; fastcgi_pass 127.0.0.1:9000; try_files $uri = 404; } }
修改host文件:
sudo vim /etc/hosts
增加:
IP地址 study.yii2.com
然后重啟nginx
sudo /etc/init.d/nginx restart
最后配置本地主機的host文件:
也增加:
IP地址 study.yii2.com
就可以在本地訪問 study.yii2.com 了。
第四步,配置apache:
進入apache的配置目錄:
cd /etc/apache2/sites-enabled
創建配置文件:
sudo touch yii2.conf
編輯配置文件:
sudo vim yii2.conf
<VirtualHost *:8888> ServerName study.yii2.com DocumentRoot /home/www/yii2/web/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
重啟apache:
sudo /etc/init.d/apache2 restart
ok,配置完成。
總結:
1、雖然看視頻或者文檔感覺簡單,很快就看完了,但是自己並沒有記住,還是需要多加練習才行;
2、實際操作起來更會有很多意外的錯誤發生,這時候就需要耐心,分析解決問題,有的錯誤可能很快找到解決方法,但是有些卻耗費時間也沒有搜到答案,只能另尋途徑或暫時跳過。
歡迎大家指點。