tutorial-shared-hosting.md 3.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
Shared Hosting Environment
==========================

Shared hosting environments are often quite limited about configuration and directory structure. Still in most cases
you can run Yii 2.0 on these.

Deploying basic application
---------------------------

Since there's typically only one webroot it is recommended to use basic application template. Refer to
[Installing Yii chapter](start-installation.md) and install application template locally.

13
### Add extras for webserver <span id="add-extras-for-webserver"></span>
14

pana1990 committed
15
If webserver used is Apache you'll need to add `.htaccess` file with the following content to `web`
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
(where `index.php` is):

```
Options +FollowSymLinks
IndexIgnore */*

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
```

In case of nginx you should not need any extra config files.

34
### Renaming webroot <span id="renaming-webroot"></span>
35 36 37 38 39

If after connecting to your shared hosting via FTP or by other means you're seeing something like the following, you're
most probably lucky.
 
```
40 41 42
config
logs
www
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
```

In the above `www` is webserver directory root (i.e. webroot). It could be named differently. Common names are: `www`,
`htdocs`, `public_html`. Since we have webroot in our basic application template named `web` we need to rename it to
whatever hosting webroot is before uploading.

### FTP root directory is writeable

If you can write to the root level directory i.e. where `config`, `logs` and `www` are, just upload `assets`, `commands`
etc. as is.

### Check requirements

In order to run Yii hosting should meet its requirements. The very minimum requirement is PHP 5.4. In order to check
the rest copy `requirements.php` from root directory into webroot directory and run it via browser using
`http://example.com/requirements.php` URL. Don't forget to delete the file afterwards.


Deploying advanced application
------------------------------

Deploying advanced application to shared hosting is a bit trickier than doing it with basic application because it has
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
two webroots which shared hosting typically donesn't have. Because of that structure could be adjusted a bit.

### Move entry scripts into single webroot

First of all we need a webroot directory. Name it the way it matches your hosting webroot as described in
[Renaming webroot](#renaming-webroot) above. Then create the following structure:

```
www
    admin
backend
common
console
environments
frontend
...
```

`www` will be our frontend directory so move contents of `frontend/web` into it. Do the same with `backend/web` moving
its contents into `www/admin`. In each case you need to adjust paths in `index.php` and `index-test.php`.

### Separate sessions and cookies

Originally backend and frontend are intended to run at different domains. When we're moving it all to the same domain it
is starting to share the same cookies creating a clash. It order to fix it adjust backend application config
`backend/config/main.php` like the following:

```php
'components' => [
    'request' => [
        'csrfParam' => '_backendCSRF',
        'csrfCookie' => [
            'httpOnly' => true,
            'path' => '/admin',
        ],
    ],
    'user' => [
        'identityCookie' => [
            'name' => '_backendIdentity',
            'path' => '/admin',
            'httpOnly' => true,
        ],
    ],
    'session' => [
        'name' => 'BACKENDSESSID',
        'cookieParams' => [
            'path' => '/admin',
        ],
    ],
],
```