Configuration file of Nginx and PHP-FPM in LNMP environment

Source: Internet
Author: User
Tags epoll ranges sendfile dedicated server

One, nginx configuration file

User nobody nobody;worker_processes 2;error_log /usr/local/nginx/logs/nginx_error.log  crit;pid /usr/local/nginx/logs/nginx.pid;worker_rlimit_nofile 51200;events{     use epoll;    worker_connections 6000;} http{    include mime.types;    default_type application/ Octet-stream;    server_names_hash_bucket_size 3526;    server_ names_hash_max_size 4096;    log_format combined_realip  ' $remote _addr  $http _x_forwarded_for [$time _local] '      ' $host   ' $request _uri '   $status '      "$http _referer"   "$http _user_agent" ';    sendfile on;     tcp_nopush on;    tcp_nodelay on;     keepalive_timeout 30;    client_header_timeout 3m;    client_body_timeout 3m;    send_timeout  3m;    connection_pool_size 256;    client_header_buffer_ size 1k;    large_client_header_buffers 8 4k;     request_pool_size 4k;    output_buffers 4 32k;     Postpone_output 1460;    client_max_body_size 10m;    client _body_buffer_size 256k;    client_body_temp_path /usr/local/nginx/client_body_ Temp;    proxy_temp_path /usr/local/nginx/proxy_temp;    fastcgi_ temp_path /usr/local/nginx/fastcgi_temp;    fastcgi_intercept_errors on;     gzip on;    gzip_min_length 1k;    gzip_ buffers 4 8k;    gzip_comp_level 5;    gzip_http_version 1.1;     gzip_types text/plain application/x-javascript text/css text/htm application/ xml;    include  vhosts/*.conf;  }

Configuration explanation

# specify NGINX worker process run user and user group

User nobody nobody;


# Specify the number of processes to open for Nginx, set to the total number of cores of the CPU

Worker_processes 2;


# Specify Nginx Global error log path and level, log levels are: Debug, info, notice, warn, error, crit

# where debug output log is most detailed, crit input log is the least;

Error_log/usr/local/nginx/logs/nginx_error.log Crit;


# Specify the location of the file where the process ID is stored

Pid/usr/local/nginx/logs/nginx.pid;


# a Nginx process opens the maximum number of file descriptors, the theoretical value should be the maximum number of open files (System value ulimit-n) and the number of nginx processes, but the Nginx allocation request is not uniform, so the recommendation is consistent with the value of ulimit-n.

# Ulimit-n Viewing system limits

Worker_rlimit_nofile 51200;


# set Nginx operating mode and maximum number of connections

Events

# Specifies a working mode of Epoll, with SELECT, poll, Kqueue, Epoll, Rtsig, and/dev/poll working modes,

# where select and poll are standard operating modes, kqueue and qpoll are efficient working modes, epoll models are high-performance network I/O models in Linux 2.6 and above, and Kqueue models if running on FreeBSD.

Use Epoll;

# Maximum number of connections per process (max connections = number of connections * Number of processes)

Worker_connections 6000;

# file name extension with file type mapping table

Include Mime.types;

# Default file type is binary stream

Default_type Application/octet-stream;

# hash table size of server name

Server_names_hash_bucket_size 3526;

# Maximum number of hash tables for server names

Server_names_hash_max_size 4096;

# Specify the output format of the Nginx log, where Combined_realip is the custom log name

Log_format combined_realip ' $remote _addr $http _x_forwarded_for [$time _local] "$host" $request _uri "$status" "$http _ Referer "" $http _user_agent ";

# Open the efficient file transfer mode, the sendfile instruction specifies whether Nginx calls the Sendfile function to output the file, for the normal application is set to ON, if used for downloading and other applications such as disk IO heavy load application, can be set to off to balance disk and network I/O processing speed, Reduce the load on the system. Note: If the picture does not appear normal, change this to off.

Sendfile on;


# to prevent network congestion

Tcp_nopush on;

Tcp_nodelay on;

# Long connection time-out, units in seconds

Keepalive_timeout 65;

# Set client request header read time-out

Client_header_timeout 10;

# Set client Request topic 2 Time Out

Client_body_timeout 10;

# Specify the time-out for responding to clients

Send_timeout 10;

# The pool of memory allocated for each request, the memory pool is used for small quota memory blocks, if a block is larger than the memory pool or larger than the paging size, then it will be allocated outside the memory pool, if the smaller allocation in the memory pool does not have enough memory, then a new block of the same memory pool size will be allocated , this command has only a fairly limited effect.

Connection_pool_size 256;

Request_pool_size 4k;

# Specify the size from the client request header

Client_header_buffer_size 1k;

# Specify maximum cache maximum and size for large request headers in a client request

Large_client_header_buffers 8 4k;

# Output Cache Size

Output_buffers 4 32k;

Postpone_output 1460;

# directive specifies the maximum request principal size allowed for client connections

Client_max_body_size 10m;

# This instruction can specify the buffer size of the connection request body.

Client_body_buffer_size 256k;

# Specifies the temporary file path to which the connection request principal is attempting to write

Client_body_temp_path/usr/local/nginx/client_body_temp;

# Reverse proxy Temporary storage directory

Proxy_temp_path/usr/local/nginx/proxy_temp;


FASTCGI related parameters are designed to improve the performance of the site: Reduce resource usage and improve access speed.

# Specify a path for the Nginx configuration fastcgi cache

Fastcgi_temp_path/usr/local/nginx/fastcgi_temp;

# If this option is not set, even if 404.html is created and Error_page is configured, there is no effect

Fastcgi_intercept_errors on;


# Enable compression

gzip on;

# Minimum Compressed file size

Gzip_min_length 1k;

# compression Buffers

Gzip_buffers 4 16k;

# compression level

Gzip_comp_level 5;

# compressed version (default 1.1, front End If it is squid2.5 please use 1.0)

Gzip_http_version 1.1;

# Types to compress

Gzip_types text/plain application/x-javascript text/css text/htm application/xml;

# Open the Virtual configuration directory

Include vhosts/*.conf;


Verify Nginx Default virtual Host

Create a new vhosts directory under the/usr/local/nginx/conf directory and a default.conf configuration file;

[Email protected] blog]# mkdir/usr/local/nginx/conf/vhosts[[email protected] blog]# cd/usr/local/nginx/conf/vhosts[    [Email protected] vhosts]# VI default.confserver{listen default_server;    server_name localhost;    Index index.html index.htm index.php; root/usr/local/nginx/html;}

Listen 80 is not added by default, and the following defaults and Default_server are available; experimental test is successful;

After saving exits,-t checks that the configuration file is correct, then restarts Nginx, and tests for success with the Curl command. or in the browser input 192.168.20.30 display Nginx Welcome page to indicate success;

[Email protected] vhosts]#/usr/local/nginx/sbin/nginx-t[[email protected] vhosts]#/etc/init.d/nginx restart
[Email protected] vhosts]# Curl 192.168.20.30-ihttp/1.1 okserver:nginx/1.6.2date:thu, may 06:28:36 Gmtcont Ent-type:text/htmlcontent-length:612last-modified:mon, 09:27:11 Gmtconnection:keep-aliveetag: " 555075ef-264 "Accept-ranges:bytes
[Email protected] vhosts]# Curl 192.168.20.30/index.html-ihttp/1.1 okserver:nginx/1.6.2date:wed, 2015 09:15 : Wuyi Gmtcontent-type:text/htmlcontent-length:612last-modified:mon, 09:27:11 Gmtconnection:keep-aliveetag: "555075ef-264" accept-ranges:bytes


Second, php-fpm.conf configuration file

Vi/usr/local/php/etc/php-fpm.conf[global]pid =/usr/local/php/var/run/php-fpm.piderror_log =/usr/local/php/var/  Log/php-fpm.log[www]listen =/tmp/php-fcgi.sockuser = Php-fpmgroup = Php-fpmlisten.owner = Nobody listen.group = nobodypm = Dynamicpm.max_children = 50pm.start_servers = 20pm.min_spare_servers = 5pm.max_spare_servers = 35pm.max_requests = 500r Limit_files = 1024x768 Slowlog =/usr/local/php/var/log/slow.logrequest_slowlog_timeout = 1php_admin_value[open_basedir]= /data/www/:/tmp/

Configuration explanation:

[Global]: Globally configured

PID: Specify the Process ID file

Error_log: specifying error log files

[www]: Specify the name of pool resource pools

Listen: The specified listening mode is consistent with the Nginx configuration; ip+ port or sock file;

User: Users who started the process

Group: User groups that start a process


Dynamic, static sub-process PM = static/dynamic

If Static is selected, the number of fixed child processes is specified by Pm.max_children.

If dynamic is selected, it is determined by the following parameters:

Pm.max_children, maximum number of child processes

Pm.start_servers, number of processes at startup

Pm.min_spare_servers, the minimum number of idle processes is guaranteed, and if the idle process is less than this value, a new child process is created

Pm.max_spare_servers, the maximum number of idle processes is guaranteed, and if the idle process is greater than this value, the cleanup

For a dedicated server, the PM can be set to static.

Slow execution logs are used for performance tracking, where there is a problem locating the PHP script.

Each pool can be written in a single slow log; The log path can be customized;

Slowlog = /usr/local/php/var/log/slow.log

Request_slowlog_timeout = 1 #慢日志的超时时间;


Open_basedir format, the directory needs to be customized;

php_admin_value[open_basedir]=/data/www/:/tmp/


If listen use ip+ port communication, do not need to specify Listen.owner;

The default listen.owner is php-fpm; If you do not change the configuration file, you do not have permission to execute/tmp/php-fcgi.sock this file , so 502 errors will be reported;

Php-fcgi.sock files are created by the PHP-FPM process, restart the PHP-FPM service, appear under the TMP directory, default permissions are 660, other users do not have execute permissions.

Manually change the sock file permission to 666, restart the PHP-FPM service, will become 660, other users do not have permission to execute ;

[[EMAIL PROTECTED] ETC]# LS -L /TMP/SRW-RW----   1 php-fpm php-fpm    0 5 Month   14 15:53  php-fcgi.sock[[email protected] etc]# chmod 666 /tmp/php-fcgi.sock [[email  protected] etc]# ls -l /tmp/srw-rw-rw-  1 php-fpm php-fpm     0 5 Month   14 15:53 php-fcgi.sock[[email protected] etc]# /etc/ init.d/php-fpm restartgracefully shutting down php-fpm . donestarting  PHP-FPM  DONE[[EMAIL PROTECTED] ETC]# LS -L /TMP/SRW-RW----  1  php-fpm php-fpm    0 5 month   14 15:57 php-fcgi.sock 

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6C/EA/wKioL1VW-8ai4B5CAAIfsL7V6kA040.jpg "title=" Sock.jpg "alt=" Wkiol1vw-8ai4b5caaifsl7v6ka040.jpg "/>


Experiment test, use sock file communication, nginx default virtual host configuration to add the following content:

[email protected] vhosts]# cat default.conf server{Listen default;    server_name localhost;    Index index.html index.htm index.php;        root/usr/local/nginx/html;    Location ~ \.php$ {include fastcgi_params;    Fastcgi_pass Unix:/tmp/php-fcgi.sock;    Fastcgi_index index.php;    Fastcgi_param Script_filename/usr/local/nginx/html$fastcgi_script_name; }}

Configuration explanation

Server: Define keywords for virtual host start

Listen: Specify the service port for the virtual host

SERVER_NAME: Specify an IP address or domain name, separated by a space between multiple domain names

Index: Set default home address for access

Root: Specifies the Web root directory of the virtual host

CharSet: Set the default encoding format for Web pages

Include Fastcgi_params: Opening fastcgi

Fastcgi_pass: Specify fastcgi Listening mode: 1, sock mode monitoring, 2, TCP/IP mode monitoring

Fastcgi_index: Specify fastcgi default Start Page

Fastcgi_param script_filename: Setting the directory for fastcgi listening


Use Curl to test, return to OK, indicating sock file communication success;

[Email protected] log]# curl-x127.0.0.1:80 192.168.20.30/phpinfo.php-ihttp/1.1 Okserver:nginx/1.6.2date:thu, M Ay 08:25:03 gmtcontent-type:text/htmlconnection:keep-alivex-powered-by:php/5.4.37



This article is from the "Model Student's Learning blog" blog, please be sure to keep this source http://8802265.blog.51cto.com/8792265/1651899

Configuration file of Nginx and PHP-FPM in LNMP environment

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.