Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install PHP for FastCGI

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you how to install FastCGI PHP, the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor to take you to study and learn "FastCGI how to install PHP" this article.

FastCGI install PHP method: 1, download php and php-fpm package; 2, configure the installation environment; 3, compile and install php and php-fpm;4, and start the FastCGI process through "/ usr/local/php/sbin/php-fpm start".

This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer

How does FastCGI install PHP? Installation, configuration and optimization of Nginx and PHP (FastCGI)

What is FastCGI?

FastCGI is a scalable, high-speed interface for communicating between HTTP server and dynamic scripting languages. FastCGI is also supported by many scripting languages, including PHP.

FastCGI is developed from CGI. The FastCGI interface adopts Cramp S structure, which can separate the HTTP server from the script parsing server, and start one or more script parsing daemons on the script parsing server.

Every time the HTTP server encounters a dynamic city, it delivers it directly to the FastCGI process for execution, and then returns the result to the browser, which improves the overall performance of the system.

Operation principle of Nginx+FastCGI

Nginx does not support direct call or parsing of external programs, and all external programs are called through the FastCGI interface. In order to invoke the CGI program, you also need a wrapper of FastCGI, which can be understood as the program used to start another program.

This wrapper is bound to a fixed socket, such as a port or file socket. When Nginx sends the CGI request to socket, through the FastCGI interface, wrapper receives the request and derives a new thread, which calls the interpreter or external program processing script and reads the return data, then wrapper sends the return data to Nginx through the FastCGI interface, and finally Nginx returns it to the client.

Spawn-fcgi and PHP-FPM

The FastCGI interface starts one or more daemon dynamic parsing scripts on the script parsing server, which is also called the FastCGI process manager, and spawn-fcgi and PHP-FPM are the two process managers.

Spawn-fcgi: part of the HTTP server lighttpd, it is currently a stand-alone project and is generally used with lighttpd to support PHP. However, when spawn-fcgi is in high concurrency, it can cause memory leaks and even automatically restart FastCGI.

PHP-FPM: a third-party FastCGI process manager, which was developed as a patch for PHP. During the installation process, you also need to compile with the PHP source code, that is, PHP-FPM is compiled into the PHP kernel, so the performance is better. At the same time, its ability to deal with high concurrency is also much better.

Because FastCGI has the advantage of separating dynamic languages from HTTP server, Nginx and PHP/PHP-FPM are often deployed to impassable servers to share the pressure on the Nginx front end, allowing Nginx to focus on static requests and PHP/PHP-FPM to parse PHP dynamic requests.

Download installation package for PHP and PHP-FPM installation and optimization

Download the php package: www.php.net, where php-5.2.13.tar.gz is used

Download the php-fpm package: php-fpm.org, where php-5.2.13-fpm-0.5.13.diff.gz is used

Note: it is best to have the same version of php and php-fpm, otherwise compatibility problems 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 et al.

Depending on the version of the system, you may depend on different packages.

Compile and install php and php-fpm

Extract the php source package and put 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 while starting support for FastCGI and activating support for fpm in FastCGI mode.

Cd php-5.2.13./configure-prefix=/usr/local/php-enable-fastcgi-enable-fpmmake & & make installcp php-ini-dist / usr/local/php/lib/php.ini configuration and Optimization of php-fpm

The main configuration file for php is php.ini

The main configuration file of php-fpm is php-fpm.conf, which is located under / usr/local/php/etc/ and is a file in xml format. Here are some commonly used tags:

127.0.0.1 IP 9000: indicates the IP address and port on which the FastCGI process is configured to listen. The default is 9000place0: indicates whether the php error message is displayed, the default is 0, and the setting is 1, which indicates that the error message is displayed. Nobody: sets the user to run the FastCGI process, which needs to be consistent with the user in the nginx configuration file. Nobody: sets the user group that runs the FastCGI process, which needs to be the same as the user group in the nginx configuration file; 5: set the number of processes in FastCGI. It is officially recommended that only 64 processes can be started if the memory is less than 2 GB. O0s: set the time for FastCGI to execute scripts, default 0s, which means unlimited execution. 1024: set the limit of php-fpm open file descriptor. Default is 1024. This value is preferably the same as the number of files opened by the kernel. 1024: set the maximum number of requests per process will be closed. Default is 127.0.0.1. Set the IP address that allows access to the FastCGI process parser. If you do not set the IP address, you cannot accept php parsing requests from nginx. Manage the FastCGI process.

