如何设置Apache2与mod_fcgid和PHP5在Fedora 14

如何使用mod_fcgid和PHP5设置Apache2在Fedora 14上

本教程将介绍如何在Fedora 14上使用mod_fcgid和PHP5安装Apache2。mod_fcgid是与旧的mod_fastcgi兼容的替代方法。 它允许您使用其所有者的权限执行PHP脚本,而不是Apache用户。

我不会保证这将为您工作!

1初步说明

本教程中使用Fedora 14服务器,主机名为server1.example.com ,IP地址为192.168.0.100

我将在本教程www.example1.comwww.example2.com中创建两个Apache vhost,以演示mod_fcgid的用法。

在我们开始之前,确保SELinux被禁用。 打开/ etc / selinux / config ...

vi /etc/selinux/config

...并将SELINUX设置为禁用

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

setenforce 0

为了改变生效。

2安装Apache2 / mod_fcgi / PHP5

我们可以安装Apache2,mod_fcgid和PHP5,如下所示:

yum install httpd mod_fcgid php-cli

如果Apache2已经安装了PHP5作为Apache模块,请立即禁用PHP5模块 - 打开/etc/httpd/conf.d/php.conf ...

vi /etc/httpd/conf.d/php.conf

...并注释掉该文件中的所有内容:

#
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#
#<IfModule prefork.c>
#  LoadModule php5_module modules/libphp5.so
#</IfModule>
#<IfModule worker.c>
#  LoadModule php5_module modules/libphp5-zts.so
#</IfModule>

#
# Cause the PHP interpreter to handle files with a .php extension.
#
#AddHandler php5-script .php
#AddType text/html .php

#
# Add index.php to the list of files that will be served as directory
# indexes.
#
#DirectoryIndex index.php

#
# Uncomment the following line to allow PHP to pretty-print .phps
# files as PHP source code:
#
#AddType application/x-httpd-php-source .phps

然后我们创建Apache的系统启动链接并启动它:

chkconfig --levels 235 httpd on
/etc/init.d/httpd restart

接下来我们打开/etc/php.ini ...

vi /etc/php.ini

...并取消注释行cgi.fix_pathinfo = 1

