Detailed explanation of ubuntu10.04 configuration nginx+php-fpm mode_PHP tutorial

WBOY
Release: 2016-07-21 15:09:37
Original
881 people have browsed it

ppa install php-fpm
Install toolkit

Copy the code The code is as follows:

$ sudo apt-get install python-software-properties

Add ppa source
Copy code The code is as follows:

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

Install php5-fpm
Copy code The code is as follows:

sudo apt-get update
sudo apt-get install php5-fpm

Other necessary software installation instructions
Copy code The code is as follows:

sudo apt-get install nginx

Configuring php-fpm
php-fpm’s parser is a C/S structure, and its configuration file is located at:
(1)/etc/php5/fpm/php-fpm.conf
(2)/etc/php5/fpm/pool.d/
There are generally no strict configuration requirements, or Speaking of which, I haven’t studied the meaning of each configuration parameter in detail
I used tcp mode to connect to the fastcgi process, so I modified the address and port of tcp listening, and modified the name of the monitoring directory, which is not included here. It is explained in detail. You can refer to the official documentation to configure according to your own needs
Restart php5-fpm



Configure nginx
Foreword
nginx itself does not parse the PHP language, which is different from apache (apache has the mod_php module for PHP parsing). nginx passes the client's PHP request to the background through fastcgi The php5-fpm process manager, php5-fpm has the function of parsing php
nginx’s main configuration file
File location: /etc/nginx/nginx.conf, my configuration parameters are as follows :
Copy code The code is as follows:

user www-data;
#Activately enable the cpu multi-core function
worker_processes 2;
worker_cpu_affinity 01 10;
#Specify the maximum number of file descriptors that the nginx process can open
worker_rlimit_nofile 65535;
pid /var/ run/nginx.pid;
events {
#Use epoll’s I/O model
use epoll;
#The number of concurrent connections of the work order process, overall Number of concurrent connections = worker_connections * worker_processes
worker_connections 2048;
#multi_accept calls 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;

server_names_hash_bucket_size 128 ;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
#The size of the file uploaded through nginx
client_max_body_size 8m;

#$remote_addr: record ip address; $remote_user: records the remote client user name; $request: requested url and http protocol; $status: used to record the request status; $body_bytes_sent: used to record the size of the body content of the file sent to the client; $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 timeout
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;
}

Log formats are separated by non-printable symbols, ctrl+v && ctrl+a
nginx virtual host configuration file
Copy Code The code is 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;
# Universal 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 8k;
gzip_types text/plain text /css application/x-javascript text/xml application/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:
include One parameter setting in /etc/nginx/fastcgi_params is required Modify as follows:
Copy code The code is as follows:

fastcgi_param SCRIPT_NAME $document_root$fastcgi_script_name;

Because $document_root is not added to the name of the script, php5-fpm cannot find the absolute path of the php script that needs to be executed
Restart nginx
Copy code The code is as follows:

sudo /etc/init.d/nginx restart

Test the fastcgi_finish_request() function
Copy code The code is as follows:

echo "OK";
fastcgi_finish_request(); /* Response completed, close connection*/
sleep(5);
file_put_contents("/tmp/fastcgi.log", "hello",FILE_APPEND);
sleep(5);
file_put_contents("/tmp/fastcgi.log", "world",FILE_APPEND);
?>

Explanation:
In the largest vernacular, fastcgi_finish_request() can advance Close the connection with the client and return the data that needs to be returned to the client, but the branch business logic after the function continues to run in the background!
php5-fpm log splitting script by day
Copy code 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 and let it 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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327268.htmlTechArticleppa installation php-fpm installation tool package copy code code is as follows: $ sudo apt-get install python-software-properties Add the ppa source copy code as follows: $ sudo add-apt-repository ppa:...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template