当前位置:   article > 正文

windows下php环境搭建_windows搭建php环境

windows搭建php环境


本文所使用的服务器为Apache

1 配置Apache服务器

1.1 下载Apache服务器

打开Apache官网,依次按如图所示点击:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

下载完之后解压到任意目录即可:

C:\wsr\Apache\Apache24\
├──bin
├──cgi-bin
├──conf
├──error
├──htdocs
├──icons
├──include
├──lib
├──logs
├──modules
├──ABOUT_APACHE.txt
├──CHANGES.txt
├──INSTALL.txt
├──LICENSE.txt
├──Licenses.txt
├──NOTICE.txt
├──OPENSSL-NEWS.txt
├──OPENSSL-README.txt
└──README.txt
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

1.2 修改配置文件httpd.conf

打开配置文件C:\wsr\Apache\Apache24\conf\httpd.conf,修改以下三处:

1、 SRVROOT。将SRVROOT的值改为解压后的文件夹路径。

# least PidFile.
#

Define SRVROOT "c:/wsr/Apache/Apache24"
ServerRoot "${SRVROOT}"

#
# Uncomment below line to enable TLS/1.3 in server.
# (Requires OpenSSL 1.1.1 and up)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2、端口号。为避免端口号冲突可以将端口号修改为其它值(也可不修改)。我将默认端口8080修改为了8081

#Listen 12.34.56.78:80
Listen 8081
  • 1
  • 2

3、ServerName。ServerName中的端口号也修改为Listen对应的值。

# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName localhost:8081
  • 1
  • 2
  • 3

1.3 安装Apache服务

经过以上两步,其实已经算是配置好了。不过每次需要使用Apache服务器的时候都要进入到其bin目录,手动执行命令开启Apache服务。

更简单的方法是安装Apache服务,让其一直运行。首先进入Apache的bin目录C:\wsr\Apache\Apache24\bin,以管理员身份运行powershell,执行下述命令。

1、安装Apache服务。

PS C:\wsr\Apache\Apache24\bin> .\httpd.exe -k install
Installing the 'Apache2.4' service
The 'Apache2.4' service is successfully installed.
Testing httpd.conf....
Errors reported here must be corrected before the service can be started.
  • 1
  • 2
  • 3
  • 4
  • 5

2、服务安装后为关闭状态。所以需要开启Apache服务。

PS C:\wsr\Apache\Apache24\bin> .\httpd.exe -k start
  • 1

3、验证Apache服务器是否运行,在浏览器地址栏访问http://localhost:8081/
在这里插入图片描述
出现如图所示界面说明Apache服务器配置成功。

服务器的更多命令请执行下述命令查看(执行命令时记得进入bin目录,且要以管理员身份执行):

PS C:\wsr\Apache\Apache24\bin> .\httpd.exe -h
Usage: C:\\wsr\\Apache\\Apache24\\bin\\httpd.exe [-D name] [-d directory] [-f file]
                                            [-w] [-k start|restart|stop|shutdown] [-n service_name]
                                            [-k install|config|uninstall] [-n service_name]
Options:
  -D name            : define a name for use in <IfDefine name> directives
  -d directory       : specify an alternate initial ServerRoot
  -f file            : specify an alternate ServerConfigFile
  -C "directive"     : process directive before reading config files
  -k start           : tell Apache to start
  -k restart         : tell running Apache to do a graceful restart
  -k stop|shutdown   : tell running Apache to shutdown
  -k install         : install an Apache service
  -k config          : change startup Options of an Apache service
  -k uninstall       : uninstall an Apache service
  -w                 : hold open the console window on error
  -e level           : show startup errors of level (see LogLevel)
  -E file            : log startup errors to file
  -v                 : show version number
  -V                 : show compile settings
  -h                 : list available command line options (this page)
  -l                 : list compiled in modules
  -L                 : list available configuration directives
  -t -D DUMP_VHOSTS  : show parsed vhost settings
  -t -D DUMP_RUN_CFG : show parsed run settings
  -S                 : a synonym for -t -D DUMP_VHOSTS -D DUMP_RUN_CFG
  -t -D DUMP_MODULES : show all loaded modules
  -M                 : a synonym for -t -D DUMP_MODULES
  -t -D DUMP_INCLUDES: show all included configuration files
  -t                 : run syntax check for config files
  -T                 : start without DocumentRoot(s) check
  -X                 : debug mode (only one worker, do not detach)
  • 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

2 配置php

2.1 下载php

打开php官网,按如图所示选择版本下载。我下载的是php 8.0.9
在这里插入图片描述
下载之后解压到任意文件夹:

