码迷,mamicode.com
首页 > Web开发 > 详细

first nginx & php

时间:2015-01-01 00:08:24      阅读:389      评论:0      收藏:0      [点我收藏+]

标签:

 

env

kernel:3.10.0-123.el7.x86_64

nginx version: nginx/1.4.7   ./configure --prefix=/usr/local/nginx/ --with-pcre=/usr/local/pcre-7.9/(source code path) --with-zlib=/usr/local/zlib-1.2.8/(source code path)

php version :5.5.20 auto enable fastcgi    ./configure --prefix=/usr/local/php --enable-debug --enable-fpm

config

技术分享
 1 #user  nobody;
 2 worker_processes  1;
 3 
 4 #error_log  logs/error.log;
 5 #error_log  logs/error.log  notice;
 6 #error_log  logs/error.log  info;
 7 
 8 #pid        logs/nginx.pid;
 9 
10 
11 events {
12     worker_connections  1024;
13 }
14 
15 
16 http {
17     include       mime.types;
18     default_type  application/octet-stream;
19 
20     #log_format  main  $remote_addr - $remote_user [$time_local] "$request" 
21     #                  $status $body_bytes_sent "$http_referer" 
22     #                  "$http_user_agent" "$http_x_forwarded_for";
23 
24     #access_log  logs/access.log  main;
25 
26     sendfile        on;
27     #tcp_nopush     on;
28 
29     #keepalive_timeout  0;
30     keepalive_timeout  65;
31 
32     #gzip  on;
33 
34     server {
35         listen       80;
36         server_name  localhost;
37 
38         #charset koi8-r;
39 
40         #access_log  logs/host.access.log  main;
41 
42         location / {
43             root   html;
44             index  index.php index.html index.htm;
45         }
46 
47         #error_page  404              /404.html;
48 
49         # redirect server error pages to the static page /50x.html
50         #
51         error_page   500 502 503 504  /50x.html;
52         location = /50x.html {
53             root   html;
54         }
55 
56         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
57         #
58         #location ~ \.php$ {
59         #    proxy_pass   http://127.0.0.1;
60         #}
61 
62         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
63         #
64         location ~ \.php$ {
65             root           /usr/local/nginx/html;
66             fastcgi_pass   127.0.0.1:9000;
67             fastcgi_index  index.php;
68             include        fastcgi.conf;
69         }
70 
71         # deny access to .htaccess files, if Apaches document root
72         # concurs with nginxs one
73         #
74         #location ~ /\.ht {
75         #    deny  all;
76         #}
77     }
78 }
nginx.conf

/usr/local/php/sbin/php-fpm

/usr/local/nginx/sbin/nginx 

sysctl stop firewalld.service
systemctl stop firewalld.service

result

 

[root@localhost conf]# curl 127.0.0.1 | head
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 56014 0 56014 0 0 9.8M 0 --:--:-- --:--:-- --:--:-- 10.6M
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
body {background-color: #ffffff; color: #000000;}
body, td, th, h1, h2 {font-family: sans-serif;}
pre {margin: 0px; font-family: monospace;}
a:link {color: #000099; text-decoration: none; background-color: #ffffff;}
a:hover {text-decoration: underline;}
table {border-collapse: collapse;}
.center {text-align: center;}

 

 

 

from php officail document

Unix 系统下的 Nginx 1.4.x

本文档包括使用 PHP-FPM 为 Nginx 1.4.x HTTP 服务器安装和配置 PHP 的说明和提示。

本指南假定您已经从源代码成功构建 Nginx,并且其二进制文件和配置文件都位于 /usr/local/nginx。 如果您使用其他方式获取的 Nginx,请参考» Nginx Wiki 并对照本文档完成安装。

本文档仅包含 Nginx 服务器的基本配置,它将通过 80 端口提供 PHP 应用的处理能力。 如果您需要超出本文档范围的安装配置指导,建议您查阅 Nginx 和 PHP-FPM 的文档。

需要注意的是,本文档一律使用 ‘x‘ 来表示版本号,请根据实际情况将 ‘x‘ 替换为对应的版本号。

  1. 建议您访问 Nginx Wiki » 安装 页面以获取并在您的系统上安装 Nginx。

  2. 获取并解压 PHP 源代码:

    tar zxf php-x.x.x
    
  3. 配置并构建 PHP。在此步骤您可以使用很多选项自定义 PHP,例如启用某些扩展等。 运行 ./configure --help 命令来获得完整的可用选项清单。 在本示例中,我们仅进行包含 PHP-FPM 和 MySQL 支持的简单配置。

    cd ../php-x.x.x
    ./configure --enable-fpm --with-mysql
    make
    sudo make install
    
  4. 创建配置文件,并将其复制到正确的位置。

    cp php.ini-development /usr/local/php/php.ini
    cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
    cp sapi/fpm/php-fpm /usr/local/bin
    
  5. 需要着重提醒的是,如果文件不存在,则阻止 Nginx 将请求发送到后端的 PHP-FPM 模块, 以避免遭受恶意脚本注入的攻击。

    将 php.ini 文件中的配置项 cgi.fix_pathinfo 设置为 0 。

    打开 php.ini:

    vim /usr/local/php/php.ini
    

    定位到 cgi.fix_pathinfo= 并将其修改为如下所示:

    cgi.fix_pathinfo=0
    
  6. 在启动服务之前,需要修改 php-fpm.conf 配置文件,确保 php-fpm 模块使用 www-data 用户和 www-data 用户组的身份运行。

    vim /usr/local/etc/php-fpm.conf
    

    找到以下内容并修改:

    ; Unix user/group of processes
    ; Note: The user is mandatory. If the group is not set, the default user‘s group
    ;       will be used.
    user = www-data
    group = www-data
    

    然后启动 php-fpm 服务:

    /usr/local/bin/php-fpm
    

    本文档未涵盖对 php-fpm 进行进一步配置的信息,如果您需要更多信息,请查阅相关文档。

  7. 配置 Nginx 使其支持 PHP 应用:

    vim /usr/local/nginx/conf/nginx.conf
    

    修改默认的 location 块,使其支持 .php 文件:

    location / {
        root   html;
        index  index.php index.html index.htm;
    }

    下一步配置来保证对于 .php 文件的请求将被传送到后端的 PHP-FPM 模块, 取消默认的 PHP 配置块的注释,并修改为下面的内容:

    #need add root
    location ~* \.php$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }

    重启 Nginx。

    sudo /usr/local/nginx/sbin/nginx -s stop
    sudo /usr/local/nginx/sbin/nginx
    
  8. 创建测试文件。

    rm /usr/local/nginx/html/index.html
    echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php


first nginx & php

标签:

原文地址:http://www.cnblogs.com/johvoo/p/4196751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!