How to install PHP with FastCGI
How to install PHP with FastCGI: 1. Download php and php-fpm packages; 2. Configure the installation environment; 3. Compile and install php and php-fpm; 4. Pass "/usr/local/php/sbin /php-fpm start" to start the FastCGI process.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
How to install PHP with FastCGI? Installation, configuration and optimization of Nginx and PHP (FastCGI)
What is FastCGI
FastCGI is A scalable, high-speed interface for communication between HTTP servers and dynamic scripting languages. FastCGI is also supported by many scripting languages, including PHP.
FastCGI is developed from CGI. The FastCGI interface adopts a C/S structure, which can separate the HTTP server and the script parsing server, and start one or more script parsing daemons on the script parsing server.
Whenever the HTTP server encounters a dynamic city area, it is directly delivered to the FastCGI process for execution, and then returns the result to the browser, which improves the overall performance of the system.
Nginx FastCGI operating principle
Nginx does not support direct calling or parsing of external programs. All external programs must be called through the FastCGI interface. In order to call a CGI program, a FastCGI wrapper is also needed, which can be understood as a program used to start another program.
This wrapper is bound to a fixed socket, such as a port or file socket. When Nginx sends a CGI request to the socket, through the FastCGI interface, the wrapper receives the request and spawns a new thread. This thread calls the interpreter or external program to process the script and reads the return data. Then the wrapper passes the return data through the FastCGI interface. to Nginx, and finally Nginx returns it to the client.
spawn-fcgi and PHP-FPM
The FastCGI interface starts one or more daemon processes on the script parsing server to dynamically parse scripts, which also becomes the FastCGI process manager, spawn- fcgi and PHP-FPM are two process managers.
spawn-fcgi: It is part of the HTTP server lighttpd. It is now an independent project and is generally used in conjunction with lighttpd to support PHP. However, spawn-fcgi will cause memory leaks and even automatically restart FastCGI when concurrency is high.
PHP-FPM: A third-party FastCGI process manager, which is developed as a patch for PHP. During the installation process, it also needs to be compiled together with the PHP source code, which means that PHP-FPM is compiled into the PHP kernel, so the performance is better. At the same time, its ability to handle high concurrency is also much better.
Because the advantage of FastCGI is to separate dynamic languages from HTTP servers, Nginx and PHP/PHP-FPM are often deployed on different servers to share the pressure on the Nginx front-end and allow Nginx to focus on processing static requests. , PHP/PHP-FPM parses PHP dynamic requests.
PHP and PHP-FPM installation and optimization
Download the installation package
Download the php package: www.php.net, php-5.2.13 is used here. tar.gz
Download the php-fpm package: php-fpm.org, here use php-5.2.13-fpm-0.5.13.diff.gz
Note: Most It is best that the versions of php and php-fpm are consistent, otherwise compatibility issues may occur.
Configure the installation environment
The required dependency packages include:
gcc gcc-c libxml2 libxml2-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel, etc.
Depending on the system version, the packages that may depend on it are also different.
Compile and install php and php-fpm
Unzip the source code package of php and enter php-fpm as a patch into the php source code.
tar zxf php-5.2.13.tar.gzgzip -cd php-5.2.13-fpm-0.5.13.diff.gz | patch -d php-5.2.13 -p1
Compile and install, start support for FastCGI at the same time, and activate support for fpm in FastCGI mode.
cd php-5.2.13./configure --prefix=/usr/local/php --enable-fastcgi --enable-fpmmake && make install cp php-ini-dist /usr/local/php/lib/php.ini
Configuration and optimization php-fpm
The main configuration file of php is php.ini;
The main configuration file of php-fpm is php-fpm.conf, located at /usr/local/php/etc/ is a file in xml format. Here are some commonly used tags:
<value name="listen_address">127.0.0.1:9000</value>:表示配置FastCGI进程监听的IP地址和端口,默认为本地的9000; <value name="display_errors">0</value>:表示是否显示php错误信息,默认为0表示不显示,设置为1表示显示错误信息; <value name="user">nobody</value>:表示设置运行FastCGI进程的用户,需要和nginx配置文件中的用户一致; <value name="group">nobody</value>:表示设置运行FastCGI进程的用户组,需要和nginx配置文件中的用户组一致; <value name="max_children">5</value>:设置FastCGI的进程数,官方建议小于2G内存,可以只开启64个进程;4G以上可以开启200个进程; <value name="request_terminate_timeout">0s</value>:设置FastCGI执行脚本的时间,默认0s,意味着无限执行下去; <value name="rlimit_files">1024</value>:设置php-fpm打开文件描述符的限制,默认1024,这个值最好要和内核打开的文件数一致; <value name="max_requests">500</value>:设置每一个进程最多处理多少个请求后会被关闭,默认500; <value name="allowed_clients">127.0.0.1</value>:设置允许访问FastCGI进程解析器的IP地址,如果不设置IP地址,则无法接受nginx传来的php解析请求;
Manage FastCGI process
The method to start the FastCGI process is as follows:
/usr/local/php/sbin/php-fpm start
The meaning of the parameters is as follows:
start: Start the fastcgi process of php;
stop: Force stop the fastcgi process of php;
quit: Smoothly terminate the fastcgi process of php Process;
restart: Restart the fastcgi process of php;
reload: Reload the fastcgi process of php so that the process loads the configuration file without interruption;
logrotate: Re-enable the log file;
查看FastCGI进程
FastCGI进程启动后,可以通过命令“ps”或者“netstat”查看到相关进程信息,默认监听9000端口;
netstat -antlp | grep 9000ps -ef | grep php-cgi
配置Nginx来支持PHP
Nginx本身不会对PHP进行解析,要想让Nginx解析PHP请求,需要将PHP页面的请求交给FastCGI进程监听的IP地址和端口。Nginx通过反向代理的功能实现对PHP的解析。下面对Nginx解析PHP进行配置实例。
配置实例
server { server_name www.ixdba.net ixdba.net; location / { index index.html index.php; root /web/www/www.ixdba.net; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME html$fastcgi_script_name; include fastcgi_params; } }
location指令,将以“.php”结尾的文件都交给“127.0.0.1:9000”来处理,这里的IP和端口就是FastCGI进程监听的IP和端口。
fastcgi_param指令,指定放置php动态程序的主目录,也就是$fastcgi_script_name前的路径,这里是/usr/local/nginx/html。
fastcgi_params文件是FastCGI进程的一个参数配置文件,安装Nginx后默认生成,这里将它包含进来。
启动nginx
/usr/local/nginx/sbin/nginx
测试Nginx对PHP的解析功能
在/usr/local/nginx/html,也就是上面的php动态程序主目录下,创建一个phpinfo.php文件,内容如下:
<?php phpinfo(); ?>
然后通过浏览器访问http://www.ixdba.net/index.html,将会显示Nginx欢迎页;
浏览器访问http://www.ixdba.net/phpinfo.php,会出现php安装及功能列表信息;
优化Nginx中FastCGI
添加一些优化指令可以让php环境高效稳定的运行,下面给出一些实例,添加到nginx配置文件的http层级:
为fastcgi缓存指定文件路径,目录结构等级、关键字区域存储时间和非活动删除时间;
fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=Test:10m inactive=5m;
连接到后端fastcgi的超时时间;
fastcgi_connect_timeout 300;
向fastcgi传送请求的超时时间,是已经完成两次握手后向fastcgi传送请求的超时时间;
fastcgi_send_timeout 300;
接收fastcgi应答的超时时间,是已经完成两次握手后接收fastcgi应答的超时时间;
fastcgi_read_timeout 300;
读取fastcgi应答第一部分需要缓冲区的大小;
fastcgi_buffer_size 64k;
本地需要多少和多大的缓冲区来缓冲fastcgi的应答请求;
fastcgi_buffers 4 64k;
默认值为fastcgi_buffers的两倍;
fastcgi_busy_buffers_size 128k;
写入缓存文件时使用多大的数据块,默认为fastcgi_buffers的两倍;
fastcgi_temp_file_write_size 128k;
开启fastcgi缓存并为其指定一个名称,可以有效降低cpu负载,防止502错误。
fastcgi_cache TEST;
fastcgi_cache_valid指定应答代码的缓存时间。
将200, 302的应答时间缓存1小时;
fastcgi_cache_valid 200 302 1h;
将301应答缓存一天;
fastcgi_cache_valid 301 1d;
其余应答缓存1分钟;
fastcgi_cache_valid any 1m;
The above is the detailed content of How to install PHP with FastCGI. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
