Linux使用Composer安装yii2的方法及bug处理

这里只讲linux下安装Yii2的方法windows安装yii2 的方法 请参见我的另一篇博文 http://blog.csdn.net/zqtsx/article/details/39344913

第一步 安装Composer:

语法:“curl -sS https://getcomposer.org/installer | php中bin目录里的php可执行文件路径”
我这里的php可执行文件所在路径是/usr/local/php/bin/php
所以实际安装命令如下:
curl -sS https://getcomposer.org/installer | /usr/local/php/bin/php
为方便日后使用将其移动到本地系统可执行文件目录 并重新命名为 composer(这样以后执行命令时就不用写composer.phar那么长的名字,直接写composer就可以了)
mv composer.phar /usr/local/bin/composer

第二步 安装Composer Asset Plugin:

在命令行执行如下命令注意语法中实际php文件地址和实际composer地址修改成你自己的安装地址:

(语法:php/bin/php文件所在路径 + composer文件安装路径 + global require “fxp/composer-asset-plugin:1.0.0-beta3” 注意空格和标点不要是中文的 )
/usr/local/php/bin/php /usr/local/bin/composer global require “fxp/composer-asset-plugin:1.0.0-beta3”
出现类似如下提示信息说明一切安装正常(摘取天上星linux下安装yii2):
“Changed current directory to /root/.composer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
– Installing fxp/composer-asset-plugin (v1.0.0-beta3)
Downloading: 100%

Writing lock file
Generating autoload files”

第三步 安装YII2

命令行下切换到一个php可以访问的站点目录

