PHP开发Apache服务器配置_PHP
照此配置流程,绝对一路畅通,可保无虞。
昨天弄了个PHP小程序,想在本地跑一下测试,可是工作电脑没有安装环境,于是下载了一个wamp,一路畅通,Apache、Mysql、PHP就全有了。启动wamp服务,在浏览器里输入“http://localhost”,访问正常,跳出wamp首页。于是,下面想把自己的CrashServer网站配置到Apache里,可以通过虚拟域名在本地访问测试,结果遇到不少问题,今日一通Google研究,终于任督二脉全通。
1、首先,Apache的配置文件是httpd.conf和httpd-vhosts.conf,我们先来看下wamp安装好后,httpd.conf的默认配置。
DocumentRoot "d:/wamp/www/" <Directory /> AllowOverride none Require all denied </Directory> <Directory "d:/wamp/www/"> Options Indexes FollowSymLinks AllowOverride all Require local </Directory> # Virtual hosts #Include conf/extra/httpd-vhosts.conf
要想通过虚拟域名访问网站,就需要配置一下httpd-vhosts.conf。那么需要启动httpd-vhosts.conf,因为默认是关闭的,所以把配置文件中#Include conf/extra/httpd-vhosts.conf前面的#去掉。于是httpd-vhosts.conf启用了,那么我们去编辑httpd-vhosts.conf这个文件。
2、httpd-vhosts.conf文件的位置,在apache目录下的conf/extra,上面的Include conf/extra/httpd-vhosts.conf其实已经告诉了你它的位置。
在这个文件中,添加配置我上面的CrashServer网站:
NameVirtualHost *:80 <VirtualHost *:80> DocumentRoot "D:/wamp/www/CrashServer" ServerName crash.com </VirtualHost>
首先,我的CrashServer是放在wamp/www下面的,这是wamp默认的网站目录,其次,我想在本地测试的时候,用crash.com就能访问到CrashServer,于是配置如上。
在这里,为了让我们能通过crash.com访问本地站点,所以需要修改hosts文件,添加 127.0.0.1 crash.com。
到这里,配置完毕了,于是重启Apache,输入crash.com访问,结果正常访问。但是,当以localhost访问时,原来出现的是wamp的主页,现在却显示CrashServer了,于是还要在hosts里追加 127.0.0.1 localhost,在httpd-vhosts.conf中,追加上localhost的站点配置,现在看起来就是这样了:
NameVirtualHost *:80 <VirtualHost *:80> DocumentRoot "D:/wamp/www" ServerName localhost </VirtualHost> <VirtualHost *:80> DocumentRoot "D:/wamp/www/CrashServer" ServerName crash.com </VirtualHost>
OK,到此基本可以结束了,网站都配置好了,看起来非常非常的简单。但是对于我却不是这样的,我昨天碰到了如下问题。
首先,我的CrashServer一开始不是放在wamp/www下的,而是放在E:\360Downloads下面的,于是就有了如下配置:
<VirtualHost *:80> DocumentRoot "E:/360Downloads/CrashServer" ServerName crash.com </VirtualHost>
这没错吧,路径都对,虚拟域名也对,可是访问的时候,却提示403 Forbidden,没有权限。于是Google,哦,知道了原来是要给CrashServer目录加上权限,于是修改配置如下:
<VirtualHost *:80> DocumentRoot "E:/360Downloads/CrashServer" ServerName crash.com <Directory E:/360Downloads/CrashServer> Order Allow,Deny Allow from All Require all granted </Directory> </VirtualHost>
重启Apache,访问正常了。首先,新增的Directory,是可以在httpd.conf中添加的,也可以在httpd-vhosts.conf中添加,我认为在后者里面添加更好,配置内容更加清晰明了,项目目录权限跟随着项目站点配置。在上面新增的Directory中,我们给了360Downloads下的CrashServer目录加了权限,允许访问了,所以就不再提示403 Forbidden了。
这个问题,现在写起来是这么的简单轻松,可问题出现的时候,却很让人困扰和郁闷。对于项目在wamp/www之外的,需要给项目目录权限才可,注意:
Order Allow,Deny Allow from All Require all granted
这三条缺一不可,这是配置了允许外部计算机访问服务器站点。
3、今天问题解决后,想到通过同一局域网下的其他设备访问我的站点,于是用手机,在浏览器中输入我的电脑的ip,不能访问,再次Google,原来需要修改在httpd.conf中的配置:
<Directory "d:/wamp/www/"> Options Indexes FollowSymLinks AllowOverride all Require local </Directory>
其中,Require local,没Google出来,但看名字知道,是只允许本地访问,于是改成Require all granted,允许所有请求访问,手机就可以访问了。
参考,http://roteg.iteye.com/blog/1465380,这里是访问验证配置的解释。
这里,有篇老外写的配置博文,很好,https://www.kristengrote.com/blog/articles/how-to-set-up-virtual-hosts-using-wamp#wamp-step-7,但是唯独一点,在他的Step 7的时候,给项目目录添加权限:
<Directory C:/Users/Kristen/Documents/Projects> Order Deny,Allow Allow from all </Directory>
却恰恰少了Require all granted,导致最后还是403 Forbidden,搞得我非常郁闷。
---------------------------------------------------------------------------------补充 2015-07-13-------------------------------------------------------------------------------------------------
感谢评论中唯一的评论者的提醒:Require all granted这是2.4上边才需要的,2.2不需要。
这就可以理解了为什么Google出来的技术文章,有些提到require all granted,而有些则没有。
---------------------------------------------------------------------------------end 补充 2015-07-13--------------------------------------------------------------------------------------------
此配置是在如下wamp环境进行的:
至此,在Apache下配置PHP站点,全部完成。
以上所述就是本文的全部内容了,希望大家能够喜欢。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
