如题

下载 nginx1.14.0

nginx-1.14

下载 winsw-v2.1.2

winsw release 列表,选:WinSW.NET4.exe

笔者下载的版本

下载 PHP7.2.6

nginx 下 php 是以 FastCGI 的方式运行,所以我们下载非线程安全也就是 nts 的 php 包。
php 最新版本下载列表页面

笔者下载的版本

将 nginx 安装成 windows 服务

  1. 将 nginx 解压至 c 盘根目录 c:/nginx,不要版本号,方便以后升级至最新版本。
  2. 将下载到的 WinSW.NET4.exe 重命名为:winsw.exe,并移到 nginx 根目录
  3. 在 nginx 目录添加配置文件:winsw.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <service>
    <id>nginx</id>
    <name>nginx</name>
    <description>nginx</description>
    <logpath>C:\nginx\logs</logpath>
    <logmode>roll</logmode>
    <depend></depend>
    <executable>c:\nginx\nginx.exe</executable>
    <stopexecutable>C:\nginx\nginx.exe -s stop</stopexecutable>
    </service>
  4. 以管理员身份运行 cmd,进入 nginx 根目录,执行如下命令

    1
    2
    C:\nginx>winsw install
    2018-06-08 08:20:19,037 INFO - Installing the service with id 'nginx'

检查配置并启动

1
2
3
4
5
6
7
8
9
10
C:\nginx>nginx -t
nginx: the configuration file C:\nginx/conf/nginx.conf syntax is ok
nginx: configuration file C:\nginx/conf/nginx.conf test is successful

C:\nginx>net start nginx
The nginx service is starting.
The nginx service was started successfully.

C:\nginx>nginx -v
nginx version: nginx/1.14.0

遇到的问题及解决过程

因为之前安装过 nginx-1.12.0,用 sc delete nginx 删除服务之后,进入新版目录安装时出错。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
C:\Windows\system32>sc delete nginx
[SC] DeleteService SUCCESS

C:\nginx>winsw install
2018-06-08 08:17:46,936 INFO - Installing the service with id 'nginx'
Service with id 'nginx' already exists
To install the service, delete the existing one or change service Id in the configuration file
2018-06-08 08:17:46,940 FATAL - Unhandled exception
System.Exception: Installation failure: Service with id 'nginx' already exists
at winsw.WrapperService.Run(String[] _args, ServiceDescriptor descriptor)
at winsw.WrapperService.Main(String[] args)
System.Exception: Installation failure: Service with id 'nginx' already exists
at winsw.WrapperService.Run(String[] _args, ServiceDescriptor descriptor)
at winsw.WrapperService.Main(String[] args)

C:\nginx>cd ..

C:\>cd nginx-1.12.0

C:\nginx-1.12.0>winsw uninstall
2018-06-08 08:18:49,533 INFO - Starting ServiceWrapper in the CLI mode
2018-06-08 08:18:49,755 INFO - Uninstalling the service with id 'nginx'
2018-06-08 08:18:49,757 WARN - The service with id 'nginx' is running. It may be impossible to uninstall it
2018-06-08 08:18:49,768 ERROR - Failed to uninstall the service with id 'nginx'. It has been marked for deletion.
2018-06-08 08:18:49,770 INFO - Completed. Exit code is 0

C:\nginx-1.12.0>net stop nginx
The nginx service is stopping.The service name is invalid.

More help is available by typing NET HELPMSG 2185.

C:\nginx-1.12.0>winsw uninstall
2018-06-08 08:19:10,163 INFO - Starting ServiceWrapper in the CLI mode
2018-06-08 08:19:10,569 INFO - Uninstalling the service with id 'nginx'
2018-06-08 08:19:10,570 WARN - The service with id 'nginx' does not exist. Nothing to uninstall
2018-06-08 08:19:10,572 INFO - Completed. Exit code is 0

C:\nginx-1.12.0>cd ..
C:\>cd nginx

C:\nginx>winsw install
2018-06-08 08:20:19,037 INFO - Installing the service with id 'nginx'

在另外一台电脑上操作,先停止 nginx,再用之前安装的程序卸载,然后再安装最新版,很顺利

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
C:\WINDOWS\system32>net stop nginx
nginx 服务正在停止.
nginx 服务已成功停止。

C:\WINDOWS\system32>cd C:\nginx-1.12.0

C:\nginx-1.12.0>winsw.exe uninstall
2018-06-09 09:10:07,109 INFO - Uninstalling the service with id 'nginx'

C:\nginx-1.12.0>cd ..

C:\>cd nginx

C:\nginx>winsw.exe install
2018-06-09 09:10:19,289 INFO - Installing the service with id 'nginx'

配置 PHP7.2.6

将 PHP 解压至目录:c:/dev/php7
复制 php.ini-development 为 php.ini,修改配置

