源码侠

Linux下Apache虚拟主机配置

󰌂发布时间 2024-04-17 热度 389

 当前位置:  首页 / 教程资源 / 运维教程

教程内容:

Apache安装目录:/usr/local/apache

目的:

配置Apache虚拟主机

具体操作:

1、修改Apache主配置文件

cp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf-bak #修改之前先备份

vi /usr/local/apache/conf/httpd.conf #修改

找到# Virtual hosts这一行,添加以下两行:

Include conf/error/*.conf
Include conf/vhost/*.conf

找到# Server-pool management (MPM specific)这一行,取消下面一行的注释

Include conf/extra/httpd-mpm.conf


找到#ErrorDocument 404 /404.html这一行,取消前面的注释

ErrorDocument 404 /404.html #404.html需要创建好放在网站根目录下面

找到# ErrorLog: The location of the error log file.这一段,添加

ErrorLog "/usr/local/apache/logs/error_log" #Apache错误日志记录,需要创建好目录文件

#ErrorLog "/dev/null" #表示不记录Apache错误日志

LogLevel crit #日志级别,致命情况才记录

找到# The location and format of the access logfile (Common Logfile Format).这一段,添加

CustomLog "|/usr/local/apache/bin/rotatelogs /usr/local/apache/logs/access_log%Y%m%d.log 86400 480" combined #按天保存Apache访问日志记录

:wq! #保存退出

2、创建虚拟主机配置文件存放目录

cd /usr/local/apache/conf

mkdir vhost #创建虚拟主机存放目录

mkdir error #创建空主机头存放目录

3、禁止Apache空主机头设置

cd /usr/local/apache/conf/error
vi error.osyunwei.com.conf #编辑
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin yourmail@mail.com
DocumentRoot /tmp
ServerName error
</VirtualHost>



:wq! #保存退出

4、添加虚拟主机配置文件

cd /usr/local/apache/conf/vhost

vi bbs.osyunwei.com.conf #编辑

<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /home/web/osyunwi.com/bbs.osyunwei.com/
ServerName bbs.osyunwei.com
ServerAlias bbs.osyunwei.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
CustomLog "|/usr/local/apache/bin/rotatelogs /usr/local/apache/logs/osyunwei.com/bbs.osyunwei.com/access_log%Y%m%d.log 86400 480" combined #访问日志,目录文件需要提前创建好
DirectoryIndex index.htm index.html index.php
<Directory "/home/web/osyunwi.com/bbs.osyunwei.com/">
php_admin_value open_basedir "/home/web/osyunwi.com/bbs.osyunwei.com/:/tmp/" #防止php木马跨站
Options Includes ExecCGI FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>



:wq! #保存退出

vi sns.osyunwei.com.conf #编辑

<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /home/web/osyunwi.com/sns.osyunwei.com/
ServerName sns.osyunwei.com
ServerAlias sns.osyunwei.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
CustomLog "|/usr/local/apache/bin/rotatelogs /usr/local/apache/logs/osyunwei.com/sns.osyunwei.com/access_log%Y%m%d.log 86400 480" combined #访问日志,目录文件需要提前创建好
DirectoryIndex index.htm index.html index.php
<Directory "/home/web/osyunwi.com/sns.osyunwei.com/">
php_admin_value open_basedir "/home/web/osyunwi.com/sns.osyunwei.com/:/tmp/" #防止php木马跨站
Options Includes ExecCGI FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>



:wq! #保存退出

5、重启Apache

service httpd restart #重启

至此,Linux下Apache虚拟主机配置教程完成。

评论(0)条

    源码侠

    站长素材 - 建站资源分享平台

    源码侠提供各类虚拟产品,虚拟资源以及教程、模板、素材等,源码侠是一个互联网虚拟产品,虚拟资源聚集地的特色网站!

    反馈建议

    kefu@xuniku.cn fuwu@xuniku.cn

    周一至周日9:00-23:00

    在线QQ咨询

    本期用户推荐小程序

    本站所有资源均为会员提供或网上搜集,版权归原作者所有,如需商业用途或转载请与原作者联系!所提供的内容仅供观摩学习交流之用,请勿用作商业用途!如有侵权,请及时 联系我们 删除

    Powered by 源码侠 © 2010-2020 流量联盟   copyright   北京流量圈信息科技有限公司