大网数据广告活动
网站运营与故障问答

How can I modify httpd.conf to add one more site in Apache 2.4.6?

· 52CE 我爱测

How can I modify httpd.conf to add one more site in Apache 2.4.6? Here is my httpd.conf: ServerRoot "/etc/httpd" Include conf.modules.d/*.conf Listen 80 User apache Group apache ServerAdmin root@localhost ServerName xx.xx.xx.xx:8…

问题原文

原帖链接 · fish man · CC BY-SA 3.0

How can I modify httpd.conf to add one more site in Apache 2.4.6? Here is my httpd.conf:

ServerRoot "/etc/httpd"
Include conf.modules.d/*.conf
Listen 80
User apache
Group apache
ServerAdmin root@localhost
ServerName xx.xx.xx.xx:80
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
ServerLimit 150
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
<Directory />
    AllowOverride none
    Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
<IfModule dir_module>
     DirectoryIndex index.html
</IfModule>
<Files ".ht*">
    Require all denied
</Files>
ErrorLog logs/error_log
LogLevel crit
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined env=!dontlog
    CustomLog "logs/my_log" combined env=!dontlog
    #CustomLog "|sbin/rotatelogs -f logs/my_log 60" combined env=!dontlog
    SetEnvIf Remote_Addr "::1" dontlog
</IfModule>
<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
    HostnameLookups Off
    ProxyPreserveHost On
    DocumentRoot /var/www/html
    RLimitCPU 120|max
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
<VirtualHost *:80>
    HostnameLookups Off
    ProxyPreserveHost On
    DocumentRoot /var/www/html
    RLimitCPU 120|max
    ServerName domain1.com
    <directory "/var/www/html">
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </directory>
</VirtualHost>
ExpiresActive On
<FilesMatch "\.(jpg|jpeg)$">
</FilesMatch>
<FilesMatch "\.(css|png|js|gif)$">
 ExpiresDefault "access plus 1 month"
</FilesMatch>

<ifmodule deflate_module>
DeflateCompressionLevel 6
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
</ifmodule>

My problem is I have already one site in '/var/www/html' with domain1.com. If I add one more site domain2.com, create a new folder like /var/www/new or /var/www/html/new, how can I modify the <VirtualHost *:80> part? Because I have defined DocumentRoot /var/www/html. Thanks.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

My problem is I have already one site in '/var/www/html' with domain1.com. If I add one more site domain2.com create a new folde like /var/www/new or /var/www/html/new, how to modify <VirtualHost *:80> part? because I have deified DocumentRoot /var/www/html.

I'm not really sure what the "problem" is? You copy the entire <VirtualHost *:80> container for each site you want to configure, changing the ServerName, DocumentRoot and <Directory> containers appropriately. The <VirtualHost *:80> opening "tag" remains unchanged for name-based virtual hosts.

Whichever ServerName matches the Host in the HTTP request header controls which <VirtualHost> is used. This <VirtualHost> overrides any default settings (DocumentRoot etc.) you might have in the main config file.

a new folde like /var/www/new or /var/www/html/new

If these are entirely separate sites it might be preferable to have them in separate directory trees because of how Apache config / .htaccess inherit along the filesystem path. (Although this can also be used to your advantage if you are managing a cluster of sites. eg. blocking bad bots in one place, rather than for each site separately.)

If you have more than a couple virtual hosts on your server it is easier (more modular) to have your virtual hosts in a separate file and include them in the main Apache config, for example:

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-05-09)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。