Configuring local subdomains using Wampserver

Compared to using a web server, developing code locally on your computer offers some significant advantages. You don't have to wait for your file modifications to upload to the web, so it's typically faster.

WampServer, a tool that enables you to run a local web environment, will be what I use (Apache, PHP & MySQL). WampServer must be downloaded and installed before you can move on with this tutorial.

INSTRUCTION

The guide presupposes that WAMP is set up in C:\wamp. In case your installation is different, you can modify this route to reflect it.

Step 1: Create the folder where your subdomains will be stored.

Just for example. Please create the following folder:
C:\wamp\www\subdomains

The subdomains folder's contents will all effectively transform into subdomains. For instance:

learnweb.localhost will point to C:\wamp\www\subdomains\learnweb\

Step 2: Ensure that the httpd-vhosts.conf file is present.

1) Open C:\wamp\bin\apache\apache2.4.46\conf\httpd.conf (or choose Wampserver > Apache > httpd.conf)
2) Find the lines:

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

3) Change to:

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

Step 3: Apache VirtualHosts Configuration

1) For Apache, enable the alias_module and vhost_alias_module .

By doing this, you can enable virtualhost aliasing for your subdomains in Apache.

2) Find and Open C:\wamp\bin\apache\apache2.4.46\conf\extra\httpd-vhosts.conf

3) The httpd-vhosts.conf file probably has some sample information. Everything should be changed to:

<VirtualHost *:80>
ServerName localhost
ServerAlias localhost
DocumentRoot "${INSTALL_DIR}/www"
<Directory "${INSTALL_DIR}/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName localhost
ServerAlias *.localhost
VirtualDocumentRoot "${INSTALL_DIR}/www/subdomains/%1"
ErrorLog "logs\errors.log"
<directory "${INSTALL_DIR}/www/subdomains/%1">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</directory>
</VirtualHost>

What is happening with the code above, then?

A host definition is contained within each <VirtualHost> tag. As indicated by the asterisk in <VirtualHost *:80>, any IP address may access this host definition. You might enter <VirtualHost 127.0.0.1:80> to limit access to just your PC.

ServerName
ServerName localhost
Localhost is the domain I use. Just keep in mind that whichever name you use will inevitably replace any real domains (EX: using facebook.com will not let you access the real facebook.com later)

ServerAlias
ServerAlias *.localhost
An alternate domain alias (usually www.localhost.com)

DocumentRoot
DocumentRoot "${INSTALL_DIR}/www"
The absolute directory to your domain’s root

ErrorLog
ErrorLog "logs\errors.log"
Path to your error log.  This is optional.

VirtualDocumentRoot
VirtualDocumentRoot "${INSTALL_DIR}/www/subdomains/%1"
A wildcard-delimited absolute directory for your subdomain that specifies the name of the subdomain. In order to obtain the name of the subdomain, I utilize a wildcard %1. The learnweb.localHost will lead to learnweb's subdomains in C:\wamp\www.

<directory ________>
<directory "${INSTALL_DIR}/www/">
<directory "${INSTALL_DIR}/www/subdomains/%1">
The directory permission definitions. Defined twice for both localhost and its virtual subdomains.

4) You must restart Apache after making the modifications to your httpd-vhosts.conf file in order for them to take effect. To restart all services, simply click the WAMPSERVER icon in your Notification Area.

Step 3: Updating Windows Host File

You must first point your localhost or learnweb.localhost to your local IP address before you may access them. Unfortunately, using a wildcard in the hosts file is not supported by Windows. For each new subdomain to function in your browser, you must add it to your hosts file.

1) Open C:\Windows\System32\drivers\etc\hosts using Notepad
2) Add a the following line to the bottom of your hosts file.

127.0.0.1       learnweb.localhost

That's all what you need to make your localhost subdomain works.

Need Support?

Sometimes, Apache configurations can be a little perplexing. If you run into trouble, let me know in the comments section below, and I'll try to help. Please describe the actions you performed so I can more accurately identify the problem with your setup.

Similar Tutorials

Comments