Basic tutorials for configuring Nginx servers for PHP programs under the Debian system _nginx

Source: Internet
Author: User
Tags epoll fpm gpg install php php script socket wrapper

Nginx is now very popular, so it is intended to replace the existing Apache first, in terms of bulk deployment and management considerations. Because all the modules of Nginx are statically compiled, unlike Apache, which can dynamically add modules, it is important to consider the need at the outset, combining the current fire lamp pattern, where p is most commonly PHP, At present, the better FastCGI manager has spaw-fcgi and PHP-FPM, and the old version of PHP is the need to install the source code through a patch of the way to PHP-FPM, spaw-fcgi lighttpd is the default fastcgi manager, Because of the source installation, the current version of the package is not easy to deploy on a large scale, but there are solutions.

Nginx Installation Method:

1 Apt Network installation

Modify Source List

Vi/etc/apt/sources.list
#加入以下源
deb http://nginx.org/packages/debian/squeeze nginx
deb-src http:// Nginx.org/packages/debian/squeeze Nginx

Update source list and install Nginx via apt

Apt-key add nginx_signing.key
apt-get update
apt-get install Nginx

2 Source Code compilation installation
Install the compilation environment first, because Nginx in later use will need to use Perl regular, compression algorithm, SSL and so on, so we need to install the relevant library files in advance.

Apt-get Install build-essential 
apt-get install libpcre3 libpcre3-dev zlib1g zlib1g-dev OpenSSL Libssl-dev libssl0.9.8

Download the latest stable version Nginx

wget http://nginx.org/download/nginx-1.2.3.tar.gz
Unpack View compilation options

The tar zxvf nginx-1.2.3.tar.gz
cd nginx-1.2.3
#--help can see the parameters that can be configured
./configure--help

View compilation of optional configuration parameters (these are just some common configuration items):


The default installation path for the--prefix=path Nginx is not specified. Default is/usr/local/nginx

--sbin-path=path Nginx The path to the executable command file without specifying the default <prefix>/sbin/nginx

--conf-path=path nginx configuration file path, not specified, default to <prefix>/conf/nginx.conf

--error-log-path=path the error log path is not specified using Error_log in nginx.conf, the default <prefix>/logs/error.log

--http-log-path=path defines the log store path of the file being accessed, if Access_log is not specified in nginx.conf, the default is <prefix>/logs/access.log

--pid-path=path when the PID file path is not specified in nginx.conf using PID, the default is <prefix>/logs/nginx.pid

The--lock-path=path lock file holds the route, and if not specified in nginx.conf, the default is <prefix>/logs/nginx.lock

--user=user The default is nobody if the owner is not running with user-defined processes in nginx.conf

--group=group If you do not use user-defined group in nginx.conf to run the process, the default is nobody

--builddir=dir Set Build Directory

--with-rtsig_module Use Rtsig mode

--with-select_module uses select mode, and if the current platform has no other valid mode, the default compilation

--without-select_module Ban Select mode

--with-poll_module using poll mode, if the current platform does not have other valid modes, the default compilation

--without-poll_module Prohibited Poll mode

--with-file-aio Enable file AIO support

--with-ipv6 Open IPV6 Support

--with-debug Turn on Debug

The following modules are not turned on by default, and you can use the--with command to open the following modules

--with-http_ssl_module Open SSL Module

--with-http_realip_module can record client IP at back end

--with-http_addition_module Enable Ngx_http_addition_module

--with-http_flv_module Open FLV Module

--with-http_mp4_module Open MP4 Module

--with-http_gzip_static_module Open gzip Module

--with-http_secure_link_module Enable Ngx_http_secure_link_module

--with-http_stub_status_module Open Status View module

The following modules are turned on by default and can be turned off by using the--without option

--without-http_charset_module Disable Ngx_http_charset_module

--without-http_gzip_module Disable gzip compression module

--without-http_ssi_module Disable SSL module

--without-http_userid_module Disable Ngx_http_userid_module

--without-http_access_module Access Module disabled

--without-http_auth_basic_module Disable Auth_basic authentication module

--without-http_autoindex_module Disable Column directory module

--without-http_geo_module Disable GEO Module

--without-http_map_module Disable Map module

--without-http_referer_module Disable Ngx_http_referer_module

--without-http_rewrite_module Disable Redirection module

--without-http_proxy_module Disable Agent Module

--without-http_fastcgi_module Disable FASTCGI module

--without-http_uwsgi_module Disable Ngx_http_uwsgi_module

--without-http_scgi_module Disable Ngx_http_scgi_module

--without-http_memcached_module Disable Ngx_http_memcached_module

