Ubuntu10.04 configure nginx + PHP-FPM Mode

Source: Internet
Author: User
Tags epoll php language printable characters
Install PHP-FPM using PPA

Installation kit
$ sudo apt-get install python-software-properties 

Add PPA Source

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

Install php5-fpm

sudo  apt-get  updatesudo  apt-get install  php5-fpm

Install other necessary software

sudo   apt-get   install   nginx

Configure PHP-FPM

Php-FPM parser is a C/S structure, its configuration file is located in :( 1)/etc/PhP5/FPM/php-fpm.conf (2)/etc/PhP5/FPM/pool. d/Generally, there are no strict Configuration Requirements, or I have not specifically studied the significance of each configuration parameter. I have used the TCP mode to connect to the FastCGI process, so I modified the address and port of the TCP listener and the name of the monitoring directory. I will not explain it in detail here, you can refer to the official documentation according to their own needs to configure restart php5-fpm to configure nginx

Preface nginx does not parse the PHP language, which is different from Apache (APACHE performs PHP parsing in the mod_php module ). nginx is through FastCGI client PHP requests to the background php5-fpm Process Manager, php5-fpm has the ability to parse PHP, for details, refer to my previous blog post mod_php to compare the location of the main configuration file of mod_fastcging.pdf:/etc/nginx. conf. My configuration parameters are as follows:
User www-data; # enable the CPU multi-core function worker_processes 2; worker_cpu_affinity 01 10; # specify the maximum number of file descriptors that can be opened by the nginx process worker_rlimit_nofile 65535; PID/var/run/nginx. PID; events {# Use epoll I/O model of epoll; # number of concurrent connections of work order processes. Total number of concurrent connections = worker_connections * worker_processesworker_connections 2048; # multi_accept call accept () after nginx receives a new connection notification to accept as many connections as possible multi_accept on;} HTTP {include/etc/nginx/mime. types; default_type application/octet-stream; charset UTF-8; bytes 128; client_header_buffer_size 2 K; bytes 4 4 K; # client_max_body_size 8 m; # $ remote_addr: record IP address; $ remote_user: Record remote client user name; $ request: request URL and HTTP protocol; $ status: used to record request status; $ body_bytes_sent: used to record the size of the content sent to the client file body; $ http_referer: Jump link; $ http_x_forwarded_for: the customer's real IP address log_format main' $ SERVER_NAME $ remote_addr $ remote_user [$ time_local] "$ request" ''$ Status $ response" $ 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 timeout value: keepalive_timeout 60; open_file_cache max = 204800 inactive = 20 s; timeout 1; open_file_cache_valid 30 s; tcp_nodelay on; gzip on; include/etc/nginx/CONF. d /*. conf ;}

Log formats are separated by non-printable characters. CTRL + V & Ctrl +

Nginx Virtual Host Configuration File
Upstream haolianxi_php {server 127.0.0.1: 9444;} server {listen192.168.1.20.: 7777; access_log/var/log/nginx/haolianxi. access. log main; error_log/var/log/nginx/haolianxi. error. log; # General match location/{root/srv/www/PHP/; Autoindex on; autoindex_exact_size off; autoindex_localtime on; access_log/var/log/nginx/haolianxi/location. default. access. logmain; error_log/var/log/nginx/haolianxi/location. Default. Error. log; allow 192.168.1.0/24; deny all ;}# Regular Expression matching # proxy the PHP scripts to PHP-fpmlocation ~ \. PHP $ {root/srv/www/PHP/; Include/etc/nginx/fastcgi_params; fastcgi_passhaolianxi_php; # The upstream determined abovefastcgi_indexindex.php ;} # PHP-FPM status monitorlocation =/phpfpm_status {fastcgi_pass 127.0.0.1: 9444; fastcgi_index index. PHP; Include/etc/nginx/fastcgi_params; allow192.168.1.127; allow127.0.0.1; deny all ;## compression # SRC: http://www.ruby-forum.com/topic/141251# SRC: http://wiki.br Ightbox. co. UK/docs: nginx gzip on; gzip_http_version 1.0; gzip_comp_level 2; gzip_proxied any; gzip_min_length 1100; gzip_buffers 16 8 K; gzip_types text/plain text/CSS application/X-JavaScript text/XML application/XML + RSS text/JavaScript; # Some version of IE 6 don't handle compression well on 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:Modify the parameter settings in include/etc/nginx/fastcgi_params as follows:

fastcgi_param   SCRIPT_NAME             $document_root$fastcgi_script_name;

Because the script name does not add $ document_root, The php5-fpm cannot find the absolute path of the PHP script to be executed restart nginx

sudo  /etc/init.d/nginx  restart

Test the fastcgi_finish_request () function.

<? Phpecho "OK"; fastcgi_finish_request ();/* response complete, close connection */sleep (5); file_put_contents ("/tmp/FastCGI. log "," hello ", file_append); sleep (5); file_put_contents ("/tmp/FastCGI. log "," world ", file_append);?>

Note: fastcgi_finish_request () can be used to close the connection with the client in advance and return the data to the client. However, the branch business logic after the function continues to run in the background!

Php5-fpm logs split by day script

#! /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 [*]} doif [-e $ php5_fpm_logs_path/php5-fpm. $ category. log] thenmv $ php5_fpm_logs_path/php5-fpm. $ category. log \ $ php5_fpm_logs_path/php5-fpm. $ category. $ postfixfidone #4. find the php5-fpm process number to generate a new log file php5fpm_pid = 'ps-Aux | grep-e 'PHP-FPM: master Process '| grep-V 'grep' | awk' {print $2} ''# usr1: reopen log files, refresh the nginx log file kill-usr1 $ php5fpm_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.