Ubuntu10.04 detailed description of configuring nginx + php-fpm mode _ php instance

Source: Internet
Author: User
Tags printable characters
This article describes how to configure nginx + php-fpm mode in buntu10.04. For more information, see Install php-fpm using ppa
Installation Kit

The code is as follows:


$ Sudo apt-get install python-software-properties


Add ppa source

The code is as follows:


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


Install php5-fpm

The code is as follows:


Sudo apt-get update
Sudo apt-get install php5-fpm


Install other necessary software

The code is as follows:


Sudo apt-get install nginx


Configure php-fpm
The php-fpm parser is in the C/S structure, and its configuration file is located:
(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 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 for configuration based on your needs.
Restart php5-fpm



Configure nginx
Preface
Nginx itself does not parse the php language, which is different from apache (apache has 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
Nginx main configuration file
File location:/etc/nginx. conf. my configuration parameters are as follows:

The code is 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 the I/O model of epoll
Use epoll;
# Number of concurrent connections of a work order process, total number of concurrent connections = worker_connections * worker_processes
Worker_connections 2048;
# Multi_accept call accept () to accept as many connections as possible after Nginx receives a new connection notification
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 2 k;
Large_client_header_buffers 4 4 k;
# File size uploaded through nginx
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 $ 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 timeout
Keepalive_timeout 60;
Open_file_cache max = 204800 inactive = 20 s;
Open_file_cache_min_uses 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

The code is as follows:


Upstream haolianxi_php {
Server 127.0.0.1: 9444;
}
Server {
Listen 192.168.1.133: 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. log main;
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-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 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:

The code is 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

The code is as follows:


Sudo/etc/init. d/nginx restart


Test the fastcgi_finish_request () function.

The code is as follows:


Echo "OK ";
Fastcgi_finish_request ();/* the response is complete, and the connection is closed */
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

The code is 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 [*]}
Do
If [-e $ php5_fpm_logs_path/php5-fpm. $ category. log]
Then
Mv $ php5_fpm_logs_path/php5-fpm. $ category. log \
$ Php5_fpm_logs_path/php5-fpm. $ category. $ postfix
Fi
Done
#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 nginx log files
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.