搜尋
首頁運維NginxDebian系統下怎麼為PHP程式設定Nginx伺服器

    nginx安裝方法:

    1 apt網路安裝

       修改原始清單

vi /etc/apt/sources.list
#加入以下源
deb http://nginx.org/packages/debian/ squeeze nginx
deb-src http://nginx.org/packages/debian/ squeeze nginx

   

apt-key add nginx_signing.key
apt-get update
apt-get install nginx

    2 原始碼編譯安裝

       先安裝編譯環境, 由於nginx在後續的使用中會需要使用perl正規、壓縮演算法、ssl等特性,所以我們需要事先安裝相關函式庫檔案。

apt-get install build-essential 
apt-get install libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev libssl0.9.8

        下載最新的穩定版nginx

wget http://nginx.org/download/nginx-1.2.3.tar.gz

 #     
       查看編譯可選擇的設定參數(以下只是一些常用的設定項目):

     

--prefix=path             預設為/usr/local/nginx


--sbin-path=path                nginx可執行指令檔案的路徑,沒有指定的話,預設/sbin/#6; conf-發現錯誤日誌路徑時,預設<prefix>/logs/error.log</prefix></prefix></p> <p>--http-log-path=path         定義被存取檔案的日誌存放路徑,若在nginx.conf中沒有使用access_log指定,預設為<prefix>/logs/access.log</prefix></p> <p>--pid-path=path                   nginx.pid</p> <p>--lock-path=path                   鎖定檔案存放路由,且在nginx.conf中沒有指定,則預設為/logs#nginx/loglock# -user=user               地運作   如果在nginx.conf中沒有使用user定義進程運行的屬組。 ##--with-select_module               以select模式,若目前平台沒有其他有效模式,則預設編譯</p> <p>--without-select_module            禁止select模式</p> <p>--with-poll_module       </p>##--without-poll_module              禁止poll模式<p></p>--with-file-aio                             開啟 ipv6支援<p></p> --with-debug                       開啟debug<p></p>#以下這些模組預設沒有開啟,可使用--with <p>  - -with-http_realip_module          後端記錄客戶端 ip.</p>##  --with-http_addition_module        enable  開啟flv模組<p></p> <p>  --with-http_mp4_module             開啟mp4模組 </p> <p>  --with-http_gzip_static_module    </p> <p>  --with-http_stub_status_module     開啟狀態檢視模組</p> <p>  以下模組預設是開啟的,可以使用--without選項關閉相關模組</p> <p>  --without-http_charset_module      disable ngx_http_charset_module## gzip壓縮模組</p> <p>  --without-http_ssi_module          停用ssl模組</p> <p>  --without-http_userid_module     禁用access模組</p> <p> --without-http_auth_basic_module   停用auth_basic認證模組</p> <p>  --without-http_autoindex_module    停用列目錄模組</p> <p>  --without-http_geo_ule#with .禁用       禁用map模塊</p> <p>  --without-http_referer_module      disable ngx_http_referer_module</p> <p>  --without-http_rewrite_module      禁用重定向模塊</p> <p>  --without-http_proxy_module        禁用代理模塊</p> <p> --without-http_fastcgi_module      禁用fastcgi模組</p> <p>  --without-http_uwsgi_module        disable ngx_http_uwsgi_module</p>##1 #1_http module<p></p>  --without-http_memcached_module    disable ngx_http_memcached_module<p></p>#  --without-http_limit_conn_module   disable ngx_http_limit_conn_module<p></p>  --without-http_limit_req_module    disable ngx_http_limit_rem_req_module#. gif_module<p></p>  --without-http_browser_module      disable ngx_http_browser_module<p></p>  --without-http_upstream_ip_hash_module    停用upstream模組<p></p> <p> --with-http_perl_module            开启perl模块<br>  --with-perl_modules_path=path      设置perl模块路径<br>  --with-perl=path                   为perl库设置路径<br>  --http-client-body-temp-path=path  set path to store http client request body temporary files<br>  --http-proxy-temp-path=path        set path to store http proxy temporary files<br>  --http-fastcgi-temp-path=path      set path to store http fastcgi temporary files<br>  --http-uwsgi-temp-path=path        set path to store http uwsgi temporary files<br>  --http-scgi-temp-path=path         set path to store http scgi temporary files<br>  --without-http                     禁用http服务<br>  --without-http-cache               禁用http cache<br>  --with-mail                       开启mail服务<br>  --with-mail_ssl_module             在mail服务中开启ssl<br>  --without-mail_pop3_module         disable ngx_mail_pop3_module<br>  --without-mail_imap_module         disable ngx_mail_imap_module<br>  --without-mail_smtp_module         disable ngx_mail_smtp_module<br>  --with-google_perftools_module     开启google_perftools模块<br>  --with-cpp_test_module             开启cpp_test模块<br>  --add-module=path                  enable an external module<br>  --without-pcre                     禁止使用perl正则库<br>  --with-pcre                        强制使用perl正则库</p> <p><strong>     编译安装</strong></p><pre class='brush:php;toolbar:false;'>./configure --prefix=/etc/nginx/ --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-file-aio --with-ipv6

    结果如下图:

