FreeUnit

Yii§

To run apps based on the Yii framework versions 1.1 or 2.0 using Unit:

  1. Install Unit with a PHP language module.

  2. Next, install Yii and create or deploy your app.

    Here, we use Yii’s basic project template and Composer:

    $ cd /path/to/
    
    $ composer create-project --prefer-dist yiisoft/yii2-app-basic app
    

    This creates the app’s directory tree at /path/to/app/. Its web/ subdirectory contains both the root index.php and the static files; if your app requires additional .php scripts, also store them here.

  3. Run the following command (as root) so Unit can access the application directory:

    # chown -R unit:unit /path/to/app/
    

    Note

    The unit:unit user-group pair is available only with official packages, Docker images, and some third-party repos. Otherwise, account names may differ; run the ps aux | grep unitd command to be sure.

    For further details, including permissions, see the security checklist.

  4. Next, prepare the Yii configuration for Unit (use real values for share and root):

    {
        "listeners": {
            "*:80": {
                "pass": "routes"
            }
        },
    
        "routes": [
            {
                "match": {
                    "uri": [
                        "!/assets/*",
                        "*.php",
                        "*.php/*"
                    ]
                },
    
                "action": {
                    "pass": "applications/yii/direct"
                }
            },
            {
                "action": {
                    "share": "/path/to/app/web$uri",
                    "fallback": {
                        "pass": "applications/yii/index"
                    }
                }
            }
        ],
    
        "applications": {
            "yii": {
                "type": "php",
                "targets": {
                    "direct": {
                        "root": "/path/to/app/web/"
                    },
    
                    "index": {
                        "root": "/path/to/app/web/",
                        "script": "index.php"
                    }
                }
            }
        }
    }
    

    For a detailed discussion, see Configuring Web Servers and Running Applications in Yii 2.0 docs.

    Note

    The difference between the pass targets is their usage of the script setting:

    • The direct target runs the .php script from the URI or index.php if the URI omits it.
    • The index target specifies the script that Unit runs for any URIs the target receives.
  5. Upload the updated configuration. Assuming the JSON above was added to config.json. Run the following command as root:

    # curl -X PUT --data-binary @config.json --unix-socket \
           /path/to/control.unit.sock http://localhost/config/
    

    Note

    The control socket path may vary; run unitd -h or see Startup and Shutdown for details.

    After a successful update, your app should be available on the listener’s IP address and port:

    Yii Basic Template App on Unit
  1. Install Unit with a PHP language module.

  2. Next, install Yii and create or deploy your app.

    Here, we use Yii’s basic project template and yiic:

    $ git clone git@github.com:yiisoft/yii.git /path/to/yii1.1/
    
    $ /path/to/yii1.1/framework/yiic webapp /path/to/app/
    

    This creates the app’s directory tree at /path/to/app/.

  3. Next, prepare the Yii configuration for Unit (use real values for share and root):

    Warning

    The share action in the configuration below serves any file that no earlier step sends elsewhere. It serves a PHP file as source; it never runs it. The uri patterns are globs and match case-sensitively, so *.php does not match /config.PHP. On a case-insensitive filesystem (macOS, Windows, or a Docker Desktop bind mount from either) that file exists, and the share returns it with the passwords in it. A file the patterns never name, such as config.php.bak or settings.inc, is served the same way.

    Add a types allow-list to the share action. Keep the fallback where the configuration has one:

    "types": ["image/*", "text/css", "application/javascript", "font/*"]
    

    Unit compares this list with the MIME type it looks up from the file’s extension. The lookup is case-insensitive, so /config.PHP still resolves to application/x-httpd-php and is refused. An extension Unit does not know, such as .phtml or .inc, has an empty type, and the allow-list refuses that too. A refused request takes the share’s fallback when there is one, and gets a 403 response when there is not. Do not write the list as a refuse-list such as [“!application/x-httpd-php”]: a negated pattern does not exclude an empty type. See MIME filtering for the pattern syntax and the MIME type table for the limits of types, including the index file case.

    The list above is a starting point. Add the types your site serves -- text/html, application/json, text/plain, the XML types, application/pdf, video/* -- or those files are not served.

    {
        "listeners": {
            "*:80": {
                "pass": "routes"
            }
        },
    
        "routes": [
            {
                "match": {
                    "uri": [
                        "!/assets/*",
                        "!/protected/*",
                        "!/themes/*",
                        "*.php",
                        "*.php/*"
                    ]
                },
    
                "action": {
                    "pass": "applications/yii/direct"
                }
            },
            {
                "action": {
                    "share": "/path/to/app$uri",
                    "fallback": {
                        "pass": "applications/yii/index"
                    }
                }
            }
        ],
    
        "applications": {
            "yii": {
                "type": "php",
                "targets": {
                    "direct": {
                        "root": "/path/to/app/"
                    },
                    "index": {
                        "root": "/path/to/app/",
                        "script": "index.php"
                    }
                }
            }
        }
    }
    

    For a detailed discussion, see Yii 1.1 docs.

    Note

    The difference between the pass targets is their usage of the script setting:

    • The direct target runs the .php script from the URI or index.php if the URI omits it.
    • The index target specifies the script that Unit runs for any URIs the target receives.
  4. Upload the updated configuration. Assuming the JSON above was added to config.json. Run the following command as root:

    # curl -X PUT --data-binary @config.json --unix-socket \
           /path/to/control.unit.sock http://localhost/config/
    

    Note

    The control socket path may vary; run unitd -h or see Startup and Shutdown for details.

    After a successful update, your app should be available on the listener’s IP address and port:

    Yii Basic Template App on Unit