Installation, configuration, and optimization of Nginx and PHP (FastCGI) _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Install, configure, and optimize Nginx and PHP (FastCGI. FastCGI introduction and working principles first of all, FastCGI: FastCGI is a language-independent, scalable CGI open extension, the main behavior is to keep the CGI interpreter in the memory FastCGI.

First, let's briefly introduce FastCGI:

FastCGI is a language-independent, scalable CGI open extension. its main behavior is to maintain the CGI interpreter in the memory and thus achieve high performance. As we all know, repeated loading of the CGI interpreter is the main cause of low CGI performance. if the CGI interpreter is kept in the memory and is scheduled by the FastCGI Process Manager, it provides good performance, scalability, and Fail-Over features.

FastCGI works as follows:

(1) FastCGI Process Manager initializes itself, starts multiple CGI interpreter processes (multiple php-cgi processes), and waits for a connection from the Web Server. In the text, use the PHP-FPM process manager to start multiple php-cgi FastCGI processes. When starting the php-cgi FastCGI process, you can configure TCP and UNIX sockets to start the process.

(2) when a client request reaches the Web server (Nginx), the Web server forwards the request to the FastCGI main process using TCP or UNIX socket, the main FastCGI process selects and connects to a CGI interpreter (child process ). The Web server sends CGI environment variables and standard input to the FastCGI sub-process php-cgi.

(3) after the FastCGI sub-process completes processing, the standard output and error messages are returned from the same connection to the Web server (Nginx ). When the FastCGI sub-process closes the connection, the request is notified that the processing is complete. The FastCGI sub-process then waits for and processes the next connection managed by the FastCGI process. In general CGI mode, php-cgi exits.

PHP-FPM

The PHP-FPM is a PHP FastCGI manager that is only used for PHP and can be used in Ingress. You must patch it to your PHP source code before using it after compiling and installing PHP.

The new version of PHP has already integrated php-fpm and is no longer a third-party package. we recommend that you use it. PHP-FPM provides a better PHP process management method, can effectively control the memory and process, can smoothly load PHP configuration, than spawn-fcgi has more advantages, so it is officially included by PHP. In./configure with The-enable-fpm parameter to open the PHP-FPM, other parameters are configured php, the specific meaning of the option can be viewed here.

Preparations before installation:

Yum-y install gcc automake autoconf libtool make

Yum-y install gcc-c ++ glibc

Yum-y install libmcrypt-devel mhash-devel libxslt-devel \

Libjpeg-devel libpng-devel freetype-devel libxml2 libxml2-devel \

Zlib-devel glibc-devel glib2 glib2-devel bzip2 bzip2-devel \

Ncurses-devel curl-devel e2fsprogs e2fsprogs-devel \

Krb5 krb5-devel libidn-devel openssl-devel

New version of php-fpm installation (recommended)

Wget http://us1.php.net/get/php-5.5.10.tar.gz/from/this/mirror

Tar zvxf php-5.5.10.tar.gz

Cd php-5.5.10

./Configure -- prefix =/usr/local/php -- enable-fpm -- with-mcrypt \

-- Enable-mbstring -- disable-pdo -- with-curl -- disable-debug -- disable-rpath \

-- Enable-inline-optimization -- with-bz2 -- with-zlib -- enable-sockets \

-- Enable-sysvsem -- enable-sysvshm -- enable-pcntl -- enable-mbregex \

-- With-mhash -- enable-zip -- with-pcre-regex -- with-mysql -- with-mysqli \

-- With-gd -- with-jpeg-dir

Make all install

After php-fpm is completed, configure its running users:

Cd/usr/local/php

Cp etc/php-fpm.conf.default etc/php-fpm.conf

Vi etc/php-fpm.conf

Modify:

User = nginx

Group = nginx

If the nginx user does not exist, add the nginx user first.

Groupadd nginx

Useradd-g nginx

Modify the nginx configuration file to support php-fpm

# Pass the PHP scripts to FastCGI server listening on Fig: 9000

Location ~ \. Php $ {

Root html;

Fastcgi_pass 127.0.0.1: 9000;

Fastcgi_index index. php;

Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;

Include fastcgi_params;

}

Create a PHP test file

Create the index. php file in/usr/local/nginx/html and enter the following content:

Echo phpinfo ();

?>

Start php-fpm and nginx

1

2

/Usr/local/php/sbin/php-fpm

/Usr/local/nginx

Parse: FastCGI is a language-independent, scalable CGI open extension. its main behavior is to keep the CGI interpreter in the memory...

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.