Window7 下 WNMP 环境搭建
WNMP 指“Windows 下的 Nginx、MariaDB/MySQL 和 PHP 环境”。
下载
- Nginx:http://nginx.org/en/download.html
- MariaDB:https://downloads.mariadb.org/
- PHP:http://windows.php.net/download
- RunHiddenConsole:http://redmine.lighttpd.net/attachments/660/RunHiddenConsole.zip
在 D 盘根目录下创建目录 wnmp。将 nginx-1.13.0.zip
、mariadb-10.1.23-winx64.msi
和 php-7.1.5-nts-Win32-VC14-x64.zip
放在里面。
C:\>mklink /d C:\wnmp D:\wnmp
为 C:\wnmp <<===>> D:\wnmp 创建的符号链接
现在在 C 盘根目录下的 wnmp 中操作。
PHP
php.ini-development
改为 php.ini
、编辑它。
; On windows:
; extension_dir = "ext"
; 改为
; On windows:
extension_dir = "C:/wnmp/php7.1.5/ext"
; Windows Extensions
; Note that ODBC support is built in, so no dll is needed for it.
; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5+)
; extension folders as well as the separate PECL DLL download (PHP 5+).
; Be sure to appropriately set the extension_dir directive.
;
;extension=php_bz2.dll
;extension=php_curl.dll
;extension=php_fileinfo.dll
;extension=php_ftp.dll
;extension=php_gd2.dll
;extension=php_gettext.dll
;extension=php_gmp.dll
;extension=php_intl.dll
;extension=php_imap.dll
;extension=php_interbase.dll
;extension=php_ldap.dll
;extension=php_mbstring.dll
;extension=php_exif.dll ; Must be after mbstring as it depends on it
;extension=php_mysqli.dll
;extension=php_oci8_12c.dll ; Use with Oracle Database 12c Instant Client
;extension=php_openssl.dll
;extension=php_pdo_firebird.dll
;extension=php_pdo_mysql.dll
;extension=php_pdo_oci.dll
;extension=php_pdo_odbc.dll
;extension=php_pdo_pgsql.dll
;extension=php_pdo_sqlite.dll
;extension=php_pgsql.dll
;extension=php_shmop.dll
; The MIBS data available in the PHP distribution must be installed.
; See http://www.php.net/manual/en/snmp.installation.php
;extension=php_snmp.dll
;extension=php_soap.dll
;extension=php_sockets.dll
;extension=php_sqlite3.dll
;extension=php_tidy.dll
;extension=php_xmlrpc.dll
;extension=php_xsl.dll
; 改为
; Windows Extensions
; Note that ODBC support is built in, so no dll is needed for it.
; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5+)
; extension folders as well as the separate PECL DLL download (PHP 5+).
; Be sure to appropriately set the extension_dir directive.
;
extension=php_bz2.dll
extension=php_curl.dll
extension=php_fileinfo.dll
;extension=php_ftp.dll
extension=php_gd2.dll
extension=php_gettext.dll
extension=php_gmp.dll
extension=php_intl.dll
extension=php_imap.dll
;extension=php_interbase.dll
extension=php_ldap.dll
extension=php_mbstring.dll
extension=php_exif.dll ; Must be after mbstring as it depends on it
extension=php_mysqli.dll
;extension=php_oci8_12c.dll ; Use with Oracle Database 12c Instant Client
extension=php_openssl.dll
;extension=php_pdo_firebird.dll
extension=php_pdo_mysql.dll
;extension=php_pdo_oci.dll
;extension=php_pdo_odbc.dll
;extension=php_pdo_pgsql.dll
extension=php_pdo_sqlite.dll
;extension=php_pgsql.dll
;extension=php_shmop.dll
; The MIBS data available in the PHP distribution must be installed.
; See http://www.php.net/manual/en/snmp.installation.php
;extension=php_snmp.dll
extension=php_soap.dll
extension=php_sockets.dll
extension=php_sqlite3.dll
;extension=php_tidy.dll
extension=php_xmlrpc.dll
extension=php_xsl.dll
;cgi.fix_pathinfo=1
; 改为
cgi.fix_pathinfo=1
Nginx
搭建不支持 PHP 的 Nginx 服务器非常简单。那就是说搭建支持 PHP 的 Nginx 服务器就会稍微麻烦些。
修改 nginx.conf
。
location / {
root html;
index index.html index.htm;
}
# 改为
location / {
root C:/wnmp/www;
index index.html index.htm index.php;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#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;
#}
# 改为
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9002
#
location ~ \.php$ {
root C:/wnmp/www;
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
$document_root
就是 C:/wnmp/www
。
启动
手工启动
启动 php-cgi.exe,监听 9002 端口。
C:\wnmp\php7.1.5>php-cgi.exe -b 127.0.0.1:9002 -c C:/wnmp/php7.1.5/php.ini
另开一个 CMD,启动 Nginx。
start nginx
后台启动
把下载好的 RunHiddenConsole.zip 包解压到 php 目录内,RunHiddenConsole.exe 的作用是在执行完命令行脚本后自动关闭脚本,而从脚本中开启的进程不被关闭。
创建启动脚本“start_nginx.bat”,内容如下。
@echo off
REM Windows 下无效
REM set PHP_FCGI_CHILDREN=5
REM 每个进程处理的最大请求数,或设置为 Windows 环境变量
set PHP_FCGI_MAX_REQUESTS=1000
echo Starting PHP FastCGI...
RunHiddenConsole C:/wnmp/php7.1.5/php-cgi.exe -b 127.0.0.1:9002 -c C:/wnmp/php7.1.5/php.ini
echo Starting nginx...
RunHiddenConsole C:/wnmp/nginx1.13.0/nginx.exe -p C:/wnmp/nginx1.13.0
创建退出脚本“stop_nginx.bat”,内容如下。
@echo off
echo Stopping nginx...
taskkill /F /IM nginx.exe > nul
echo Stopping PHP FastCGI...
taskkill /F /IM php-cgi.exe > nul
exit
以后就可以用这两个脚本开启、关闭服务了。
MariaDB
MariaDB 就是开源、无公司的 Mysql。它的安装和 MySQL 完全一样。默认服务名是 mysql,建议改为 MariaDB 以便区分。
连接 MariaDB 的方式和 MySQL,如果以前用 MySQL 现在用 MariaDB,连接信息不需要更改。
参考链接
附录
搭建不支持 PHP 的 Nginx 服务器
下载 Nginx 的 Windows 版安装文件(在此以 nginx-1.13.0 为例)。
- 将
nginx-1.13.0.zip
放在 C 盘根目录下(即路径为C:\
)。 nginx-1.13.0.zip
解压到当前文件夹,得到目录C:\nginx-1.13.0
。cd nginx-1.13.0
然后start nginx
就开启了服务器。- 浏览器输入
http://localhost/
看到“Welcome to nginx!”。
查看 Nginx 进程信息
tasklist /fi "imagename eq nginx.exe"
杀死 Nginx 进程
taskkill /F /IM nginx.exe > nul
启动 Nginx 服务器
start nginx
杀死 PID
# 查看占用 9002 端口的 PID
> netstat -aon|findstr 9002
TCP 127.0.0.1:9002 0.0.0.0:0 LISTENING 7732
TCP 192.168.10.119:49484 120.92.58.186:9002 CLOSE_WAIT 7640
# 杀死 pid 是 7732 的进程
taskkill /pid 7732
为 Nginx 开 Virtual Host
开放 8888 端口,根目录在 C:/wnmp/laravel/template/public
。
server {
listen 8888;
server_name localhost;
location / {
root C:/wnmp/laravel/template/public;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
root C:/wnmp/laravel/template/public;
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}