--without-http_limit_conn_module Disable Ngx_http_limit_conn_module

--without-http_limit_req_module Disable Ngx_http_limit_req_module

--without-http_empty_gif_module Disable Ngx_http_empty_gif_module

--without-http_browser_module Disable Ngx_http_browser_module

--without-http_upstream_ip_hash_module Disable Upstream module

--with-http_perl_module Open Perl module
--with-perl_modules_path=path Set Perl module path
--with-perl=path set the path for the Perl library
--http-client-body-temp-path=path set path to store HTTP client request body temporary files
--http-proxy-temp-path=path set path to store HTTP proxy temporary files
--http-fastcgi-temp-path=path set path to store HTTP fastcgi temporary files
--http-uwsgi-temp-path=path set path to store HTTP Uwsgi temporary files
--http-scgi-temp-path=path set path to store HTTP scgi temporary files
--without-http Disable HTTP Service
--without-http-cache Disable HTTP Cache
--with-mail Open Mail Service
--with-mail_ssl_module to open SSL in mail Service
--without-mail_pop3_module Disable Ngx_mail_pop3_module
--without-mail_imap_module Disable Ngx_mail_imap_module
--without-mail_smtp_module Disable Ngx_mail_smtp_module
--with-google_perftools_module Open Google_perftools Module
--with-cpp_test_module Open Cpp_test Module
--add-module=path Enable an external module
--without-pcre prohibit the use of Perl regular libraries
--with-pcre forced to use Perl regular libraries

Compiling the installation

./configure--prefix=/etc/nginx/--user=nginx--group=nginx--with-http_ssl_module--with-http_realip_module- With-http_addition_module 
--with-http_sub_module--with-http_dav_module--with-http_flv_module--with-http_ Mp4_module--with-http_gzip_static_module 
--with-http_random_index_module--with-http_secure_link_module-- With-http_stub_status_module--with-file-aio--with-ipv6

The results are shown below:

Compiling and installing

Make && make install

PS:FASTCGI Review
FastCGI is a scalable, high-speed interface for communicating between HTTP server and dynamic scripting languages. Most popular HTTP servers support FASTCGI, including Apache, Nginx, and lighttpd, and fastcgi are supported by a number of scripting languages, including PHP. FastCGI is from the CGI development improvement. The main disadvantage of the traditional CGI interface approach is poor performance, because every time the HTTP server encounters a dynamic program, it needs to restart the script parser to perform the parsing, and the result is returned to the HTTP server. This is almost not available when handling high concurrent access. In addition, the traditional CGI interface is also very poor security, it is now rarely used. The FastCGI interface adopts the C/s structure, which separates the HTTP server from the script resolution server, and initiates one or more script resolution Daemons on the script resolution server. Each time the HTTP server encounters a dynamic program, it can be delivered directly to the fastcgi process to execute, and the resulting results are returned to the browser. This approach allows HTTP servers to handle static requests in a single-minded manner or to return the results of a dynamic script server to the client, which greatly improves the performance of the entire application system.
Nginx does not support direct parsing of external programs, all external programs (including PHP) must be invoked through the FastCGI interface. FastCGI interface in Linux is the socket, (this socket can be a file socket, can also be an IP socket). To invoke a CGI program, you also need a fastcgi wrapper (wrapper can be understood as a program to start another program), which is bound to a fixed socket, such as a port or a file socket. When Nginx sends a CGI request to this socket, the FastCGI interface wrapper the request and derives a new thread that invokes the interpreter or external program to process the script and read the return data; Wrapper then passes the returned data through the FastCGI interface to the nginx along the fixed socket, and Nginx sends the returned data to the client, which is the whole operation of the nginx+fastcgi.

php-fpm

PHP5.3 version of the source code has the default support PHP-FPM, but debian6 that it has not been widely tested, so in the debian6 of the Software warehouse, although PHP version of 5.3.3, but did not include PHP-FPM, if you do not want to manually compile and install PHP can be replaced by a source.

Modify Source List

Vi/etc/apt/sources.list
Deb http://packages.dotdeb.org stable all
deb-src http://packages.dotdeb.org stable All

Update source list, install PHP5-FPM

Apt-get update wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | apt-key Add-
apt-get install PHP5-FPM

Install other common PHP5 components

Apt-get install php5 php5-cgi php5-cli php5-mysql

Start PHP-FPM

/ETC/INIT.D/PHP5-FPM start

In this case, the most basic nginx+php environment is completed.

Nginx configuration file Rough Solution
the Nginx configuration file structure is similar to the following diagram:

