ubuntu10.04 configuration nginx+php-fpm Mode of detailed _php example

Source: Internet
Author: User
Tags fpm php language php script

PPA installation PHP-FPM
Installation Kit

Copy Code code as follows:

$ sudo apt-get install python-software-properties

Add PPA Source
Copy Code code as follows:

$ sudo add-apt-repository ppa:yola/php5

Install PHP5-FPM
Copy Code code as follows:

sudo apt-get update
sudo apt-get install PHP5-FPM

other necessary software installation to connect
Copy Code code as follows:

sudo apt-get install Nginx

Configure PHP-FPM
The PHP-FPM parser is the C/s structure, and its configuration file is located at:
(1)/etc/php5/fpm/php-fpm.conf
(2)/etc/php5/fpm/pool.d/
Generally there is no strict configuration requirements, or this piece I haven't specifically studied the meaning of each configuration parameter
I used the TCP mode to connect with the fastcgi process, so I modified the TCP listening address and port, modified the name of the watch directory, here without a detailed explanation, you can refer to the official documents according to their needs to configure
Restart PHP5-FPM



Configure Nginx
Objective
Nginx itself does not parse the PHP language, which is different from Apache (Apache has mod_php modules in the band for PHP parsing). Nginx fastcgi the client's PHP request to the backend PHP5-FPM process manager. PHP5-FPM has the ability to parse PHP
Nginx Main configuration file
File location:/etc/nginx/nginx.conf, my configuration parameters are as follows:
Copy Code code as follows:

User Www-data;
#主动开启cpu多核功能
Worker_processes 2;
worker_cpu_affinity 01 10;
#指定nginx进程可以打开的最大文件描述符数量
Worker_rlimit_nofile 65535;
Pid/var/run/nginx.pid;
Events {
#使用epoll的I/O model
Use Epoll;
#工作单进程的并发连接数, the total number of concurrent connections = Worker_connections * worker_processes
Worker_connections 2048;
#multi_accept在Nginx接到一个新连接通知后调用accept () to accept as many connections as possible
Multi_accept on;
}
HTTP {
Include/etc/nginx/mime.types;
Default_type Application/octet-stream;
CharSet Utf-8;

Server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
Large_client_header_buffers 4 4k;
#通过nginx上传文件的大小
Client_max_body_size 8m;

# $remote _ADDR: Record IP address; $remote _user: Log remote client user name; $request: requested URL and HTTP protocol; $status: for logging request status; $body _bytes_sent: Used to record the size of the main body content sent to the client file; $http _referer: Jump link; $http _x_forwarded_for: Customer's real IP address
Log_format Main ' $server _name$remote_addr$remote_user[$time _local] "$request"
' $status $body_bytes_sent ' $http _referer '
' $http _user_agent ', ' $http _x_forwarded_for ';
Access_log/var/log/nginx/access.log main;
Error_log/var/log/nginx/error.log;

Sendfile on;
Tcp_nopush on;
#keepalive的超时时间
Keepalive_timeout 60;
Open_file_cache max=204800 inactive=20s;
Open_file_cache_min_uses 1;
Open_file_cache_valid 30s;
Tcp_nodelay on;
gzip on;
include/etc/nginx/conf.d/*.conf;
}

The log formats are delimited by nonprinting symbols, CTRL + V && CTRL + A
nginx Virtual Host configuration file
Copy Code code as follows:

Upstream haolianxi_php {
Server 127.0.0.1:9444;
}
server {
Listen 192.168.1.137:7777;

Access_log/var/log/nginx/haolianxi/haolianxi.access.log main;
Error_log/var/log/nginx/haolianxi/haolianxi.error.log;
#通用匹配
Location/{
root/srv/www/php/;
AutoIndex on;
Autoindex_exact_size off;
Autoindex_localtime on;
Access_log/var/log/nginx/haolianxi/location.default.access.log main;
Error_log/var/log/nginx/haolianxi/location.default.error.log;
Allow 192.168.1.0/24;
Deny all;
}
#正则表达式匹配
#proxy the PHP scripts to PHP-FPM
Location ~ \.php$ {
root/srv/www/php/;
Include/etc/nginx/fastcgi_params;
Fastcgi_pass haolianxi_php; # The upstream determined above
Fastcgi_index index.php;
}
#php-FPM Status Monitor
Location =/phpfpm_status {
Fastcgi_pass 127.0.0.1:9444;
Fastcgi_index index.php;
Include/etc/nginx/fastcgi_params;
Allow 192.168.1.127;
Allow 127.0.0.1;
Deny all;
}
# # Compression
# src:http://www.ruby-forum.com/topic/141251
# Src:http://wiki.brightbox.co.uk/docs:nginx
gzip on;
Gzip_http_version 1.0;
Gzip_comp_level 2;
Gzip_proxied any;
Gzip_min_length 1100;
Gzip_buffers 8k;
Gzip_types text/plain text/css application/x-javascript text/xml application/xml;

# Some version of IE 6 don ' t handle compression down Some mime-types, so just disable for them
Gzip_disable "MSIE [1-6]. (?!. *SV1) ";

# Set a vary header so downstream proxies don ' t send cached gzipped content to IE6
Gzip_vary on;
# #/compression
}

Note:
A parameter setting in Include/etc/nginx/fastcgi_params needs to be modified as follows:
Copy Code code as follows:

Fastcgi_param script_name $document _root$fastcgi_script_name;

Because the name of the script does not add $DOCUMENT_ROOT,PHP5-FPM is unable to find the absolute path of the PHP script that needs to be executed
Restart Nginx
Copy Code code as follows:

Sudo/etc/init.d/nginx restart

Testing the Fastcgi_finish_request () function
Copy Code code as follows:

<?php
echo "OK";
Fastcgi_finish_request (); /* Response complete, close the connection * *
Sleep (5);
File_put_contents ("/tmp/fastcgi.log", "Hello", file_append);
Sleep (5);
File_put_contents ("/tmp/fastcgi.log", "World", File_append);
?>

Description:
With the largest white words, fastcgi_finish_request () can be closed in advance and the client's connection, the need to return the data returned to the client, but after the function of the branch business logic or continue to run in the background!
PHP5-FPM log Split script by day
Copy Code code as follows:

#!/bin/bash-
#1. PHP5-FPM Log Storage Path
Php5_fpm_logs_path= "/var/log/php5-fpm/"
category_array= ("Access" "Error")
#2. PHP5-FPM Log name suffix
postfix= ' date-d '-1 days ' +%y%m%d ' ". Log"
#3. PHP5-FPM Log Cutting
For category in ${category_array[*]}
Todo
If [-e $php 5_fpm_logs_path/php5-fpm. $category. log]
Then
MV $php 5_fpm_logs_path/php5-fpm. $category. log \
$php 5_fpm_logs_path/php5-fpm $category. $postfix
Fi
Done
#4. Find the PHP5-FPM process number so that it generates a new log file
Php5fpm_pid= ' Ps-aux |grep-e ' php-fpm:master process ' |grep-v ' grep ' |awk ' {print $} '
#USR1: Reopen log files, refreshing nginx logfile
KILL-USR1 $php 5fpm_pid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.