安装
一、下载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的内容;