C:\wsr\php\
├──dev
├──ext
├──extras
├──lib
├──deplister.exe
├──glib-2.dll
├──gmodule-2.dll
├──icudt68.dll
├──icuin68.dll
├──icuio68.dll
├──icuuc68.dll
├──libcrypto-1_1-x64.dll
├──libenchant2.dll
├──libpq.dll
├──libsasl.dll
├──libsodium.dll
├──libsqlite3.dll
├──libssh2.dll
├──libssl-1_1-x64.dll
├──license.txt
├──news.txt
├──nghttp2.dll
├──phar.phar.bat
├──pharcommand.phar
├──php-cgi.exe
├──php-win.exe
├──php.exe
├──php.ini
├──php.ini-development
├──php.ini-production
├──php8apache2_4.dll
├──php8embed.lib
├──php8phpdbg.dll
├──php8ts.dll
├──phpdbg.exe
├──readme-redist-bins.txt
├──README.md
└──snapshot.txt
  • 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

2.2 修改配置文件php.ini

php.ini-developmentphp.ini-production复制一份,将文件名修改为php.ini

PS C:\wsr\php> ls .\php.ini*


    目录: C:\wsr\php


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         2021/7/30     16:55          74559 php.ini
-a----         2021/7/29     22:42          74326 php.ini-development
-a----         2021/7/29     22:42          74474 php.ini-production
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

打开C:\wsr\php\php.ini,修改以下四处:

1、设置扩展文件路径。将extension_dir的值改为php解压后的路径值。

; On windows:
;extension_dir = "ext"
extension_dir = "c:/wsr/php/ext"
  • 1
  • 2
  • 3

2、设置要加载的扩展文件。这些扩展文件都在目录extension_dir = "c:/wsr/php/ext"中。下面将此目录下的扩展全部添加进去。

PS C:\wsr\php> ls .\ext\ |select Name

Name
----
php_bz2.dll
php_com_dotnet.dll
php_curl.dll
php_dba.dll
php_enchant.dll
php_exif.dll
php_ffi.dll
php_fileinfo.dll
php_ftp.dll
php_gd.dll
php_gettext.dll
php_gmp.dll
php_imap.dll
php_intl.dll
php_ldap.dll
php_mbstring.dll
php_mysqli.dll
php_oci8_19.dll
php_odbc.dll
php_opcache.dll
php_openssl.dll
php_pdo_firebird.dll
php_pdo_mysql.dll
php_pdo_oci.dll
php_pdo_odbc.dll
php_pdo_pgsql.dll
php_pdo_sqlite.dll
php_pgsql.dll
php_phpdbg_webhelper.dll
php_shmop.dll
php_snmp.dll
php_soap.dll
php_sockets.dll
php_sodium.dll
php_sqlite3.dll
php_sysvshm.dll
php_tidy.dll
php_xsl.dll
php_zend_test.dll

  • 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
; - Many DLL files are located in the extensions/ (PHP 4) or 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=bz2
;extension=curl
;extension=ffi
;extension=ftp
;extension=fileinfo
;extension=gd
;extension=gettext
;extension=gmp
;extension=intl
;extension=imap
;extension=ldap
;extension=mbstring
;extension=exif      ; Must be after mbstring as it depends on it
;extension=mysqli
;extension=oci8_12c  ; Use with Oracle Database 12c Instant Client
;extension=oci8_19  ; Use with Oracle Database 19 Instant Client
;extension=odbc
;extension=openssl
;extension=pdo_firebird
;extension=pdo_mysql
;extension=pdo_oci
;extension=pdo_odbc
;extension=pdo_pgsql
;extension=pdo_sqlite
;extension=pgsql
;extension=shmop

; The MIBS data available in the PHP distribution must be installed.
; See http://www.php.net/manual/en/snmp.installation.php
;extension=snmp

;extension=soap
;extension=sockets
;extension=sodium
;extension=sqlite3
;extension=tidy
;extension=xsl

;zend_extension=opcache

; 这里添加扩展文件
extension=php_bz2.dll
extension=php_com_dotnet.dll
extension=php_curl.dll
extension=php_dba.dll
extension=php_enchant.dll
extension=php_exif.dll
extension=php_ffi.dll
extension=php_fileinfo.dll
extension=php_ftp.dll
extension=php_gd.dll
extension=php_gettext.dll
extension=php_gmp.dll
extension=php_imap.dll
extension=php_intl.dll
extension=php_ldap.dll
extension=php_mbstring.dll
extension=php_mysqli.dll
extension=php_oci8_19.dll
extension=php_odbc.dll
extension=php_opcache.dll
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_phpdbg_webhelper.dll
extension=php_shmop.dll
extension=php_snmp.dll
extension=php_soap.dll
extension=php_sockets.dll
extension=php_sodium.dll
extension=php_sqlite3.dll
extension=php_sysvshm.dll
extension=php_tidy.dll
extension=php_xsl.dll
extension=php_zend_test.dll
;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;
  • 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

