← Back to Apache Course | Chapter 2: Configuration Basics | Lesson 2 of 4

Virtual Hosts का परिचय

एक single Apache server एक साथ कई अलग-अलग websites host कर सकता है -- Virtual Hosts वह mechanism है जो Apache को उन sites में फर्क बताने और हर एक को उसकी अपनी directory से अपनी settings के साथ serve करने देता है।

Virtual Hosts क्यों Exist करते हैं

Virtual Hosts के बिना, एक server असल में हर IP address और port पर सिर्फ एक site serve कर सकता है। Virtual Hosts एक Apache instance को एक साथ site-a.com, site-b.com, और blog.site-a.com के लिए answer करने देते हैं, हर एक की अपनी DocumentRoot, logs, और settings के साथ -- बिल्कुल वही जिस पर shared hosting और ज़्यादातर multi-site servers depend करते हैं।

Name-Based Virtual Hosts

आज सबसे common setup name-based virtual hosting है: हर site same IP और port share करती है, और Apache decide करता है कि कौन सी serve करनी है browser द्वारा भेजे गए Host header (जो बस वह domain name है जो आपने type किया) को पढ़कर। एक <VirtualHost *:80> block, जो इसके ServerName से match किया जाता है, हर site define करता है।

एक VirtualHost Block की Anatomy

एक typical block ServerName (primary domain जिसे यह answer करता है) set करता है, optionally ServerAlias (दूसरे domains/subdomains जिन्हें same block से match होना चाहिए, जैसे www.example.com), DocumentRoot (जहाँ इसकी files रहती हैं), और अपने खुद के ErrorLog/CustomLog paths ताकि हर site के logs अलग रहें।

Apache Match कैसे चुनता है

Apache आने वाली requests को हर <VirtualHost> block के against उसी order में check करता है जिस order में वे load होते हैं, और पहला वही इस्तेमाल करता है जिसका ServerName/ServerAlias request के Host header से match करे। अगर कुछ भी match न हो, तो यह उस IP/port के लिए define किए गए बिल्कुल पहले VirtualHost block पर fall back करता है -- यही कारण है कि जानबूझकर एक "default" या catch-all site पहले रखना आम practice है।

Warning: same ServerName (या overlapping ServerAlias) वाले दो VirtualHost blocks का चुपचाप मतलब यह है कि दूसरा effectively unreachable है -- Apache warn नहीं करेगा, यह बस हमेशा पहले वाले को match करता है।

एक नया Virtual Host Enable करना

Ubuntu पर, एक नई site का config sites-available/ में एक file है, जिसे a2ensite yoursite.conf से enable किया जाता है, फिर systemctl reload apache2 से activate किया जाता है। RHEL-family systems पर, file को बस included conf.d/ directory में होना चाहिए और Apache इसे अगले reload पर pick up कर लेता है -- कोई अलग enable step नहीं है।

उदाहरण: A basic name-based VirtualHost

apacheconf
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
{# Flagged by hand after confirming a runner can't handle this example (a shell command / go.mod file stored as a TopicExample, a language feature the configured runner version doesn't support, or output that blows a runner's sandbox limit) -- see TopicExample.norun. Never render the run button for these, regardless of language, since it would just fail at execute_code (or worse, hang the Judge0 queue on a submission that can never finish cleanly). #}

⚠️ This example can't run in the browser editor. Try it in your own local environment instead.

{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक VirtualHost block पर ServerName भूल जाना, जो Apache को guess करने पर छोड़ देता है और एक startup warning log कर सकता है या requests को unpredictably match कर सकता है।
  2. बिल्कुल same ServerName वाले दो VirtualHost blocks define करना, ताकि दूसरा असल में कभी इस्तेमाल ही न हो।
  3. VirtualHost file edit करना लेकिन Apache reload करना भूल जाना (या, Ubuntu पर, पहले a2ensite भूल जाना) -- नई site बस अभी तक reachable नहीं होगी।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.