Installation, configuration and optimization of Nginx and PHP (FastCGI)

Source: Internet
Author: User

FastCGI introduction and working principles first of all, FastCGI: FastCGI is a language-independent, scalable CGI open extension, the 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. 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 liblibi Dn-devel openssl-devel New Version php-fpm installation (recommended installation) wget http://us1.php.net/get/php-5.5.10.tar.gz/from/this/mirrortar zvxf php-5.5.10.tar.gzcd 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 -- e Nable-pcntl -- enable-mbregex \ -- with-mhash -- enable-zip -- with-pcre-regex -- with-mysql -- with-mysqli \ -- with-gd -- with-jpeg- after dir make all install completes php-fpm, configure its running user: cd/usr/local/phpcp etc/php-fpm.conf.default etc/php-fpm.confvi etc/php-fpm.conf modify: user = nginxgroup = nginx if the nginx user does not exist, add nginx user groupadd nginxuseradd-g nginx modify nginx configuration file to support php-fpm # pass the PHP scripts to FastCGI serv Er listening on 127.0.0.1: 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 test php file and create an index under/usr/local/nginx/html. PHP file, enter the following content <? Php echo phpinfo ();?> Start php-fpm and nginx 12/usr/local/php/sbin/php-fpm/usr/local/nginx

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.