FreeBSD:Nginx FastCGI PHP配置

时间:2020-01-09 10:38:30  来源:igfitidea点击:

问题描述:如何在FreeBSD Nginx Web服务器下将PHP配置为FastCGI?

解决方法:您可以在Nginx下轻松地将php配置为FastCGI应用程序以提高性能。
您需要以下组件:

  • spawn-fcgi binary用于产生FastCGI进程。
    可以从Lighttpd Webserver项目安装此二进制文件。

Nginx不会自动产生FCGI进程。
您必须使用spawn-fcgi分别启动它们。

  • start.php.sh:用于停止/启动/重新启动php fastcgi进程的shell脚本。
    此脚本在127.0.0.1 IP和9000端口上交换php进程。
    一旦启动后端,就可以将Nginx配置为连接到PHP。

FreeBSD安装PHP5

如果未安装php5,请输入以下命令(确保选择FastCGI选项):

# cd /usr/ports/lang/php5

# make config

# make install clean

通过选择FastCGI选项启用FastCGI支持

还安装了必需的php扩展名,例如php-msyql,php-gd等:

# cp /usr/local/etc/php.ini-recommended /usr/local/etc/php.ini

# cd /usr/ports/lang/php5-extensions/

# make install clean

安装spawn-fcgi

从另一个Lighttpd安装中获取最新的spawn-fcgi或按以下方式安装:

# pkg_add -r -v lighttpd

输出示例:

scheme:   [ftp]
user:     []
password: []
host:     [ftp.freebsd.org]
port:     [0]
document: [/pub/FreeBSD/ports/amd64/packages-7.0-release/Latest/lighttpd.tbz]
---> ftp.freebsd.org:21
looking up ftp.freebsd.org
connecting to ftp.freebsd.org:21
>> USER anonymous

Now you can use /usr/local/bin/spawn-fcgi to swap process. If you want you can copy /usr/local/bin/spawn-fcgi to /root or other directory and just delete lighttpd package by typing the following commands:

java#cp/usr/local/bin/spawn-fcgi/root /#pkg_delete -v lighttpd-1.4.18_1#cp/root/spawn-fcgi/usr/local/bin/spawn-fcgi```现在,您可以从命令行启动PHP Fastcgi,如下所示:

# /usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www -g www -f /usr/local/bin/php-cgi

其中

  • -a 127.0.0.1:PHP FastCGI绑定IP地址,Nginx将连接到该IP地址。
  • -p 9000:PHP FastCGI端口号,Nginx将连接到该端口号。
  • -u www:PHP FastCGI用户名。
  • -g www:PHP FastCGI组名。
  • -f/usr/local/bin/php-cgi:PHP5 fastcgi二进制文件的路径。

验证php是否以FastCGI运行,执行:

# sockstat -4 | grep 9000

输出示例:

www      php-cgi    8998  0  tcp4   127.0.0.1:9000    *:*
www      php-cgi    8997  0  tcp4   127.0.0.1:9000    *:*
www      php-cgi    8996  0  tcp4   127.0.0.1:9000    *:*
www      php-cgi    8995  0  tcp4   127.0.0.1:9000    *:*
www      php-cgi    8994  0  tcp4   127.0.0.1:9000    *:*
www      php-cgi    8993  0  tcp4   127.0.0.1:9000    *:*

启动/停止PHP FastCGI进程的示例Shell脚本

#!/bin/sh
# NGINX FastCGI php5 startup shell script
# Feedback <[email protected]>
# http://bash.theitroad.local/web-server/fastcgi-php-server-start-stop-script/
# Set ME #
PROVIDES=php-cgi
LIGHTTPD_FCGI=/usr/local/bin/spawn-fcgi 
SERVER_IP=127.0.0.1
SERVER_PORT=9000 
SERVER_USER=www
SERVER_GROUP=www 
PHP_CGI=/usr/local/bin/php-cgi
PGREP=/bin/pgrep
KILLALL=/usr/bin/killall
### No editing below ####
cmd=
 
pcgi_start(){
  	echo "Starting $PROVIDES..."
	$LIGHTTPD_FCGI -a $SERVER_IP -p $SERVER_PORT -u $SERVER_USER -g $SERVER_GROUP -f $PHP_CGI
}
 
pcgi_stop(){
	echo "Killing $PROVIDES..."
	$KILLALL $PROVIDES
}
 
pcgi_restart(){
	pcgi_stop
	pcgi_start
}
 
pcgi_status(){
        $PGREP $PROVIDES > /dev/null
	[ $? -eq 0  ] && echo "$PROVIDES running" || echo "$PROVIDES NOT running" 
 
}
 
pcgi_help(){
  	echo "Usage: 
# cd /tmp

# fetch http://bash.theitroad.local/dl/251.sh.zip

# unzip 251.sh.zip

# mv 251.sh /usr/local/etc/rc.d/php.cgi.sh

# chmod +x /usr/local/etc/rc.d/php.cgi.sh

# rm 251.sh.zip
{start|stop|restart|status}" }     case ${cmd} in [Ss][Tt][Aa][Rr][Tt]) pcgi_start;; [Ss][Tt][Oo][Pp]) pcgi_stop;; [Rr][Ee][Ss][Tt][Aa][Rr][Tt]) pcgi_restart;; [Ss][Tt][Aa][Tt][Uu][Ss]) pcgi_status ;; *) pcgi_help ;; esac

安装以上shell脚本:

# /usr/local/etc/rc.d/php.cgi.sh start

# sockstat -4 | less

要启动PHP FastCGI,请执行:

# /usr/local/etc/rc.d/php.cgi.sh stop

要停止php FastCGI,请执行:

# vi /usr/local/etc/nginx/nginx.conf

将Nginx Web服务器连接到PHP FastCGI进程

打开您的ngixn.conf文件,执行:

location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /websites/example.com/http$fastcgi_script_name;
            include        fastcgi_params;
    }

追加以下配置指令,以便Nginx可以将PHP脚本传递给侦听127.0.0.1:9000的FastCGI服务器:

server {
	listen  80;
        server_name  theitroad.com www.theitroad.com;
 
        access_log  /var/log/nginx/theitroad.com/access.log  main;
 
        location / {
            root   /home/www/theitroad.com/http;
            index  index.php index.html index.htm;
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/local/www/nginx-dist;
        }
 
       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/www/theitroad.com/http$fastcgi_script_name;
            include        fastcgi_params;
        }
 
        location ~ /\.ht {
            deny  all;
        }
}

如果您的网站位于/home/www/theitroad.com/http,则您的配置应如下所示:

# nginx -c /usr/local/etc/nginx/nginx.conf -t

# /usr/local/etc/rc.d/nginx restart

重新启动Nginx Web服务器

<?php
  phpinfo();
?>

使用以下示例脚本测试php:

##代码##