First Nginx & PHP

Source: Internet
Author: User
Tags php source code nginx server

Env

Kernel:3.10.0-123.el7.x86_64

Nginx version:nginx/1.4.7./configure--prefix=/usr/local/nginx/--with-pcre=/usr/local/pcre-7.9/(source code path)- -with-zlib=/usr/local/zlib-1.2.8/(source code path)

PHP version:5.5.20 Auto Enable fastcgi ./configure--prefix=/usr/local/php--enable-debug--enable-fpm

Config
1 #user Nobody;2Worker_processes1;3 4#error_log logs/Error.log;5#error_log logs/Error.log notice;6#error_log logs/Error.log info;7 8#pid logs/Nginx.pid;9 Ten  One Events { AWorker_connections1024x768; - } -  the  - http { - include mime.types; -Default_type application/octet-stream; +  -#log_format Main'$remote _addr-$remote _user [$time _local] "$request"' +#'$status $body _bytes_sent "$http _referer"' A#'"$http _user_agent" "$http _x_forwarded_for "'; at  -#access_log logs/Access.log main; -  - sendfile on; - #tcp_nopush on; -  in#keepalive_timeout0; -Keepalive_timeout $; to  + #gzip on; -  the server { *Listen the; $ server_name localhost;Panax Notoginseng  -#charset koi8-R; the  +#access_log logs/Host.access.log main; A  theLocation/ { + root html; - index index.php index.html index.htm; $         } $  -#error_page404/404. html; -  the# REDIRECT Server error pages to theStaticPage/50x.html -         #WuyiError_page - 502 503 504/50x.html; theLocation =/50x.html { - root html; Wu         } -  About# Proxy The PHP scripts to Apache listening on127.0.0.1: the $         # -#location ~\.php$ { -# Proxy_pass http://127.0.0.1; -         #} A  +# Pass the PHP scripts to FastCGI server listening on127.0.0.1:9000 the         # -Location ~\.php$ { $root/usr/local/nginx/html; theFastcgi_pass127.0.0.1:9000; the Fastcgi_index index.php; the include fastcgi.conf; the         } -  in# Deny access to. htaccess files,ifApache's Document Root the# concurs with Nginx'S One the         # About#location ~/\.ht { the # deny all; the         #} the     } +}
nginx.conf

/usr/local/php/sbin/php-fpm

/usr/local/nginx/sbin/nginx

Sysctl Stop Firewalld.service
Systemctl Stop Firewalld.service

Result

[Email protected] conf]# Curl 127.0.0.1 | Head
% total% Received% xferd Average speed Time Time current
Dload Upload Total spent
56014 0 56014 0 0 9.8M 0--:--:----:--:----:--:--10.6M
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Dtd/xhtml1-transitional.dtd" >
<style type= "Text/css" >
Body {background-color: #ffffff; color: #000000;}
Body, TD, TH, H1, H2 {Font-family:sans-serif;}
Pre {margin:0px; font-family:monospace;}
a:link {color: #000099; text-decoration:none; Background-color: #ffffff;}
a:hover {text-decoration:underline;}
Table {border-collapse:collapse;}
. center {Text-align:center;}

From PHP officail document

The Nginx 1.4.x under Unix system

This document includes instructions and tips for installing and configuring PHP with PHP-FPM for Nginx 1.4.x HTTP servers.

This guide assumes that you have successfully built Nginx from source code, and that the binaries and configuration files are located in /usr/local/nginx. If you use other methods to obtain Nginx, please refer to the»nginx Wiki and complete the installation against this document.

This document contains only the basic configuration of the Nginx server, which will provide the processing power of the PHP application via port 80. If you need instructions for installation beyond the scope of this document, we recommend that you review the documentation for Nginx and PHP-FPM.

Note that this document uses ' X ' to denote the version number, and replace ' X ' with the corresponding version number, depending on the situation.

  1. It is recommended that you visit the Nginx wiki» installation page to obtain and install Nginx on your system.

  2. Get and unzip the PHP source code:

    Tar zxf php-x.x.x
  3. Configure and build PHP. In this step you can customize PHP with a number of options, such as enabling certain extensions. Run the./configure--help command to get a complete list of available options. In this example, we only have a simple configuration that includes PHP-FPM and MySQL support.

    Cd.. /php-x.x.x./configure--enable-fpm--with-mysqlmakesudo make install
  4. Create the configuration file and copy it to the correct location.

    CP PHP.INI-DEVELOPMENT/USR/LOCAL/PHP/PHP.INICP/USR/LOCAL/ETC/PHP-FPM.CONF.DEFAULT/USR/LOCAL/ETC/PHP-FPM.CONFCP Sapi/fpm/php-fpm/usr/local/bin
  5. It should be emphasized that if the file does not exist, it will prevent Nginx from sending the request to the backend PHP-FPM module to avoid a malicious script injection attack.

    Set the configuration item cgi.fix_pathinfo in the php.ini file to 0 .

    Open php.ini:

    Vim/usr/local/php/php.ini

    Navigate to cgi.fix_pathinfo= and modify it to resemble the following:

    Cgi.fix_pathinfo=0
  6. before starting the service, you need to modify the php-fpm.conf configuration file to ensure that the PHP-FPM module runs as Www-data user and Www-data user group.

     vim/usr/local/etc/php-fpm.conf 

    find the following and modify:

    ; Unix User/group of processes; Note:the user is mandatory.       If The group is not set, the default user ' s group; would be used.user = Www-datagroup = Www-data 

    /usr/local/bin/php-fpm 

    This document does not cover further configuration of PHP-FPM, and if you need more information, please consult the relevant documentation.

  7. configure Nginx to support PHP applications:

     vim/usr/local/nginx/conf/nginx.conf 

    modifies the default location block so that it supports. php files:

    location/{root HTML; Index index.php index.html index.htm;} 

    next configuration to ensure that requests for the. php file will be routed to the backend PHP-FPM module, uncomment the default PHP configuration block, and modify to the following:

     #need add root 
    Location ~* \.php$ {Fastcgi_index index.php; Fastcgi_pass 127.0.0.1:9000; Include Fastcgi_params; Fastcgi_param script_filename $document _root$fastcgi_script_name; Fastcgi_param script_name $fastcgi _script_name; }

    restart Nginx.

     sudo/usr/local/nginx/sbin/nginx-s Stopsudo/usr/local/nginx/sbin/nginx 
  8. Create a test file.

    Rm/usr/local/nginx/html/index.htmlecho "<?php phpinfo ()?>" >>/usr/local/nginx/html/index.php


First Nginx & PHP

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.