Configuration of Yii2 Advanced setup for single domain
Yii2 Advanced can be setup in single domain where frontend and backend can be accessed with same domain.
Instructions for configuration of the Apache or Nginx web servers for the configuration of Yii2 Advanced setup for single domain
Files to be modified during configuration
- advanced/frontend/config/main.php
- advanced/backend/config/main.php
- advanced/frontend/web/robots.txt
Make sure you have mod_rewrite
module is enabled or not before configuring
Example Vhost for Apache Setup
<VirtualHost *:80> ServerName advanced.local #ErrorLog /var/log/apache2/advanced.local.error.log #CustomLog /var/log/apache2/advanced.local.access.log combined AddDefaultCharset UTF-8 Options FollowSymLinks DirectoryIndex index.php index.html RewriteEngine on RewriteRule /\. - [L,F] DocumentRoot /path/to/advanced/frontend/web <Directory /path/to/advanced/frontend/web> AllowOverride none <IfVersion < 2.4> Order Allow,Deny Allow from all </IfVersion> <IfVersion >= 2.4> Require all granted </IfVersion> # 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 [L] </Directory> # redirect to the URL without a trailing slash (uncomment if necessary) #RewriteRule ^/admin/$ /admin [L,R=301] Alias /admin /path/to/advanced/backend/web # prevent the directory redirect to the URL with a trailing slash RewriteRule ^/admin$ /admin/ [L,PT] <Directory /path/to/advanced/backend/web> AllowOverride none <IfVersion < 2.4> Order Allow,Deny Allow from all </IfVersion> <IfVersion >= 2.4> Require all granted </IfVersion> # 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 [L] </Directory> </VirtualHost>
Example Vhost for nginx setup
server { listen 80; server_name advanced.local; set $base_root /path/to/advanced; root $base_root; #error_log /var/log/nginx/advanced.local.error.log warn; #access_log /var/log/nginx/advanced.local.access.log main; charset UTF-8; index index.php index.html; location / { root $base_root/frontend/web; try_files $uri $uri/ /frontend/web/index.php$is_args$args; # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary) #location ~ ^/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ { # log_not_found off; # access_log off; # try_files $uri =404; #} location ~ ^/assets/.+\.php(/|$) { deny all; } } location /admin { alias $base_root/backend/web/; # redirect to the URL without a trailing slash (uncomment if necessary) #location = /admin/ { # return 301 /admin; #} # prevent the directory redirect to the URL with a trailing slash location = /admin { # if your location is "/backend", try use "/backend/backend/web/index.php$is_args$args" # bug ticket: https://trac.nginx.org/nginx/ticket/97 try_files $uri /backend/web/index.php$is_args$args; } # if your location is "/backend", try use "/backend/backend/web/index.php$is_args$args" # bug ticket: https://trac.nginx.org/nginx/ticket/97 try_files $uri $uri/ /backend/web/index.php$is_args$args; # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary) #location ~ ^/admin/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ { # log_not_found off; # access_log off; # try_files $uri =404; #} location ~ ^/admin/assets/.+\.php(/|$) { deny all; } } location ~ ^/.+\.php(/|$) { rewrite (?!^/((frontend|backend)/web|admin))^ /frontend/web$uri break; rewrite (?!^/backend/web)^/admin(/.+)$ /backend/web$1 break; fastcgi_pass 127.0.0.1:9000; # proxy requests to a TCP socket #fastcgi_pass unix:/var/run/php-fpm.sock; # proxy requests to a UNIX domain socket (check your www.conf file) fastcgi_split_path_info ^(.+\.php)(.*)$; include /etc/nginx/fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; try_files $fastcgi_script_name =404; } location ~ /\. { deny all; } }
Update the path of advanced/backend/config/main.php
<?php return [ 'homeUrl' => '/admin', 'components' => [ 'request' => [ 'baseUrl' => '/admin', ], 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, ], ], ];
Update the path of advanced/frontend/config/main.php
<?php return [ 'components' => [ 'request' => [ 'baseUrl' => '', ], 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, ], ], ];
Update the path of advanced/frontend/web/robots.txt
Disallow: /frontend/web Disallow: /backend/web
Update .htacess from root path
AddDefaultCharset UTF-8 Options FollowSymLinks DirectoryIndex index.php index.html RewriteEngine on RewriteRule /\. - [L,F] # define the app environment variable RewriteCond %{REQUEST_URI} !^/((frontend|backend)/web|admin) RewriteRule ^ - [E=APP:frontend] RewriteCond %{REQUEST_URI} (?!^/backend/web)^/admin RewriteRule ^ - [E=APP:backend] # rewrite the URI of the frontend app RewriteCond %{ENV:APP} =frontend RewriteRule ^ frontend/web%{REQUEST_URI} # if a directory or a file exists, use the request directly RewriteCond %{ENV:APP} =frontend RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward the request to index.php RewriteRule ^ frontend/web/index.php [L] # redirect to the URL without a trailing slash (uncomment if necessary) #RewriteRule ^admin/$ /admin [L,R=301] # rewrite the URI of the backend app RewriteCond %{ENV:APP} =backend RewriteRule ^admin/?(.*)$ backend/web/$1 # if a directory or a file exists, use the request directly RewriteCond %{ENV:APP} =backend RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward the request to index.php RewriteRule ^ backend/web/index.php [L] # handle a directory trailing slash, redirect to the initial URI instead the rewritten one RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [L,R=301]
Comments for this post