我在2008年那会儿自学了一下 PHP,那时候这个语言很流行,当时还是 php5.2。但是自此就没怎么用过了。

现在搭建这个站点,没有选择目前流行的(git pages + jekyll 或者 hexo)静态博客,而是选择了老旧的 php 程序搭建。

为了测试程序,在本地搭建了一个 php 环境,现在已经是 php7.2 了, 目前由于 xdebug 官网还没有 php7.2 的已编译版本下载,所以我选用了 php7.1 版本。

PHP 配置

  • 首先去 http://windows.php.net/download/ 下载 PHP 7.1.12 Released VC14 x64 Non Thread Safe (2017-Nov-23 18:28:55)。
  • 下载下来后解压到一个目录,例如我是 D:\var\php 目录。
  • php.ini-development 复制一份重命名为 php.ini, 编辑 php.ini.

    设置扩展目录, 将其中的 ; extension_dir = "ext" 替换为 extension_dir = "ext",(即把前面的分号(注释符号)去掉)。

    使用扩展, 将 extension=php_bz2.dll, extension=php_curl.dll, extension=php_fileinfo.dll, extension=php_gd2.dll, extension=php_gettext.dll, extension=php_mbstring.dll, extension=php_mysqli.dll, php_openssl.dll, php_pdo_mysql.dll, php_pdo_sqlite.dll, php_sqlite3.dll 等前的注释符去掉。
    对于扩展按需把相应的注释去掉就好, 我使用了如下扩展:

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

; If you wish to have an extension loaded automatically, use the following
; syntax:
;
;   extension=modulename.extension
;
; For example, on Windows:
;
;   extension=msql.dll
;
; ... or under UNIX:
;
;   extension=msql.so
;
; ... or with a path:
;
;   extension=/path/to/extension/msql.so
;
; If you only provide the name of the extension, PHP will look for it in its
; default extension directory.
;
; 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

为了使用 nginx, 把cgi.fix_pathinfo=1 前的注释符去掉。

nginx 配置

下载 nginx, 解压到 D:\var\nginx, 进入 nginx 的 conf 目录,打开 nginx 的配置文件 nginx.conf,找到

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

root html; 更换为 root D:\\var\\www;. D:\var\www 是站点的根目录

添加以下配置以支持 php:

location ~ [^/]\.php(/|$) {
    root D:\\var\\www;
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;

    include fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
}

启动服务器测试

启动 php fastcgi

C:\Users\Wan>D:/var/php/php-cgi.exe -b 127.0.0.1:9000 -c D:/var/php/php.ini

启动 nginx

C:\Users\Wan>cd /d D:var\nginx
D:\var\nginx>nginx.exe

可用创建两个批处理文件来启动和停止服务器, 需要下载 RunHiddenConsole

php-start.bat

@ECHO OFF
ECHO Starting PHP FastCGI...
RunHiddenConsole D:\var\php\php-cgi.exe  -b 127.0.0.1:9000 -c D:\var\php\php.ini

echo Starting nginx...
RunHiddenConsole D:\var\nginx\nginx.exe -p D:\var\nginx

ping 127.0.0.1 -n 1>NUL
echo .
echo .
echo .
ping 127.0.0.1 >NUL

php-stop.bat

@ECHO OFF
echo stop nginx
taskkill /f /IM nginx.exe >> nul
taskkill /f /IM nginx.exe >> nul
taskkill /f /IM nginx.exe >> nul
taskkill /f /IM nginx.exe >> nul
echo stop php-cgi
taskkill /f /IM php-cgi.exe >> nul
taskkill /f /IM php-cgi.exe >> nul
taskkill /f /IM php-cgi.exe >> nul
taskkill /f /IM php-cgi.exe >> nul
EXIT

扩展: Xdebug 配置

参考:

  1. PHP FastCGI Example
  2. PHP-FastCGI on Windows

标签: php, php搭建

添加新评论