您的位置:首页 > 运维架构 > Nginx

将yii2高级模板的入口文件移到根目录 ,以及nginx对于Yii2的前后台的配置

2018-03-17 13:34 483 查看

一、将yii2高级模板的入口文件移到根目录

打开/var/www/html/yii2advanced/frontend/web 文件夹,将里面的index.php文件复制(或剪切)至项目根目录/var/www/html/yii2advanced 。然后编辑里面的内容为:
<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/common/config/bootstrap.php');
require(__DIR__ . '/frontend/config/bootstrap.php');

$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/common/config/main.php'),
require(__DIR__ . '/common/config/main-local.php'),
require(__DIR__ . '/frontend/config/main.php'),
require(__DIR__ . '/frontend/config/main-local.php')
);

$application = new yii\web\Application($config);
$application->run();
然后再修改/var/www/html/yii2advanced/frontend/config 里的main.php文件,在component里加入
'assetManger' => [
'basePath' => '@webroot/frontend/web/assets',
'baseUrl' => '@web/frontend/web/assets'
],
整个main.php应该为:
<?php$params = array_merge(require(__DIR__ . '/../../common/config/params.php'),require(__DIR__ . '/../../common/config/params-local.php'),require(__DIR__ . '/params.php'),require(__DIR__ . '/params-local.php'));return ['id' => 'app-frontend','basePath' => dirname(__DIR__),'bootstrap' => ['log'],'controllerNamespace' => 'frontend\controllers','components' => ['assetManger' => [
'basePath' => '@webroot/frontend/web/assets',
'baseUrl' => '@web/frontend/web/assets'
],'user' => ['identityClass' => 'common\models\User','enableAutoLogin' => true,],'log' => ['traceLevel' => YII_DEBUG ? 3 : 0,'targets' => [['class' => 'yii\log\FileTarget','levels' => ['error', 'warning'],],],],'errorHandler' => ['errorAction' => 'site/error',],],'params' => $params,];
最后修改X:/var/www/html/yii2advanced/frontend/assets 里的AppAsset.php文件,将里面的:
public $css = ['css/site.css',];
修改为:
public $css = ['frontend/web/css/site.css',];
后台修改方法是将/var/www/html/yii2advanced/backend/web 里的index.php文件夹复制(剪切)至根目录并重命名为admin.php,其他修改地方与上述类似,只是将frontend改为backend即可。

二、nginx对于Yii2的前后台的配置

将/etc/nginx/sites-available/default的内容修改为
server {
        listen 80 default_server;        listen [::]:80 default_server;
        # SSL configuration        #        # listen 443 ssl default_server;        # listen [::]:443 ssl default_server;        #        # Note: You should disable gzip for SSL traffic.        # See: https://bugs.debian.org/773332         #        # Read up on ssl_ciphers to ensure a secure configuration.        # See: https://bugs.debian.org/765782         #        # Self signed certs generated by the ssl-cert package        # Don't use them in a production server!        #        # include snippets/snakeoil.conf;        root /var/www/html/yii2advanced;        # Add index.php to the list if you are using PHP        index index.html index.php index.htm index.nginx-debian.html;        server_name _;        location / {                # First attempt to serve request as file, then                # as directory, then fall back to displaying a 404.                # try_files $uri $uri/ =404;                try_files $uri $uri/ /index.php?$args;        }        location /admin {                # First attempt to serve request as file, then                # as directory, then fall back to displaying a 404.                # try_files $uri $uri/ =404;                try_files $uri $uri/ /admin.php?$args;        }        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        location ~ \.php$ {                include snippets/fastcgi-php.conf;        #        #       # With php7.0-cgi alone:        #       fastcgi_pass 127.0.0.1:9000;        #       # With php7.0-fpm:                fastcgi_pass unix:/run/php/php7.0-fpm.sock;        }
        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #location ~ /\.ht {        #       deny all;        #}}
这样,你就可以用http://localhost访问前台,用http://localhost/admin.php访问后台了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  yii2 nginx Ubuntu16.04