Configure Nginx+PHP5 FastCGI server configuration on ubuntu

  • 2020-05-06 12:11:44
  • OfStack

First install or compile Nginx.
Install Nginx
The source package can be downloaded from the official home page. Ubuntu 9.04 can be installed directly from apt, or you can download the latest deb package:
sudo apt-get install nginx
If you want to compile on your own, make sure you already have the compiler and PCRE libraries (the rewrite module for Nginx, if you don't need it, you can use it for configure./configure, without-rewrite). The compilation method is as follows:
wget http://sysoev.ru/nginx/nginx-0.5.34.tar.gz
tar zxvf nginx-0.5.34.tar.gz
cd nginx-0.5.34
./configure # default configuration installation path to /usr/local/nginx can be appended --prefix=/usr set to /usr
make && make install   # esinstall requires root permission

The file structure after Ubuntu installation is roughly
All configuration files are under /etc/nginx, and each virtual host is already under /etc/nginx/ sites-available under
The program file is at /usr/sbin/nginx
The log is
in /var/log/nginx The startup script nginx
has been created under /etc/ init.d / The default virtual host directory is set to /var/www/ nginx-default

Those compiled by default are placed under /usr/local/nginx. Here is the directory structure:
/ usr local nginx/conf
configuration directory / usr local nginx/root directory
html default website / usr/local/nginx/logs log and pid
file directory / usr local nginx/

sbin executable directory

Now you can launch nginx to see what happens (make sure that no other services are using port 80) :
Ubuntu please run:
sudo /etc/init.d/nginx start
Others please run:
/usr/local/nginx/sbin/nginx
Then you can see the effect by http://localhost/.
To configure nginx run automatically, can be/usr/local/nginx/sbin/nginx added to/etc/rc local, Ubuntu can perform
update-rc.d nginx defaults

Install PHP5
As for how to install PHP on Linux, what we need here is php in cgi mode. Ubuntu can be run directly:
sudo apt-get install php5-cgi
And you can install bytecode optimizations and cache extensions on your own, such as eaccelerator, apc, xcache, and so on.
One of the great advantages of CGI in PHP5 is the built-in support for FastCGI, which allows you to run as FastCGI simply by specifying the address and port parameters of the binding, as follows:
php-cgi -b 127.0.0.1:9000

How do I configure it to run with nginx?
Configure PHP FastCGI
for Nginx Please save the following content for fastcgi_params file, save under/usr local/nginx/conf (Ubuntu can be save in/etc/nginx), he set up the basic environment for our FastCGI module variables:
#fastcgi_params
fastcgi_param   GATEWAY_INTERFACE   CGI/1.1;
fastcgi_param   SERVER_SOFTWARE       nginx;
fastcgi_param   QUERY_STRING             $query_string;
fastcgi_param   REQUEST_METHOD         $request_method;
fastcgi_param   CONTENT_TYPE             $content_type;
fastcgi_param   CONTENT_LENGTH         $content_length;
fastcgi_param   SCRIPT_FILENAME       $document_root$fastcgi_script_name;
fastcgi_param   SCRIPT_NAME               $fastcgi_script_name;
fastcgi_param   REQUEST_URI               $request_uri;
fastcgi_param   DOCUMENT_URI             $document_uri;
fastcgi_param   DOCUMENT_ROOT           $document_root;
fastcgi_param   SERVER_PROTOCOL       $server_protocol;
fastcgi_param   REMOTE_ADDR               $remote_addr;
fastcgi_param   REMOTE_PORT               $remote_port;
fastcgi_param   SERVER_ADDR               $server_addr;
fastcgi_param   SERVER_PORT               $server_port;
fastcgi_param   SERVER_NAME               $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param   REDIRECT_STATUS       200;

Note the bold line, PHP-CGI in particular needs this line of information to determine the location of the PHP file.
Also need the PHP - CGI configuration file (Ubuntu on this configuration file located in/etc/php5 / cgi/php ini), open cgi. fix_pathinfo options:
cgi.fix_pathinfo=1;
This way php-cgi can use the variable SCRIPT_FILENAME properly.

Next, in the nginx configuration, configure the php file to execute using the FastCGI process:
server {
      index index.php;
      root   /usr/local/nginx/html;

      location ~ .*.php$ {
              include/usr/local/nginx/conf/fastcgi_params;   # please set
according to the path you saved               fastcgi_index index.php;
            fastcgi_pass   127.0.0.1:9000; Configure
according to the address and port of your FastCGI binding       }
}

Notify Nginx to reload the configuration :
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
Ubuntu users can use init script: sudo/etc/init d/nginx reload
Then launch php-cgi-b 127.0.0.1:9000

. Assuming that you put in the document root index php, and contains a "content", now look at http: / / localhost index php then should be able to see php debugging information.

Configure php process
There are two problems with FastCGI using php-cgi directly (there seems to be a solution, so teach me if you know) :
If the process crashes, it is difficult to configure restarts, and single process is inefficient
Therefore, we can use spawn-fcgi of Lighttpd to control the running of the process. The way to obtain spawn-fcgi is as follows:
wget http: / / www. lighttpd. net download/lighttpd - 1.4.18. tar. bz2 # get Lighttpd
source packages tar -xvjf lighttpd-1.4.18.tar.bz2
cd lighttpd-1.4.18
./configure   # compile
make
cp src/spawn - fcgi usr/local/bin/spawn - fcgi # remove spawn -
fcgi programs Now we can control the FastCGI process of php-cgi using spawn-fcgi /usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi

The parameter has the following meaning:
-f < fcgiapp > Specify the execution location of the process that invokes FastCGI, and set
according to the PHP installed on the system -a < addr > Bind to address addr
-p < port > Bind to port port
-s < path > The path path
is bound to unix socket -C < childs > Specifies the number of processes to produce FastCGI, with a default of 5 (for PHP only)
-P < path > Specifies the PID file path
for the resulting process -u and -g FastCGI use what identity (-u user-g user group), www-data can be used under Ubuntu, other configuration according to the situation, such as nobody, apache

We can then add this line of code to the bottom of the /etc/ rc.local file so that the FastCGI process of PHP can also be started when the system starts.


Related articles: