WinPC搭建nginx服务器的实现步骤
下面是WinPC搭建nginx服务器的实现步骤的完整攻略,同时包含两个实例说明。
步骤一:安装nginx
- 下载Windows版的nginx,建议选择稳定版本
- 解压缩到指定目录,例如
D:\nginx - 进入
D:\nginx目录,双击nginx.exe打开nginx
步骤二:配置nginx
nginx的配置文件为D:\nginx\conf\nginx.conf,具体配置如下:
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
具体说明:
listen 80;表示监听80端口,也就是http协议的默认端口server_name localhost;表示当客户端请求的域名为localhost时,使用该虚拟主机处理请求location / {}表示匹配所有请求,根据root和index指定的路径和文件名,返回响应内容
示例一:发布静态网页
假设在D:\nginx\html目录下存放了一个静态网页文件index.html,使用nginx发布该静态网页的步骤如下:
- 将
D:\nginx\html\index.html复制到D:\nginx\html目录下 - 重启nginx服务:进入
D:\nginx目录,双击nginx.exe打开nginx,点击任务栏图标,选择quit停止nginx,双击nginx.exe重新打开nginx服务 - 打开浏览器,访问
http://localhost:80/,即可看到静态网页的内容
示例二:运行PHP脚本
假设在D:\nginx\php目录下存放了一个PHP脚本文件index.php,使用nginx运行该脚本的步骤如下:
- 在
nginx.conf文件中添加如下配置:
location /php/ {
root D:/nginx;
index index.php index.html index.htm;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME D:/nginx/$fastcgi_script_name;
include fastcgi_params;
}
其中/php/表示匹配请求路径,root D:/nginx;表示根目录为D:/nginx,fastcgi_pass表示将请求转发到本地的PHP解释器,fastcgi_index表示默认脚本为index.php,fastcgi_param表示设置参数,include表示引入配置文件
-
将
D:\nginx\php\index.php复制到D:\nginx\php目录下 -
安装php运行环境,例如安装PHP7.0,将解压缩的PHP文件夹复制到
D:\php目录下,并配置php.ini文件,具体配置文件请自行搜索。 -
安装php-cgi并配置fastcgi组件,具体配置文件也是请自行搜索。
-
重启nginx服务:进入
D:\nginx目录,双击nginx.exe打开nginx,点击任务栏图标,选择quit停止nginx,双击nginx.exe重新打开nginx服务 -
打开浏览器,访问
http://localhost:80/php/index.php,即可运行PHP脚本并查看结果。
希望这个WinPC搭建nginx服务器实现步骤的完整攻略对您有帮助!
