Compile and install nginx + php (fastcgi) in linux

Source: Internet
Author: User
Nginx uses the fastcgi method to connect to php. during compilation and installation in linux, php must support the fastcgi method. Other things can be installed according to the original method. Perform the following steps: 1. Install fastcgi version of php 2. Use spawn-fcgi to start the fastcgi engine of php 3. Configure nginx to connect to the fastcgi engine of php 1. Install the fastcgi version of php

Nginx connection using fastcgiPhpIn linux, php must be compiled to support the fastcgi method during compilation and installation. Other things can be installed according to the original method.

Perform the following steps:

1. Install fastcgi of php
2. Use spawn-fcgi to start the php fastcgi Engine
3. Configure nginx to connect to the fastcgi engine of php

1. Install fastcgi of php

Mkdir/Usr/local/moDuLes

# Jpeg directory
Mkdir/usr/local/modules 6
Mkdir/usr/local/modules 6/bin
Mkdir/usr/local/modules 6/lib
Mkdir/usr/local/modules 6/INcLude
Mkdir/usr/local/modules 6/man
Mkdir/usr/local/modules 6/man/man1

A1. Install zlib

Tar xzvf zlib-1.2.1.tar.gz
CdZlib-1.2.1
# Do not use -- prefix to customize the installation directory, which affects gd installation.
./Configure
Make
Make install

A2. InstallationFreeType

Tar xzvf freetype-2.1.5.tar.gz
Cd freetype-2.1.5
./Configure -- prefix =/usr/local/modules/freetype
Make
Make install

A3 install libpng

Tar xzvf libpng-1.2.5.tar.gz
# Do not use -- prefix to customize the installation directory, which affects gd installation.
Cd libpng-1.2.5
CpScripts/makeFile. Std makefile
Make test
Make install

A3 install jpeg

Tar xzvf restart src.v6b.tar.gz
Cd jpeg-6b
./Configure -- prefix =/usr/local/modules 6 --Enable-SharEd-- Enable-StatIc
Make
Make install

A4. Install GD

Tar xzvf gd-2.0.28.tar.gz
. /Configure -- prefix =/usr/local/modules/gd -- with-jpeg =/usr/local/modules 6 -- with-png -- with-zlib -- with-freetype =/ usr/local/modules/freetype
Make
Make install

Then compile and install php. Add the -- enable-fastcgi parameter and the -- enable-force-cgi-redirect parameter.

. /Configure -- prefix =/usr/local/php -- with-gd =/usr/local/modules/gd -- with-jpeg-dir =/usr/local/modules 6 -- with-zlib -- with-png -- with-freetype-dir =/usr/local/modules/freetype -- enable-magic-quotes -- enable-fastcgi -- with-mysql =/usr/ local/mysql -- enable-TrAck-vars -- enable-Ftp-- With-config-file-path =/usr/local/php/etc ---Zip-- Enable-force-cgi-redirect
Make-j10
Make install

2. Use spawn-fcgi to start the php fastcgi Engine

After compilation, the executable php-cgi program will appear under/usr/local/php/bin/. Use spawn-fcgi to start the php fastcgi engine:

/Data/nginx/sbin/spawn-fcgi-a 127.0.0.1-p 9000-u nobody-f/usr/bin/php-cgi-C 20

Fastcgi uses the ip address of the Local Machine and port 9000 to provide services. With the nobody permission, it starts 20 processes. Among them, pay attention to the-C parameter. Generally, 20 processes are enough and cannot be opened. On my machine, each php-cgi process occupies 7-8 MB of memory, open 100 MB to MB.

Spawn-fcgi is not provided in nginx.

Http: // www.SuDone.com/download/spawn-fcgi

Download To nginx, which is generally placed in the sbin directory of nginx, and then add executable permissions to it:

Chmod+ X/data/nginx/sbin/spawn-fcgi

3. Configure nginx to connect to the fastcgi engine of php

First, configure fastcgi-params and create a text file under the nginx conf directory. The content is:

# Fastcgi-params
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_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 GATEWAY_INTERFACE CGI/1.1;
Fastcgi_param SERVER_SOFTWARE nginx;

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;

#PHPOnly, required if PHP was built with -- enable-force-cgi-redirect
Fastcgi_param REDIRECT_STATUS 200;
# End

This is the most primitive fastcgi-params, and a more optimized version may be available on the Internet.

Then configure nginx. conf, such as www.sudone.com, so that the configuration can be used:

Server {
Include port. conf;
Server_name www.sudone.com sudone.com;

LoCatIon /{
IndExIndex.html index. php;
Root/data/sudone/php /;
}

Location ~ . Php $ {
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fastcgi_param SCRIPT_FILENAME/data/sudone/php $ fastcgi_script_name;
Include fastcgi_params;
}
}

After getting it, there are several points to change:

1. server_name
2. root in location/
3. location ~ Fastcgi_param SCRIPT_FILENAME in. php $

Among the fastcgi_param SCRIPT_FILENAME parameters, the path of the PHP file before $ fastcgi_script_name is generally the same as that of the root file. In the end, you don't need to write/. It seems that it may be a problem and you don't need to draw any more.

Note:

Some administrators may forget to start spawn-fcgi after starting nginx, so it is best to write a script to start it and then develop the habit of testing service availability.

Author Ayou: http://sudone.com/

Related Article

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.