Ubuntu 下安裝 Nginx ( Nginx + Php5-fpm )

來源:互聯網
上載者:User

標籤:php5   ubuntu   nginx   伺服器軟體   

     nginx 的介紹我就不羅嗦了,先說下系統的安裝環境。


     0. 安裝環境

     系統:Ubuntu 14.04

     PHP 版本:5.5.9

     已有伺服器軟體:Apache (基本不影響,後文會提到)


     1.安裝 Nginx 

sudo apt-get install nginx
     如果你安裝了 Apache ,並且此時 Apache 在運行,那麼請先修改一下設定檔(沒有的同學直接跳過):
#開啟設定檔sudo vim /etc/nginx/sites-available/default

     然後按以下兩處修改(為了不和 Apache 衝突):

server {        #修改這裡 我將 80 改為 88listen 88 default_server;        #還有這裡 同樣改為你想要的監聽連接埠listen [::]:88 default_server ipv6only=on;        .....
      
      此時,我們測試 nginx 是否安裝成功,沒做上述修改的直接在瀏覽器輸入 localhost, 改了的記得加上連接埠,我的是 localhost:88, 如果出現下面介面則安裝成功:


      2.安裝 php5-fpm

      同樣使用 apt-get:

sudo apt-get install php5-fpm


      3. 修改設定檔以支援 PHP

      同樣開啟設定檔:

#開啟設定檔sudo vim /etc/nginx/sites-available/default
      首先,在 index 後添加 index.php:
server {listen 88 default_server;listen [::]:88 default_server ipv6only=on;root /usr/share/nginx/html;        #修改這裡,在 index 後添加 index.phpindex index.php index.html index.htm;# Make site accessible from http://localhost/server_name localhost;        ....
     然後,可以再去掉如下注釋,看清楚是哪裡哦:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000#location ~ \.php$ {#fastcgi_split_path_info ^(.+\.php)(/.+)$;## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini### With php5-cgi alone:#fastcgi_pass 127.0.0.1:9000;## With php5-fpm:fastcgi_pass unix:/var/run/php5-fpm.sock;fastcgi_index index.php;include fastcgi_params;}
     接著進行重啟:

#這一句雖然沒必要但可以給你檢查設定檔的錯誤sudo nginx -s reload#重啟 php5-fpmsudo /etc/init.d/php5-fpm restart #重啟 nginxsudo /etc/init.d/nginx restart 
     4. 測試 

#修改許可權sudo chmod 777 /usr/share/nginx/html/#建立測試檔案vim /usr/share/nginx/html/index.php
     在檔案中輸入以下內容就好:

<?phpphpinfo();?>
      開啟瀏覽器,再次輸入地址,localhost 或 localhost:連接埠 (我是 localhost:88),看到如下介面則成功:



     

     問題:安裝後開啟網頁時出現 “502 Bad Gateway”

     有的同學可能按照網上某些教程安裝之後,測試時顯示 “502 Bad Gateway”,噢,不要緊張,因為教程是錯的而已....

      不少教程是比較久之前的,所以在修改設定檔時,會告訴你注釋得到這三行:

location ~ \.php$ {    fastcgi_pass 127.0.0.1:9000;    fastcgi_index index.php;    include fastcgi_params;}
      這裡的 fastcgi_pass 變數應該是錯的,適用於 PHP 5.3 及以下,在 PHP 5.4 之後,php5-fpm 並不是監聽 9000 連接埠,可以看一下它的設定檔:

#php5-fpm 的設定檔sudo vim /etc/php5/fpm/pool.d/www.conf
     裡面有一段如下,注意最後一行的 listen,原版本是 listen = 127.0.0.1:9000, 但最新的版本是下面的:

; The address on which to accept FastCGI requests.; Valid syntaxes are:;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on;                            a specific port;;   'port'                 - to listen on a TCP socket to all addresses on a;                            specific port;;   '/path/to/unix/socket' - to listen on a unix socket.; Note: This value is mandatory.listen = /var/run/php5-fpm.sock
     所以,所以 502 了.....

     解決方案:

     開啟 nginx 設定檔:

#開啟設定檔sudo vim /etc/nginx/sites-available/default
     然後把 9000 的那句注釋了,換成新的 fast_pass 變數:

location ~ \.php$ {#fastcgi_pass 127.0.0.1:9000;fastcgi_pass unix:/var/run/php5-fpm.sock;fastcgi_index index.php;include fastcgi_params;}
      應該是可以正常工作的,不信試試? 

Ubuntu 下安裝 Nginx ( Nginx + Php5-fpm )

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.