nginx結合fastcgi

wadeson發表於2017-07-18

1、首先安裝nginx,這裡採用編譯安裝

useradd -M -s /sbin/nologin nginx
安裝一些依賴包:
yum -y install pcre-devel libxslt-devel gd gd-devel 
GeoIP GeoIP-devel 最後的這兩個包,需要epel源進行安裝
 
note:epel源安裝
cd /etc/yum.repos.d
rpm -ivh epel-release-latest-6.noarch.rpm
 
2、nginx的安裝
cd /root/tools
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar xf nginx-1.8.1.tar.gz
cd nginx-1.8.1
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module
make && make install
 
3、php的安裝,這裡不做介紹,看部落格以前的文章即可
 
4、結合fastcgi協議
啟用nginx配置檔案支援fastcgi:
location / {
root html;
index index.html index.htm index.php; 第一步修改
}

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME html$fastcgi_script_name;第二步修改

include fastcgi_params;
}

fastcgi_param指令指定放置PHP動態程式的主目錄,也就是$fastcgi_script_name前面指定的路徑,這裡是/usr/local/nginx/html目錄,建議將這個目錄與Nginx虛擬主機指定的根目錄保持一致,當然也可以不一致
由於nginx編譯安裝,nginx的網站根目錄在/usr/local/nginx/html,相對路徑為html
所以這裡設定html
 
然後在fastcgi_params配置檔案中加入以下一行:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;第三步修改
修改後的fastcgi_params檔案如下:

[root@wadeson nginx]# cat conf/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_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;這裡是新增的一行
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;

執行完上面三步之後,編寫php檔案確認是否成功:

[root@wadeson nginx]# cat html/index.php
<?php
phpinfo();
?>

訪問瀏覽器檢視效果:

note:如果需要在現有的基礎上編譯一個新的模組,那麼重新執行configure,後面加上引數

編譯引數之後加上--add-module=PATH
重新再次編譯一次

相關文章