在FreeBsd上搭建nginx + php環境,雖然難度不小,可是由於使用曆史的問題,網路關於這方面的經驗文章還是比較多的,隨著Ubuntu的逐漸推廣,研究下如何在ubuntu下搭建nginx + php環境還是有必要的,方便有這方面需求的朋友,並且可以推動Ubuntu的普及,其實,在Ubuntu下搭建nginx + php環境比FreeBsd下速度要快得多,因為在FreeBsd我是用ports方式安裝,需要編譯,Ubuntu下直接apt-get install 的方式安裝,速度很快,下面就來開始安裝吧!
1,安裝nginx,執行以下命令,很快完成,不過目前apg-get方式安裝預設是0.5.33的版本
sudo apt-get install nginx
設定檔預設安裝位置:
conf: /etc/nginx/nginx.conf
bin:/usr/sbin/nginx
vhost: /etc/nginx/sites-enable/default
cgi-params: /etc/nginx/fastcgi-params
例:建一個虛擬Server
server {
listen 80;
server_name www.yourdomain.com; #根據自己的需要改成相應網域名稱
access_log /var/log/nginx/home.ucenter.access.log;location / {
root /var/www/yourdomain.com;
index index.php;location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/yourdomain.com$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
2,安裝php-cgi模組
執行sudo apt-get install php5-cgi
設定檔預設安裝位置:
php-cgi: /usr/bin/php-cgi
php5-cgi: /usr/bin/php5-cgi
cgi config: /usr/bin/cgi/php.ini
修改php.ini檔案的cgi.fix_pathinfo資料為1,預設為0 cgi.fix_pathinfo=1; 這樣php-cgi方能正常使用SCRIPT_FILENAME這個變數
3,安裝spawn-fcgi spawn-fcgi是lighttpd的一個用來控制php-cgi的工具.
如果系統沒有安裝GCC編譯環境,www.6688.cc剛需要在安裝lighttpd之前要安裝build-essential工具包,執行以下命令
sudo apt-get install build-essential
wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz
tar -xvf lighttpd-1.4.19.tar.gz
cd lighttpd-1.4.19/
sudo apt-get install libpcre3-dev
./configure –without-zlib –without-bzip2
make
sudo cp src/spawn-fcgi /usr/local/bin/spawn-fcgi
這樣cgi控制器就安裝完成.
4.啟動測試系統.啟動fast_cgi:
spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-cgi
注意:ip,連接埠與nginx伺服器中的cgi-pass要對應. -C表示開啟幾個cgi進程
啟動nginx
sudo /etc/init.d/nginx start
好了,如果沒有出錯資訊,則說明配置成功了,現在寫個phpinfo測試下吧!
最後,附上我的/etc/nginx/sites-enable/default的設定檔,此設定檔啟用了rewrite功能
server {
listen 80;
server_name localhost;access_log /var/log/nginx/localhost.access.log;location / {
root /var/www/nginx-default;
index index.php;if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /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 /var/www/nginx-default;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
#proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /.ht {
#deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#listen 8000;
#listen somename:8080;
#server_name somename alias another.alias;
#location / {
#root html;
#index index.html index.htm;
#}
#}
# HTTPS server
#
#server {
#listen 443;
#server_name localhost;
#ssl on;
#ssl_certificate cert.pem;
#ssl_certificate_key cert.key;
#ssl_session_timeout 5m;
#ssl_protocols SSLv2 SSLv3 TLSv1;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#ssl_prefer_server_ciphers on;
#location / {
#root html;
#index index.html index.htm;
#}
#}
怎麼樣?是不是快速又方便,快試試吧,打算做WordPress的朋友可以試試哦,nginx + php效率可比Apache + php高哦!