[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://www.php.net/manual/en/ini.core.php#ini.cgi.fix-pathinfo
cgi.fix_pathinfo=1
[...]

打开/etc/httpd/conf.d/fcgid.conf ...

vi /etc/httpd/conf.d/fcgid.conf

...并添加行PHP_Fix_Pathinfo_Enable 1 (此行不能进入<VirtualHost>部分,因为那会得到此错误: PHP_Fix_Pathinfo_Enable不能在<VirtualHost>部分中发生 ):

# This is the Apache server configuration file for providing FastCGI support
# through mod_fcgid
#
# Documentation is available at
# http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html

LoadModule fcgid_module modules/mod_fcgid.so

# Use FastCGI to process .fcg .fcgi & .fpl scripts
AddHandler fcgid-script fcg fcgi fpl

# Sane place to put sockets and shared memory file
FcgidIPCDir /var/run/mod_fcgid
FcgidProcessTableFile /var/run/mod_fcgid/fcgid_shm

PHP_Fix_Pathinfo_Enable 1

然后重新加载Apache:

/etc/init.d/httpd reload

3为www.example1.com和www.example2.com创建Vhosts

现在我将创建两个vhosts: www.example1.com (使用文档root / var / www / web1 / web )和www.example2.com (使用文档root / var / www / web2 / web )。 www.example1.com将由用户和组web2由用户和组web1www.example2.com所有。

首先我们创建用户和组:

groupadd web1
groupadd web2
useradd -s /bin/false -d /var/www/web1 -m -g web1 web1
useradd -s /bin/false -d /var/www/web2 -m -g web2 web2
chmod 755 /var/www/web1
chmod 755 /var/www/web2

然后,我们创建文档根,并使其由用户/组web1或它们拥有。 web2

mkdir -p /var/www/web1/web
chown web1:web1 /var/www/web1/web
mkdir -p /var/www/web2/web
chown web2:web2 /var/www/web2/web

我们将使用suExec运行PHP; suExec的文档根目录是/ var / www ,如下所示:

/usr/sbin/suexec -V
[root@server1 ~]# /usr/sbin/suexec -V
 -D AP_DOC_ROOT="/var/www"
 -D AP_GID_MIN=100
 -D AP_HTTPD_USER="apache"
 -D AP_LOG_EXEC="/var/log/httpd/suexec.log"
 -D AP_SAFE_PATH="/usr/local/bin:/usr/bin:/bin"
 -D AP_UID_MIN=500
 -D AP_USERDIR_SUFFIX="public_html"
[root@server1 ~]#

因此,我们不能直接调用PHP二进制文件( / usr / bin / php-cgi ),因为它位于suExec的文档根目录之外。 由于suExec不允许符号链接,解决问题的唯一方法是为/ var / www子目录中的每个网站创建一个包装脚本; 然后,包装器脚本将调用PHP二进制文件/ usr / bin / php-cgi 。 包装器脚本必须由每个网站的用户和组拥有,因此我们需要每个网站的一个包装器脚本。 我将在/ var / www / php-fcgi-scripts的子目录中创建包装器脚本,例如/ var / www / php-fcgi-scripts / web1/ var / www / php-fcgi-scripts / web2

mkdir -p /var/www/php-fcgi-scripts/web1
mkdir -p /var/www/php-fcgi-scripts/web2
vi /var/www/php-fcgi-scripts/web1/php-fcgi-starter
#!/bin/sh
PHPRC=/etc/
export PHPRC
export PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_CHILDREN=8
exec /usr/bin/php-cgi
vi /var/www/php-fcgi-scripts/web2/php-fcgi-starter
#!/bin/sh
PHPRC=/etc/
export PHPRC
export PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_CHILDREN=8
exec /usr/bin/php-cgi

PHPRC行包含php.ini文件所在的目录(即/ etc /转换为/etc/php.ini )。 PHP_FCGI_MAX_REQUESTS是在fcgid进程停止并启动新请求之前的最大请求数。 PHP_FCGI_CHILDREN定义将启动的PHP子代数

php-fcgi-starter脚本必须是可执行的,它们(以及它们所在的目录)必须由网站的用户和组拥有:

chmod 755 /var/www/php-fcgi-scripts/web1/php-fcgi-starter
chmod 755 /var/www/php-fcgi-scripts/web2/php-fcgi-starter
chown -R web1:web1 /var/www/php-fcgi-scripts/web1
chown -R web2:web2 /var/www/php-fcgi-scripts/web2

现在我们为www.example1.comwww.example2.com创建Apache vhosts。 在/etc/httpd/conf/httpd.conf的末尾添加以下两个vhosts:

vi /etc/httpd/conf/httpd.conf
[...]
NameVirtualHost *:80

<VirtualHost *:80>
  ServerName www.example1.com
  ServerAlias example1.com
  ServerAdmin webmaster@example1.com
  DocumentRoot /var/www/web1/web/

  <IfModule mod_fcgid.c>
    SuexecUserGroup web1 web1
    <Directory /var/www/web1/web/>
      Options +ExecCGI
      AllowOverride All
      AddHandler fcgid-script .php
      FCGIWrapper /var/www/php-fcgi-scripts/web1/php-fcgi-starter .php
      Order allow,deny
      Allow from all
    </Directory>
  </IfModule>

  # ErrorLog /var/log/apache2/error.log
  # CustomLog /var/log/apache2/access.log combined
  ServerSignature Off

</VirtualHost>

<VirtualHost *:80>
  ServerName www.example2.com
  ServerAlias example2.com
  ServerAdmin webmaster@example2.com
  DocumentRoot /var/www/web2/web/

  <IfModule mod_fcgid.c>
    SuexecUserGroup web2 web2
    <Directory /var/www/web2/web/>
      Options +ExecCGI
      AllowOverride All
      AddHandler fcgid-script .php
      FCGIWrapper /var/www/php-fcgi-scripts/web2/php-fcgi-starter .php
      Order allow,deny
      Allow from all
    </Directory>
  </IfModule>

  # ErrorLog /var/log/apache2/error.log
  # CustomLog /var/log/apache2/access.log combined
  ServerSignature Off

</VirtualHost>

确保您填写正确的路径(以及SuexecUserGroup行中的正确用户和组)。

之后重新加载Apache:

/etc/init.d/httpd reload
赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