如何在FreeBSD中安装配置Nginx FastCGI PHP

时间:2019-11-20 08:53:59  来源:igfitidea点击:

在Nginx 中如何安装配置PHP启用FastCGI支持?

很容易将php配置为Nginx下的FastCGI应用程序。

需要安装下面软件:

  • spawn-fcgi程序 –用于生成FastCGI进程。
  • start.php.sh 用于停止/启动/重新启动php fastcgi进程的shell脚本。

安装php5

在FreeBSD中安装php

# cd /usr/ports/lang/php5
# make config
# make install clean

还安装了必需的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

安装最新的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

现在,您可以使用/usr/local/bin/spawn-fcgi交换进程。

可以将/usr/local/bin/spawn-fcgi复制到/root或其他目录,
然后执行以下命令删除lighttpd软件包:

# 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脚本

php.cgi.sh

#!/bin/sh
# NGINX FastCGI php5 startup shell 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: 
# chmod +x php.cgi.sh
# php.cgi.sh start
# sockstat -4 | less
{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

启动PHP FastCGI:

# /php.cgi.sh stop

要停止php FastCGI,请执行:

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

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

打开nginx配置文件:

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;
    }

添加下面配置,将PHP脚本传递给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;
        }
}

站点配置示例:

# nginx -c /usr/local/etc/nginx/nginx.conf -t
# /usr/local/etc/rc.d/nginx restart

重启Nginx Web服务器

<?php
  phpinfo();
?>

使用以下代码测试php:

test.php

##代码##