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; } }