然后执行命令(注意php可执行文件路径和 composer路径修改成你自己的安装路径):
如下命令将Yii2.0.0版本安装在名为 yii2 的目录中,具体目录名可以根据自己的需要修改:
/usr/local/php/bin/php /usr/local/bin/composer create-project yiisoft/yii2-app-basic yii2 2.0.0
需要耐心等待,安装过程中可能会出现如下信息要求你输入用户名 和密码(自己到YII官网”https://github.com/”注册一个账户,把账户密码填进去就OK了):
如果遇到如下认证提示信息请点击下面https://github.com/settings/tokens/new?scopes=repo&description=Composer+on+localhost.localdomain+2015-05-14+1600语句登录Git网站,然后生成一个类似 的认证秘钥复制粘贴到Token(hidden)处回车即可:
Could not fetch https://api.github.com/repos/RobinHerbots/jquery.inputmask/contents/bower.json?ref=f44d5deec804c72fe85da31819b18bdcdad26f65, please create a GitHub OAuth token to go over the API rate limit
Head to https://github.com/settings/tokens/new?scopes=repo&description=Composer+on+localhost.localdomain+2015-05-14+1600
to retrieve a token. It will be stored in “/root/.composer/auth.json” for future use by Composer.
Token (hidden):输入在官网生成的秘钥回车即可
Installing yiisoft/yii2-app-basic (2.0.0)
– Installing yiisoft/yii2-app-basic (2.0.0)
Downloading: 100%

Created project in yii2
Loading composer repositories with package information
Installing dependencies (including require-dev)
Reading bower.json of bower-asset/jQuery.inputmask (3.1.29)
Could not fetch https://api.github.com/repos/RobinHerbots/jquery.inputmask/contents/bower.json?ref=faf19625822fbd54765d26baac3154cd132769d1, enter your GitHub credentials to go over the API rate limit
The credentials will be swapped for an OAuth token stored in /root/.composer/auth.json, your password will not be stored
To revoke access to this token you can visit https://github.com/settings/applications
Username:myyiiname (这里填写你在官网注册的账户名)
Password:myyiipwd (密码,你懂得)
输入账户密码后会出现如下认证成功的提示(如果没有就证明你的账户密码错了,或者你刚在YII官网注册的新账户没有通过邮件激活):
Token successfully created

认证通过的提示信息下面紧跟着一句如下的进度提示信息,说明正在安装过程中,需要你耐心等待,后面的小数点会随着安装进度有微妙的变化,
Reading bower.json of bower-asset/typeahead.js (v0.10.0)

然后紧接着缓缓有序的出现一些列如下的进度条,还需要耐心等待(摘取天上星linux下安装yii2):

– Installing yiisoft/yii2-composer (2.0.0)
Downloading: 100%

– Installing swiftmailer/swiftmailer (v5.3.0)
Downloading: 100%

– Installing bower-asset/jquery (2.1.1)
Downloading: 100%

– Installing bower-asset/yii2-pjax (v2.0.1)
Downloading: 100%

– Installing bower-asset/punycode (v1.3.2)
Downloading: 100%

– Installing bower-asset/jquery.inputmask (3.1.41)
Downloading: 100%

– Installing cebe/markdown (1.0.1)
Downloading: 100%

– Installing ezyang/htmlpurifier (v4.6.0)
Downloading: 100%

– Installing yiisoft/yii2 (2.0.0)
Downloading: 100%

– Installing yiisoft/yii2-swiftmailer (2.0.0)
Downloading: 100%

– Installing yiisoft/yii2-codeception (2.0.0)
Downloading: 100%

– Installing bower-asset/bootstrap (v3.2.0)
Downloading: 65%

直到出现类似如下完整信息,说明YII2下载安装结束
Installing yiisoft/yii2-app-basic (2.0.0)
– Installing yiisoft/yii2-app-basic (2.0.0)
Loading from cache

Created project in yii2
Loading composer repositories with package information
Installing dependencies (including require-dev)
– Installing yiisoft/yii2-composer (2.0.0)
Loading from cache

– Installing swiftmailer/swiftmailer (v5.3.0)
Loading from cache

– Installing bower-asset/jquery (2.1.1)
Loading from cache

– Installing bower-asset/yii2-pjax (v2.0.1)
Loading from cache

– Installing bower-asset/punycode (v1.3.2)
Loading from cache

– Installing bower-asset/jquery.inputmask (3.1.41)
Loading from cache

– Installing cebe/markdown (1.0.1)
Loading from cache

– Installing ezyang/htmlpurifier (v4.6.0)
Loading from cache

– Installing yiisoft/yii2 (2.0.0)
Loading from cache

– Installing yiisoft/yii2-swiftmailer (2.0.0)
Loading from cache

– Installing yiisoft/yii2-codeception (2.0.0)
Loading from cache

– Installing bower-asset/bootstrap (v3.2.0)
Downloading: 100%

– Installing yiisoft/yii2-bootstrap (2.0.0)
Downloading: 100%

– Installing yiisoft/yii2-debug (2.0.0)
Downloading: 100%

– Installing bower-asset/typeahead.js (v0.10.5)
Downloading: 100%

– Installing phpspec/php-diff (v1.0.2)
Downloading: 100%

– Installing yiisoft/yii2-gii (2.0.0)
Downloading: 100%

– Installing fzaninotto/faker (v1.4.0)
Downloading: 100%

– Installing yiisoft/yii2-faker (2.0.0)
Downloading: 100%

Writing lock file
Generating autoload files
chmod(‘runtime’, 0777)…done.
chmod(‘web/assets’, 0777)…done.
chmod(‘yii’, 0755)…done.

到这里整个YII2就安装结束了

第四步 测试yii2是否安装成功

首先修改nginx 或apache 配置信息,将yii2目录添加为虚拟站点目录

我这里是nginx配置的,直接在nginx 里添加一个yii2的站点 目录就成了
在nginx里添加类似如下站点设置即可,我们这里的yii2安装目录是 /var/www/yii2 所以 设置站点目录为/var/www/yii2
因为之前127.0.0.1 和127.0.0.2 已经有两个项目在了,所以这里就设置yii2站点本地 ip为 127.0.0.3
我的具体配置如下:

server {
listen       80;
listen       127.0.0.3;
server_name  somename  alias  another.alias;

location / {
root   /var/www/yii2/;
index  index.php index.html index.htm;
}

location ~ \.php$ {
#root   html;
root           /var/www/yii2/;
#an quan guo lv
try_files $uri =404;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
#include        fastcgi_params;
include       fastcgi.conf;
}

location ~* \.(?:gif|ico|jpe?g|png|swf|bmp|mp3)$ {

valid_referers none blocked 127.0.0.3 localhost baidu.com *.baidu.com google.com.hk *.google.com.hk *.google.com;
if ( $invalid_referer ) {
return 404;
}
#
#expires 30d;
log_not_found off;
## No need to bleed constant updates. Send the all shebang in one
#
#    fell swoop.
#
tcp_nodelay off;
#
#    Set the OS file cache.
#
open_file_cache max=1000 inactive=120s;
open_file_cache_valid 125s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
root /var/www/yii2/;
#
}

location ~* \.(js|css|mid)$ {
expires   1d;
root /var/www/yii2/;
}

}

然后在浏览器中输入http://127.0.0.3/web/ 即可看到 yii2站点的主页信息
至此,说明yii2.0.0版本安装、测试一切OK,可以开始您的开发之旅了……

第五步 yii2出现Calling unknown method: yii\web\UrlManager::addRules()错误的解决方案:

安装后访问yii时如果遇到类似Calling unknown method: yii\web\UrlManager::addRules() 的错误,请参见摘取天上星的另一篇博文:

http://blog.csdn.net/zqtsx/article/details/39345015

通过 Composer 安装 Yii 2.0

一、在 Windows 中,你需要下载并运行 Composer-Setup.exe

二、安装composer后,更新镜像为国内镜像

C:\Users\Administrator>d:

D:\>cd wamp

D:\wamp>cd www

D:\wamp\www>composer config -g repo.packagist composer https://packagist.phpcomp
oser.com(更新镜像为国内)
You are running composer with xdebug enabled. This has a major impact on runtime
performance. See https://getcomposer.org/xdebug(提示php.ini中xdebug需关闭)

三、更新composer为最新版本

D:\wamp\www>composer self-update(更新为最新)
You are already using composer version 1.1.0 (stable channel).

四、开始安装yii

D:\wamp\www>composer global require “fxp/composer-asset-plugin:~1.1.0”
Changed current directory to C:/Users/Administrator/AppData/Roaming/Composer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
– Installing fxp/composer-asset-plugin (v1.1.4)
Downloading: 100%

Writing lock file
Generating autoload files

五、创建项目

D:\wamp\www>composer create-project –prefer-dist yiisoft/yii2-app-basic basic
Installing yiisoft/yii2-app-basic (2.0.8)
– Installing yiisoft/yii2-app-basic (2.0.8)
Downloading: 100%

Created project in basic
Loading composer repositories with package information
Updating dependencies (including require-dev)
Reading bower.json of bower-asset/jquery (2.1.0)
Could not fetch https://api.github.com/repos/jquery/jquery-dist/contents/bower.j
son?ref=9434e03193c45d51bbd063a0edd1a07a6178d33f, please create a GitHub OAuth t
oken to go over the API rate limit
Head to https://github.com/settings/tokens/new?scopes=repo&description=Composer+
on+7RBSGPCTR5ZQFHB+2016-05-13+0824
to retrieve a token. It will be stored in “C:/Users/Administrator/AppData/Roamin
g/Composer/auth.json” for future use by Composer.
Token (hidden):
Token stored successfully.
– Installing yiisoft/yii2-composer (2.0.4)
Downloading: 100%

– Installing swiftmailer/swiftmailer (v5.4.2)
Downloading: 100%

– Installing bower-asset/jquery (2.2.3)
Downloading: 100%

– Installing bower-asset/yii2-pjax (v2.0.6)
Downloading: 100%

– Installing bower-asset/punycode (v1.3.2)
Downloading: 100%

– Installing bower-asset/jquery.inputmask (3.2.7)
Downloading: 100%

– Installing cebe/markdown (1.1.0)
Downloading: 100%

– Installing ezyang/htmlpurifier (v4.7.0)
Downloading: 100%

– Installing yiisoft/yii2 (2.0.8)
Downloading: 100%

– Installing yiisoft/yii2-swiftmailer (2.0.5)
Downloading: 100%

– Installing yiisoft/yii2-codeception (2.0.5)
Downloading: 100%

– Installing bower-asset/bootstrap (v3.3.5)
Downloading: 100%

– Installing yiisoft/yii2-bootstrap (2.0.6)
Downloading: 100%

– Installing yiisoft/yii2-debug (2.0.6)
Downloading: 100%

– Installing bower-asset/typeahead.js (v0.11.1)
Downloading: 100%

– Installing phpspec/php-diff (v1.1.0)
Downloading: 100%

– Installing yiisoft/yii2-gii (2.0.5)
Downloading: 100%

– Installing fzaninotto/faker (v1.6.0)
Downloading: 100%

– Installing yiisoft/yii2-faker (2.0.3)
Downloading: 100%

Writing lock file
Generating autoload files
> yii\composer\Installer::postCreateProject
chmod(‘runtime’, 0777)…done.
chmod(‘web/assets’, 0777)…done.
chmod(‘yii’, 0755)…done.

D:\wamp\www>