installation.md 6.74 KB
Newer Older
Qiang Xue committed
1 2 3
Installation
============

4 5
There are two ways you can install the Yii framework:

6
* Via [Composer](http://getcomposer.org/) (recommended)
Larry Ullman committed
7
* By downloading an application template containing all of the site requirements, including the Yii framework itself
8

9

10 11 12
Installing via Composer
-----------------------

13
The recommended way to install Yii is to use the [Composer](http://getcomposer.org/) package manager. If you do not already
Larry Ullman committed
14
have Composer installed, you may download it from [http://getcomposer.org/](http://getcomposer.org/), or run the following command to download and install it:
15 16 17 18 19

```
curl -s http://getcomposer.org/installer | php
```

Larry Ullman committed
20
(It is strongly recommended to perform a [global Composer installation](https://getcomposer.org/doc/00-intro.md#globally)).
21

Larry Ullman committed
22
For problems with, or more information on, installing Composer, see the official Composer guide:
23

24
* [Linux](http://getcomposer.org/doc/00-intro.md#installation-nix)
25
* [Windows](http://getcomposer.org/doc/00-intro.md#installation-windows)
26

Larry Ullman committed
27
With Composer installed, you can create a new Yii site using one of Yii's ready-to-use application templates. Based on your needs, choosing the right template can help bootstrap your project.
28

Larry Ullman committed
29
Currently, there are two Yii application templates available:
30

Larry Ullman committed
31 32
- [Basic Application Template](https://github.com/yiisoft/yii2-app-basic), a basic frontend application template
- [Advanced Application Template](https://github.com/yiisoft/yii2-app-advanced), consisting of a frontend, a backend, console resources, common (shared code), and support for environments
33

Larry Ullman committed
34 35
For template installation instructions, see the above linked pages.
To read more about the ideas behind these application templates and the proposed usage,
36 37
refer to the [basic application template](apps-basic.md) and [advanced application template](apps-advanced.md) documents.

Larry Ullman committed
38
If you do not want to use a template, rather starting from scratch, you'll find information in the [creating your own application structure](apps-own.md) document. This approach is only recommended for advanced users.
39

40 41 42 43

Installing from zip
-------------------

44
Installation from a zip file involves two steps:
Qiang Xue committed
45

46
   1. Downloading an application template from [yiiframework.com](http://www.yiiframework.com/download/).
47
   2. Unpacking the downloaded file.
Qiang Xue committed
48

Larry Ullman committed
49
If you only want the Yii Framework files you can download a zip file directly from [github](https://github.com/yiisoft/yii2-framework/releases).
50 51 52
To create your application you might want to follow the steps described in [creating your own application structure](apps-own.md).
This is only recommended for advanced users.

Larry Ullman committed
53 54
> Tip: The Yii framework itself does not need to be installed under a web-accessible directory (in fact, it should not be).
A Yii application has one entry script, which is usually the only file that absolutely must be
55
exposed to web users (i.e., placed within the web directory). Other PHP scripts, including those
Larry Ullman committed
56
in the Yii Framework, should be protected from web access to prevent possible exploitation by hackers.
57

Qiang Xue committed
58 59 60 61

Requirements
------------

Larry Ullman committed
62 63 64 65
Yii 2 requires PHP 5.4.0 or higher. Yii has been tested with the [Apache HTTP server](http://httpd.apache.org/) and
[Nginx HTTP server](http://nginx.org/) on both Windows and Linux.
Yii may also be usable on other web servers and platforms, provided that PHP 5.4 or higher is present.

Qiang Xue committed
66
After installing Yii, you may want to verify that your server satisfies
67
Yii's requirements. You can do so by running the requirement checker
68 69
script in a web browser or from the command line.

Larry Ullman committed
70
If you have installed a Yii application template via the downloaded zip file or Composer, you'll find a `requirements.php` file in the
71
base directory of your application.
Qiang Xue committed
72

Larry Ullman committed
73
In order to run this script on the command line use the following command (after navigating to the directory where `requirements.php` can be found):
74 75 76 77 78

```
php requirements.php
```

Larry Ullman committed
79
In order to run this script in your browser, you must make sure it's within a web directory, and then
80
access `http://hostname/path/to/yii-app/requirements.php` in your browser.
Carsten Brandt committed
81

Qiang Xue committed
82 83 84 85

Recommended Apache Configuration
--------------------------------

86
Yii is ready to work with a default Apache web server configuration. As a security measure, Yii comes with `.htaccess`
Larry Ullman committed
87
files in the Yii framework folder to deny access to the application's restricted resources.
88

89
By default, requests for pages in a Yii-based site go through the bootstrap file, usually named `index.php`, and placed
Larry Ullman committed
90
in the application's home directory. The result will be URLs in the format `http://hostname/index.php/controller/action/param/value`.
91

92
To hide the bootstrap file in your URLs, add `mod_rewrite` instructions to the `.htaccess` file in your web document root
93 94
(or add the instructions to the virtual host configuration in Apache's `httpd.conf` file, `Directory` section for your webroot).
The applicable instructions are:
Qiang Xue committed
95 96 97 98

~~~
RewriteEngine on

Larry Ullman committed
99
# If a directory or a file exists, use the request directly
Qiang Xue committed
100 101
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Larry Ullman committed
102
# Otherwise forward the request to index.php
Qiang Xue committed
103 104 105
RewriteRule . index.php
~~~

106

Qiang Xue committed
107 108 109
Recommended Nginx Configuration
-------------------------------

110 111 112 113
Yii can also be used with the popular [Nginx](http://wiki.nginx.org/) web server, so long it has PHP installed as
an [FPM SAPI](http://php.net/install.fpm). Below is a sample host configuration for a Yii-based site on Nginx.
The configuration tells the server to send all requests for non-existent resources through the bootstrap file,
resulting in "prettier" URLs without the need for `index.php` references.
Qiang Xue committed
114

115
```
Qiang Xue committed
116
server {
Carsten Brandt committed
117
    set $yii_bootstrap "index.php";
Qiang Xue committed
118
    charset utf-8;
Carsten Brandt committed
119
    client_max_body_size 128M;
Qiang Xue committed
120

Carsten Brandt committed
121 122 123 124 125 126
    listen 80; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name mysite.local;
    root        /path/to/project/web;
    index       $yii_bootstrap;
Qiang Xue committed
127

128
    access_log  /path/to/project/log/access.log  main;
Carsten Brandt committed
129
    error_log   /path/to/project/log/error.log;
Qiang Xue committed
130

131
    location / {
Carsten Brandt committed
132 133
        # Redirect everything that isn't real file to yii bootstrap file including arguments.
        try_files $uri $uri/ /$yii_bootstrap?$args;
Qiang Xue committed
134 135
    }

Carsten Brandt committed
136 137 138 139 140 141
    # uncomment to avoid processing of calls to unexisting static files by yii
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    #    try_files $uri =404;
    #}
    #error_page 404 /404.html;

142 143
    location ~ \.php$ {
        include fastcgi.conf;
Qiang Xue committed
144
        fastcgi_pass   127.0.0.1:9000;
lancecoder committed
145
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
Qiang Xue committed
146 147
    }

148 149
    location ~ /\.(ht|svn|git) {
        deny all;
Qiang Xue committed
150 151
    }
}
152
```
Qiang Xue committed
153

154
When using this configuration, you should set `cgi.fix_pathinfo=0` in the `php.ini` file in order to avoid many unnecessary system `stat()` calls.
155 156


Carsten Brandt committed
157
Note that when running a HTTPS server you need to add `fastcgi_param HTTPS on;` in order for Yii to properly detect if
158
connection is secure.