├── README.md ├── SECURITY.md ├── advanced ├── .htaccess ├── backend │ └── config │ │ └── main.php └── frontend │ ├── config │ └── main.php │ └── web │ └── robots.txt └── vhosts ├── apache.conf └── nginx.conf /README.md: -------------------------------------------------------------------------------- 1 | Yii 2 Advanced Application. One Domain Configuration 2 | === 3 | 4 | There are times when there is no need or no possibility to place the frontend and backend parts on different domains. Here are the instructions for configuration of the Apache or Nginx web servers for using multiple applications on a single domain. 5 | 6 | **Final frontend URL: http://advanced.local** 7 | **Final backend URL: http://advanced.local/admin** 8 | 9 | Preparation 10 | --- 11 | 12 | You need to make some changes in the configuration files of the applications according to the bundled `advanced/frontend/config/main.php` and `advanced/backend/config/main.php` files. 13 | 14 | The next step is the adding two rules from the bundled `advanced/frontend/web/robots.txt` file to the content of your main `robots.txt` file. 15 | 16 | Apache Configuration (.htaccess or VirtualHost) 17 | --- 18 | 19 | > Note: If you have **a shared hosting** or want to use `.htaccess`, simply copy the bundled `advanced/.htaccess` file into the root directory of your project. 20 | 21 | The first thing to do is to execute the following command with enough permissions to make sure whether the `mod_rewrite` module is enabled: 22 | 23 | ```bash 24 | [ -f /etc/apache2/mods-enabled/rewrite.load ] && echo "Found" || echo "Not Found" 25 | ``` 26 | 27 | If the `Not Found` message appears, just execute the `a2enmod rewrite` command and restart the Apache server. 28 | 29 | Now you can use the configuration specified in the bundled `vhosts/apache.conf` file to set up a virtual host. But don't forget to replace `advanced.local` with an actual hostname and `/path/to/advanced` with an actual path. 30 | 31 | Nginx Configuration 32 | --- 33 | 34 | In this case, use only the configuration of the bundled `vhosts/nginx.conf` file. If you don't like use `advanced.local` as a hostname, change it. Also replace `/path/to/advanced` with the actual path to your project. 35 | 36 | --- 37 | 38 | Inspired by discussions in [the russian community] of the Yii framework. 39 | 40 | [the russian community]:https://yiiframework.ru/ 41 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | Use the contacts from the link on the profile page if you find a vulnerability. 4 | -------------------------------------------------------------------------------- /advanced/.htaccess: -------------------------------------------------------------------------------- 1 | AddDefaultCharset UTF-8 2 | 3 | Options FollowSymLinks 4 | DirectoryIndex index.php index.html 5 | RewriteEngine on 6 | 7 | RewriteRule /\. - [L,F] 8 | 9 | # define the app environment variable 10 | RewriteCond %{REQUEST_URI} !^/((frontend|backend)/web|admin) 11 | RewriteRule ^ - [E=APP:frontend] 12 | RewriteCond %{REQUEST_URI} (?!^/backend/web)^/admin 13 | RewriteRule ^ - [E=APP:backend] 14 | 15 | # rewrite the URI of the frontend app 16 | RewriteCond %{ENV:APP} =frontend 17 | RewriteRule ^ frontend/web%{REQUEST_URI} 18 | # if a directory or a file exists, use the request directly 19 | RewriteCond %{ENV:APP} =frontend 20 | RewriteCond %{REQUEST_FILENAME} !-f 21 | RewriteCond %{REQUEST_FILENAME} !-d 22 | # otherwise forward the request to index.php 23 | RewriteRule ^ frontend/web/index.php [L] 24 | 25 | # redirect to the URL without a trailing slash (uncomment if necessary) 26 | #RewriteRule ^admin/$ /admin [L,R=301] 27 | 28 | # rewrite the URI of the backend app 29 | RewriteCond %{ENV:APP} =backend 30 | RewriteRule ^admin/?(.*)$ backend/web/$1 31 | # if a directory or a file exists, use the request directly 32 | RewriteCond %{ENV:APP} =backend 33 | RewriteCond %{REQUEST_FILENAME} !-f 34 | RewriteCond %{REQUEST_FILENAME} !-d 35 | # otherwise forward the request to index.php 36 | RewriteRule ^ backend/web/index.php [L] 37 | 38 | # handle a directory trailing slash, redirect to the initial URI instead the rewritten one 39 | RewriteCond %{REQUEST_FILENAME} -d 40 | RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [L,R=301] 41 | -------------------------------------------------------------------------------- /advanced/backend/config/main.php: -------------------------------------------------------------------------------- 1 | '/admin', 5 | 'components' => [ 6 | 'request' => [ 7 | 'baseUrl' => '/admin', 8 | ], 9 | 'urlManager' => [ 10 | 'enablePrettyUrl' => true, 11 | 'showScriptName' => false, 12 | ], 13 | ], 14 | ]; 15 | -------------------------------------------------------------------------------- /advanced/frontend/config/main.php: -------------------------------------------------------------------------------- 1 | [ 5 | 'request' => [ 6 | 'baseUrl' => '', 7 | ], 8 | 'urlManager' => [ 9 | 'enablePrettyUrl' => true, 10 | 'showScriptName' => false, 11 | ], 12 | ], 13 | ]; 14 | -------------------------------------------------------------------------------- /advanced/frontend/web/robots.txt: -------------------------------------------------------------------------------- 1 | Disallow: /frontend/web 2 | Disallow: /backend/web 3 | -------------------------------------------------------------------------------- /vhosts/apache.conf: -------------------------------------------------------------------------------- 1 | 2 | ServerName advanced.local 3 | 4 | #ErrorLog /var/log/apache2/advanced.local.error.log 5 | #CustomLog /var/log/apache2/advanced.local.access.log combined 6 | AddDefaultCharset UTF-8 7 | 8 | Options FollowSymLinks 9 | DirectoryIndex index.php index.html 10 | RewriteEngine on 11 | 12 | RewriteRule /\. - [L,F] 13 | 14 | DocumentRoot /path/to/advanced/frontend/web 15 | 16 | AllowOverride none 17 | 18 | Order Allow,Deny 19 | Allow from all 20 | 21 | = 2.4> 22 | Require all granted 23 | 24 | 25 | # if a directory or a file exists, use the request directly 26 | RewriteCond %{REQUEST_FILENAME} !-f 27 | RewriteCond %{REQUEST_FILENAME} !-d 28 | # otherwise forward the request to index.php 29 | RewriteRule ^ index.php [L] 30 | 31 | 32 | # redirect to the URL without a trailing slash (uncomment if necessary) 33 | #RewriteRule ^/admin/$ /admin [L,R=301] 34 | 35 | Alias /admin /path/to/advanced/backend/web 36 | # prevent the directory redirect to the URL with a trailing slash 37 | RewriteRule ^/admin$ /admin/ [L,PT] 38 | 39 | AllowOverride none 40 | 41 | Order Allow,Deny 42 | Allow from all 43 | 44 | = 2.4> 45 | Require all granted 46 | 47 | 48 | # if a directory or a file exists, use the request directly 49 | RewriteCond %{REQUEST_FILENAME} !-f 50 | RewriteCond %{REQUEST_FILENAME} !-d 51 | # otherwise forward the request to index.php 52 | RewriteRule ^ index.php [L] 53 | 54 | 55 | -------------------------------------------------------------------------------- /vhosts/nginx.conf: -------------------------------------------------------------------------------- 1 | server { 2 | listen 80; 3 | server_name advanced.local; 4 | 5 | set $base_root /path/to/advanced; 6 | root $base_root; 7 | 8 | #error_log /var/log/nginx/advanced.local.error.log warn; 9 | #access_log /var/log/nginx/advanced.local.access.log main; 10 | charset UTF-8; 11 | index index.php index.html; 12 | 13 | location / { 14 | root $base_root/frontend/web; 15 | try_files $uri $uri/ /frontend/web/index.php$is_args$args; 16 | 17 | # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary) 18 | #location ~ ^/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ { 19 | # log_not_found off; 20 | # access_log off; 21 | # try_files $uri =404; 22 | #} 23 | 24 | location ~ ^/assets/.+\.php(/|$) { 25 | deny all; 26 | } 27 | } 28 | 29 | location /admin { 30 | alias $base_root/backend/web/; 31 | 32 | # redirect to the URL without a trailing slash (uncomment if necessary) 33 | #location = /admin/ { 34 | # return 301 /admin; 35 | #} 36 | 37 | # prevent the directory redirect to the URL with a trailing slash 38 | location = /admin { 39 | # if your location is "/backend", try use "/backend/backend/web/index.php$is_args$args" 40 | # bug ticket: https://trac.nginx.org/nginx/ticket/97 41 | try_files $uri /backend/web/index.php$is_args$args; 42 | } 43 | 44 | # if your location is "/backend", try use "/backend/backend/web/index.php$is_args$args" 45 | # bug ticket: https://trac.nginx.org/nginx/ticket/97 46 | try_files $uri $uri/ /backend/web/index.php$is_args$args; 47 | 48 | # omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary) 49 | #location ~ ^/admin/.+\.(css|js|ico|png|jpe?g|gif|svg|ttf|mp4|mov|swf|pdf|zip|rar)$ { 50 | # log_not_found off; 51 | # access_log off; 52 | # try_files $uri =404; 53 | #} 54 | 55 | location ~ ^/admin/assets/.+\.php(/|$) { 56 | deny all; 57 | } 58 | } 59 | 60 | location ~ ^/.+\.php(/|$) { 61 | rewrite (?!^/((frontend|backend)/web|admin))^ /frontend/web$uri break; 62 | rewrite (?!^/backend/web)^/admin(/.+)$ /backend/web$1 break; 63 | 64 | fastcgi_pass 127.0.0.1:9000; # proxy requests to a TCP socket 65 | #fastcgi_pass unix:/var/run/php-fpm.sock; # proxy requests to a UNIX domain socket (check your www.conf file) 66 | fastcgi_split_path_info ^(.+\.php)(.*)$; 67 | include /etc/nginx/fastcgi_params; 68 | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 69 | try_files $fastcgi_script_name =404; 70 | } 71 | 72 | location ~ /\. { 73 | deny all; 74 | } 75 | } 76 | --------------------------------------------------------------------------------