-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathapache2.xml
347 lines (298 loc) · 9.79 KB
/
apache2.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: e51792efe0f103ec8724ad199524d7543610c978 Maintainer: Avenger Status: ready -->
<!-- CREDITS: Luffy, mowangjuanzi -->
<sect1 xml:id="install.unix.apache2" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Unix 系统下的 Apache 2.x</title>
<para>
本节包括在 Unix 平台的 Apache 2.x 下安装 PHP 的说明和提示。
</para>
&warn.apache2.compat;
<para>
推荐阅读 <link xlink:href="&url.apache2.docs;">Apache 文档</link>,了解一下
Apache 2.x 服务器,以及详细的安装参数。
</para>
<para>
可以从 <link xlink:href="&url.apache;">Apache 下载站点</link>下载最新版本的Apache,并且根据上文选择合适版本的
PHP 下载。本向导仅包含最基础的内容,只能让 Apache 2.x 和 PHP
能够正常工作。更多信息请阅读 <link xlink:href="&url.apache2.docs;">Apache
文档</link>。这里省略所有的版本号,以保证本文的正确性。需要将本文的“NN”替换为相应的版本号。
</para>
<para>
当前 Apache 2.x 有两个流行的版本 - 2.4 和 2.2。虽然选择某个版本会有种种原因,但是如果可以考虑的话,我们还是建议使用最新的
Apache 2.4 版本。当然,以下的介绍同样适合 Apache 2.4 和 2.2。请注意 Apache 2.2 已经不再被官方支持,并且不再发布新功能和补丁更新。
</para>
<orderedlist>
<listitem>
<para>
从上面列出的地方获取 Apache 源码包,然后解压:
</para>
<informalexample>
<screen>
<![CDATA[
tar -xzf httpd-2.x.NN.tar.gz
]]>
</screen>
</informalexample>
</listitem>
<listitem>
<para>
同样,获取 PHP 源码包并解压:
</para>
<informalexample>
<screen>
<![CDATA[
tar -xzf php-NN.tar.gz
]]>
</screen>
</informalexample>
</listitem>
<listitem>
<para>
编译并安装 Apache。请参考 Apache 安装文档了解编译 Apache 的更多细节。
</para>
<informalexample>
<screen>
<![CDATA[
cd httpd-2_x_NN
./configure --enable-so
make
make install
]]>
</screen>
</informalexample>
</listitem>
<listitem>
<para>
现在已经将 Apache 2.x.NN 安装在 /usr/local/apache2。本安装支持可装载模块和标准的
MPM prefork。之后,可以使用如下命令启动 Apache 服务器:
<informalexample>
<screen>
<![CDATA[
/usr/local/apache2/bin/apachectl start
]]>
</screen>
</informalexample>
如果成功,可以停止 Apache 服务器并继续安装 PHP:
<informalexample>
<screen>
<![CDATA[
/usr/local/apache2/bin/apachectl stop
]]>
</screen>
</informalexample>
</para>
</listitem>
<listitem>
<para>
现在需要配置并编译 PHP。在这里可以用各种各样的参数来自定义
PHP,例如启动哪些扩展功能包的支持等。用 <command>./configure --help</command>
命令可以列出当前可用的所有参数。在此例中,将给出一个在有
MySQL 支持的 Apache 2 上进行配置的范例。
</para>
<para>
如果按照上面的说明从源代码编译了 Apache,下面的例子会正确匹配 <command>apxs</command>
的路径。如果通过其他方式安装了 Apache,需要相应的调整 <command>apxs</command>
的路径。注意,在有些发行版本中,可能将 <command>apxs</command> 更名为 <command>apxs2</command>。
</para>
<informalexample>
<screen>
<![CDATA[
cd ../php-NN
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-pdo-mysql
make
make install
]]>
</screen>
</informalexample>
<para>
如果决定在安装后改变配置选项,只需重复最后的三步
<command>configure</command>,<command>make</command>,以及 <command>make install</command>,然后需要重新启动 Apache
使新模块生效。Apache 不需要重新编译。
</para>
<para>
请注意,除非明确有提示,否则“make install”命令将安装 PEAR、各种
PHP 工具诸如 phpize,并安装 PHP CLI 等等。
</para>
</listitem>
<listitem>
<para>
配置 <filename>php.ini</filename>。
</para>
<informalexample>
<screen>
<![CDATA[
cp php.ini-development /usr/local/lib/php.ini
]]>
</screen>
</informalexample>
<para>
可以编辑 <literal>.ini</literal> 来设置 PHP
选项。如果想要把 <filename>php.ini</filename> 放到另外的位置,需要在步骤
5 使用 <literal>--with-config-file-path=/some/path</literal> 选项。
</para>
<para>
如果选择了 <filename>php.ini-production</filename>,请务必阅读其中的变更列表,它们将如何影响
PHP 的行为。
</para>
</listitem>
<listitem>
<para>
编辑 <filename>httpd.conf</filename> 文件以调用 PHP 模块。<literal>LoadModule</literal>
表达式右边的路径必须指向系统中的 PHP 模块。以上的 <command>make install</command>
命令可能已经完成了这些,但务必要检查。
</para>
<informalexample>
<para>
PHP 8 版本:
</para>
<programlisting role="apache-conf">
<![CDATA[
LoadModule php_module modules/libphp.so
]]>
</programlisting>
</informalexample>
<informalexample>
<para>
PHP 7 版本:
</para>
<programlisting role="apache-conf">
<![CDATA[
LoadModule php7_module modules/libphp7.so
]]>
</programlisting>
</informalexample>
</listitem>
<listitem>
<para>
告知 Apache 将特定的扩展名解析成 PHP,例如,让 Apache 将扩展名 <literal>.php</literal>
解析成 PHP。为了避免潜在的危险,例如上传或者创建类似 <filename>exploit.php.jpg</filename>
的文件并被当做 PHP 执行,我们不再使用 Apache 的 <literal>AddType</literal>
指令来设置。参考下面的例子,你可以简单的将需要的扩展名解释为 PHP。
下面将添加 <literal>.php</literal> 来进行演示。
</para>
<informalexample>
<programlisting role="apache-conf">
<![CDATA[
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
]]>
</programlisting>
</informalexample>
<para>
或者也想将 <literal>.php</literal>、<literal>.php2</literal>、<literal>.php3</literal>、<literal>.php4</literal>、<literal>.php5</literal>、<literal>.php6</literal>
以及 <literal>.phtml</literal> 文件都当做 PHP 来运行,我们无需额外的设置,仅需按照下面这样来:
</para>
<informalexample>
<programlisting role="apache-conf">
<![CDATA[
<FilesMatch "\.ph(p[2-6]?|tml)$">
SetHandler application/x-httpd-php
</FilesMatch>
]]>
</programlisting>
</informalexample>
<para>
然后,可以将 <literal>.phps</literal> 文件由 PHP 源码过滤器处理,使得其在显示时可以高亮源码,设置如下:
</para>
<informalexample>
<programlisting role="apache-conf">
<![CDATA[
<FilesMatch "\.phps$">
SetHandler application/x-httpd-php-source
</FilesMatch>
]]>
</programlisting>
</informalexample>
<para>
<literal>mod_rewrite</literal> 也有助于将那些不需要运行的 <literal>.php</literal> 文件的源码高亮显示,而并不需要将他们更名为 <literal>.phps</literal> 文件:
</para>
<informalexample>
<programlisting role="apache-conf">
<![CDATA[
RewriteEngine On
RewriteRule (.*\.php)s$ $1 [H=application/x-httpd-php-source]
]]>
</programlisting>
</informalexample>
<para>
不要在正式生产运营的系统上启动 PHP 源码过滤器,因为这可能泄露系统机密或者嵌入的代码中的敏感信息。
</para>
</listitem>
<listitem>
<para>
按照通常的方式启动 Apache 服务:
</para>
<informalexample>
<screen>
<![CDATA[
/usr/local/apache2/bin/apachectl start
]]>
</screen>
</informalexample>
<para>或者</para>
<informalexample>
<screen>
<![CDATA[
service httpd restart
]]>
</screen>
</informalexample>
</listitem>
</orderedlist>
<para>
按照上面的步骤便可以使 Apache 2.x 将 PHP 作为 <literal>SAPI</literal>
模块。当然 Apache 和 PHP 都还有很多配置选项,可以在相应的源代码目录中使用
<command>./configure --help</command> 获得更多信息。
</para>
<para>
可以选择 <filename>worker</filename> MPM 而不是标准的
<filename>prefork</filename> MPM 来编译多线程版本的 Apache。只需在上面的第
3 步,将选项使用如下参数传递到 <command>./configure</command>中:
</para>
<informalexample>
<screen>
<![CDATA[
--with-mpm=worker
]]>
</screen>
</informalexample>
<para>
如果不是很明确这样做的后果并且大概理解其含义的话,最好不要进行这一步。更多信息请参考
Apache 文档中关于 <link xlink:href="&url.apache2.mpm;">MPM-Modules</link> 的部分。
</para>
<note>
<para>
<link linkend="faq.installation.apache.multiviews">Apache MultiViews
常见问题</link>中讨论了在 PHP 中使用 MultiViews。
</para>
</note>
<note>
<para>
要编译多线程版本的 Apache,系统必须支持多线程。这也意味着需要将 PHP
编译为 Zend 线程安全(ZTS)。在此配置下。并不是所有的扩展都可以使用。推荐设置是使用默认的
<filename>prefork</filename> MPM-Module 编译 Apache。
</para>
</note>
</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
-->