ایجاد هاست مجازی (VirtualHost) آپاچی در ویندوز

بدون دیدگاه

یکی از کارهایی که معمولا برای هر برنامه نویس PHP انجام می دهد ایجاد هاست مجازی یا دومین مجازی برای پروژه ها در لوکال هاست یا کامپیوتر خودش می باشد.این کار در مرحله اول شروع پروژه انجام می شود و باعث می شود پروژه به جای آدرس معمول (http://localhost/MyProject) با آدرس کوتاه تر و بهتری (مثل http://myproject.dev) در دسترس باشد.

در واقع شما در کامپیوتر خودتان یک دامین دلخواه به هر پروزه می توانید اختصاص دهید که این کار با باستفاده از قابلیت VirtualHosts در وب سرور Apache انجام می شود.

در ادامه می خواهیم برای پروژه مان یک هاست مجازی در ویندوز ایجاد کنیم و نرم افزار مورد استفاده ما XAMPP است.

[alert-note]نکته ! فرقی نمی کند شما از XAMPP استفاده می کنید یا WAMP یا ….فقط ممکن است بعضی از مسیرها برای هر کدام کمی متفاوت باشد.[/alert-note]

 

۱.اضافه کردن آدرس هاست مجازی به فایل hosts

این فایل را از مسیر (C:\Windows\System32\drivers\etc) پیدا و با NotePad باز کنید.محتویات آن شبیه این خواهد بود

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      ۱۰۲.۵۴.۹۴.۹۷     rhino.acme.com          # source server
#       ۳۸.۲۵.۶۳.۱۰     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	۱۲۷.۰.۰.۱       localhost
#	::۱             localhost

۱۲۷.۰.۰.۱             localhost

در انتهای فایل کد زیر را اضافه کنید

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      ۱۰۲.۵۴.۹۴.۹۷     rhino.acme.com          # source server
#       ۳۸.۲۵.۶۳.۱۰     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	۱۲۷.۰.۰.۱       localhost
#	::۱             localhost

۱۲۷.۰.۰.۱             localhost
۱۲۷.۰.۰.۱             myproject.dev

 

در همین رابطه :   چطور یک رزومه بی‌نقص بنویسیم؟

حالا باید تغییرات در این فایل را ذخیره کنیم ولی اجازه ذخیره داده نخواهد شد.باید فایل رو با همین اسم (hosts) در مسیر دیگری مثل دسکتاپ ذخیره کنید و بعد با روش Copy/Paste به مسیر اصلی کپی کنید.

۲.بروزرسانی فایل vhosts آپاچی

به مسیر (C:\xampp\apache\conf\extra) رفته و فایل httpd-vhosts.conf را باز کنید. محتویات آن شبیه زیر خواهد بود

# Virtual Hosts
#
# Required modules: mod_log_config

# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#
##NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ##ServerName or ##ServerAlias in any <VirtualHost> block.
#
##<VirtualHost *:80>
    ##ServerAdmin [email protected]
    ##DocumentRoot "C:/xampp/htdocs/dummy-host.example.com"
    ##ServerName dummy-host.example.com
    ##ServerAlias www.dummy-host.example.com
    ##ErrorLog "logs/dummy-host.example.com-error.log"
    ##CustomLog "logs/dummy-host.example.com-access.log" common
##</VirtualHost>

##<VirtualHost *:80>
    ##ServerAdmin [email protected]
    ##DocumentRoot "C:/xampp/htdocs/dummy-host2.example.com"
    ##ServerName dummy-host2.example.com
    ##ErrorLog "logs/dummy-host2.example.com-error.log"
    ##CustomLog "logs/dummy-host2.example.com-access.log" common
##</VirtualHost>

NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
</VirtualHost>

 

در انتهای این فایل کد زیر را اضافه کنید

<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/myproject"
ServerName myproject.dev
ServerAlias www.myproject.dev
<Directory "c:/xampp/htdocs/myproject">
AllowOverride All
Require all Granted
</Directory>
</VirtualHost>

 

۳.مرحله آخر

در مرحله آخر می خواهیم مطمئن بشیم که فایل httpd-vhosts.conf در تنظیمات اصلی Apache لحاظ شده است. برای این به آدرس (C:\xampp\apache\conf) رفته و فایل httpd.conf را باز کنید.خط زیر را در آن بیابید

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

دقت کنید که علامت # باید از اول خط دوم برداشته شود . به این صورت

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

حالا کار تمام است . کافیست سرویس Apache رو از کنترل پنل XAMPP اول Stop و بعدش Start کنید . به آدرس (http://myproject.dev) برید . باید محتویات پوشه رو ببینید

در همین رابطه :   آموزش استفاده از FontAwesome به جای آیکن های وب سایت

امیدوارم مفید باشه و استفاده کنید.منتظر نظرات و نکات شما هستیم.

 

دسته بندی : طراحي وب

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *