Installing Nginx and php is not much to say here, after all, the online tutorials are already very comprehensive. Whether it is rpm, apt-get or downloading source code installation, just follow the steps, (PHP I use dpkg
?P php5-cgi and then apt-get install php5-cgi to install, the platform is Ubuntu) Here I want to talk about the php support mode, this part is a repost
1) Currently variousserversHTTP ServerSupport for PHP Total There are three:
a. Through the built-in module of HTTPServer,
For example, Apache‘s mod_php5, similar Apache’s built-in mod_perl can support perl;
b. Realized by CGI, this is like the CGI of perl before, the disadvantage of this method is poor performance, because every time the server encounters these scripts, the script parser needs to be restarted to execute the script and then return the result to the server; On the one hand, it is not very secure; this aspect is almost rarely used.
c. The latest one is called FastCGI. The so-called FastCGI is an improvement on CGI. It generally adopts a C/S structure. Generally, the script processor will start one or more daemon processes. Every time HTTPServer encounters a script, it will be directly delivered to the FastCGI process for execution, and then the obtained result (usually html) will be returned. to the browser.
>The problem with this method is that when encountering frequent requests with large traffic, the daemon process of the script processor may be overloaded and become very slow, and even memory leaks occur;
>However, compared with Apache’s built-in module, the advantage is that the Server and the script parser are completely separate and responsible, so the server is no longer bloated, and you can concentrate on responding to static files or returning the results of the dynamic script parser to the user client. So compared with Apache’s built-in module method, sometimes the performance is much improved. Someone test may reach
5~10 times of Apache+mod_php.
2)UsingFastCGIThere are two common ways nowstack :ligthttpd+spawn-fcgi; The other isnginx +PHP-FPM (also availablespawn-fcgi) .
a. As mentioned above, the two structures both use FastCGI to support PHP, so the HTTPServer is completely liberated and can better respond and concurrently process. So both lighttpd and nginx have small,
but powerful and efficient reputation.
b.
The two can also be divided into one good and one bad, because spawn-fcgi is part of lighttpd, so after installing lighttpd, spawn-fcgi will generally be used to pair
PHP supports it, but currently some users say that when spwan-fcgi of ligttpd has high concurrent access, the memory leak mentioned above will appear and even fastcgi will be restarted automatically. Namely: PHP
The script processor is down, and if the user visits at this time, a white page may appear (that is, PHP cannot be parsed or an error occurs).
Another: First of all, nginx does not include fastcgi (spawn-fcgi) like lighttpd itself, so it is completely lightweight and must rely on third-party FastCGI
The processor can analyze PHP, so in fact, it seems that nginx is very flexible. It can be connected with any third-party processor that provides analysis to realize the analysis of PHP (in
It is easy to set in nginx.conf).
nginx can use spwan-fcgi (you need to install lighttpd together, but you need to avoid ports for nginx, some earlier blogs have tutorials on this aspect of installation), but because spawn-fcgi has the defects gradually discovered by users mentioned above, now Slowly reduce the use of nginx+spawn-fcgi combination.
c. Due to the defect of spawn-fcgi, a new third party has emerged (currently, I heard that it is working hard to join PHP in the near future
The FastCGI processor of PHP in core) is called PHP-FPM (you can google for details). Compared with spawn-fcgi, it has the following advantages:
Since it is developed as a PHP patch, it needs to be compiled with the php source code when installed, that is to say compiled to php
It is in the core, so it is better in terms of performance;
At the same time, it is also better than spawn-fcgi in handling high concurrency, at least it will not automatically restart the fastcgi processor. The specific algorithm and design used can be understood by google.
Therefore, as mentioned above, due to the light weight and flexibility of nginx, the current performance is superior, and more and more people are gradually using this combination: nginx+PHP/PHP-FPM .
3)So to summarize:
Currently in HTTPServer you can basically see that there are three kinds of stack that are more popular:
>Apache+mod_php5
>lighttp+spawn-fcgi
>nginx+PHP-FPM
Well, the above is the reposting part, and the following is back to the topic. Here we mainly introduce lighttp+spawn-fcgi to control phalive_timeout 65;
gzip on;
# server {
# listen 8080;
# server_name
localhost;
# root /var/www/php/website;
# index index.php
index.html;
# error_page 404 > /404.html;
# error_page 500
502 503 504 /50x.html;
# location
~ .*\.(php|php5)?$ {
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
# }
# }
include /usr/local/nginx/sites-enabled/*;
}
In contrast, this configuration is completely pointing to the configuration file under /usr/local/nginx/sites-enabled/*, such as index.php corresponding to the content of the file under sites-enabled is:
server {
listen
8080;
server_name
*.xxx.com;
access_log
/var/log/nginx/php_xxx.access.log;
location
/ {
root
/var/www/php/website;
index
index.php index.html index.htm;
error_page 500
502 503 504 /50x.html;
location
= /50x.html {
root html;
}
if
(-f $request_filename/index.html)
{
> rewrite
(.*) $1/index.html break;
}
if
(-f $request_filename/index.htm)
{
> rewrite
(.*) $1/index.htm break;
}
if
(-f $request_filename/index.php)
{
> rewrite
(.*) $1/index.php break;
}
location ~ \.php$ {
> fastcgi_pass
127.0.0.1:9000;
> fastcgi_index
index.php;
> fastcgi_param
SCRIPT_FILENAME /var/www/php/website$fastcgi_script_name;
> fastcgi_param
SCRIPT_NAME /var/www/php/website$fastcgi_script_name;
> include
/usr/local/nginx/conf/fastcgi_params;
}
}
}
It is not difficult to find that the files in this directory are actually links to the files in the /usr/local/nginx/sites-available directory.
After that reload kill -HUP `cat /usr/local/nginx/logs/nginx.pid` or sudo
/etc/init.d/nginx reload
Then sudo spawn-fcgi -a 127.0.0.1 -p 9000 -C 3 -u www-data -f
php5-cgi start PHP
Put index.php under /var/www/php/website/, and include the content of “phpinfo();”. Now look at http://localhost:8080 and you should be able to see the debugging information of php.
bsp; }
if
(-f $request_filename/index.htm)
{
> rewrite
(.*) $1/index.htm break;
}
if
(-f $request_filename/index.php)
{
> rewrite
(.*) $1/index.php break;
}
location ~ \.php$ {
> fastcgi_pass
127.0.0.1:9000;
> fastcgi_index
index.php;
> fastcgi_param
SCRIPT_FILENAME /var/www/php/website$fastcgi_script_name;
> fastcgi_param
SCRIPT_NAME /var/www/php/website$fastcgi_script_name;
> include
/usr/local/nginx/conf/fastcgi_params;
}
}
}
It is not difficult to find that the files in this directory are actually links to the files in the /usr/local/nginx/sites-available directory.
After that reload kill -HUP `cat /usr/local/nginx/logs/nginx.pid` or sudo
/etc/init.d/nginx reload
Then sudo spawn-fcgi -a 127.0.0.1 -p 9000 -C 3 -u www-data -f
php5-cgi start PHP
Put index.php under /var/www/php/website/, and include the content of “phpinfo();”. Now look at http://localhost:8080 and you should be able to see the debugging information of php.