首页 >PHP >Apache配置讲解

Apache配置讲解

  • PHP4715
  • 2018-02-26 17:05:11

在配置PHP运行环境时很多初学者都会选择使用phpstudy作为开发环境工具,我们这就来讲一下在phpstudy中的apache的配置和PHP的配置,希望能帮助到大家。

Apache 配置详解

配置文件:httpd.conf

1.基本配置

ServerRoot "D:/Apache" Apache的安装目录

Listen 80 服务器监听的端口号

ServerName www.xxx.com:80 主站点名称(网站的主机名)

ServerAdmin xxx@qq.com 管理员的邮件地址

DocumentRoot "D:/WWW" 网站的根目录

2.以下是对主站点目录进行访问控制

Options FollowSymLinks

AllowOverride None

Order allow,deny

Allow from all

选项详解:

Options:配置在特定目录中使用那些属性,其值和含义如下

ExecCGI 允许在此目录下执行CGI脚本

FollowSymLinks 在此目录下允许文件系统使用符号连接

Indexs 在用户访问该目录时,如果找不到DirectoryIndex指定的主页文件(如index.html)则返回该目录的文件列表给用户

SymLinksIfOwnerMatch 当使用符号连接时,只有符号连接的拥有者与文件的拥有者相同时才可以访问

AllowOverride: 允许村子与.htaccess文件中的指令(.htaccess的文件名可以更改,其文件名有AccessFileName指令决定)

None: 当设置为None时,默认不搜索服务器目录的.htaccess文件,可以减小服务器开销

All: 在.htaccess文件中可以使用所有的指令

Order: 控制在访问时Allow,deny两个访问规则哪个优先

All:允许访问的主机列表

Deny:拒绝访问的主机列表

DirectoryIndex: index.html index.htm index.php 默认主页的文件

3.虚拟站点的配置

条件:在http.conf 中将 httpd-vhosts.conf包含进来

# Virtual hosts
      Include conf/extra/httpd-vhosts.conf

   

在 httpd-vhost.conf中配置

(1)基于IP的虚拟主机

修改hosts文件,添加3个域名与之对应

192.168.1.11 www.test1.com
192.168.1.12 www.test2.com
192.168.1.13 www.test3.com

建立虚拟主机存放文件的根目录,如

   

www/test1/1.html
www/test2/2.html
www/test3/3.html

   

在httpd-vhosts.conf进行如下配置

   
<VirtualHost 192.188.1.11*80>
         ServerName www.test1.com
         DocumentRoot "www/test1"
         <Directory "www/test1">
             Options Indexs FollowSysLinks
             AllowOverride None
             Order allow deny
             allow from all
             DirectoryIndex  index.html index.htm index.php
         </Directory>
</VirtualHost>
         
<VirtualHost 192.168.1.12:80>
          ServerName www.test2.com
           DocumentRoot /www/test2/
          <Directory "/www/test2">
             Options Indexes FollowSymLinks
              AllowOverride None
              Order allow,deny
              Allow From All
           </Directory>
</VirtualHost>
   
<VirtualHost 192.168.1.13:80>
        ServerName www.test3.com
        DocumentRoot /www/test3/
        <Directory "/www/test3">
         Options Indexes FollowSymLinks
         AllowOverride None
         Order allow,deny
          Allow From All
       </Directory>
</VirtualHost>

   

(2)基于主机名

设置域名映射同一个主机

   
192.168.1.10 www.test1.com
192.168.1.10 www.test2.com
192.168.1.10 www.test3.com

   

设置存放网页的根目录

   
www/test1/1.html
www/test2/2.html
www/test3/3.html

   

在使用基于域名的虚拟主机时,必须指定服务器的IP地址和可能的访问端口来使主机接受请求,可以使用NameVirtualHost指令来配置,如果服务器上所有的IP都会用到,则可以使用*来表示,在NameVirtualHost指定的ip并不会让服务器监听这个IP

然后配置<VirtualHost>

如果在现有的WEB服务器上配置虚拟主机,则必须为现存的虚拟主机也配置<VirtualHost>,其中ServerName 和 DocumentRoot包含的内容应该与全局的内容一致,且要放在配置文件的最前面,作为默认主机的配置

   
NameVirtualHost *:80
<VirtualHost *:80>
        ServerName www.test1.com
        DocumentRoot "www/test2"
        <Directory "www/test1">
             Options Indexs FollowSymLinks
             AllowOverride None
             Order allow,deny
             allow from all
        </Directory>
</VirtualHost>
       <VirtualHost *:80>
        ServerName www.test2.com
        DocumentRoot "www/test2"
        <Directory "www/test2">
             Options Indexs FollowSymLinks
             AllowOverride None
             Order allow,deny
             allow from all
        </Directory>
</VirtualHost>
<VirtualHost *:80>
        ServerName www.test3.com
        DocumentRoot "www/test3"
        <Directory "www/test3">
             Options Indexs FollowSymLinks
             AllowOverride None
             Order allow,deny
             allow from all
        </Directory>
</VirtualHost>

   

(3)基于端口

修改httpd.conf
设置为 Listen 8001、Listen 8002

修改虚拟主机配置文件 httpd-vhosts.conf

   
<VirtualHost *:8001>
         ServerName www.test1.com
         DocumentRoot "www/test1"
     </VirtualHost>
      <VirtualHost *:8002>
         ServerName www.test2.com
         DocumentRoot "www/test2"
</VirtualHost>

   

PHP 配置

配置文件:php.in

1. 模块加载:

   
extension = php_mysql.dll

   

2. 修改模块的目录

extension_dir = "D:/php/ext"

   

也可以将 D:/php ,D:/php/ext 添加到系统环境变量中

3. 在Apache中配置php

更改httpd.conf

LoadModule php5_module "D:/php/php5apache2_2.dll 添加PHP模块

PHPIniDir "D:/php" 配置php.in路径

配置AddType

   
AddType application/x-httpd-php .php
AddType application/x-httpd-php .txt

   

4. register_globals = Off 设置是否开启全局变量

若设置为On

已GET/POST提交的参数,直接可以使用变量用调用, 建议不开启

5.设置时区:date.timezone =PRC 

以上就是本篇文章的所有内容,希望对你的PHP学习提供到帮助!