Jinyun's Notes 🚀

没什么天赋,爱好也不多,但愿坚持做些喜欢的事情

0%

Windows 打造 PHP 开发环境

201904101011.jpg

🤙在 Windows 上开发 PHP,一般情况下我们会直接安装 PhpStudy、WampServer 或者 XAMPP 等集成开发环境。当然作为一名有逼格的开发者,我们还会自己动手丰衣足食,毕竟这个门槛不高,搭不上怎么能证明你是个有节操的 PHPer 呢。

WAMP:Windows & Apache & MySQL & PHP 组合,即在 Windows 下搭建的 Web 运行环境。在以下的操作中,我顺便把 Nginx 的安装配置也加上了,Windows 环境还是比较建议使用 Apache,减少不必要的折腾。

友情提示:以下操作皆在本人 Win10 32 位操作系统上折腾,64 位系统未做尝试。

关于 TS & NTS

  • TS - Thread-Safety:即线程安全,多线程访问时,采用了加锁机制,当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读取完,其他线程才可使用。不会出现数据不一致或者数据污染。PHP 以 ISAPI 方式加载的时候选择这个版本;
  • NTS - None-Thread Safe:即非线程安全,就是不提供数据访问保护,有可能出现多个线程先后更改数据造成所得到的是脏数据。PHP 以 FastCGI 方式运行的时候选择这个版本,具有更好的性能;

在以下的配置方式中,笔者使用 PHP-TS & Apache、PHP-NTS & Nginx 的方式来搭建,指定 G:\ 为代码目录、D:\Wamp 为软件安装目录,相关的配置文件在安装的具体软件目录中。

关于 VC6 & VC9 & VC11

Apache

下载 Apache

下载官方 Apache 或者 下载 Apache Lounge,找对应版本下载。

解压 Apache

将 httpd-2.4.35-win32-VC15.zip 解压到 httpd-2.4.35 文件夹下并移动到 C:\Wamp 目录下。

配置 Apache

打开 httpd.conf 文件:

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 修改服务器根路径
ServerRoot "C:/Wamp/httpd-2.4.35"

# 打开重写模块
LoadModule rewrite_module modules/mod_rewrite.so

# 修改服务器名称
ServerName 127.0.0.1:80

# 打开路径重写
<Directory />
AllowOverride All
Require all denied
</Directory>

# 配置项目根路径和打开根路径重写
DocumentRoot "G:/"
<Directory "G:/">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All

#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>

# 添加默认处理文件
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>

# 开启虚拟主机模式
Include conf/extratpd-vhosts.conf

# 添加 Apache 处理 PHP 扩展和配置
LoadModule php5_module "C:/Wamp/php-7.2.11-ts/php5apache2_4.dll"
AddHandler application/x-httpd-php .php
PHPIniDir "C:/Wamp/php-7.2.11-ts"

Nginx

下载 Nginx

下载 Nginx,找对应版本下载。

解压 Nginx

将 nginx-1.15.5.zip 解压到 nginx-1.15.5 文件夹下并移动到 D:\Wamp 目录下

配置 Nginx

  • 编辑 nginx.conf 文件:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
worker_processes  1;

events {
worker_connections 1024;
}

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

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain text/css text/javascrip text/xml application/javascript application/x-javascript application/json application/xml;
gzip_vary on;
gzip_disable "MSIE [1-6].";

server_names_hash_bucket_size 128;
client_max_body_size 100m;
client_header_buffer_size 256k;
large_client_header_buffers 4 256k;