Debian系統下怎麼為PHP程式設定Nginx伺服器

  编译并安装

make && make install

     ps:fastcgi回顾
     fastcgi是一个可伸缩地、高速地在http server和动态脚本语言间通信的接口。多数流行的http server都支持fastcgi,包括apache、nginx和lighttpd等,同时,fastcgi也被许多脚本语言所支持,其中就有php。fastcgi是从cgi发展改进而来的。传统cgi接口方式的主要缺点是性能很差,因为每次http服务器遇到动态程序时都需要重新启动脚本解析器来执行解析,然后结果被返回给http服务器。这在处理高并发访问时,几乎是不可用的。另外传统的cgi接口方式安全性也很差,现在已经很少被使用了。fastcgi接口方式采用c/s结构,可以将http服务器和脚本解析服务器分开,同时在脚本解析服务器上启动一个或者多个脚本解析守护进程。当http服务器每次遇到动态程序时,可以将其直接交付给fastcgi进程来执行,然后将得到的结果返回给浏览器。这种方式可以让http服务器专一地处理静态请求或者将动态脚本服务器的结果返回给客户端,这在很大程度上提高了整个应用系统的性能。
    nginx不支持对外部程序的直接解析,所有的外部程序(包括php)必须通过fastcgi接口来调用。fastcgi接口在linux下是socket,(这个socket可以是文件socket,也可以是ip socket)。为了调用cgi程序,还需要一个fastcgi的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当nginx将cgi请求发送给这个socket的时候,通过fastcgi接口,wrapper接纳到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过fastcgi接口,沿着固定的socket传递给nginx;最后,nginx将返回的数据发送给客户端,这就是nginx+fastcgi的整个运作过程。

Debian系統下怎麼為PHP程式設定Nginx伺服器

php-fpm

php5.3版本源码已经默认支持php-fpm了,但是debian6认为它还没经过广泛的测试,所以在debian6的软件仓库中,虽然php版本为5.3.3,但是却没包含php-fpm,如果不想手工编译安装php的话可以换一个源。
    
     修改源列表

vi /etc/apt/sources.list
deb http://packages.dotdeb.org stable all
deb-src http://packages.dotdeb.org stable all

    更新源列表,安装php5-fpm

apt-get update wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | apt-key add -
apt-get install php5-fpm

    安装其他常用php5组件

apt-get install php5 php5-cgi php5-cli php5-mysql php5-memcache

    启动php-fpm

/etc/init.d/php5-fpm start

    这样的话最基本的nginx+php环境就搭建完毕了。   

nginx配置文件粗解
nginx的配置文件结构类似下图这样的结构:

Debian系統下怎麼為PHP程式設定Nginx伺服器

