Configure NGINX+PHP5 fastcgi server configuration on Ubuntu _nginx

Source: Internet
Author: User
Tags bz2 install php

First install or compile nginx.
Install Nginx
The source pack can be downloaded on the official home page. Ubuntu 9.04 can be installed directly via apt, or you can download the latest Deb package from here:
sudo apt-get install Nginx
If you want to compile it yourself, you need to make sure that you already have a compiler and Pcre library (for the Nginx rewrite module, if you don't need this module to be used at 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 #默认配置安装路径为/usr/local/nginx can append--prefix=/usr settings to/usr
&& make install # Install requires root permission

After Ubuntu installation, the file structure is roughly:
All configuration files are under/etc/nginx, and each virtual host is scheduled under/etc/nginx/sites-available
Program Files in/usr/sbin/nginx
The log is in the/var/log/nginx.
and a startup script has been created under/etc/init.d/nginx
The default virtual host's directory setting is in the/var/www/nginx-default

If you compile it with your default configuration, it is placed under/usr/local/nginx, and the following is the directory structure:
/usr/local/nginx/conf Configuration Directory
/usr/local/nginx/html the default Web site root directory
/usr/local/nginx/logs log and PID files directory
/usr/local/nginx/sbin Execution File directory

The following can start Nginx to see the effect (please make sure that 80 ports are not using any other service):
Ubuntu Please run:
Sudo/etc/init.d/nginx start
Please run:
/usr/local/nginx/sbin/nginx
Then you can see the effect through http://localhost/.
To configure Nginx Autorun, you can add/usr/local/nginx/sbin/nginx to/etc/rc.local and Ubuntu can perform
UPDATE-RC.D Nginx Defaults

Install PHP5
As for how to install PHP on Linux, what we need here is the CGI way of PHP. Ubuntu can run directly under:
sudo apt-get install php5-cgi
and can be installed according to their own situation some bytecode optimization and caching extensions, such as Eaccelerator, APC, XCache and so on.
One of the great advantages of PHP5 's CGI is the built-in support for fastcgi, which can be run as fastcgi by simply 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 the Nginx PHP FastCGI
Save the following as a fastcgi_params file, saved under/usr/local/nginx/conf (Ubuntu can be saved under/etc/nginx), and he sets the basic environment variables for our fastcgi module:
#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 is built with--enable-force-cgi-redirect
Fastcgi_param Redirect_status 200;

Please pay special attention to the bold line, php-cgi specifically need this information to determine the location of the PHP file.
In addition, you need to open the Cgi.fix_pathinfo option in the php-cgi configuration file (this profile is located in/etc/php5/cgi/php.ini) on Ubuntu:
Cgi.fix_pathinfo=1;
This allows the php-cgi to normally use the script_filename variable.

Next, configure the PHP file in the Nginx configuration to use the fastcgi process to execute:
server {
Index index.php;
root/usr/local/nginx/html;

Location ~. *.php$ {
Include/usr/local/nginx/conf/fastcgi_params; #请根据自己保存的路径进行设置
Fastcgi_index index.php;
Fastcgi_pass 127.0.0.1:9000; #请根据自己的FastCGI绑定的地址和端口进行配置
}
}

Notify Nginx to reload configuration:
Kill-hup ' Cat/usr/local/nginx/logs/nginx.pid '
Ubuntu users can use the init script:Sudo/etc/init.d/nginx Reload
Then start php-cgi-b 127.0.0.1:9000

Suppose you put a index.php in the root of the document and include the contents of the "", and now look at http://localhost/index.php should be able to see the debugging information of PHP.

Configuring the PHP Process
There are two problems with the fastcgi running directly using php-cgi (seemingly there should be a solution, if you know it can teach me):
If the process crashes, it is difficult to configure the reboot, single process inefficient
Therefore, we can use the LIGHTTPD spawn-fcgi to control the running of the process. The methods for obtaining spawn-fcgi are as follows:
wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2 #获取Lighttpd的源码包
TAR-XVJF lighttpd-1.4.18.tar.bz2
CD lighttpd-1.4.18
./configure #编译
Make
Procedure of CP src/spawn-fcgi/usr/local/bin/spawn-fcgi #取出spawn-fcgi
Here we can use spawn-fcgi to control php-cgi fastcgi process.
/usr/local/bin/spawn-fcgi-a 127.0.0.1-p 9000-c 5-u www-data-g www-data-f

The parameter meaning is as follows
-F <fcgiapp> Specifies the location of the executing program of the process that invokes the fastcgi, set according to the situation of the PHP installed on the system
-A <addr> bind to address addr
-P <port> bind to ports port
-S <path> path to a UNIX socket
-C <childs> Specifies the number of fastcgi processes generated, default to 5 (PHP only)
-P <path> Specifies the PID file path of the resulting process
-U and-G fastcgi use what identity (-u user-G user group) to run, Ubuntu can use Www-data, other according to the situation configuration, such as nobody, Apache, etc.

Then we can add this line of code to the bottom of the/etc/rc.local file so that when the system starts, it can also start the PHP fastcgi process.

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.