Ubuntu/Debian上安裝Nginx+php環境

  儘管Apache已經佔據半片江山,但不少人仍然在尋找其餘的方式去託管他們的站點,Apache不僅是一個選擇,其餘不少優秀的服務器程序例如lighthttp和nginx也是不錯的選擇。本教程將要向您展示如何在Ubuntu操做系統上面安裝,教程一樣適用在Debian,儘管有一點點小差異,但並無太大影響。怎麼樣,下面咱們開始吧。   0.開始注意   爲了完成教程中提到的操做,咱們假設您已經安裝了一個基本的Debian或者Ubuntu操做系統。怎麼安裝系統這是不一樣的教程了,這裏就再也不詳細說明。本教程主要介紹如何簡單獲取Nginx+php的運行環境。   1.安裝Nginx   第一步要作的就是從庫中下載,這個操做是很是簡單的。php

sudo apt-get install nginx

```  更改默認的虛擬站點配置,文件在:

sudo vim /etc/nginx/sites-available/defaultcss

  一個漂亮的關鍵配置是:

server { listen 80; server_name localhost; access_log /var/log/nginx/localhost.access.log;nginx

Default location

location / {
    root   /var/www;
    index  index.php;
}

Images and static content is treated different

location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
  access_log        off;
  expires           30d;
  root /var/www;
}

Parse all .php file in the /var/www directory

location ~ .php$ {
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    fastcgi_pass   backend;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_param  QUERY_STRING     $query_string;
    fastcgi_param  REQUEST_METHOD   $request_method;
    fastcgi_param  CONTENT_TYPE     $content_type;
    fastcgi_param  CONTENT_LENGTH   $content_length;
    fastcgi_intercept_errors        on;
    fastcgi_ignore_client_abort     off;
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
}

Disable viewing .htaccess & .htpassword

location ~ /\.ht {
    deny  all;
}

}upstream backend { server 127.0.0.1:9000;}web

  2.安裝PHP
  不少站點都使用PHP提供動態內容,他們多是一個wiki一個博客,或者一個論壇什麼的。
  若是你如今運行的是Ubuntu,咱們首先須要解決兩種不一樣的deb包,若是你運行的是amd64版本,那須要更改i386爲amd64
  注意:若是是debian則不須要作這些

cd /tmp wget http://us.archive.ubuntu.com/ubuntu/pool/main/k/krb5/libkrb53_1.6.dfsg.4~beta1-5ubuntu2_i386.deb wget http://us.archive.ubuntu.com/ubuntu/pool/main/i/icu/libicu38_3.8-6ubuntu0.2_i386.deb sudo dpkg -i *.deb數據庫

  下面剩餘部分將在Ubuntu和Debian上面都通用。
  咱們須要增長APT源來增長deb包,因此咱們能夠使用php5.3和php-fpm

sudo echo "deb http://php53.dotdeb.org stable all" >> /etc/apt/sources.listubuntu

 

sudo apt-get updatevim

 

sudo apt-get install php5-cli php5-common php5-suhosin瀏覽器

 

sudo apt-get install php5-fpm php5-cgi安全

  好了,咱們如今已經安裝完了nginx和php
  一個注意點:若是你使用"php的短標記(<?)",你須要在php.ini中開啓(fpm和cli一樣),若是你不改變這些,你將看到代碼以文本的方式顯示。
  3.完成
  重啓nginx服務器

sudo /etc/init.d/nginx restart服務器

 

sudo /etc/init.d/php5-fpm restart

  4.測試
  咱們能夠在/var/www中建立一個index.php的文件,內容以下:

<?php phpinfo(); ?>

  5.問題和最終提示
  若是你沒有看到phpinfo,可能會出現一些錯誤,那能夠執行:

sudo tail /var/log/nginx/error.log

  在默認的配置中nginx是啓用了fastcgi的錯誤日誌的,若是看到一個嚴重的錯誤(例如出現"cannot redeclare class xyz"),nginx能夠看到這個「漂亮的」錯誤頁面,那多是一些錯誤發生了。
  若是這樣你能夠關閉,若是php5-fpm不能運行,你的php文件將不會被解析,而且nginx會顯示一個錯誤頁面。
  好了,我猜你已經完成了,而且如今使用nginx做爲你的web服務器,nginx是一個小巧、漂亮而且你能夠配置不少東西的一個東東,若是你須要僞靜態,注意nginx不會運行 .htaccess 文件,你須要僞靜態更改vhost配置。
  若是對在Linux下感興趣的朋友能夠查看《Linux下的DedeCMS站點高級安全策略》
  翻譯:天涯
  原文:http://www.howtoforge.com/installing-php-5.3-nginx-and-php-fpm-on-ubuntu-debian  
相關文章
相關標籤/搜索