vagrant的学习 之 Yii2
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、实际操作起来更会有很多意外的错误发生,这时候就需要耐心,分析解决问题,有的错误可能很快找到解决方法,但是有些却耗费时间也没有搜到答案,只能另寻途径或暂时跳过。
欢迎大家指点。