You can start the FastCGI process as follows:

/ usr/local/php/sbin/php-fpm start

The meaning of the parameter is as follows:

Start: start the fastcgi process of php

Stop: force to stop the fastcgi process of php

Quit: smoothly terminates the fastcgi process of php

Restart: restart the fastcgi process of php

Reload: reload php's fastcgi process so that the process loads the configuration file without interruption

Logrotate: re-enabling log files

View FastCGI processes

After the FastCGI process starts, you can check the relevant process information by using the command "ps" or "netstat". By default, you can listen to port 9000.

Netstat-antlp | grep 9000ps-ef | grep php-cgi configures Nginx to support PHP

Nginx itself does not parse the PHP. In order for Nginx to parse the PHP request, you need to give the request of the PHP page to the IP address and port that the FastCGI process listens to. Nginx parses PHP through the function of reverse proxy. Let's configure an example of Nginx parsing PHP.

Configure the instance 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 index index.html index.php; root 9000 Fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME html$fastcgi_script_name; include fastcgi_params;}}

Location directive, handing over files ending in ".php" to "127.0.0.1 FastCGI 9000" for processing, where the IP and port are the IP and port on which the FastCGI process listens.

The fastcgi_param directive, which specifies the home directory where the php dynamic program is placed, that is, the path before $fastcgi_script_name, in this case / usr/local/nginx/html.

The fastcgi_params file is a parameter configuration file for the FastCGI process, which is generated by default after installing Nginx, and is included here.

Start nginx/usr/local/nginx/sbin/nginx to test the parsing function of Nginx to PHP

Under / usr/local/nginx/html, the php dynamic program home directory above, create a phpinfo.php file with the following contents:

Then access http://www.ixdba.net/index.html through the browser, and the Nginx welcome page will be displayed

When the browser accesses http://www.ixdba.net/phpinfo.php, the php installation and feature list information will appear.

Optimize FastCGI in Nginx

Add some optimization instructions to make the php environment run efficiently and stably. Here are some examples to add to the http level of the nginx configuration file:

Specify the file path, directory structure level, keyword area storage time, and inactive delete time for the fastcgi cache

Fastcgi_cache_path / usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=Test:10m inactive=5m

Timeout for connecting to the backend fastcgi

Fastcgi_connect_timeout 300

The timeout for sending a request to fastcgi, which is the timeout for sending a request to fastcgi after two handshakes have been completed

Fastcgi_send_timeout 300

The timeout for receiving a fastcgi reply, which is the timeout for receiving a fastcgi reply after two handshakes have been completed

Fastcgi_read_timeout 300

The size of the buffer required to read the first part of the fastcgi reply

Fastcgi_buffer_size 64k

How many buffers and how many buffers are needed locally to buffer fastcgi reply requests

Fastcgi_buffers 4 64k

The default value is twice that of fastcgi_buffers

Fastcgi_busy_buffers_size 128k

How many blocks are used when writing to cache files, which defaults to twice the size of fastcgi_buffers

Fastcgi_temp_file_write_size 128k

Turning on the fastcgi cache and assigning it a name can effectively reduce the cpu load and prevent 502 errors.

Fastcgi_cache TEST

Fastcgi_cache_valid specifies the cache time for the reply code.

Cache the response time of 200302 for 1 hour

Fastcgi_cache_valid 200 302 1h

Cache 301 reply for one day

Fastcgi_cache_valid 301 1d

The rest of the reply is cached for 1 minute

What are the tools for building fastcgi_cache_valid any 1m PHP development environment? phpStudy is the most commonly used development environment for beginners. Second, WampServer,WampServer is also as simple as phpStudy and more friendly to rookies. Third, XAMPP,XAMPP (Apache+MySQL+PHP+PERL) is a powerful integrated software package for building stations; fourth, MAMP,MAMP is divided into two types: MAMP and MAMP Pro for Mac. Pagoda panel, pagoda panel is a server management software that supports windows and linux systems. 6. UPUPW,UPUPW is the most characteristic Web server PHP suite under the Windows platform.

The above is about "how to install PHP in FastCGI". If this article is helpful and well written, please share it with your friends to learn new knowledge. if you want to know more about it, please pay more attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report