配置文件主要参照编译完成后生成的默认配置文件。

   主模块的配置选项

    user指令设置进程以什么用户运行,在源码编译安装时指定的nginx用户,如果在编译时没有指定,默认是nobody账户,在配置文件中此行处于注释状态,  user 指令可以设置两个参数,第一个指定进程所属用户,第二个是可选,指定进程所属组

  user nginx nobody;

    设置工作进程数,一个工作进程为一个单线程,在cpu密集型环境中,可以设置worker_processes数目为cpu核数

  worker_processes 4;

    指定nginx错误日志文件的位置,如果要禁止错误日志使用error_log /dev/null,error_log可以存在于不同的字段main、http、server等,文件后面可以指定记录的日志的默认等级。

  error_log logs/error.log;

    设置pid文件路径,可以使用kill命令发送相关信号

  pid    logs/nginx.pid;

    event模块配置选项,event模块主要控制nginx处理连接的方式

  events {

  #如果在configure时指定的不止一个事件模型,可以通过use告诉nginx要使用哪一个模型:seletc、poll、kqueue、epoll、rtsig、/dev/poll、eventport等

  use epoll;

  #worker_connections和worker_processes可以计算你的理论最大链接数, worker_connections*worker_processes

  worker_connections 1024;

}

    http模块里面主要是对http服务器相关属性进行设置

 http {

  #可以用include指令包含一些其他文件,支持通配符,可以使用绝对路径,也可以使用相对路径,相对路径以nginx.conf为根据

  include    mime.types;

  #设置默认的mime类型

  default_type application/octet-stream;

  #描述记录日志的格式,在定义格式时可以使用一些变量,各个变了的含义可以从各个模块汇总查找

  log_format main &#39;$remote_addr - $remote_user [$time_local] "$request" &#39;

           &#39;$status $body_bytes_sent "$http_referer" &#39;

           &#39;"$http_user_agent" "$http_x_forwarded_for"&#39;;

  #指定访问日志的路径和格式,缓冲区大小

  access_log logs/access.log main;

  #sendfile拷贝文件在内核态完成,更加高效

  sendfile    on;

  #tcp_nopush   on;

  #可以设置两个值,第一个表示客户端与服务器长连接的超时时间,超过这个时间,服务器将关闭连接。第二个值指定的应答头中keep-alive中timeout的值,让浏览器知道什么时候关闭连接。

  keepalive_timeout 65;

  #开启gzip压缩

  gzip on;

  #在三次握手时,发送给客户端应答后的超时时间,目前还没进入连接状态,只完成了两次握手,如果在规定时间没收到应答包,nginx将关闭链接

  send_timeout 30

  server {

     xxx

  }

}

    server模块嵌在http模块中,主要用来配置虚拟主机

  server {

    #指定server字段中可以被访问到的ip地址及端口

    listen    80;

    #将http请求的主机头与server中的server_name参数进行匹配,并找出第一个结果,如果没有server_name参数匹配上,则第一个出现listen的server将被匹配,多域名用空格分割

    server_name www.nginx.com;

    #设个指令是应答头重的content-type字段使用指定的编码集,off表示不在应答头重添加content-type信息

    charset off;

    #指定www.nginx.com域名的访问日志路径及格式

    access_log logs/host.access.log main;

    #如果在url中没有指定文件,则设置一个默认主页,可以设置多个文件,空格分开,可以在http、server、location中设置

    index index.php index.htm;

    #根据url的不同需求进行配置,可以使用字符串和正则匹配,最确切的匹配被使用,搜索到第一个后会停止

    # ~* 不区分大小写;~ 区分大小写;^~ 禁止在字符串匹配后检查正则;= 在url和location之间精确匹配,匹配完成后不做额外搜索。

    location /i/ {

      #请求到达后的文件根目录,在请求中root会把location匹配的值加到root指定的值后面,请求/i/a.php,则会是/html/i/a.php响应

      root  html;

      #在location中设置index

      index index.html index.htm;

    }

    #为错误代码指定相应的错误界面,可以用在http、server、location字段中。

    error_page 404       /404.html;

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

    error_page  500 502 503 504 /50x.html;

    #精确匹配50x.html,真实响应是/html/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;

    }

    #配置php脚本传至fastcgi

    location ~ \.php$ {

     root      html;

     fastcgi_pass  127.0.0.1:9000;

     fastcgi_index index.php;

     #/scripts是php脚本所在的目录

      fastcgi_param script_filename /scripts$fastcgi_script_name;

      include    fastcgi_params;

    }

    #拒绝访问.htaccess文件

    location ~ /\.ht {

      deny all;

    }

  }

