Setting single domain Yii2 advanced di webserver Apache

Cara setting single domain di yii2 sebenarnya beragam. Ada juga settingan rekomendasi dari dokumentasinya yiiframework langsung di : http://www.yiiframework.com/wiki/799/yii2-app-advanced-on-single-domain-apache-nginx/ .

Tapi cara disana tidak cocok setelah saya coba di lokal saya yg di folder htdocs saya terdapat banyak folder project, file di yii2 advanced harus diletakkan di root folder htdocs atau bisa menggunakan virtualhost terlebih dahulu. Sedangkan saya ingin yii2 advanced bisa diakses “locahost/yii2advanced” <- (Untuk /frontend), dan “localhost/yii2advanced/backend” <- (Untuk /backend).

Cara ini saya coba di webserver Apache, sedangkan untuk ngix saya belum explore. Saya tidak yakin cara berikut simple dan benar atau tidak, tapi setidaknya cara ini yang saya tau,hhe :v

  • Pertama Install Yii2 advanced di lokal anda dan buat folder baru di “htdocs/yii2advanced/{isi aplikasi}” atau www/yii2advanced/{isi aplikasi}
  • Setelah melakukan settingan dasar instalasi yii2 advanced, pastikan aplikasi anda bisa di jalankan di “localhost/yii2advanced/frontend/web/index” dan “localhost/yii2advanced/backed/web/index”
  • Buat file “.htaccess” dengan isi berikut ini dan letakkan di folder htdocs/yii2advanced/{.htaccess} , lokasinya sama dengan lokasi file composer.json
    # ----------------------------------------------------------------------
    # Adds some security for the Apache server configuration for use with
    # yii2-app-practical template.
    # @author Kartik Visweswaran <kartikv2@gmail.com>
    # @see http://demos.krajee.com/app-practical
    # ----------------------------------------------------------------------
    
    # "-Indexes" will have Apache block users from browsing folders without a default document
    # Usually you should leave this activated, because you shouldn't allow everybody to surf through
    # every folder on your server (which includes rather private places like CMS system folders).
    <IfModule mod_autoindex.c>
      Options -Indexes
    </IfModule>
    
    
    # Block access to "hidden" directories whose names begin with a period. This
    # includes directories used by version control systems such as Subversion or Git.
    <IfModule mod_rewrite.c>
      RewriteCond %{SCRIPT_FILENAME} -d
      RewriteCond %{SCRIPT_FILENAME} -f
      RewriteRule "(^|/)\." - [F]
    </IfModule>
    
    
    # Block access to backup and source files
    # This files may be left by some text/html editors and
    # pose a great security danger, when someone can access them
    <FilesMatch "(\.(bak|bat|config|sql|fla|md|psd|ini|log|sh|inc|swp|dist)|~|init|composer\.json|composer\.lock)$">
      Order allow,deny
      Deny from all
      Satisfy All
    </FilesMatch>
    
    # Increase cookie security
    <IfModule php5_module>
      php_value session.cookie_httponly true
    </IfModule>
    
    # Settings to hide index.php and ensure pretty urls
    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
  • Buat file “index.php” di tempat yang sama. “htdocs/yii2advanced/{index.php}”
    <?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();
  • Edit file “htdocs/yii2advanced/backend/config/main.php”. Tambahkan baris yang saya tandai merah
    <?php
    
    use \yii\web\Request;
    $baseUrl = str_replace('/backend/web', '', (new Request)->getBaseUrl());
    
    $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-backend',
     'basePath' => dirname(__DIR__),
     'controllerNamespace' => 'backend\controllers',
     'bootstrap' => ['log'],
     'modules' => [],
     'components' => [
     'request' => [
     'csrfParam' => '_csrf-backend',
     'baseUrl' => $baseUrl,
     ],
     'user' => [
     'identityClass' => 'common\models\User',
     'enableAutoLogin' => true,
     'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
     ],
     'session' => [
     // this is the name of the session cookie used for login on the backend
     'name' => 'advanced-backend',
     ],
     'log' => [
     'traceLevel' => YII_DEBUG ? 3 : 0,
     'targets' => [
     [
     'class' => 'yii\log\FileTarget',
     'levels' => ['error', 'warning'],
     ],
     ],
     ],
     'errorHandler' => [
     'errorAction' => 'site/error',
     ],
     
     'urlManager' => [
     'enablePrettyUrl' => true,
     'showScriptName' => false,
     'rules' => [
     ],
     ],
     
     ],
     'params' => $params,
    ];
  • Tambahkan file “index.php” di folder “htdocs/yii2advanced/backend/{index.php}”
    <?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__ . '/config/bootstrap.php');
    
    $config = yii\helpers\ArrayHelper::merge(
     require(__DIR__ . '/../common/config/main.php'),
     require(__DIR__ . '/../common/config/main-local.php'),
     require(__DIR__ . '/config/main.php'),
     require(__DIR__ . '/config/main-local.php')
    );
    
    $application = new yii\web\Application($config);
    $application->run();
  • Tambahkan juga file “.htaccess” di folder “htdocs/yii2advanced/backend/{.htaccess}”
    # ----------------------------------------------------------------------
    # Adds some security for the Apache server configuration for use with
    # yii2-app-practical template.
    # @author Kartik Visweswaran <kartikv2@gmail.com>
    # @see http://demos.krajee.com/app-practical
    # ----------------------------------------------------------------------
    
    # "-Indexes" will have Apache block users from browsing folders without a default document
    # Usually you should leave this activated, because you shouldn't allow everybody to surf through
    # every folder on your server (which includes rather private places like CMS system folders).
    <IfModule mod_autoindex.c>
     Options -Indexes
    </IfModule>
    
    
    # Block access to "hidden" directories whose names begin with a period. This
    # includes directories used by version control systems such as Subversion or Git.
    <IfModule mod_rewrite.c>
     RewriteCond %{SCRIPT_FILENAME} -d
     RewriteCond %{SCRIPT_FILENAME} -f
     RewriteRule "(^|/)\." - [F]
    </IfModule>
    
    
    # Block access to backup and source files
    # This files may be left by some text/html editors and
    # pose a great security danger, when someone can access them
    <FilesMatch "(\.(bak|bat|config|sql|fla|md|psd|ini|log|sh|inc|swp|dist)|~|init|composer\.json|composer\.lock)$">
     Order allow,deny
     Deny from all
     Satisfy All
    </FilesMatch>
    
    # Increase cookie security
    <IfModule php5_module>
     php_value session.cookie_httponly true
    </IfModule>
    
    # Settings to hide index.php and ensure pretty urls
    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
  • Edit file “htdocs/yii2advanced/backend/assets/AppAsset.php”, ganti pada baris yang saya tandai merah
    <?php
    
    namespace backend\assets;
    
    use yii\web\AssetBundle;
    
    /**
     * Main backend application asset bundle.
     */
    class AppAsset extends AssetBundle
    {
        public $basePath = '@webroot';
        public $baseUrl = '@web/web';
        public $css = [
            'css/site.css',
        ];
        public $js = [
        ];
        public $depends = [
            'yii\web\YiiAsset',
            'yii\bootstrap\BootstrapAsset',
        ];
    }

 

  • Edit file “htdocs/yii2advanced/frontend/config/main.php”. Tambahkan baris yang saya tandai merah
    <?php
    
    use \yii\web\Request;
    $baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());
    
    $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' => [
     'request' => [
     'csrfParam' => '_csrf-frontend',
     'baseUrl' => $baseUrl,
     ],
     'user' => [
     'identityClass' => 'common\models\User',
     'enableAutoLogin' => true,
     'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
     ],
     'session' => [
     // this is the name of the session cookie used for login on the frontend
     'name' => 'advanced-frontend',
     ],
     'log' => [
     'traceLevel' => YII_DEBUG ? 3 : 0,
     'targets' => [
     [
     'class' => 'yii\log\FileTarget',
     'levels' => ['error', 'warning'],
     ],
     ],
     ],
     'errorHandler' => [
     'errorAction' => 'site/error',
     ],
     
     'urlManager' => [
     'enablePrettyUrl' => true,
     'showScriptName' => false,
     'rules' => [
     'transaction/getrequestdetail/<id>/' => 'transaction/getrequestdetail',
     ],
     ],
     
     ],
     'params' => $params,
    ];
  • Edit file “htdocs/yii2advanced/frontend/assets/AppAsset.php”, ganti pada baris yang saya tandai merah
    <?php
    
    namespace frontend\assets;
    
    use yii\web\AssetBundle;
    
    /**
     * Main frontend application asset bundle.
     */
    class AppAsset extends AssetBundle
    {
        public $basePath = '@webroot';
        public $baseUrl = '@web/frontend/web/';
        public $css = [
            'css/site.css',
        ];
        public $js = [
        ];
        public $depends = [
            'yii\web\YiiAsset',
            'yii\bootstrap\BootstrapAsset',
        ];
    }
  • Terakhir buat folder “assets” di “htdocs/yii2advanced/assets
  • Selesai. Mudah-mudahan gk ada yang kelupaan

Leave a Reply

Your email address will not be published. Required fields are marked *