常规配置

  1. 找到下面的配置项,并作相应的修改
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    error_log = C:\dev\php7\logs\php_errors.log

    extension_dir = "C:\dev\php7\ext"
    date.timezone = "Asia/Shanghai"
    cgi.fix_pathinfo=1
    extension=curl
    extension=gd2
    extension=openssl
    extension=mysqli
    extension=pdo_mysql

备注一下,php-7.0.30-nts-Win32-VC14-x64 的扩展跟 7.2 的不一样,如下

1
2
extension=php_mysqli.dll
extension=php_curl.dll

  1. 在 [opcache] 下面添加如下配置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    ; dll地址
    zend_extension=php_opcache.dll
    ; 开关打开
    opcache.enable=1
    ; 开启CLI
    opcache.enable_cli=1
    ; 可用内存, 酌情而定, 单位为:Mb
    opcache.memory_consumption=528
    ; Zend Optimizer + 暂存池中字符串的占内存总量.(单位:MB)
    opcache.interned_strings_buffer=8
    ; 对多缓存文件限制, 命中率不到 100% 的话, 可以试着提高这个值
    opcache.max_accelerated_files=10000
    ; Opcache 会在一定时间内去检查文件的修改时间, 这里设置检查的时间周期, 默认为 2, 定位为秒
    opcache.revalidate_freq=1
    ; 打开快速关闭, 打开这个在PHP Request Shutdown的时候回收内存的速度会提高
    opcache.fast_shutdown=1

配置 XDebug

下载列表,找到对应的版本,记得是针对 PHP 7.2 并且带 nts 的版本,本文下载的版本
XDebug install
XDebug windows install
将 phpinfo(); 的页面,Ctrl + A, Ctrl + c 的结果 Ctrl + v 到上面页面的输入框,得到如下结果:


下面是我们要关注的信息:
1
2
3
4
5
6
Instructions
Download php_xdebug-2.6.0-7.2-vc15-nts-x86_64.dll
Move the downloaded file to C:\dev\php7\ext
Edit C:\dev\php7\php.ini and add the line
zend_extension = C:\dev\php7\ext\php_xdebug-2.6.0-7.2-vc15-nts-x86_64.dll
Restart the webserver

添加之后,首先端口跟 php 冲突,添加其它端口之后,依然不能进入断点。
解决方法,请参考我之前的文章:windows7 + nginx + php + xdebug(搭配 PhpStorm 调试) + memcache
下载之后,剪切至 PHP 的 ext 目录,在 php.ini 最后添加如下信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[xdebug]
zend_extension = C:\dev\php7\ext\php_xdebug-2.6.0-7.2-vc15-nts-x86_64.dll
;开启远程调试
xdebug.remote_enable=1
xdebug.profiler_enable=1
;xdebug.profiler_enable=off
xdebug.profiler_enable_trigger=off
xdebug.profiler_output_name=cachegrind.out.%t.%p
xdebug.profiler_output_dir="tmp"
xdebug.show_local_vars=0
xdebug.remote_connect_back=1

;开启自动跟踪
xdebug.auto_trace=1
;开启异常跟踪
xdebug.show_exception_trace=1
;开启异常跟踪
xdebug.remote_autostart=1
;收集变量
xdebug.collect_vars=1
;收集参数
xdebug.collect_params=1
;trace输出路径
xdebug.trace_output_dir="tmp/xdebug"
;以下三个分别是主机、端口、句柄
xdebug.remote_host="localhost"
;注意,这里的端口不能跟 php-cgi 冲突,php-cgi 默认为 9000
xdebug.remote_port=9001
xdebug.remote_handler="dbgp"

运行 php

运行 cmd,进入 php 根目录,启动 php

1
php-cgi.exe -b 127.0.0.1:9000 -c C:/dev/php7/php.ini

遇到的问题

不支持 advice_mysqli_connect

运行 Discuz!X3.4 简体中文 UTF8 版 20180101 的安装向导,提示:不支持 advice_mysqli_connect
解决:php.ini 找到下面项,将前面的分号删除

1
extension=mysqli

  1. 配置站点,将 nginx.conf 里面的 php 配置 copy 出来,可是有问题
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    server {
    listen 80;
    server_name dz.dev.com;
    charset utf-8;
    access_log logs/dz.dev.com.access.log;
    error_log logs/dz.dev.com.com.error.log;
    root C:/workspace/DiscuzX/upload;
    index index.php;

    #location ~ [^/].php(/|$) {
    # fastcgi_pass unix:/tmp/php-cgi.sock;
    # fastcgi_index index.php;
    # include fastcgi.conf;
    #}

    location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    include fastcgi_params;
    }
    }

No input file specified.

解决:

1
SCRIPT_FILENAME  /scripts$fastcgi_script_name;

改为:

1
SCRIPT_FILENAME  $document_root$fastcgi_script_name;

php5.6.30 找不到 MSVCR110.dll

1
2
php-cgi.exe - 系统错误
由于找不到 MSVCR110.dll,无法继续执行代码。重新安装程序可能会解决此问题。

解决,下载并安装:Visual C++ Redistributable for Visual Studio 2012 Update 4