前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >lnmp真实部署yii2高级版

lnmp真实部署yii2高级版

作者头像
botkenni
发布2019-09-02 16:59:27
1.5K0
发布2019-09-02 16:59:27
举报
文章被收录于专栏:IT码农IT码农

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、添加YII配置
  • 二、添加.htaccess文件
  • 三、配置 rules
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档