天天看點

nginx+php配置簡單介紹

1.nginx安裝

下載下傳nginx源碼

。/configre

make  

make test 

make install

就完成安裝了 缺少什麼包就 yum  install  xx

[[email protected] nginx_nocomment]# ./configure --help

  --help                             print this message

  --prefix=PATH                      set installation prefix

  --sbin-path=PATH                   set nginx binary pathname

  --conf-path=PATH                   set nginx.conf pathname

  --error-log-path=PATH              set error log pathname

  --pid-pa

set build directory --with-rtsig_module enable rtsig module --with-select_module enable select module --without-select_module disable select module --with-poll_module enable poll module --without-poll_module disable poll module

以上的這些選項都是configure是可以選擇的具體是幹什麼請看選項後面的解釋:

--prefix=PATH                      set installation prefix

例如這個就是指定安裝的目錄

對于這些選項的詳解如果有興趣請看 《深入了解nginx子產品開發與架構解析》陶輝著 12頁

5.安裝源碼安裝php我安裝的是:http://php.net/  

PHP 5.4.14 and PHP 5.3.24 released!

6.下載下傳玩源碼後:

[[email protected] lnmp]$ cd php-5.4.14

[[email protected] php-5.4.14]$ ./configure  --prefix=/usr/local/php  --enable-fpm

[[email protected] php-5.4.14]$ make

[[email protected] php-5.4.14]$ make test

[[email protected] php-5.4.14]$ make install

其中。/configure後面的  --enable-fpm是開啟fpm這選項用于解析php檔案

在我安裝的過程出出現了 “not find libxml2”

指令安裝:yum search libxml2 

                : yum install libxml  libxml2-devel

然後重複上的指令就可以安裝完成

7.安裝問成後php的配置

我的預設安裝路徑是  /usr/local/php

配置如下:

[[email protected] php-5.4.14]$ cp -a php.ini-production /usr/local/php/lib/php.ini

[[email protected] php-5.4.14]$ cp -a php.ini-production /usr/local/php/etc/php.ini

以上的兩個指令是将下載下傳後解壓好的php-5.4.14中的  php.init-production 檔案拷貝到  相應的目錄中 php.ini-production 檔案是php全局的配置檔案

以上動作完成以後

[[email protected] etc]$ cd /usr/local/php/etc/

将該目錄下的  php-fpm.conf-deauful檔案該名為  php-fpm.conf 

[[email protected] etc]$ ls

pear.conf  php-fpm.conf  php.ini

ls 後會看到以上檔案 此時你就可以啟動 php的檔案解釋程序了

[[email protected] etc]$ /usr/local/php/sbin/php-fpm 

[[email protected] conf]# ps -ef | grep php

root     31512     1  0 12:34 ?        00:00:01 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)

nobody   31513 31512  0 12:34 ?        00:00:00 php-fpm: pool www          

nobody   31514 31512  0 12:34 ?        00:00:00 php-fpm: pool www          

root     32512 31195  0 16:48 pts/1    00:00:00 grep php

這就證明你已經成功啟動php-fpm 程序了。

php的安裝到此以經完成了。下面就是就是和nginx的連結了。通過nginx來解析php檔案

8.nginx配置檔案  在/usr/local/nginx/conf  目錄中

此時啟動nginx 

看看你的nginx啟動了沒有,我開啟了兩個worker程序。

一下是我的nginx.conf 檔案的配置

user  nobody;

worker_processes  2;               //啟動的worker程序數目 最好和cpu核數相當

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;         // worker程序最大處理的連接配接數

}

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" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    #gzip  on;

    server {

        listen       80;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html;             //表示接到請求以後首相會從 /html目錄下通路檔案。

            index  index.html index.htm index.php;

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

location ~ \.php$ {

            proxy_pass   http://127.0.0.1:9000;  //需要指定端口 如果不指定 預設的是 80端口

        }

        # pass the PHP scripts to FastCGI server 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  /usr/local/nginx/html$fastcgi_script_name;

            include        fastcgi_params;

        }

此時隻要你在  /usr/local/nginx/html  目錄下建立一個XXX.php  檔案就可以通過浏覽器可以看到了。

以上就是簡單的過程。