以上是Debian系統下怎麼為PHP程式設定Nginx伺服器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
NGINX與Apache:性能,可伸縮性和效率NGINX與Apache:性能,可伸縮性和效率Apr 19, 2025 am 12:05 AM

NGINX和Apache都是強大的Web服務器,各自在性能、可擴展性和效率上有獨特的優勢和不足。 1)NGINX在處理靜態內容和反向代理時表現出色,適合高並發場景。 2)Apache在處理動態內容時表現更好,適合需要豐富模塊支持的項目。選擇服務器應根據項目需求和場景來決定。

終極攤牌:nginx vs. apache終極攤牌:nginx vs. apacheApr 18, 2025 am 12:02 AM

NGINX適合處理高並發請求,Apache適合需要復雜配置和功能擴展的場景。 1.NGINX採用事件驅動、非阻塞架構,適用於高並發環境。 2.Apache採用進程或線程模型,提供豐富的模塊生態系統,適合複雜配置需求。

nginx行動:示例和現實應用程序nginx行動:示例和現實應用程序Apr 17, 2025 am 12:18 AM

NGINX可用於提升網站性能、安全性和可擴展性。 1)作為反向代理和負載均衡器,NGINX可優化後端服務和分擔流量。 2)通過事件驅動和異步架構,NGINX高效處理高並發連接。 3)配置文件允許靈活定義規則,如靜態文件服務和負載均衡。 4)優化建議包括啟用Gzip壓縮、使用緩存和調整worker進程。

NGINX單元:支持不同的編程語言NGINX單元:支持不同的編程語言Apr 16, 2025 am 12:15 AM

NGINXUnit支持多種編程語言,通過模塊化設計實現。 1.加載語言模塊:根據配置文件加載相應模塊。 2.應用啟動:調用語言運行時執行應用代碼。 3.請求處理:將請求轉發給應用實例。 4.響應返回:將處理後的響應返回給客戶端。

在Nginx和Apache之間進行選擇:適合您的需求在Nginx和Apache之間進行選擇:適合您的需求Apr 15, 2025 am 12:04 AM

NGINX和Apache各有優劣,適合不同場景。 1.NGINX適合高並發和低資源消耗場景。 2.Apache適合需要復雜配置和豐富模塊的場景。通過比較它們的核心特性、性能差異和最佳實踐,可以幫助你選擇最適合需求的服務器軟件。

nginx怎麼啟動nginx怎麼啟動Apr 14, 2025 pm 01:06 PM

問題:如何啟動 Nginx?答案:安裝 Nginx啟動 Nginx驗證 Nginx 是否已啟動探索其他啟動選項自動啟動 Nginx

怎麼查看nginx是否啟動怎麼查看nginx是否啟動Apr 14, 2025 pm 01:03 PM

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎麼關閉nginx怎麼關閉Apr 14, 2025 pm 01:00 PM

要關閉 Nginx 服務,請按以下步驟操作:確定安裝類型:Red Hat/CentOS(systemctl status nginx)或 Debian/Ubuntu(service nginx status)停止服務:Red Hat/CentOS(systemctl stop nginx)或 Debian/Ubuntu(service nginx stop)禁用自動啟動(可選):Red Hat/CentOS(systemctl disable nginx)或 Debian/Ubuntu(syst

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器