安裝
一、下載nginx;下載鏈接http://nginx.org/download/nginx-1.0.11.zip;
二、將其進行解壓,放到你自己喜歡的目錄下,已D盤爲例;
三、使用進入nginx文件夾下,
四、啓動nginx,start nginx.exe;(在使用nginx時,不要啓動Apache)
nginx -s stop 強制停止
nginx -s reload 重新加載配置文件(配置文件發生變化的時候會用到)
nginx -s quit 退出
五、運行nginx;在瀏覽器中輸入http://127.0.0.1,看到nginx的歡迎頁面,守門你已安裝成功了;
配置
用過Apache的知道,有個叫httpd.conf的配置文件,那麼nginx中也有一個類似的文件,文件名爲nginx.conf
下面來看看這個文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include D:/nginx/conf/mime.types;//這段代碼不能註釋,不然,css不能被加載
#default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include D:/nginx/conf/VirtualHost/*.conf;
}
該文件中有個worker_connections:最大連接數,可以隨便填(只要cpu給力)
原來一開始打開的時候default_type,include是沒有被註釋的,這邊爲啥要進行註釋呢?如果不進行註釋的話,訪問文件的時候,文件內容不會在頁面上進行顯示,而被另存爲一個文件了。
重點在 D:/nginx/conf/VirtualHost/test.conf;這個目錄是自己創建的,打開這個文件
文件內容爲
server {
listen 80;
server_name www.testapp.com; //虛擬域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root D:/wamp/www/;//根目錄
index default.php index.php index.html index.htm;//入口文件
rewrite ^/(\w+)\.html$ /$1.php;
rewrite ^/(\w+)/(\w+)$ /$1/$2.php;
}
error_page 404 /error.html;
error_page 500 502 503 504 /50x.html; //錯誤目錄
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root D:/wamp/www/;//根目錄
fastcgi_pass 127.0.0.1:9000;//請求端口
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
修改的只有虛擬域名和根目錄,其他的默認就好了(細心的會發現,其實這個文件內容和nginx.conf下的sever{........}類似,這樣寫的好處就是,以後不要在修改nginx.conf中的內容,只要在VirtualHost文件夾小新建conf文件就好了,不會影響其他的文件)
將nginx和php進行聯合
在命令行中輸入php-cgi.exe -b 127.0.0.1:9000 ,監聽從nginx中發來的請求(如果需要與mysql數據庫想結和,請修改php.ini中的內容(安裝mysql擴展))
測試
在www的目錄下新建一個index.php文件內容爲<?php phpinfo();?>
在瀏覽器中運行虛擬域名,會看到phpinfo的內容;