3、设置时区。

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =
date.timezone = Asia/Shanghai
  • 1
  • 2
  • 3
  • 4
  • 5

4、设置openssl。

[openssl]
; The location of a Certificate Authority (CA) file on the local filesystem
; to use when verifying the identity of SSL/TLS peers. Most users should
; not specify a value for this directive as PHP will attempt to use the
; OS-managed cert stores in its absence. If specified, this value may still
; be overridden on a per-stream basis via the "cafile" SSL stream context
; option.
;openssl.cafile=
openssl.cafile=cacert.pem
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.3 再次修改配置文件httpd.conf

打开配置文件C:\wsr\Apache\Apache24\conf\httpd.conf,修改以下三处:

1、设置默认索引。按如下所示修改DirectoryIndex的值。

#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    # DirectoryIndex index.html
    DirectoryIndex index.html index.htm index.php
</IfModule>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2、开启 rewrite 功能。将下面第2行的注释去掉即可。

#LoadModule reqtimeout_module modules/mod_reqtimeout.so
LoadModule rewrite_module modules/mod_rewrite.so
#LoadModule sed_module modules/mod_sed.so
  • 1
  • 2
  • 3

3、在文件末尾添加如下设置。

PS C:\wsr\php> ls php.ini,php*.dll


    目录: C:\wsr\php


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         2021/7/31     10:35          75438 php.ini
-a----         2021/7/29     22:42          35840 php8apache2_4.dll
-a----         2021/7/29     22:42         339968 php8phpdbg.dll
-a----         2021/7/29     22:42        9449472 php8ts.dll
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
#php8
LoadModule php_module "c:/wsr/php/php8apache2_4.dll"
PHPIniDir "c:/wsr/php"
AddType application/x-httpd-php .php .html
  • 1
  • 2
  • 3
  • 4
  • 若使用php 5,则使用LoadModule php5_module xxx
  • 若使用php 7,则使用LoadModule php7_module xxx
  • 若使用php 8,则使用LoadModule php_module xxx

我使用的为php8,所以用的是php_module。上面的三个配置中,前两个根据自己解压文件的路径设置其对应的值,第三个直接照抄。

2.4 验证

2.4.1 重启Apache服务

1、方法一

按快捷键win+r,输入services.msc,找到Apache服务,按如图方式重启:
在这里插入图片描述

2、方法二

管理员身份运行powershell,进入Apache的bin目录C:\wsr\Apache\Apache24\bin,执行下述命令:

PS C:\wsr\Apache\Apache24\bin> .\httpd.exe -k restart
  • 1

3、方法三

管理员身份运行powershell,执行下述命令:

PS C:\wsr\Apache\Apache24\bin> Restart-Service -Name Apache2.4
PS C:\wsr\Apache\Apache24\bin> Get-Service -Name Apache2.4

Status   Name               DisplayName
------   ----               -----------
Running  Apache2.4          Apache2.4

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.4.2 验证效果

C:\wsr\Apache\Apache24\htdocs\目录下分别新建index.phpdir1\test.html

C:\wsr\Apache\Apache24\htdocs\
├──dir1
│  └──test.html
├──index.html
└──index.php
  • 1
  • 2
  • 3
  • 4
  • 5

其内容分别如下所示:

PS C:\wsr\Apache\Apache24\htdocs> cat .\index.php
<?php phpinfo(); ?>
PS C:\wsr\Apache\Apache24\htdocs> cat .\dir1\test.html
<!DOCTYPE html>
<html>
<body>

<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>

</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在浏览器地址栏分别访问http://localhost:8081/index.phphttp://localhost:8081/dir1/test.html,可以看到如下的效果:
在这里插入图片描述
在这里插入图片描述

2.4.3 说明

其中,目录C:\wsr\Apache\Apache24\htdocs为部署项目的目录,如果不想使用默认值,可以修改配置文件C:\wsr\Apache\Apache24\conf\httpd.conf,将DocumnetRoot的值修改为其它的:

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
# DocumentRoot "${SRVROOT}/htdocs"
DocumentRoot "c:/Users/fy/Desktop/project"
# <Directory "${SRVROOT}/htdocs">
<Directory "c:/Users/fy/Desktop/project">

    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

每次修改配置文件后都需要重启Apache服务,否则修改不生效。至此,所有配置结束。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/708155
推荐阅读
相关标签
  

闽ICP备14008679号