共计 2874 个字符,预计需要花费 8 分钟才能阅读完成。
今年7月,换了一家新的公司,虽然说是前端,但是更多的时候要求的能力也是全栈的工程师,自身以前对后端也只是了解,正好在借着现在这个公司,学习下后端。
现在开始正题,如何开始快速搭建一个win下的PHP开发环境,编辑器就不用说了,推荐Atom,Sublime等。
软件准备
安装Apache
首先建议每个开发者准备一个工作空间,workspace.
1.在workspace创建一个Server的文件夹。
2.把Apache24解压在Server下。
3.将Server拖入Sublime中,再在D:\WorkSpace\Server\Apache24\conf
找到httpd.conf
文件并打开,找到Define SRVROOT
,将后面的替换成你Apache24所在的目录:"D:/WorkSpace/Server/Apache24"
* 如果是新下载的Apache24这个简单多了,在Sublime中Ctrl+D
选中所有${SRVROOT}
然后替换成"D:/WorkSpace/Server/Apache24"
* 如果是别人修改过的文件:你需要找到下面的位置
####### 位置1
Define SRVROOT "D:/WorkSpace/Server/Apache24"
####### 位置2
ServerRoot "D:/WorkSpace/Server/Apache24"
####### 位置3
Listen 80
####### 解释一下为什么开一个81端口,是为了配置PHP之后查看Apache是否正常,当然你也可以不用开这个端口
Listen 81
####### 位置4
DocumentRoot "D:/WorkSpace/Server/Apache24/htdocs"
####### 位置5
<Directory "D:/WorkSpace/Server/Apache24/htdocs">
####### 位置6
DirectoryIndex index.html 修改为 DirectoryIndex index.html index.php index.htm
####### 位置7
ScriptAlias /cgi-bin/ "D:/WorkSpace/Server/Apache24/cgi-bin/"
####### 位置8
<Directory "D:/WorkSpace/Server/Apache24/cgi-bin/">
4.在用管理员身份打开CMD,记得是管理员,否则可能会出现安装报错,输入命令"D:/WorkSpace/Server/Apache24/bin/httpd.exe" -k install
5.再在"D:/WorkSpace/Server/Apache24/bin
下打开ApacheMonitor.exe
,启动Apache24,这是在浏览器输入http://localhost:80/
看到Apache的欢迎页面就说明安装成功了。
安装PHP
安装PHP就简单,将下载好的PHP复制到D:\WorkSpace\Server
下,即可。
配置PHP到Apache中
1.配置httpd.conf
用Sublime打开Server.在D:\WorkSpace\Server\Apache24\conf
找到httpd.conf
;
在最后面添加如下:
PHPIniDir "D:/WorkSpace/Server/php-7.2.0"
LoadModule php7_module "D:/WorkSpace/Server/php-7.2.0/php7apache2_4.dll"
AddType application/x-httpd-php .php .html .htm
2.配置httpd-vhosts.conf
在找到# Virtual hosts
将下面的# Include conf/extra/httpd-vhosts.conf
打开为Include conf/extra/httpd-vhosts.conf
3.配置虚拟域名
方法1,在D:\WorkSpace\Server\Apache24\conf
找到httpd.conf
<Directory />
AllowOverride none
#Require all denied
</Directory>
然后在D:\WorkSpace\Server\Apache24\conf\extra
找到httpd-vhosts.conf
配制一个虚拟域名
<VirtualHost *:80>
ServerAdmin xxxx@q1.com
DocumentRoot "C:/Users/TongH/Desktop/DEMO/php" #说明下,这个是你开发的PHP目录
ServerAlias www.phpstart.com # 你虚拟的域名
ServerName phpstart.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>
方法2,在D:\WorkSpace\Server\Apache24\conf
找到httpd.conf
<Directory />
AllowOverride none
Require all denied
</Directory>
然后在D:\WorkSpace\Server\Apache24\conf\extra
找到httpd-vhosts.conf
配制一个虚拟域名
<VirtualHost *:80>
ServerAdmin xxxx@q1.com
DocumentRoot "C:/Users/TongH/Desktop/DEMO/php"
ServerAlias www.phpstart.com
ServerName phpstart.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
<Directory C:/Users/TongH/Desktop/DEMO/php>
Options +Indexes +FollowSymLinks +Multiviews
AllowOverride all
Require local
</Directory>
</VirtualHost>
4.配置HOST
在Sublime中打开C:\Windows\System32\drivers\etc
下的host
文件,添加127.0.0.1 www.phpstart.com
即可。
PHP开发目录
在目录"C:/Users/TongH/Desktop/DEMO/php"
新建index.php
并写入
<?php
phpinfo();
?>
然后在浏览器中打开www.phpstart.com
如果你能正常看见PHP7.2的欢迎界面,恭喜你成功搭建了PHP与Apache的简单服务