安装与配置nginx下的php (PHP-FPM模式)

如何:安装与配置nginx下的php (PHP-FPM模式)

准备工作

使用FreeBSD的Ports安装软件时,请一定先把Ports更新到最新版本

#portsnap fetch update

如果你是第一次使用portsnap,可能需要先执行

#portsnap extract

安装并启用nginx服务

首先,安装nginx:

#cd /usr/ports/www/nginx
#make install clean

在出现的选项中,可能有一些你不熟悉的选项存在,如果你不明白各个选项的含义,可以用下面的默认值:

[X] HTTP_MODULE               Enable HTTP module
[X] HTTP_ADDITION_MODULE      Enable http_addition module
[X] HTTP_CACHE_MODULE         Enable http_cache module
[X] HTTP_DAV_MODULE           Enable http_webdav module
[X] HTTP_FLV_MODULE           Enable http_flv module
[X] HTTP_GEOIP_MODULE         Enable http_geoip module
[X] HTTP_GZIP_STATIC_MODULE   Enable http_gzip_static module
[X] HTTP_IMAGE_FILTER_MODULE  Enable http_image_filter module
[X] HTTP_PERL_MODULE          Enable http_perl module
[X] HTTP_RANDOM_INDEX_MODULE  Enable http_random_index module
[X] HTTP_REALIP_MODULE        Enable http_realip module
[X] HTTP_REWRITE_MODULE       Enable http_rewrite module
[X] HTTP_SECURE_LINK_MODULE   Enable http_secure_link module
[X] HTTP_SSL_MODULE           Enable http_ssl module
[X] HTTP_STATUS_MODULE        Enable http_stub_status module
[X] HTTP_SUB_MODULE           Enable http_sub module
[X] HTTP_XSLT_MODULE          Enable http_xslt module

在命令执行完成后,使用编辑器把下面的内容添加到/etc/rc.conf中

nginx_enable="YES"

安装PHP-FPM及相关连的包

以root身份继续执行下面的操作

#cd /usr/ports/devel/pcre
#make install clean
#cd /usr/ports/devel/libtool
#make install clean
#cd /usr/ports/lang/php5
#make install clean
#cd /usr/ports/lang/php5-extensions
#make install clean

在php5-extensions的config页面中,需要勾选PHP-FPM项。

执行完成后,把下面的代码加入/etc/rc.conf以启用PHP-FPM

php_fpm_enable="YES"

同时还要创建一个php.ini文件:

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

同时可以编辑php.ini,把里面的timezone设置为Asia/Shanghai或者需要使用的时区。

启动服务

#/usr/local/etc/rc.d/php-fpm start
#/usr/local/etc/rc.d/nginx start

这样就成功启动了服务

配置虚拟主机运行PHP程序

准备网站目录

首先建立网站的目录和日志存放目录,日志即可以配置到/var/logs/nginx下面,也可以配置到任何你希望存放的位置。

mkdir /home/saharabear.com/public_html
mkdir /home/saharabear.com/logs

配置nginx.conf

可以直接参考下面的配置文件来修改/usr/local/etc/nginx/nginx.conf

user  nobody;
worker_processes  1;

error_log  logs/error.log;

pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  ‘$remote_addr – $remote_user [$time_local] "$request" ‘
                      ‘$status $body_bytes_sent "$http_referer" ‘
                      ‘"$http_user_agent" "$http_x_forwarded_for"’;

    sendfile        on;

    keepalive_timeout  65;

    gzip  on;

    # We define the virtual host here
    server {
        listen       208.62.123.122:80;
        server_name  saharabear.com www.saharabear.com;

        access_log  /home/saharabear.com/logs/access.log  main;

        location / {
            root   /home/saharabear.com/public_html;
            index  index.html index.htm index.php;
        }
    # Let nginx know how to handle PHP using fastcgi
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /home/saharabear.com/public_html$fastcgi_script_name;
            # 上面这一行很重要,别忘了修改这里
            include        fastcgi_params;
        }

        location ~ /\.ht {
            deny  all;
        }
    }
}

重新启动nginx服务器就可以了

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

配置虚拟主机运行使用Symfony2框架的PHP程序

对于PHP的Symfony2框架,配置nginx可能会有一些不同,比如Symfony2使用app.php和app_dev.php作为前端入口等等,可以参考下面的代码配置Symfony2的PHP程序

    server {
        listen       80;
        server_name  test.saharabear.com ;
        root /home/test.saharabear.com/symfony/web;
        charset utf-8;

        access_log  /home/test.saharabear.com/logs/access.log  main;
        error_log /home/test.saharabear.com/logs/errors.log;
        # strip app.php/ prefix if it is present
        rewrite ^/app\.php/?(.*)$ /$1 permanent;

        location / {
          index app.php;
          try_files $uri @rewriteapp;
        }

        location @rewriteapp {
          rewrite ^(.*)$ /app.php/$1 last;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ ^/(app|app_dev)\.php(/|$) {
          fastcgi_pass   127.0.0.1:9000;
          fastcgi_split_path_info ^(.+\.php)(/.*)$;
          include fastcgi_params;
          fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
          fastcgi_param  HTTPS              off;
        }

        # deny access to .htaccess files, if Apache’s document root
        # concurs with nginx’s one
        #
        location ~ /\.ht {
            deny  all;
        }
    }

原文链接:https://wiki.freebsdchina.org/howto/n/php_php-fpm_nginx

FreeBSD 7.3下Nginx+PHP(php-fpm)配置笔记

FreeBSD 7.3下Nginx+PHP(php-fpm)配置笔记

都说Nginx的性能比Apache NB,找了很多相关的安装资料,终于实现完成了FreeBSD+Nginx+PHP的配置。网上多数Nginx+PHP的配置都是使用的spawn-fcgi,需要额外安装lighttpd或者单独安装spawn-fcgi,其实PHP自带的php-fpm也挺好用的。

一、安装Nginx

# cd /usr/ports/www/nginx
# make install clean

选上自己需要的模块

FreeBSD Nginx安装

二、安装PHP

# cd /usr/ports/lang/php52
# make install clean

记得选择FASTCGI和FPM模块

FreeBSD PHP5安装

然后安装php52-extensions

# cd /usr/ports/lang/php52-extensions
# make install clean

根据需要选择相关的模块

三、配置php-fpm

# vi /usr/local/etc/php-fpm.conf
根据实际环境修改如下参数:

<value name=”listen_address”>127.0.0.1:9000</value>
<value name=”owner”>www</value>
<value name=”group”>www</value>
<value name=”mode”>0666</value>
<value name=”max_children”>5</value>
<value name=”request_slowlog_timeout”>0s</value>
<value name=”max_requests”>500</value>

四、启动php-fpm

# vi /etc/rc.conf

php_fpm_enable=”YES”

# /usr/local/etc/rc.d/php_fpm start

五、配置Nginx

user  www www;
worker_processes  8;

events {
use kqueue;
worker_connections  51200;
}

http {
include       mime.types;
default_type  application/octet-stream;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;

sendfile        on;
tcp_nopush     on;
keepalive_timeout  65;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

gzip  on;
gzip_min_length  1k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;

tcp_nodelay on;

server {
listen      80;
server_name  www.bsdart.org;
root  /data/web/htdocs;
index index.htm index.html index.php;

location ~ .*\.(php|php5)?$ {
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
include        fastcgi.conf;
}

error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
}
}

五、启动Nginx

# vi /etc/rc.conf

nginx_enable=”YES”

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