Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create localhost domain management for windows in php?

I am crazy about this thing, but i want to make it myself in php itself.

I have installed the following in my system.

WAMP Server 2.2 which includes following

  • Apache Web Server Version 2.2.222
  • MySQL Server Version 5.5.24
  • PHP Version 5.3.13
  • OS Version Windows 8 64Bit

Location of Hosts file:

C:\Windows\System32\Drivers\etc\hosts

Location of WAMP Server:

C:\wamp

Location of Apache Web Server:

 - C:\wamp\bin\apache\apache2.2.22

 - C:\wamp\bin\apache\apache2.2.22\conf\extra\httpd-vhosts.conf

Location of MySQL Server:

C:\wamp\bin\mysql\mysql5.5.24

Location of PHP:

C:\wamp\bin\php\php5.3.13

I have simple example of Create the VHost in localhost e.g. I want to create simple domain www.mylocalsite.com on 80 port

For that I have following steps:

(1) Enagle Apache modules

  • rewrite_module
  • vhosts_alias_module

(2) Open httpd.conf file to enable Vhost settings

C:\wamp\bin\apache\apache2.2.22\conf\httpd.conf

Virtual hosts

Include conf/extra/httpd-vhosts.conf // Remove the # before Include and save file

(3) Add VHost entry in httpd-vhosts.conf file

<VirtualHost *:90>
   DocumentRoot "C:/mylocalsite.com/"
   ServerName www.mylocalsite.com

   # This should be omitted in the production environment
   SetEnv APPLICATION_ENV development

   <Directory "C:/mylocalsite.com/">
       Options Indexes MultiViews FollowSymLinks
       AllowOverride All
       Order allow,deny
       Allow from all
   </Directory>

   ErrorLog "C:/mylocalsite.com/logs/error.log"   // Logs folder should be exists
   CustomLog "C:/mylocalsite.com/logs/access.log" common

</VirtualHost>

(4) Add entry in hosts file Open file in Notepad with Admin Permission C:\Windows\System32\Drivers\etc\hosts add following line at end of the file and Save it.

127.0.0.1    www.mylocalsite.com

(5) Restart the (From WAMP) Apache Web Server and run in browser http://www.mylocalsite.com/ will work.

Now, My Question is how can I do the above steps in dynamic nature using PHP/JSP or any other language.

Suppose I will create one Form in HTML with following fields and when submit it will create new MySQL entry for that domain.

EDIT:

Domain Type: select option or Radio Options ( Root/Sub-Domain )
Sub-Domain Name ( Optional ): Text field
Domain Name: Text field
Project Path: text field
Log Folder Path: text field
Tmp Folder Path: text field
Database Type: text field ( mysql/pgsql )
<Submit>

when i click on button it will automatically create the domain entry in hosts file, vhosts entry in httpd-vhosts.conf file.

And, when restart the Apache server it will run automatically created domain or sub-domain dynamically.

Can anyone knows how can i established the following things in any language for local system only?

like image 586
Ashwin Parmar Avatar asked Oct 02 '13 04:10

Ashwin Parmar


People also ask

How do I create a local host domain?

To set up a custom domain in localhost we have to apply the change in three places: Setting local development in XAMPP & identifying directory, pointing a custom domain to local computer from Windows host file, and redirecting the domain to a specific application folder through VirtualHost.

Can I use localhost as domain?

The name localhost is reserved by the Internet Engineering Task Force (IETF) in RFC 2606 (June 1999) as a domain name label that may not be installed as a top-level domain in the Domain Name System (DNS) of the Internet.


1 Answers

Don't use web form. You can see my demo with batch file:

(1) Create vhost template template.txt

<VirtualHost *:90>
DocumentRoot "_ROOT_"
ServerName _DOMAIN_

# This should be omitted in the production environment
SetEnv APPLICATION_ENV development

<Directory "_ROOT_">
  Options Indexes MultiViews FollowSymLinks
  AllowOverride All
  Order allow,deny
  Allow from all
</Directory>

ErrorLog "_ROOT_/logs/error.log"// Logs folder should be exists
CustomLog "_ROOT_/logs/access.log" common

</VirtualHost>

(2) Create add_vhost.bat

@echo off  
set /p domain=Domain (www.mylocalsite2.com):   
set lineHost=127.0.0.1 %domain%  

REM Create the domain entry in hosts file  
echo %lineHost% >> C:\Windows\System32\drivers\etc\hosts  

set /p folder=Folder (C:/mylocalsite2.com/):   
REM Create vhost entry in httpd-vhosts.conf file  
setlocal enabledelayedexpansion  
for /f "tokens=*" %%i in (template.txt) do (  
    set str=%%i  
    set str=!str:_ROOT_=%folder%!  
    set str=!str:_DOMAIN_=%domain%!  
    echo !str! >> C:\wamp\bin\apache\apache2.2.22\conf\extra\httpd-vhosts.conf  
)  

(3) Run add_vhost.bat as Administrator (to write HOSTS file)

like image 105
HuyPV Avatar answered Oct 03 '22 21:10

HuyPV