server {
listen 80;
server_name localhost;

charset utf-8;

#access_log logs/host.access.log main;

location / {
root G:/;
autoindex on;
index index.html index.htm index.php;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root G:/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}

include vhosts/*.conf;
}
  • 在 conf 目录下新建 vhosts 目录,进入该目录,新建 sample.conf 文件:
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
server {
listen 80;
root G:/Sample/www.sample.com;

server_name www.sample.com;
server_name_in_redirect off;

location / {
autoindex on;
index index.php;
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}

location = /favicon.con {
log_not_found off;
access_log off;
}

location ~ /(\.ht|\.git|\.svn) {
deny all;
}
}

MySQL

详情请参阅 Installing MySQL on Microsoft Windows Using a noinstall ZIP Archive 文档。

下载 MySQL

下载 MySQL,找对应版本下载。

解压 MySQL

将 mysql-8.0.12-winx64.zip 解压到 mysql-8.0.12-winx64.zip 文件夹下并移动到 D:\Wamp 目录下。

配置 MySQL

  • 编辑 MySQL 配置文件 my.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
[client]
port = 3306
default-character-set = utf8mb4

[mysql]
default-character-set=utf8mb4

[mysqld]
port = 3306
basedir = D:/Wamp/mysql-8.0.12
datadir = D:/Wamp/mysql-8.0.12/data
socket = D:/Wamp/mysql-8.0.12/tmp/mysql.sock
default_authentication_plugin = mysql_native_password
character_set_server = utf8mb4
default-storage-engine = INNODB
server_id = 1

max_connections = 100
table_open_cache = 256
query_cache_size = 1M

[mysqld_safe]
log-error = D:/Wamp/mysql-8.0.12/logs/mysql-error.log
pid-file = D:/Wamp/mysql-8.0.12/run/mysql.pid

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  • MySQL 操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 配置(需要在管理员命令行下操作)
D:\Wamp\mysql-8.0.12\bin> mysqld --defaults-file="D:/Wamp/mysql-8.0.12/my.ini" --initialize-insecure --explicit_defaults_for_timestamp=true --user=mysql --console

// 安装服务
D:\Wamp\mysql-8.0.12\bin> mysqld --install mysql --defaults-file="D:/Wamp/mysql-8.0.12/my.ini"

// 启动服务
D:\Wamp\mysql-8.0.12\bin> net start mysql

// 停止服务
D:\Wamp\mysql-8.0.12\bin> net stop mysql

// 移除服务
D:\Wamp\mysql-8.0.12\bin> sc delete mysql
或者
D:\Wamp\mysql-8.0.12\bin> mysqld --remove

PHP

下载 PHP

下载 PHP,找对应版本下载。

解压 PHP

根据上面的约定,在安装 Apache 时我们安装 TS 版本的 PHP,安装 Nginx 时我们安装 NTS 版本的 PHP。

  • 安装 Apache 情况下,将 php-7.2.11-ts-Win32-VC15-x64.zip 解压到 php-7.2.11-ts 文件夹下并移动到 D:\Wamp 目录下;
  • 安装 Nginx 情况下,将 php-7.2.11-nts-Win32-VC15-x64.zip 解压到 php-7.2.11-nts 文件夹下并移动到 D:\Wamp 目录下;

配置 PHP

相关的 PHP 模块需要酌情开启。

将 php.ini-development 重命名为 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// 设置模块加载路径
; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
; extension_dir = "ext"

// 注意 Apache 与 Nginx 的 PHP 目录
extension_dir = "D:/Wamp/php-7.2.11-nts/ext"

// 开启需要的模块
extension=php_bz2.dll
extension=php_curl.dll
extension=php_fileinfo.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

// 设置时区
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = PRC

脚本

RunHiddenConsole.exe 的作用是在执行完命令行脚本后可以自动关闭脚本,而从脚本中开启的进程不被关闭,RunHiddenConsole.exe 下载地址。

Apache 脚本

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
cls
@echo off
set APACHE_PATH=D:
set APACHE_DIR=D:/Wamp/httpd-2.4.35/bin/
color 0a
title Apache Management Process Power By Yun
goto MENU
:MENU

cls
echo ====================== Begin ======================
echo.
echo. **************************************************
echo. --Apache Management Process Power By Yun
echo. **************************************************
echo. ┏━━━━━━━━━━━━━━━━━━━━━━━┓
echo. ┃ 1:Apache install ┃ 2:Apache start ┃
echo. ┣━━━━━━━━━━╋━━━━━━━━━━━━┫
echo. ┃ 3:Apache restart ┃ 4:Apache stop ┃
echo. ┣━━━━━━━━━━╋━━━━━━━━━━━━┫
echo. ┃ 5:Apache uninstall┃ 6:Apache extension ┃
echo. ┗━━━━━━━━━━━━━━━━━━━━━━━┛
echo. **************************************************
echo.
echo. Please select a item number input:

set /p id=
if "%id%"=="1" goto cmd1
if "%id%"=="2" goto cmd2
if "%id%"=="3" goto cmd3
if "%id%"=="4" goto cmd4
if "%id%"=="5" goto cmd5
else exit
pause

:cmd1
echo.
echo. Apache is installing......
if not exist %APACHE_DIR%httpd.exe echo %APACHE_DIR%httpd.exe不存在
%APACHE_PATH%
cd %APACHE_DIR%
if exist %APACHE_DIR%httpd.exe %APACHE_DIR%httpd.exe -k install
echo. Apache Server install successful!
echo.
pause
goto MENU

:cmd2
echo.
echo. Apache is starting......
if not exist %APACHE_DIR%httpd.exe echo %APACHE_DIR%httpd.exe不存在
%APACHE_PATH%
cd %APACHE_DIR%
if exist %APACHE_DIR%httpd.exe %APACHE_DIR%httpd.exe -k start
echo. Apache start successful!
echo.
pause
goto MENU

:cmd3
echo.
echo. Apache is restarting......
if not exist %APACHE_DIR%httpd.exe echo %APACHE_DIR%httpd.exe不存在
%APACHE_PATH%
cd %APACHE_DIR%
if exist %APACHE_DIR%httpd.exe %APACHE_DIR%httpd.exe -k restart
echo. Apache is restarting successful!
echo.
pause
goto MENU

:cmd4
echo.
echo. Apache is stoping......
if not exist %APACHE_DIR%httpd.exe echo %APACHE_DIR%httpd.exe不存在
%APACHE_PATH%
cd %APACHE_DIR%
if exist %APACHE_DIR%httpd.exe %APACHE_DIR%httpd.exe -k stop
echo. Apache is stoping successful!
echo.
pause
goto MENU

:cmd5
echo.
echo. Apache is uninstalling......
if not exist %APACHE_DIR%httpd.exe echo %APACHE_DIR%httpd.exe不存在
%APACHE_PATH%
cd %APACHE_DIR%
if exist %APACHE_DIR%httpd.exe %APACHE_DIR%httpd.exe -k uninstall
echo. Apache is uninstalling successful!
echo.
pause

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
cls
@echo off
set NGINX_PATH=D:
set NGINX_DIR=D:/Wamp/nginx-1.15.5/
color 0a
title Nginx Management Process Power By Yun
goto MENU
:MENU

cls
echo ====================== Begin ======================
echo.
echo. **************************************************
echo. --Nginx Management Process Power By Yun
echo. **************************************************
echo. ┏━━━━━━━━━━━━━━━━━━━━━━━┓
echo. ┃ 1:Nginx start ┃ 2:Nginx stop ┃
echo. ┣━━━━━━━━━━╋━━━━━━━━━━━━┫
echo. ┃ 3:Nginx reload ┃ 4:Nginx quit ┃
echo. ┗━━━━━━━━━━━━━━━━━━━━━━━┛
echo. **************************************************
echo.
echo. Please select a item number input:

set /p id=
if "%id%"=="1" goto cmd1
if "%id%"=="2" goto cmd2
if "%id%"=="3" goto cmd3
if "%id%"=="4" goto cmd4
else exit
pause

:cmd1
echo.
echo. Nginx is starting......
if not exist %NGINX_DIR%nginx.exe echo %NGINX_DIR%nginx.exe不存在
%NGINX_PATH%
cd %NGINX_DIR%
if exist %NGINX_DIR%nginx.exe start %NGINX_DIR%nginx.exe
echo. Nginx start successful!
echo.
pause
goto MENU

:cmd2
echo.
echo. Nginx正在关闭中......
taskkill /f /im nginx.exe > nul
echo. Nginx is stop successful!
echo.
pause
goto MENU

:cmd3
echo.
echo. Nginx is reloading......
if not exist %NGINX_DIR%nginx.exe echo %NGINX_DIR%nginx.exe不存在
%NGINX_PATH%
cd %NGINX_DIR%
if exist %NGINX_DIR%nginx.exe %NGINX_DIR%nginx.exe -s reload
echo. Nginx is reloadding successful!
echo.
pause
goto MENU

:cmd4
echo.
echo. Nginx is quitting......
taskkill /f /im %NGINX_DIR%nginx.exe > nul
echo. Nginx is quitting successful!
echo.
pause

PHP 脚本

1
2
3
@echo off
echo PHP FastCGI is starting......
RunHiddenConsole.exe D:/Wamp/php-7.2.11-nts/php-cgi.exe -b 127.0.0.1:9000 -c D:/Wamp/php-7.2.11-nts/php.ini

到了这里我只能说,你已经拥有一个基本的 PHP 开发环境而已,因为要配合 Redis、Memcached、MongoDB 之类的软件,还得安装相应的 PHP 扩展,在这些过程中奇葩的问题层出不穷,像缺个 *.dll 文件之类的。因此在 Windows 上手工搭建环境的成本是比较高的,话又说回来,不折腾一下,怎么能知道呢?所以,我换了 Mac。

本笔记是笔者在学习和工作中的一些整理,如对您有用,请鼓励我继续写作