Centos Nginx+PHP Install 史上最完美

來源:互聯網
上載者:User

標籤:安裝   nginx+php/yum   

周氏一族,整理技術文檔,給下一代留點教程...

Nginx 的功能就不多說了,不懂的請直接百度,Nginx + php 安裝是比較負責,經常會出錯,網上也是大家說的千篇一律,沒有一個比較完美的做法

下面是我自己研究的一種最簡單化的安裝方式,記錄一下,以防自己忘記。

先說明哦,這個是用yum安裝,但是他們是公用yum,而不像哪些人,要把機器的repo修改掉,再來yum,我這個yum是所有機器,不需要修改任何yum源就行的,也就是公用資源yum,但是nginx,必須用源碼,源碼到官網擷取


1、安裝Nginx+php之前呢,需要先安裝PHP,我一條命令搞定,大家請看

[[email protected]_server ~]# yum -y install gcc make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel php php-devel php-fpm

千萬不要去學人家原始碼搞的,一塌糊塗,又是php,又是php-fpm,很糟糕,直接yum搞定,又穩定,除非你是需要修改php的原始碼,那麼,你就開始蛋疼的php-fpm原始碼吧!


2、查看一下php是否安裝成功,很明顯,直接看看php-fpm有沒有起來

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/41/52/wKioL1PTBEOjMVwlAAEzhpkN9oA677.jpg" title="QQ20140726091305.png" alt="wKioL1PTBEOjMVwlAAEzhpkN9oA677.jpg" />

最好是把他設定成開機,自動啟動,這個你懂得,這麼簡單的事情


3、下面開始安裝Nginx,安裝之前,先安裝一下環境,yum即可

[[email protected]_server ~]# yum -y install pcre* openssl* zlib*


4、上傳Nginx檔案到指定目錄,/usr/tmp下面

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/41/52/wKiom1PTBIPjnHqLAAC9a72GpTQ520.jpg" title="QQ20140726091945.png" alt="wKiom1PTBIPjnHqLAAC9a72GpTQ520.jpg" />


5、開始安裝

[[email protected]_server ~]# cd nginx-1.6.0

[[email protected]_server nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=apache --group=apache --with-http_ssl_module --with-http_gzip_static_module --with-http_spdy_module --with-http_stub_status_module --with-pcre

[[email protected]_server nginx-1.6.0]# make && make install

注意咯,這裡configure,因為php採用yum安裝,那麼php官網的預設使用者和組使用者都是apache,所以,我們就要保證nginx的運行使用者也是apache,故在編譯的時候,就要指定--user=apache --group=apache


6、啟動nginx

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/41/52/wKiom1PTBdTCMoyiAAFHngkqd5Q132.jpg" title="QQ20140726092521.png" alt="wKiom1PTBdTCMoyiAAFHngkqd5Q132.jpg" />

網頁開啟測試看看

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/41/52/wKioL1PTBzigBRwmAAF78dOqzd0223.jpg" title="QQ20140726092634.png" alt="wKioL1PTBzigBRwmAAF78dOqzd0223.jpg" />


7、測試PHP,按照Apache正常思路,我們直接上傳一個php檔案到nginx目錄,應該就能開啟

[[email protected]_server ~]# cd /usr/local/nginx/html/

[[email protected]_server html]# touch test.php

[[email protected]_server html]# vi test.php 

<?php

phpinfo();

?>

現在網頁開啟看看,有沒有問題

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/41/52/wKioL1PTCHOxno30AAGrkOO6Po0249.jpg" title="QQ20140726093146.png" alt="wKioL1PTCHOxno30AAGrkOO6Po0249.jpg" />


竟然,提示我下載?那就是說,php它還沒解析到,如果可以解析到,或者說支援的話,它是直接執行php內部代碼的,所以,得改。因為nginx預設只是解析html,這也就是為什麼本文標題 nginx+php


8、修改配置,完成php+nginx

[[email protected]_server html]# vi /usr/local/nginx/conf/nginx.conf


user  nobody;      改成  user apache apache;

worker_processes  2;     你伺服器cpu有多少個就搞多少個吧,越多越好


        #location ~ \.php$ {

        #    root           html;

        #    fastcgi_pass   127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        #}


把他們的前面  #  號全部去掉,然後修改成下面

        location ~ \.php$ {

            root           html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

            include        fastcgi_params;

        }

至於為什麼要這樣改,你可以自己測試一下,我這裡不講

看到前面這個配置,我想大家現在的思路應該比較清晰了,首先是調用9000連接埠服務,也就是php-fpm,所以說嘛,php對nginx來說,就只不過是一個模組來的而已。


9、讓nginx重新載入一下配置

[[email protected]_server html]# /usr/local/nginx/sbin/nginx -s reload

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/41/52/wKioL1PTCqyApveUAAMN-nXmmUk174.jpg" title="QQ20140726094118.png" alt="wKioL1PTCqyApveUAAMN-nXmmUk174.jpg" />


10、搞定



本文出自 “周氏一族” 部落格,謝絕轉載!

Centos Nginx+PHP Install 史上最完美

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.