Skip to content

Instantly share code, notes, and snippets.

@Buravo46
Last active January 31, 2021 05:23
Show Gist options
  • Save Buravo46/d3fbe74259cdfc73001c to your computer and use it in GitHub Desktop.
Save Buravo46/d3fbe74259cdfc73001c to your computer and use it in GitHub Desktop.
【XAMPP】バーチャルホストを設定し、同じLAN内の他PCからアクセスする方法

【XAMPP】バーチャルホストを設定し、同じLAN内の他PCからアクセスする方法

概要

同じLAN内の他PCやスマートフォンから、 ローカルで公開している画面にアクセスできるよう設定する方法です。

構築前の環境

環境 環境名
OS Windows 7 Professional SP1 64-bit
XAMPP XAMPP Version 5.6.8

手順

手順としては下記項目となります。

httpd.confの設定

1. リクエストを受け付けるポート番号の指定

Apacheが外部からリクエストを受け付けるポート番号を下記のように記述します。

#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80
Listen ポート番号
2. ホスト名の指定

Apacheサーバが自分自身のホスト名を示す時に使われる名前を下記のように記述します。

#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName IPアドレス:ポート番号

httpd-vhosts.confの設定

1. 名前ベースの仮想ホストの設定

アクセス先のIPアドレスに対して、名前ベースの仮想ホストを設定していることを表すために、NameVirtualHostを下記のように設定します。

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
NameVirtualHost *:ポート番号
2. ServerName毎の個別の設定

ServerName毎に個別の設定を下記のようにします。

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

<VirtualHost *:ポート番号>
DocumentRoot C:/xampp/htdocs/設定したいディレクトリ
ServerName サーバ名
</VirtualHost>
<Directory "C:/xampp/htdocs/設定したいディレクトリ">
order deny,allow
allow from ALL
</Directory>

hosts

1.IPアドレスに対するホスト名の定義

IPアドレスとホスト名を下記のように記述します。

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
127.0.0.1       localhost
IPアドレス   サーバ名

hostsファイルはC:\Windows\System32\drivers\etc直下に存在します。

このファイルは管理者権限でないと開くことが出来ないので、注意する必要があります。

開く際は全てのファイル(*.*)として探す必要があります。

httpd-xampp.conf

1.ローカルネットワーク内のアクセス許可

New XAMPP security conceptを下記のように修正します。

#
# New XAMPP security concept
#
<LocationMatch "^/(?i:(?:xampp|security|licenses|phpmyadmin|webalizer|server-status|server-info))">
        #Require local
	ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var
	Order deny,allow
	Deny from all
	Allow from 127.0.0.1
	Allow from IPアドレス
</LocationMatch>

Allow fromの箇所にはアクセス許可をしたいIPアドレスを記述します。

注意点

ローカルからアクセスできない場合は下記が問題かもしれません。

  • ポート番号80がすでに使用されている。Skypeなど他で使用している可能性があります。
  • プロキシサーバを使用している。

他PCからアクセスできない場合は、下記が問題かもしれません。

  • ファイアウォールを使用している。

##アクセス方法

URLにhttp://IPアドレス:ポート番号を入力すればアクセスできます。

参考サイト

xamppのバーチャルホスト設定|ポートベース編

【xampp】LAN内の他のPCからページが見れない場合の対処法

XAMPP にイントラネット内 (LAN 内) のPC から接続する設定

Aoplanning.com - XAMPP設定

サーバ名とポート番号(ServerName, Listen)

ホスト名によるアクセス設定(hostsファイル)

名前ベースの仮想ホスト

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment