virtualhost / virtualhost.sh Goto Github PK
View Code? Open in Web Editor NEWA script for Mac OS X to create virtual hosts under Apache
Home Page: http://patrickgibson.com/utilities/virtualhost/
A script for Mac OS X to create virtual hosts under Apache
Home Page: http://patrickgibson.com/utilities/virtualhost/
When tab completing a directory name it automatically adds a trailing slash to the name, e.g. . virtualhost.sh mysite.kerns.co/
...which completely screws up everything if you forget to remove it.
So there must be a way to strip out the last slash, which isn't really valid or useful anyway I suppose, but which causes damage when accidentally included.
It would be amazing to have a --list parameter, listing all virtualhosts added by the script.
Congrats about the project!
In my computer safari launches a little bit before apache restarts, so the page isn't displayed until refresh.
maybe if we have something like
sleep 0.5
before invoking Safari it would fix it
Bash session when document root doesn't exist
[cweagans@Camerons-MacBook-Pro ~/Sites] $ ls testhost.dev
ls: testhost.dev: No such file or directory
[cweagans@Camerons-MacBook-Pro ~/Sites] $ virtualhost testhost.dev
virtualhost.sh requires super-user privileges to work.
Enter your password to continue...
Checking for updates... none found
Create http://testhost.dev:80/? [Y/n]: Y
Creating a virtualhost for testhost.dev...
+ Adding testhost.dev to /etc/hosts... done
+ Checking for an existing document root to use...
- Use /Users/cweagans/Sites/testhost.dev as the virtualhost folder? [Y/n] Y
+ Creating folder /Users/cweagans/Sites/testhost.dev... done
+ Creating virtualhost file... done
+ Flushing cache... done
+ Restarting Apache... done
http://testhost.dev:80/ is setup and ready for use.
[cweagans@Camerons-MacBook-Pro ~/Sites] $ cat /etc/apache2/virtualhosts/testhost.dev
# Created Sat Oct 13 23:48:02 MDT 2012
<VirtualHost *:80>
DocumentRoot "/Users/cweagans/Sites/testhost.dev"
ServerName testhost.dev
#ServerAlias your.alias.here
ScriptAlias /cgi-bin "/Users/cweagans/Sites/testhost.dev/cgi-bin"
<Directory "/Users/cweagans/Sites/testhost.dev">
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
CustomLog "/Users/cweagans/logs/access_log-testhost.dev" combined
ErrorLog "/Users/cweagans/logs/error_log-testhost.dev"
</VirtualHost>
This is when the docroot already exists:
[cweagans@Camerons-MacBook-Pro ~/Sites] $ ls testhost.dev/
index.html
[cweagans@Camerons-MacBook-Pro ~/Sites] $ virtualhost testhost.dev
virtualhost.sh requires super-user privileges to work.
Enter your password to continue...
Checking for updates... none found
Create http://testhost.dev:80/? [Y/n]: Y
Creating a virtualhost for testhost.dev...
+ Adding testhost.dev to /etc/hosts... done
+ Checking for an existing document root to use...
Y
+ Creating virtualhost file... done
+ Flushing cache... done
+ Restarting Apache... done
http://testhost.dev:80/ is setup and ready for use.
[cweagans@Camerons-MacBook-Pro ~/Sites] $ cat /etc/apache2/virtualhosts/testhost.dev
# Created Sat Oct 13 23:48:47 MDT 2012
<VirtualHost *:80>
DocumentRoot "testhost.dev"
ServerName testhost.dev
#ServerAlias your.alias.here
ScriptAlias /cgi-bin "testhost.dev/cgi-bin"
<Directory "testhost.dev">
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
CustomLog "/Users/cweagans/logs/access_log-testhost.dev" combined
ErrorLog "/Users/cweagans/logs/error_log-testhost.dev"
</VirtualHost>
The apache config should be identical in both cases (the first is the correct behavior)
I ran this under Lion and everything worked great with one exception. The line in the host file was appended to the last item in the hosts file rather than on a new line.
The looping issue for virtualhost creation has been fixed, but the error still exists for --list and --delete. Error is "/path/to/virtualhost.sh: line 130: [: =: unary operator expected"
Create http://blue.dev.*.xip.io:80/? [Y/n]: y
+ Looking in /Users/dvk/Sites for an existing document root to use...
searching to a maximum directory depth of 5. This could take some time... - Use /Users/dvk/Sites/projects/blue.dev as the virtualhost folder? [Y/n] y
- Found a public folder suggesting a Rails/Rack project. Use as DocumentRoot? [Y/n]
Missing a line break to get the - Use...
item on it's own line.
Sometimes when I try to add a virtualhost to an existent folder, it hangs up trying to find the folder
I have to move the folder away, create it again and delete it again
and when it prompts me to choose a folder name inside ~/Sites, it ends up making the folder but pointing the addon domain to the incorrect place, ex:
<VirtualHost *:80>
DocumentRoot "matrizdesenho"
ServerName matrizdesenho
...
Since 1.28 the script hangs at "Checking for an existing document root to use..."
Previously I was using 1.24
Instead of checking against /etc/hosts for deletes, you should check to see if the file exists in /etc/apache2/virtualhosts/
This project has been dormant for some time now but still viable. I have come onboard as a maintainer and will be updating the script to support Ventura and later Macos.
This will best be accomplished by incrementing the major version to 2.x. I will create a separate branch for 1.x code base.
Roadmap:
Any suggestions, comments or pull requests are appreciated.
Hello
First, thank you very much. This script is very useful! Keep up the good work. :D
I have a suggestion to give a possibility to the users to select the projects folder.
If I execute
/Users/pablo/workspace:~ virtualhost.sh mysite.local
The script will create a folder in
/Users/pablo/Sites/mysite.local
My suggestion
If I execute
/Users/pablo/workspace:~ virtualhost.sh mysite.local
The script will create a folder inside my current directory (/Users/pablo/workspace) if the folder doesn't exist and then points the Document Root to
DocumentRoot "/Users/pablo/workspace/mysite.local"
Cheers,
Pablo Cantero
Two things:
Just an idea at this point. The talented folks at https://github.com/freshshell/fresh/ are in the process of switching from shunit2 to RSpec so that may be an avenue to explore.
I updated to the public beta a couple of weeks? ago and I just noticed that my VirtualHosts are not working.
Trying to access them in any browser yields an error,
Accessing via wget
yields a ERROR 403: Forbidden
ย
Looking a bit more into it, Yosemite upgraded Apache to
Server version: Apache/2.4.9 (Unix)
Server built: Jun 28 2014 12:11:10
Comparing against the VirtualHosts on my VPS's I found the issue.
virtualhost.sh is using
Order allow,deny
Allow from all
On newer versions (at least what I'm doing on my VPS), I use
Require all granted
Here is a test virtualhost that I used to test this:
cat virtualhosts/test.local
# Created Mon Aug 4 21:51:24 PDT 2014
<VirtualHost *:80>
DocumentRoot "/Users/ruben/Sites/test.local"
ServerName test.local
#ServerAlias your.alias.here
ScriptAlias /cgi-bin "/Users/ruben/Sites/test.local/cgi-bin"
DocumentRoot "/Users/ruben/Sites/test.local"
<Directory "/Users/ruben/Sites/test.local">
Options All
AllowOverride All
# Order allow,deny
# Allow from all
Require all granted
</Directory>
CustomLog "/Users/ruben/Sites/test.local/logs/access_log" combined
ErrorLog "/Users/ruben/Sites/test.local/logs/error_log"
</VirtualHost>
This works.
I have less than 10 virtualhosts so changing this manually is quite easy. Just a heads up to anyone else that hits this issue.
This issue is to discuss/track progress on an automated upgrade path for Apache 2.4.x and OS X Yosemite. Original discussion is here: #55 (comment)
Otherwise access log might not work. Needs more testing
hi and thank you for a super useful utility!
I was forced to switch to brewed apache24, as the brewed php 5.6.30 no longer supports the build-in apache
my virtualhosts in /private/etc/apache2/virtualhosts are not being honored as
brewed apache24 reads the configuration from /usr/local/etc/apache2/2.4
I've added a line of
Include /private/etc/apache2/virtualhosts
to /usr/local/etc/apache2/2.4/httpd.conf
've and restarted apache using sudo apachectl restart
but I'm not sure whether this is the preferred solution,
a detection of brewed 2.4 would probably simplify life to a lot of osx folks :)
Did you remove the ability to create individual access and error logs (i.e. ALWAYS_CREATE_LOGS="yes") , or is it just the default position now? I really enjoy a /logs folder on the root of each project.
I've been wanting this for a while, so here's an issue for it. Opens the passed in host in $EDITOR
.
@pgib Sorry, I've tried everything I can think of on a number of machines now, and it really feels like this feature is broken in the most recent version of your wonderful script.
Note that vhost-stage
is just my alias for your script,...It is running in batch mode, but still...it seems to completely ignore the existence of web or public subfolders. And if I disable batch mode it just ignores or fails to ask about web or public altogether.
$ vhost-stage what.kerns.co
Create http://what.kerns.co:80/? [Y/n]: Y
+ Looking in /Users/dvk/Sites for an existing document root to use...
- Use /Users/dvk/Sites/what.kerns.co as the virtualhost folder? [Y/n] Y
- Found a public folder suggesting a Rails/Merb/Rack project. Use as DocumentRoot? [y/N] + Creating virtualhost file... done
+ Restarting Apache... done
I rely on this feature as I typically organize project materials like /docs and /logs and other things I don't want part of the public-facing web part of the project at the root level of the folder.
Would you mind just testing this in your own environment at some point?
The access and error logs for subdirectories of ~/Sites are being incorrectly written to the ~/Sites root.
Example: Adding a new virtual host for a site called "kerns.dev" which is located at "~/Sites/projects/kerns.dev" will add and maintain logs in "~Sites/Users/dvk/Sites/projects/kerns.dev"
Partial output from apachectl -t
:
AH00548: NameVirtualHost has no effect and will be removed in the next release
Ref. https://httpd.apache.org/docs/2.4/mod/core.html#namevirtualhost
So for backwards compatibility it's still needed, but we don't need it for OS X 10.10 and up.
@pgib Any thought ever to making a 3rd argument $3 that accepts an absolute path to the doc root? Not sure but it seems useful to be able to set that...or I can say at least, I've personally run into situations where it would be useful. Just a thought.
Might be because I also use this script on a Debian VPS, and not just on my Mac, that this strikes me as useful (I stage sites in /home/dvk/Sites/, and host live in /var/www). As it is now, I need to maintain two versions of the same script to control the location of the document root.
Is it possible to add an option to share virtualhosts over a local network?
I'd like for colleagues to be able to access my virtualhosts as so:
http://matt.dev/project/
Thanks
First of all, I don't think I've said this yet but thank you @pgib for creating (and open sourcing) virtualhost.sh!
I've been making quite a lot of use of it recently and have a few ideas (and many more I haven't yet created issues for): https://github.com/pgib/virtualhost.sh/pulls/Cottser
Would you consider moving the repo to github.com/virtualhost/virtualhost.sh (or similar) and adding more collaborators/maintainers? I think in the long term that could help the health of the project and I've seen a number of projects I've followed and helped with go along this same path (notice these redirect to an organization repo):
https://github.com/phinze/homebrew-cask
http://github.com/josegonzalez/homebrew-php
got virtualhost.sh working via homebrew for developing my websites with.
I was able to create virtualhosts absolutley fine and had multiple sites running magento and wordpress.
Unfortunately i created a new virtualhost the other day and when i went to the web address it shows the error cannot connect to server.
The only thing i done differently this time was extend the root directory to point to a subfolder. I have done this on other setups many times with no issues
When i then tried to visit my other virtualhost websites they were all showing the same error message.
I have all my files on bitbucket but i really don't want to have to reinstall osx and all the programs i use again.
Any ideas what could help me out?
Using Mavericks, preinstalled php and apache
Needs more info, but I get ./virtualhost.sh: 418: [: missing ]
when running on Ubuntu Server 12.04.
I think we should be able to generate VirtualHosts that are compatible with either version like so:
<IfModule mod_authz_core.c>
Require all granted
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
Allow from all
</IfModule>
The rough idea would be to abstract and group options like SKIP_VERSION_CHECK and SKIP_BROWSER into broader pre and post operation actions. Users could enable these and write their own...or maybe just add a path to an external pre or post script for routines that should happen before and after the creation of a virtual host.
I'm getting the "Symbolic link not allowed or link target not accessible:" error message when I try to drop a sym link to a different directory on my system. Copying the files into the Sites hierarchy doesn't really make a whole lot of sense for development. Any ideas?
Hi
i have enable WILDCARD_ZONE in config file, it is showing Create http://virtualhost.dev:80/? [Y/n]: but is not adding to domain you can access domain http://virtualhost not like virtualhost.dev
It would be nice to have a parameter that skips confirmations [Y/n] and executes the entire script with default options, so it could be called inside other scripts too
It would be nice to have an optional parameter to keep combined logs in apache installation.
this is useful on OS X because sometimes erasing a log file cause "thottling respawn" issues.
Thanks for the --list parameter, loved it!
What do you think about adding support for xip.io ...either by default or as an option?
I'm getting the notice that "A newer version (1.255) of virtualhost.sh is available." even though i have just downloaded the latest version from github.
Any ideas?
log: + Creating virtualhost file... /usr/bin/virtualhost.sh: line 258: /private/etc/apache2/virtualhosts/bmwclubmoto.dev: No such file or directory
f3c4a55 doesn't work as expected, it actually dies even if creating the document root is successful. I'm going to revert it for now, so this issue is to track adding this back in a way that works better.
The script assumes that the Sites folder exists, which is no longer a safe assumption in Lion; in 10.7, it only is there if the user turns on the "web sharing" option in the Sharing section of the System Preferences. I didn't check to see whether there were any other lion incompatibilities.
I would like a recursive Search of subfolders in ~/Sites, for matching new virtual hosts with existing subfolders.
Like a lot of developers I think, I keep the very broad category of Sites divided into subgroups... like "clients", and "projects" for example.
So when I create a new virtual host, for example "kerns.dev",... if a folder named "kerns.dev" exists under ~/Sites, can't the script suggest that as the logical default? It would speed things up a great deal.
Get this when running on Ubuntu Server 12.04 headless, due to X not being available. Need to add checks to script
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.