ubuntu版本:Ubuntu 10.04 LTS
1、首先使用apt-get下載Nginx,php,mysql,phpmyadmin,spawn-fcgi。
sudo apt-get install nginx php5-cgi php5-cli mysql-server-5.1 phpmyadmin spawn-fcgi
期間可能要輸入mysql的密碼,按照提示一步一步安裝就是了。
OK後,你在Firefox中訪問http://127.0.0.1/或者http://localhost/應該就能看見Nginx的歡迎介面了。
2、此時Nginx並不能跑PHP程式。需要修改一些設定檔。
$ cd /etc/nginx
$ sudo vim fastcgi_params,修改如下(紅色部分):
fastcgi_ignore_client_abort on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
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 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;
修改Nginx 設定檔nginx.conf
sudo vim nginx.conf,最後如下:
user codebean codebean; #使用者和使用者組
worker_processes 2;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
}
http {
include /etc/nginx/mime.types;
access_log /var/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
3、接下來我們來配置一個預設網站:
cd /etc/nginx/sites-available
sudo vim default
修改後如下:
server {
listen 80 default; #default表示是預設網站
server_name localhost; #訪問的名稱
root /var/www/nginx-default; #網站根目錄
access_log /var/log/nginx/localhost.access.log;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
include fastcgi_params; #這個很重要
}
}
接下來你在目錄/var/www/nginx-default建立一個index.php,輸入:
phpinfo();
然後重啟nginx服務和開啟fastcgi:
$ sudo /etc/init.d/nginx restart
$ /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 /usr/bin/php-cgi
再訪問http://127.0.0.1/或者http://localhost/看看