Windows上安装ngixn php并添加服务自启动

环境准备

Nginx

php

winsw

安装Ngixn服务

把下载的winsw放入ngixn安装文件夹,重命名为nginx-service.exe,新建nginx-service.xml,写入配置

<service>
<id>nginxsvc</id>
<name>Nginx</name>
<description>Nginx Web Server service.</description>
<executable>c:\nginx\nginx.exe</executable>
<logpath>c:\nginx\logs\</logpath>
<logmode>roll</logmode>
<depend>phpsvc</depend>
<startargument>-p</startargument>
<startargument>c:\nginx</startargument>
<stopexecutable>c:\nginx\nginx.exe</stopexecutable>
<stopargument>-p</stopargument>
<stopargument>c:\nginx</stopargument>
<stopargument>-s</stopargument>
<stopargument>stop</stopargument>
</service>

以管理员身份打开cmd,运行

cd \c:\svc\nginx
nginx-service.exe install

安装PHP服务

winsw改为php-service.exe,放入php安装文件夹,写入配置php-service.xml

<service>
<id>phpsvc</id>
<name>PHP</name>
<description>PHP-FCGI service.</description>
<executable>c:\php\php-cgi.exe</executable>
<logpath>c:\php\logs\</logpath>
<logmode>roll</logmode>
<depend></depend>
<startargument>-b</startargument>
<startargument>127.0.0.1:9123</startargument>
<startargument>-c</startargument>
<startargument>C:\php\php.ini</startargument>
</service>

以管理员身份打开cmd,运行

cd \c:\svc\php
php-service.exe install

修改nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9123;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

}