-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathnginx.xml
273 lines (237 loc) · 6.97 KB
/
nginx.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: f0261e36dc250410f352fe33ad4c4e699cb18b02 Maintainer: yuanyuqiang Status: ready -->
<!-- Reviewed: no -->
<sect1 xml:id="install.unix.nginx" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Unix 系统下的 Nginx 1.4.x</title>
<para>
本文档包括使用 PHP-FPM 为 Nginx 1.4.x HTTP 服务器安装和配置 PHP 的说明和提示。
</para>
<para>
本指南假定您已经从源代码成功构建 Nginx,并且其二进制文件和配置文件都位于 <literal>/usr/local/nginx</literal>。
如果您使用其他方式获取的 Nginx,请参考 <link xlink:href="&url.nginx;">Nginx Wiki</link> 并对照本文档完成安装。
</para>
<para>
本文档仅包含 Nginx 服务器的基本配置,它将通过 80 端口提供 PHP 应用的处理能力。
如果您需要超出本文档范围的安装配置指导,建议您查阅 Nginx 和 PHP-FPM 的文档。
</para>
<para>
需要注意的是,本文档一律使用 'x' 来表示版本号,请根据实际情况将 'x' 替换为对应的版本号。
</para>
<orderedlist>
<listitem>
<para>
建议您访问 Nginx Wiki
<link xlink:href="&url.nginx.wiki.install;">安装</link> 页面以获取并在您的系统上安装 Nginx。
</para>
</listitem>
<listitem>
<para>
获取并解压 PHP 源代码:
</para>
<informalexample xml:id="install.unix.nginx.extract.php">
<screen>
<![CDATA[
tar zxf php-x.x.x
]]>
</screen>
</informalexample>
</listitem>
<listitem>
<para>
配置并构建 PHP。在此步骤您可以使用很多选项自定义 PHP,例如启用某些扩展等。
运行 ./configure --help 命令来获得完整的可用选项清单。
在本示例中,我们仅进行包含 PHP-FPM 和 MySQL 支持的简单配置。
</para>
<informalexample xml:id="install.unix.nginx.build.php">
<screen>
<![CDATA[
cd ../php-x.x.x
./configure --enable-fpm --with-mysqli
make
sudo make install
]]>
</screen>
</informalexample>
</listitem>
<listitem>
<para>
创建配置文件,并将其复制到正确的位置。
</para>
<informalexample xml:id="install.unix.nginx.configure.php">
<screen>
<![CDATA[
cp php.ini-development /usr/local/php/php.ini
cp /usr/local/etc/php-fpm.d/www.conf.default /usr/local/etc/php-fpm.d/www.conf
cp sapi/fpm/php-fpm /usr/local/bin
]]>
</screen>
</informalexample>
</listitem>
<listitem>
<para>
需要着重提醒的是,如果文件不存在,则阻止 Nginx 将请求发送到后端的 PHP-FPM 模块,
以避免遭受恶意脚本注入的攻击。
</para>
<para>
将 php.ini 文件中的配置项
<link linkend="ini.cgi.fix-pathinfo">cgi.fix_pathinfo</link>
设置为 <literal>0</literal> 。
</para>
<para>
打开 php.ini:
</para>
<informalexample xml:id="install.unix.nginx.configure.ini">
<screen>
<![CDATA[
vim /usr/local/php/php.ini
]]>
</screen>
</informalexample>
<para>
定位到 <literal>cgi.fix_pathinfo=</literal> 并将其修改为如下所示:
</para>
<informalexample xml:id="install.unix.nginx.configure.pathinfo">
<screen>
<![CDATA[
cgi.fix_pathinfo=0
]]>
</screen>
</informalexample>
</listitem>
<listitem>
<para>
在启动服务之前,需要修改 php-fpm.conf 配置文件,确保 php-fpm 模块使用
www-data 用户和 www-data 用户组的身份运行。
</para>
<informalexample xml:id="install.unix.nginx.modify.phpfpm">
<screen>
<![CDATA[
vim /usr/local/etc/php-fpm.d/www.conf
]]>
</screen>
</informalexample>
<para>
找到以下内容并修改:
</para>
<informalexample xml:id="install.unix.nginx.modify.phpfpm.usergroup">
<screen>
<![CDATA[
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = www-data
group = www-data
]]>
</screen>
</informalexample>
<para>
然后启动 php-fpm 服务:
</para>
<informalexample xml:id="install.unix.nginx.start.phpfpm">
<screen>
<![CDATA[
/usr/local/bin/php-fpm
]]>
</screen>
</informalexample>
<para>
本文档未涵盖对 php-fpm 进行进一步配置的信息,如果您需要更多信息,请查阅相关文档。
</para>
</listitem>
<listitem>
<para>
配置 Nginx 使其支持 PHP 应用:
</para>
<informalexample xml:id="install.unix.nginx.configure.nginx">
<programlisting>
<![CDATA[
vim /usr/local/nginx/conf/nginx.conf
]]>
</programlisting>
</informalexample>
<para>
修改默认的 location 块,使其支持 .php 文件:
</para>
<informalexample xml:id="install.unix.nginx.configure.nginx.location">
<programlisting role="nginx-conf">
<![CDATA[
location / {
root html;
index index.php index.html index.htm;
}
]]>
</programlisting>
</informalexample>
<para>
下一步配置来保证对于 .php 文件的请求将被传送到后端的 PHP-FPM 模块,
取消默认的 PHP 配置块的注释,并修改为下面的内容:
</para>
<informalexample xml:id="install.unix.nginx.configure.nginx.php">
<programlisting role="nginx-conf">
<![CDATA[
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
]]>
</programlisting>
</informalexample>
<para>
重启 Nginx。
</para>
<informalexample xml:id="install.unix.nginx.restart.nginx">
<screen>
<![CDATA[
sudo /usr/local/nginx/sbin/nginx -s stop
sudo /usr/local/nginx/sbin/nginx
]]>
</screen>
</informalexample>
</listitem>
<listitem>
<para>
创建测试文件。
</para>
<informalexample xml:id="install.unix.nginx.test.nginx.php">
<screen>
<![CDATA[
rm /usr/local/nginx/html/index.html
echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php
]]>
</screen>
</informalexample>
<para>
打开浏览器,访问 http://localhost,将会显示 phpinfo() 。
</para>
</listitem>
</orderedlist>
<para>
通过以上步骤的配置,Nginx 服务器现在可以以 <literal>SAPI</literal> <literal>SAPI</literal> 模块的方式支持 PHP 应用了。
当然,对于 Nginx 和 PHP 的配置,还有很多可用的选项,
请在对应的源代码目录执行 <command>./configure --help</command> 来查阅更多配置选项。
</para>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->