Install the configuration Nginx Server +php development environment _php instance from scratch under Linux

Source: Internet
Author: User
Tags php development environment nginx server
Nginx is a very lightweight HTTP server written by Russians, written in an event-driven manner, so it has very good performance and is also a very efficient reverse proxy, load balancing. It has the performance of matching lighttpd, and there is no lighttpd memory leak problem, and lighttpd Mod_proxy has some problems and has not been updated for a long time.

So I'm going to use it instead of Apache on Linux servers. However, Nginx does not support CGI operations because it reduces some of the procedural vulnerabilities that can be caused. Then we must use the FastCGI method to execute the PHP program.

Here is the process I successfully configured Nginx + PHP5 fastcgi

Install or compile Nginx first

Installing Nginx

The source package can be downloaded on the official homepage. Ubuntu 7.10 can be installed directly from apt, or you can download the latest Deb package from here:

sudo apt-get install Nginx

If you want to compile yourself, you need to make sure that you already have the compiler and Pcre library (for Nginx rewrite module, if you do not need this module can be used in configure./configure–without-rewrite), compile the method as follows:

wget http://sysoev.ru/nginx/nginx-0.5.34.tar.gztar zxvf nginx-0.5.34.tar.gzcd nginx-0.5.34./configure #默认配置安装路径为/ Usr/local/nginx can append--prefix=/usr settings to/usrmake && make install # Install requires root permission

The file structure of Ubuntu after installation is roughly:

    • All configuration files are under/etc/nginx, and each virtual host is already under/etc/nginx/sites-available
    • Program Files in/usr/sbin/nginx
    • The logs were put in/var/log/nginx.
    • and has created a startup script Nginx under/etc/init.d/
    • The default virtual host directory is set to/var/www/nginx-default

When you compile with the default configuration, you put it 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 Execute file directory

Below you can start the nginx look at the effect (make sure that port 80 does not have other services in use):

Ubuntu Please run:

Sudo/etc/init.d/nginx start

Other please run:

/usr/local/nginx/sbin/nginx

Then you can look at the effect by 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

Installing PHP5

As for how to install PHP on Linux, there are many articles, and even many platforms have ready-made software packages, do not need to compile themselves.

1. Install the PHP helper package as follows
The usual packages are

    • zlib-1.2.3.tar.bz2
    • jpegsrc.v6b.tar.gz libpng-1.2.22.tar.bz2 libmcrypt-2.5.8.tar.gz
    • Mhash-0.9.9.9.tar.gz mcrypt-2.6.8.tar.gz

Cases:

TAR-JXF zlib-1.2.3.tar.bz2

Extract

Tar zxf tar-jxf zlib-1.2.3.tar.bz2

Into the

After execution

And then execute

The rest of the installation methods are the same.
2. After the installation of the above auxiliary package, the installation of PHP package
Steps

TAR-ZXVF php-5.2.14.tar.gz && CD php-5.2.14

Method above execution

Add the modules that need to be loaded later

(These are loaded modules, not all) press ENTER to execute.
The PHP configuration file is php.ini.

One of the great advantages of PHP5 's CGI approach is the built-in fastcgi support, which simply indicates that the bound address and port parameters can be run in fastcgi manner, as follows:

Php-cgi-b 127.0.0.1:9000

How do I configure it to run with Nginx?

Configuring 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_paramsfastcgi_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-redirectfastcgi_param redirect_status  200;

Pay particular attention to the "Fastcgi_script_name" line, where php-cgi specifically needs 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 (Ubuntu on this profile located in/etc/php5/cgi/php.ini):

Cgi.fix_pathinfo=1;

This allows the php-cgi to use the script_filename variable normally.

Next in the Nginx configuration for PHP files configured 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 the 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 index.php in the document root directory and include "Phpinfo ();" Content, now look at http://localhost/index.php should be able to see the debugging information PHP.

Configuring the PHP Process

There are two problems with using PHP-CGI's fastcgi running mode (there seems to be a solution, if you know, you can teach me):

1. If the process crashes, it is difficult to configure the restart
2. Low efficiency of single process
Therefore, we can use LIGHTTPD's spawn-fcgi to control the running of the process. Here's how to get spawn-fcgi:

wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2 #获取Lighttpd的源码包tar-XVJF LIGHTTPD-1.4.18.TAR.BZ2CD Lighttpd-1.4.18./configure #编译makecp src/spawn-fcgi/usr/local/bin/spawn-fcgi #取出spawn the-FCGI program

So we can use spawn-fcgi to control the 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/usr/bin/php-cgi
The parameters have the following meanings

    • -F Specifies the execution program location of the process that calls fastcgi, depending on the situation of PHP installed on the system
    • -a bind to address addr
    • -p binding to port ports
    • -S bound to the path of the UNIX socket
    • -C Specifies the number of processes generated by the fastcgi, which defaults to 5 (PHP only)
    • -P Specifies the PID file path of the resulting process
    • What identity is used by-u and-G fastcgi (-u user-G user group) to run, Ubuntu can be used under Www-data, other configurations, such as nobody, Apache, etc.

Then we can add this line of code to the bottom of the/etc/rc.local file, so that the system can start the PHP fastcgi process at the same time.

  • 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.