The configuration file mainly references the default profile that is generated when the compilation is complete.

Configuration options for the main module

The user command sets the process to run with what users, the Nginx user specified at the source code compilation installation, if not specified at compile time, default is nobody account, the row in the configuration file is commented, the user directive can set two parameters, the first specifies the user of the process, and the second is optional, Specify the group to which the process belongs

  User Nginx nobody;

Set the number of worker processes, one process for a single thread, and in a CPU-intensive environment, you can set the number of worker_processes to CPU cores

  Worker_processes 4;

Specifies the location of the Nginx error log file, and if you want to prevent the error log from using Error_log/dev/null,error_log can exist in different fields main, HTTP, server, and so on, you can specify the default level of logging for the log after the file.

  Error_log Logs/error.log;

Set the PID file path, you can use the KILL command to send the relevant signal

  PID    Logs/nginx.pid;

Event module configuration options, the event module mainly controls the way Nginx handles connections

  Events {

  #如果在configure时指定的不止一个事件模型, you can tell Nginx which model to use by using: Seletc, Poll, Kqueue, Epoll, Rtsig,/dev/poll, Eventport and other use

  epoll;

  #worker_connections和worker_processes可以计算你的理论最大链接数, worker_connections*worker_processes

  worker_connections 1024;

}

HTTP module inside the main set of HTTP server-related properties

 HTTP {

  #可以用include指令包含一些其他文件, wildcard characters are supported, absolute paths can be used, relative paths can be used, relative paths are nginx.conf by

  include    Mime.types;

  #设置默认的MIME类型

  default_type Application/octet-stream;

  #描述记录日志的格式, you can use variables when you define a format, and the different meanings can be summarized from each module to find

  log_format main ' $remote _addr-$remote _user [$time _local] "$ Request "

           $status $body _bytes_sent $http _referer" "

           $http _user_agent" "$http _x_forwarded_for";

  #指定访问日志的路径和格式, buffer size

  access_log logs/access.log main;

  #sendfile拷贝文件在内核态完成, more efficient

  sendfile on    ;

  #tcp_nopush on   ;

  #可以设置两个值, the first represents the timeout for a client-server long connection, over which the server closes the connection. The second value specifies the value of the timeout in the keep-alive in the answer header, letting the browser know when to close the connection.

  keepalive_timeout;

  #开启gzip压缩

  gzip on;

  #在三次握手时, the timeout after sending to the client answer is not yet in the connection state, only two handshake completed, if not received at the specified time, Nginx will close the link

  send_timeout

  server {

     xxx

  }

}

The server module is embedded in the HTTP module and is used primarily to configure the virtual host

  server {#指定server字段中可以被访问到的ip地址及端口 Listen 80; #将http请求的主机头与server中的server_name参数进行匹配, and find the first result, if there is no server_name parameter matching, then the first occurrence of listen server will be matched, multiple domain name separated by space Server_

    Name www.nginx.com;

    #设个指令是应答头重的content-type field uses the specified encoding set, off indicates that content-type information charset off is not added to the answer header;

    #指定www. nginx.com Domain name access log path and format Access_log Logs/host.access.log main;

    #如果在url中没有指定文件, set a default home page, you can set up multiple files, spaces separate, you can set up in HTTP, server, location index index.php index.htm; #根据URL的不同需求进行配置, you can use strings and regular matches, and the most exact matches are used, and the search will stop after the first one, and the # ~* is case-insensitive; ~ Case sensitive; ^~ prohibit checking regular after string match; = exact match between URL and location, not done after matching

    Additional search.

      location/i/{#请求到达后的文件根目录, in the request root will location match the value after the value specified by root, request/i/a.php, then/html/i/a.php response root HTML;

    #在location中设置index index index.html index.htm;

    #为错误代码指定相应的错误界面, can be used in HTTP, server, location fields.

    Error_page 404/404.html;

    # REDIRECT Server error pages to the static page/50x.html Error_page 502 503 504/50x.html; #精确匹配50x.HTML, the real response is/html/50x.html location =/50x.html {root html; # Proxy The PHP scripts to Apache listening on 127.0.0.1:80 location ~ \.php$ {Proxy_pass http://127.

    0.0.1;

     } #配置php脚本传至fastcgi Location ~ \.php$ {root html;

     Fastcgi_pass 127.0.0.1:9000;

     Fastcgi_index index.php;

      #/scripts is the directory where the php script Fastcgi_param script_filename/scripts$fastcgi_script_name;

    Include Fastcgi_params;

    #拒绝访问. htaccess file Location ~/\.ht {deny all;
 }

  }

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.