免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2889 | 回复: 0
打印 上一主题 下一主题

FreeBSD: Nginx FastCGI PHP Configuration [复制链接]

论坛徽章:
2
丑牛
日期:2013-09-29 09:47:222015七夕节徽章
日期:2015-08-21 11:06:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-04 10:47 |只看该作者 |倒序浏览

Q. How do I configure
PHP
as
FastCGI
under
FreeBSD

Nginx
webserver?
A. You can easily configure php as FastCGI application under Nginx for performance. You need following components:
[a] spawn-fcgi binary - For spawning a FastCGI process. This binary can be installed from Lighttpd webserver project. Nginx does not automatically spawn FCGI processes. You must start them separately using spawn-fcgi.
start.php.sh : A
shell
script to stop / start / restart php fastcgi process. This script swap php process on 127.0.0.1 IP and 9000 port. Once backend is started Nginx can be configured to connect to PHP.
FreeBSD Install PHP5
If php5 is not installed type the following commands (make sure you select FastCGI option):
# cd /usr/ports/lang/php5
# make config
# make install clean


Fig.01 Enable FastCGI support by selecting FastCGI option
Also installed required php extensions such as php-msyql, php-gd and so on:
# cp /usr/local/etc/php.ini-recommended /usr/local/etc/php.ini
# cd /usr/ports/lang/php5-extensions/
# make install clean
Install spawn-fcgi
Grab latest spawn-fcgi from another Lighttpd installation or just install it as follows:
# pkg_add -r -v lighttpd
Sample output:
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
#
http://bash.cyberciti.biz/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=$1

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: $0 {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 ;;
esacInstall above shell script:
# cd /tmp
# fetch
http://bash.cyberciti.biz/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
To start php FastCGI, enter:
# /usr/local/etc/rc.d/php.cgi.sh start
# sockstat -4 | less
To stop php FastCGI, enter:
# /usr/local/etc/rc.d/php.cgi.sh stop
Connect Nginx Webserver To PHP FastCGI Process
Open your ngixn.conf file, enter:
# vi /usr/local/etc/nginx/nginx.conf
Append following config directives so that Nginx can 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  /websites/example.com/http$fastcgi_script_name;
            include        fastcgi_params;
    }If your website is hosted at /home/www/theos.in/http, your config should look like as follows:
server {
listen  80;
        server_name  theos.in
www.theos.in
;

        access_log  /var/log/nginx/theos.in/access.log  main;

        location / {
            root   /home/www/theos.in/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/theos.in/http$fastcgi_script_name;
            include        fastcgi_params;
        }

        location ~ /\.ht {
            deny  all;
        }
}Restart Nginx web server
# nginx -c /usr/local/etc/nginx/nginx.conf -t
# /usr/local/etc/rc.d/nginx restart
Test php with following sample script:


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4206/showart_1359052.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP