1. 程式人生 > >php關聯Apache和nginx

php關聯Apache和nginx

rect connect close scripts 使用 數據 apache配置 ace ring

編輯apache配置文件httpd.conf,以apache支持php

vim /etc/httpd/httpd.conf
添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

定位至DirectoryIndex index.html

修改為:
DirectoryIndex index.php index.html

而後重新啟動httpd,或讓其重新載入配置文件即可測試php是否已經可以正常使用。

編輯nginx配置文件nginx.conf,以nginx支持php

編輯/etc/nginx/nginx.conf,啟用如下選項:
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;
}

編輯/etc/nginx/fastcgi_params,將其內容更改為如下內容:
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
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_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $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 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格式的主頁,類似如下:
location / {
root html;
index index.php index.html index.htm;
}

而後重新載入nginx的配置文件:
# service nginx restart

在網頁根目錄下新建index.php的測試頁面,測試php是否能正常工作:
# cat > /usr/html/index.php << EOF
<?php
phpinfo();
?>

這個可以查看PHP的信息;

測試頁面index.php示例如下:
<?php
$link = mysql_connect(‘127.0.0.1‘,‘root‘,‘123.abc‘);
if ($link)
echo "Success...";
else
echo "Failure...";

mysql_close();
?>

這個可以查看PHP和數據庫是否鏈接成功

php關聯Apache和nginx