Last active
August 29, 2015 14:27
-
-
Save maxkostinevich/3eadc019b4514ad655b2 to your computer and use it in GitHub Desktop.
Shell script to create new Virtual Host on WAMP for Windows and XAMPP on Ubuntu.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: Create new Virtual Host for WAMP SERVER | |
:: IMPORTANT!!! Run this script as Administrator | |
:: IMPORTANT!!! Make sure that variable vhosts_path is configured properly | |
@ECHO OFF | |
:: Path to the Apache VHosts file | |
SET vhosts_path=D:\work\wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhosts.conf | |
:: Path to the WWW Directory | |
SET www_path=D:\work\wamp\www | |
SET www_path_unix=D:/work/wamp/www | |
:: Path to the Windows hosts | |
SET hosts_path=C:\Windows\System32\drivers\etc\hosts | |
:: Check if variables are configured properly | |
SET config_ok=false | |
IF EXIST %vhosts_path% IF EXIST %hosts_path% IF EXIST %www_path% SET config_ok=true | |
IF NOT "%config_ok%"=="true" ( | |
echo =============================================================================== | |
echo ERROR!!! | |
echo Wrong path for Apache VHosts file or WWW Directory or Windows hosts file | |
echo Please check the following paths: | |
echo vhosts_path="%vhosts_path%" | |
echo www_path="%www_path%" | |
echo hosts_path="%hosts_path%" | |
echo =============================================================================== | |
pause | |
EXIT | |
) | |
echo =============================================================================== | |
echo LATEST VHOST: | |
echo =============================================================================== | |
:: Get the number of lines in the file | |
set LINES=0 | |
for /f "delims==" %%I in (%vhosts_path%) do ( | |
set /a LINES=LINES+1 | |
) | |
:: Print the last 3 lines | |
set /a LINES=LINES-3 | |
more +%LINES% < %vhosts_path% | |
echo =============================================================================== | |
echo Create new Virtual Host | |
echo =============================================================================== | |
:: Set Variables | |
SET /P vhost_name=Enter host name e.g. demo.local: | |
SET /P vhost_folder=Enter host folder name: | |
SET /P vhost_ip=Enter host ip e.g. 127.0.0.2: | |
:: Check if variables are OK | |
SET config_ok=false | |
IF NOT "%vhost_name%"=="" IF NOT "%vhost_folder%"=="" IF NOT "%vhost_ip%"=="" SET config_ok=true | |
IF "%config_ok%"=="true" ( | |
:: Create VHost Folder | |
MKDIR %www_path%\%vhost_folder% | |
:: Create sample index.php file | |
echo ^<^?php echo ^"%vhost_name%^"^; ^?^> >> %www_path%\%vhost_folder%\index.php | |
echo The folder "%vhost_folder%" in "%www_path%" has been created | |
:: Update Apache VHosts file | |
echo. >> %vhosts_path% | |
echo. >> %vhosts_path% | |
echo ^<VirtualHost %vhost_ip%^> >> %vhosts_path% | |
echo ServerAdmin webmaster@localhost >> %vhosts_path% | |
echo DocumentRoot ^"%www_path_unix%/%vhost_folder%^" >> %vhosts_path% | |
echo ServerName %vhost_name% >> %vhosts_path% | |
echo ErrorLog ^"logs/%vhost_name%-error.log^" >> %vhosts_path% | |
echo CustomLog ^"logs/%vhost_name%-access.log^" common >> %vhosts_path% | |
echo ^</VirtualHost^> >> %vhosts_path% | |
echo Apache VHosts file has been updated successfully | |
:: Update Windows Hosts file | |
echo. >> %hosts_path% | |
echo %vhost_ip% %vhost_name% >> %hosts_path% | |
echo Windows Hosts file has been updated successfully | |
:: Restart Apache | |
:: Works on Wamp x64 | |
:: For x32 just remove '64' from service name | |
net stop wampapache64 | |
net start wampapache64 | |
echo Apache has been restarted successfully | |
:: Clear the screen and print result | |
CLS | |
echo =============================================================================== | |
echo New Virtual Host has been created | |
echo Host Name: %vhost_name% | |
echo Host IP: %vhost_ip% | |
echo Host Folder: %vhost_folder% | |
echo Host Path: %www_path_unix%/%vhost_folder% | |
echo =============================================================================== | |
pause | |
EXIT | |
) ELSE ( | |
echo =============================================================================== | |
echo ERROR!!! | |
echo VHost name and folder name can't be empty | |
echo =============================================================================== | |
pause | |
EXIT | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Create new Virtual Host | |
#Description: This script will create new virtual host for XAMPP on UBUNTU | |
#Author: Max Kostinevich | |
clear | |
#Config | |
hostsPath="/etc/hosts" | |
xamppPath="/opt/lampp" | |
vhostConfPath="${xamppPath}/etc/extra/httpd-vhosts.conf" | |
# Replace USERNAME with your username and USERGROUP with your usergroup | |
wwwPath="/home/USERNAME/work/www" | |
wwwOwner="USERNAME:USERGROUP" | |
# Display Config | |
echo "CONFIG REVIEW" | |
echo "---------------------------------------------" | |
echo "hostsPath: ${hostsPath}" | |
echo "xamppPath: ${xamppPath}" | |
echo "vhostConfPath: ${vhostConfPath}" | |
echo "wwwPath: ${wwwPath}" | |
echo "wwwOwner: ${wwwOwner}" | |
echo "---------------------------------------------" | |
# Display HOSTS file (last 5 lines) | |
echo "HOSTS RECORDS (last 5 lines)" | |
echo "---------------------------------------------" | |
tail "${hostsPath}" -n 5 | |
echo "---------------------------------------------" | |
# Ask to create new VHOST | |
echo 'This script will create new virtual host' | |
echo '---------------------------------------------' | |
read -p "Enter VHost name (e.g example.local): " vhostName | |
read -p "Enter folder name (e.g example): " vhostFolder | |
read -p "Enter IP (e.g 127.0.0.2): " vhostIP | |
clear | |
#Create website directory | |
echo -e "\e[1mCreate website directory \e[0m" | |
sudo mkdir -p "${wwwPath}/${vhostFolder}" | |
sudo chown -R ${wwwOwner} "${wwwPath}/${vhostFolder}" | |
clear | |
echo -e "\e[1mWebsite directory has been created successfully\e[0m" | |
#Add test INDEX.PHP file | |
echo -e "\e[1mCreate test index.php in website directory \e[0m" | |
sudo chmod 755 ${wwwPath} | |
sudo printf "${vhostName} <br /> <?php echo 'PHP is OK'?>" > "${wwwPath}/${vhostFolder}/index.php" | |
clear | |
echo -e "\e[1mTest index.php has been created successfully\e[0m" | |
#Update httpd-vhosts file for new host | |
echo -e "\e[1mAdd new VHOST to httpd-vhosts.conf\e[0m" | |
sudo cat <<EOT >> "${vhostConfPath}" | |
<VirtualHost ${vhostIP}> | |
ServerAdmin webmaster@localhost | |
DocumentRoot "${wwwPath}/${vhostFolder}" | |
ServerName ${vhostName} | |
ErrorLog "logs/${vhostName}-error_log" | |
CustomLog "logs/${vhostName}_log" common | |
<Directory "${wwwPath}/${vhostFolder}"> | |
AllowOverride All | |
Require all granted | |
</Directory> | |
</VirtualHost> | |
EOT | |
clear | |
echo -e "\e[1mConfig file has been updated successfully\e[0m" | |
#Update HOSTS file | |
sudo echo "${vhostIP} ${vhostName}" >> "${hostsPath}" | |
#Restart Apache | |
echo -e "\e[1mRestart XAMPP\e[0m" | |
sudo ${xamppPath}/lampp restart | |
clear | |
echo -e "\e[1mApache has been restarted successfully\e[0m" | |
#Final output | |
echo '---------------------------------------------' | |
echo -e "\e[1m$vhostName\e[0m has been added successfully" | |
echo -e "\e[1mhttp://${vhostName}/\e[0m" | |
echo '---------------------------------------------' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: Create new Virtual Host for the Vagrant LAMP stack | |
:: https://github.com/maxkostinevich/vagrant-lamp | |
:: This scipt requires GIT | |
:: IMPORTANT!!! Run this script as Administrator | |
:: IMPORTANT!!! Make sure that variable www_path is configured properly | |
@ECHO OFF | |
:: Path to the WWW Directory | |
SET www_path=D:\work\www | |
:: Path to the Windows hosts | |
SET hosts_path=C:\Windows\System32\drivers\etc\hosts | |
:: Check if variables are configured properly | |
SET config_ok=false | |
IF EXIST %hosts_path% IF EXIST %www_path% SET config_ok=true | |
IF NOT "%config_ok%"=="true" ( | |
echo =============================================================================== | |
echo ERROR!!! | |
echo Wrong path for WWW Directory or Windows hosts file | |
echo Please check the following paths: | |
echo www_path="%www_path%" | |
echo hosts_path="%hosts_path%" | |
echo =============================================================================== | |
pause | |
EXIT | |
) | |
echo =============================================================================== | |
echo Latest hosts: | |
echo =============================================================================== | |
:: Get the number of lines in the file | |
set LINES=0 | |
for /f "delims==" %%I in (%hosts_path%) do ( | |
set /a LINES=LINES+1 | |
) | |
:: Print the last 3 lines | |
set /a LINES=LINES-3 | |
more +%LINES% < %hosts_path% | |
echo =============================================================================== | |
echo Create new vhost | |
echo =============================================================================== | |
:: Set Variables | |
SET /P vhost_name=Enter host name e.g. demo.local: | |
SET /P vhost_folder=Enter host folder name: | |
SET /P vhost_ip=Enter host ip e.g. 192.168.10.2: | |
:: Check if variables are OK | |
SET config_ok=false | |
IF NOT "%vhost_name%"=="" IF NOT "%vhost_folder%"=="" IF NOT "%vhost_ip%"=="" SET config_ok=true | |
IF "%config_ok%"=="true" ( | |
:: Create VHost Folder | |
echo "%www_path%\%vhost_folder%" | |
MKDIR %www_path%\%vhost_folder% | |
echo The folder "%vhost_folder%" in "%www_path%" has been created | |
:: Clone GIT repository | |
git clone https://github.com/maxkostinevich/vagrant-lamp.git %www_path%\%vhost_folder% | |
:: Cleanup folder | |
RD /S /Q "%www_path%\%vhost_folder%\.git" | |
DEL "%www_path%\%vhost_folder%\LICENSE" | |
DEL "%www_path%\%vhost_folder%\README.md" | |
:: Update Windows Hosts file | |
echo. >> %hosts_path% | |
echo %vhost_ip% %vhost_name% >> %hosts_path% | |
echo Windows Hosts file has been updated successfully | |
:: Flush DNS | |
ipconfig /flushdns | |
CLS | |
:: Clear the screen and print result | |
CLS | |
echo =============================================================================== | |
echo New Virtual Host has been created | |
echo Host Name: http://%vhost_name% | |
echo Host IP: %vhost_ip% | |
echo Host Folder: %vhost_folder% | |
echo Do not forget to configure Vagrantfile | |
echo =============================================================================== | |
pause | |
EXIT | |
) ELSE ( | |
echo =============================================================================== | |
echo ERROR!!! | |
echo VHost name and folder name can't be empty | |
echo =============================================================================== | |
pause | |
EXIT | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment