apache与nginx的虚拟域名配置

由于开发需求,项目有时候需要设置虚拟域名进行测试。下面是分别是apache和nginx的配置

一、apache

环境:wampserver2.5

1.修改host文件

C:\Windows\System32\drivers\etc\host

底部追加

127.0.0.1       test.com

2.修改apache配置文件

F:\wamp\bin\apache\apache2.4.9\conf\httpd.conf

前面#号去掉,打开vhost模块

LoadModule vhost_alias_module modules/mod_vhost_alias.so

去掉#,加载虚拟配置文件,并编辑

Include conf/extra/httpd-vhosts.conf

这文件清空吧。一堆注释没什么用

#让localhost可以访问

<VirtualHost *:80>

ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "F:/wamp/www"
ServerName localhost
ServerAlias localhost
ErrorLog "logs/local-error.log"
CustomLog "logs/local-access.log" common
</VirtualHost>

#测试

<VirtualHost *:80>
DocumentRoot "F:/wamp/www/test.com"
ServerName test.com
ErrorLog "logs/test-error.log"
CustomLog "logs/test-access.log" common

</VirtualHost>

如果有遇到拒绝访问,那便是要设置目录访问权限了。可以再httpd.conf 中找到根目录修改

<Directory F:/wamp/www/>
AllowOverride All
Require all granted
</Directory>

  

甚至你可以把这段代码放到上面的<virtualhost>标签里面。

重启apache。http://test.com 访问ok!

二、nginx

1.host文件

vi /etc/hosts

追加

Include conf/extra/httpd-vhosts.conf

wq保存退出

2.修改nginx配置

vi /etc/nginx/nginx.conf

末端打开加载虚拟配置目录   

Include conf/extra/httpd-vhosts.conf

3.添加虚拟配置

vi /etc/nginx/conf.d/test.com.conf

server{

listen 80;

server_name test.com;

index index.html index.php;

root /var/www/html/test.com;

#支持php

location ~ \.php$

{
 root /var/www/html/test.com;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "open_vasedir=$document_root:/temp;";
include fastcgi_params; } }

重启nginx服务。

service nginx restart

访问test.com,OK!

补充:如果需要nginx打开错误信息报告。需要配置一下

vi /etc/php.ini

shift+: / display_error = On

因为我开启了php-fpm。所以,还要编辑 php-fpm.conf文件,把php_flag[display_errors]设为on:

vi /etc/php-fpm.conf

有的可能在/etc/php-fpm.d/www.conf

php_flag[display_errors] = on

重启服务器。就能看到报错信息了。

如果还没看到输出错误。查看phpinfo(),display_error是否为0

按理说这样,错误信息是不会输出了,但是当PHP有错误时,会把报错提示显示在页面上。
搜索了下,发现有人说当log_errors开启时,如果error_log的路径不对,会导致报错显示。经查与此无关。
输出phpinfo(),查看到display_errors是On。在页面中指定:ini_set(“DISPLAY_ERRORS”,0),仍然无效!
最后发现php-fpm.conf里有句:

Additional php.ini defines, specific to this pool of workers.

 <value name="php_defines">

 <value name="sendmail_path">/usr/sbin/sendmail -t -i</value>

 <value name="display_errors">0</value>

 </value>  

  

原来忘了修改这个地方的配置,display_errors被设置为1了!改成0就好了。这里面也说明了在php_defines里可以额外指定某些php的参数。
直接将这两段注释掉,php-fpm reload后,报错提示不再显示到页面上了。

如果是0,没修改成功,可以在php代码中加上

ini_set('display_error',1);//0 or 1
ini_set('error_reporting',E_ALL);

  

 

 http://www.jbxue.com/article/13758.html

posted @ 2015-10-12 11:04  Lion_coder  阅读(422)  评论(0编辑  收藏  举报