A
A
AlexSer2020-11-03 10:58:42
Yii
AlexSer, 2020-11-03 10:58:42

How to set up yii2 advanced on ubuntu?

On request in URl

server/mysite/frontend
everything starts up fine.
But if I want to go to the admin panel
server/my_site/admin
it doesn't flip.
With the same .htaccess file at the root, OSpanel works fine, but on the Ubuntu server it won't let me into /admin.
I changed the site config in /etc/apache2 and added a separate DocumentRoot /home/admini/lism.ru/backend/web
, but to no avail.
Here is the .htaccess at the root
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

# Если запрос начинается с /admin, то заменяем на /backend/web/
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin\/?(.*) /backend/web/$1

# Добавляем другой запрос /frontend/web/$1
RewriteCond %{REQUEST_URI} !^/(frontend/web|backend/web|admin)
RewriteRule (.*) /frontend/web/$1

# Если frontend запрос
RewriteCond %{REQUEST_URI} ^/frontend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /frontend/web/index.php

# Если backend запрос
RewriteCond %{REQUEST_URI} ^/backend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /backend/web/index.php


.htaccess what's in backend/web
# use mode rewrite for pretty URL support
RewriteEngine on
# if a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward the request to index.php
RewriteRule . index.php


Yii2 backend config:
'homeUrl' => '/admin',
    'id' => 'app-backend',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'backend\controllers',
    'bootstrap' => ['log'],
    'modules' => [],
    'components' => [
        'request' => [
            'baseUrl' => '/admin',
            'csrfParam' => '_csrf-backend',
        ],

What else could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-11-04
@AlexSer

Good evening.
RootDirectory set to the root of the site, not backend / web and not frontend / web
At the root of the site .htaccess

RewriteEngine On

RewriteRule ^(frontend|backend)/web/ - [L]

RewriteCond %{REQUEST_URI} ^/(admin)
RewriteRule ^admin(\/?.*)$ backend/web/$1 [L]
 
RewriteCond %{REQUEST_URI} ^/
RewriteRule ^(\/?.*)$ frontend/web/$1 [L]

In backend/web .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

In backend/config/main.php, in the request section By going
'baseUrl' => '/admin'
to the address http://site.comyou will get to the frontend, and to the address http://site.com/admin- to the admin panel.
Now, if you want to move the site to a subdirectory, for example subsite, the address will change to http://site.com/subsite.
Make changes to .htaccess, which is at the root of the site.
RewriteEngine On

RewriteRule ^(frontend|backend)/web/ - [L]

RewriteCond %{REQUEST_URI} ^/(subsite/admin)
RewriteRule ^admin(/.*)?$ backend/web/$1 [L]

RewriteCond %{REQUEST_URI} ^/(subsite)
RewriteRule ^(.*)$ frontend/web/$1 [L]

In backend/config/main.php add $baseUrl variable
$baseUrl = str_replace('/backend/web', '', (new yii\web\Request)->getBaseUrl());

and substitute in the value of baseUrl In this case, you do not bother with the name of the subdirectory into which you will transfer the site, you can change the names as you like, but do not forget to change the name in the root .htaccess
'baseUrl' => $baseUrl . '/admin'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question