lnmp真实部署yii2高级版

1.首先把代码上传到linux配置的目录中(如:/www/你的站点目录(如: /www/wwwroot/my.com/))

2.cd /www/你的站点目录( 如:/www/wwwroot/my.com/) php ./init 选择生产环境(0:开发环境,1:生产环境) (进行项目初始化,会给项目相应的权限否则无法访问)

3.比如我的项目在 /www/wwwroot/my.com 配置前台项目(如: /www/wwwroot/my.com/frontend/web)

4.我使用的是centos7.0+nginx1.10+mysql5.7+php5.6,用的是宝塔面板,这个时候需要我开始php.ini中的open_basedir= /www/wwwroot/my.com/ (一定要对应到yii2项目,并且后面加/,否则只能访问前台,而不能你访问后台)

否则会报错:Warning: require(): open_basedir restriction in effect. File(/www/wwwroot/my.com/vendor/autoload.php) is not within the allowed path(s): (/www/wwwroot/my.com/frontend/web/:/tmp/:/proc/) in /www/wwwroot/my.com/frontend/web/index.php on line 5


Warning: require(/www/wwwroot/my.com/vendor/autoload.php): failed to open stream: Operation not permitted in /www/wwwroot/my.com/frontend/web/index.php on line 5

Fatal error: require(): Failed opening required '/www/wwwroot/my.com/frontend/web/../../vendor/autoload.php' (include_path='.:/www/server/php/56/lib/php') in /www/wwwroot/my.com/frontend/web/index.php on line 5

5.同时需要配置nginx文件,否则yii2部署上线只能访问首页,其他都404

  • location / {  
  •     try_files $uri $uri/ /index.php?$args;  

6.如果想要开启伪静态(路由配置)

<?php

return [

    'components' => [

        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

            'rules' => [

            ],

        ]

    ],

];

'showScriptName' => false, // 禁用 index.php

'enablePrettyUrl' => true, // 启用 URL美化

'suffix' =>; '.html', // 在这里我们不配置,如果启用后缀,那么你的每个请求都会默认有.html的后缀

下面是来自网络的配置:

一、添加YII配置

配置文件:config/main.php

PHP

1

2

3

4

5

6

7

8

9

10

11

<?php

return [

    'components' => [

        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

            'rules' => [

            ],

        ]

    ],

];

相应的配置介绍

1

2

3

'showScriptName' =&gt; false, // 禁用 index.php

'enablePrettyUrl' =&gt; true, // 启用 URL美化

'suffix' =&gt; '.html', // 在这里我们不配置,如果启用后缀,那么你的每个请求都会默认有.html的后缀

二、添加.htaccess文件

1

2

3

4

5

6

7

8

Options +FollowSymLinks

IndexIgnore */*

RewriteEngine on

# if a directory or a file exists, use it directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php

RewriteRule . index.php

这样一来,你就可以隐藏路径中的index.php了

三、配置 rules

关键部分来了,下面要配置相应的解析规则了

转换前 URL:http://www.xxx.com/product/view?pid=10
转换后 URL:http://www.xxx.com/product/view/10

那么就需要下面的配置

1

2

3

'rules' => [

      '<controller:\w+>/<action:\w+>/<pid:\d+>'=>'<controller>/<action>'

]

1

2

3

<controller:\w+> //这是指匹配控制器

<action:\w+> //这是指匹配控制器内的方法

<pid:\d+> //这里指获取相应的请求参数的key \Yii::$app-&gt;request->get('pid');

如果希望添加后缀.html

1

2

3

'rules' => [

       '<controller:\w+>/<action:view>/<pid:\d+>.html'=>'<controller>/<action>',

  ]

注意:
1.这样的配置会匹配所有的控制器
2.参数中的\d+代表匹配数字 如果你的想是字符串 那么请修改成\w+

如果想修改成匹配固定的控制器或者方法,请参考下面配置

1

2

3

'rules' => [

       '<controller:product>/<action:view>/<pid:\d+>.html'=>'<controller>/<action>',

  ]

 
 
G
M
T
 
 
Detect languageAfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanLaoLatinLatvianLithuanianMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPersianPolishPortuguesePunjabiRomanianRussianSerbianSesothoSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshYiddishYorubaZulu
 
AfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanLaoLatinLatvianLithuanianMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPersianPolishPortuguesePunjabiRomanianRussianSerbianSesothoSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshYiddishYorubaZulu
 
 
 
 
 
 
 
 
 
Text-to-speech function is limited to 200 characters
 
 
Options : History : Feedback : Donate Close

猜你喜欢

转载自my.oschina.net/botkenni/blog/1823367