roverwire / virtualhost Goto Github PK
View Code? Open in Web Editor NEWBash Script to easy create or delete apache virtual hosts on ubuntu
Bash Script to easy create or delete apache virtual hosts on ubuntu
I'm using Ubuntu Server 18.04 LTS with directories for source existing.
When using trying to set the virtualhost command:
ubuntu:/var/www/customer.staging.testing.io/deployment$ sudo chmod +x virtualhosts.sh
ubuntu:/var/www/customer.staging.testing.io/deployment$ sudo cp /var/www/customer.staging.testing.io/deployment/virtualhosts.sh /usr/local/bin/virtualhost
ubuntu:/var/www/customer.staging.testing.io/deployment$ sudo virtualhost create customer.staging.testing.io
sudo: virtualhost: command not found
ubuntu:/var/www/customer.staging.testing.io/deployment$
When trying to execute directly by the shell script:
ubuntu:/var/www/customer.staging.testing.io/deployment$ sudo sh virtualhosts.sh create customer.staging.testing.io customer.staging.testing.io
update_virtualhosts.sh: 30: [: customer.staging.testing.io: unexpected operator
update_virtualhosts.sh: 36: [: customer.staging.testing.io: unexpected operator
update_virtualhosts.sh: 41: virtualhosts.sh: [[: not found
update_virtualhosts.sh: 47: [: create: unexpected operator
-e $This domain does not exist.
Please try another one
ubuntu:/var/www/customer.staging.testing.io/deployment$ sudo sh virtualhosts.sh create customer.staging.testing.io /var/www/customer.staging.testing.io/public
update_virtualhosts.sh: 30: [: customer.staging.testing.io: unexpected operator
update_virtualhosts.sh: 36: [: /var/www/customer.staging.testing.io/public: unexpected operator
update_virtualhosts.sh: 41: virtualhosts.sh: [[: not found
update_virtualhosts.sh: 47: [: create: unexpected operator
-e $This domain does not exist.
Please try another one
Not sure what I'm missing here.
When I try and create a new virtualhost with this command: sudo virtualhost create dan.test dan
I get the following errors:
/usr/local/bin/virtualhost: 31: [: dan.test: unexpected operator
/usr/local/bin/virtualhost: 37: [: dan: unexpected operator
/usr/local/bin/virtualhost: 42: /usr/local/bin/virtualhost: [[: not found
/usr/local/bin/virtualhost: 48: [: create: unexpected operator
-e $This domain does not exist.
Please try another one
The virtualhost is not created. I have tried this a few times with a few different commands and it still doesn't work.
If i want to create a VH like this, no problem at all.
user@ubuntu:/usr/local/bin$ sudo vhost create test
Added content to /var/www/test/phpinfo.php
New Virtual Host Created
Host added to /etc/hosts file
Enabling site test.
To activate the new configuration, you need to run:
service apache2 reload
[ ok ] Reloading apache2 configuration (via systemctl): apache2.service.
Complete!
You now have a new Virtual Host
Your new host is: http://test
And its located at /var/www/test
user@ubuntu:/usr/local/bin$
But if i only say "create" without hostname i get this issue:
user@ubuntu:/usr/local/bin$ sudo vhost create
Please provide domain. e.g.dev,staging
test2
/etc/apache2/sites-available/.conf
This domain already exists.
Please Try Another one
user@ubuntu:/usr/local/bin$
The script generates a .conf file without domainname
Hi Luis,
I have tried your codes and took some errors on which is used "==" lines
for example
26: [: blabla.dev: unexpected operator
/home/nexima/virtualhost/virtualhost.sh: 32: [: blabla: unexpected operator
/home/nexima/virtualhost/virtualhost.sh: 36: [: create: unexpected operator
-e This domain dont exists.
Please Try Another one
i have changed "==" to "=" over if statements but still i am taking some different error
newhost=${domain//./.}
I have just started to learn shell could you help me?
Thanks
Hi,
i was run this bash file from crontab but its not working in this line
a2ensite $domain
when i run the shell script by root from terminal its working
Cheers
Thank you for this script, you helped me and i am sure of, a lot of others out.
But, i think it is a good idea to add this:
After the script has created a new virtual host and its folders like this:
sudo ./virtualhost.sh create example.com
Succes!
Directory made in /var/www/examplecom
That after all of this, it asks you if you want to add a user to this folder like this:
Do you want to make an user for this domain? y/n
y
sudo adduser $2
sudo usermod -d $3/$2 $2
sudo chroot the webfolder (/var/www/examplecom) and give the right permssions to the corresponding user (examplecom)
USERMOD COMMAND: sudo usermod -d new_home_dir username
Something like this ^ I have no idea to make this myself but i think it would be a great addition to your script!
EDIT:
The main reason i want to use it for is for FTP users, so that when i have an chroot FTP server running, the directory of the user would then be /var/www/examplecom. This is very usefull for people that are using wordpress or just to add and remove files from their part of the server (their web folder)
Well, recently i installed your bash script in order to create nginx VH. What works for me is this edited script
https://gist.github.com/endrureza/df7c0971e429be5a3c68ac9821f463d0
but whenever i created freshly from your script it doesn't work. let's say nginx return 502 bad gateway, below the not working code
https://gist.github.com/endrureza/dd206e48112daa0befa8667188981d0f
what did i miss? is that related to new configuration of nginx somehow?
I installed and run exactly but those errors show up ...
/usr/local/bin/virtualhost: line 89: /etc/apache2/sites-available/phyo.test.conf: No such file or directory There is an ERROR creating phyo.test file
How can I solve it ? Please Help me ..
Hi, I had created a virtual host using the script but now not able to find out where the files are located. Is there a way to find out please? Many thanks,
i have error report like this :
# sudo virtualhost create mysite.dev sudo: virtualhost: command not found # virtualhost create mysite.dev Added content to /var/www/mysitedev/phpinfo.php /usr/local/bin/virtualhost: line 81: /etc/apache2/sites-available/mysite.dev.conf: No such file or directory There is an ERROR creating mysite.dev file
I get the following error when using the scripts in 14.04:
eric@ubuntu:~/virtualhost$ sudo sh virtualhost.sh create anothersite.dev my_dir
virtualhost.sh: 26: [: anothersite.dev: unexpected operator
virtualhost.sh: 32: [: my_dir: unexpected operator
virtualhost.sh: 36: [: create: unexpected operator
-e This domain dont exists.
Please Try Another one
I know 14.04 has not been officially released yet, but why would it not work on there when this is just an sh script.
Thanks for this script. Very useful.
It fails if the parent directory of $rootdir
does not exist. For example:
sudo virtualhost.sh create laravel_domain laravel_domain/public
will fail because directory laravel_domain
does not exist. It is common for some frameworks like Laravel to point the virtualhosts to a subdirectory.
To fix, change the mkdir command from mkdir
to mkdir -p
.
Running Ubuntu 18.04 in WSL2.
sudo virtualhost create test.test
returns the following error:
line 109: /mnt/c/Windows/System32/drivers/etc/hosts: Permission denied ERROR: Not able to write in /mnt/c/Windows/System32/drivers/etc/hosts (Hint: Try running Bash as administrator)
Hi
Just feature request I am using Ubuntu 16 and it has following structure
/var/www/html and the script create folder in /var/www ie not inside the www folder. I need to create public_html folder inside the Virtual host. I give following command.
sudo virtualhost create dev.local /var/www/html/dev.local/public_html prashant www-data
mkdir: cannot create directory ‘/var/www/html/dev.local/public_html’: No such file or directory
chmod: cannot access '/var/www/html/dev.local/public_html': No such file or directory
/usr/local/bin/virtualhost: line 61: /var/www/html/dev.local/public_html/phpinfo.php: No such file or directory
Somewhat related to #49, it'd be awesome if we could add support for SSL virtualhosts. Now that Service Workers are so big in web development, it's key to be able to test with SSL.
This would work by setting an --ssl_cert=/path/to/certificate.crt
and/or --ssl_key=/path/to/key.key
flag that would add an additional virtualhost to the file. Setting one of these parameters and not the other would default the other to the same path, swapping the file extension (i.e. if only -ssl_cert=/path/to/certificate.crt
was passed, ssl_key
would be assumed to be /path/to/certificate.key
).
Ideally this script could also generate the keys themselves, but I think that may be a bit out of scope.
I suggest adding a command that modifies /etc/hosts
to include the newly created domain as a fake host.
virtualhost.sh hosts laravel_domain 127.0.0.1
That would add the following line to /etc/hosts:
127.0.0.1 laravel_domain
Great work. Thanks.
Good evening to all. Please help me, I have encountered the same error, I have cloned your project, followed your instructions but I encounter this error.
My operating system ubuntu 20.04
houngbemey@houngbemey-HP-ProBook-450-G6:~$ sudo sh /home/houngbemey/virtualhost/virtualhost.sh create smartenterprise.site
/home/houngbemey/virtualhost/virtualhost.sh: 30: [: smartenterprise.site: unexpected operator
/home/houngbemey/virtualhost/virtualhost.sh: 36: [: unexpected operator
/home/houngbemey/virtualhost/virtualhost.sh: 41: [[: not found
/home/houngbemey/virtualhost/virtualhost.sh: 47: [: create: unexpected operator
-e $This domain does not exist.
Please try another one
How to change default dir to something else ?
Any interest in adding support for automating the adding of ssl to domains when they get added. Along with adding the auto updating the ssl cert with graceful reloading of the server?
is the variable $sitesAvailabledomain not complete at this point?
I had to change it to $sitesAvailable$domain.conf - so it works for me
https://github.com/RoverWire/virtualhost/blob/master/virtualhost.sh#L81
There is a mistake on line 40 of nginx script.
firstly it concatenates userDir and rootDir, and after that it does it again in next blocks of code.
as result it tries to create directory like "/var/www//var/www/domain.com" which is not right.
simply remove "rootDir=$userDir$rootDir" on line 40 and you are good.
Hi
it's have root right, instead of www-data for directory.
regards
Hi
I have this errors when I runnnig yours script - nginx version
sudo virtualhost create test.lo
/usr/local/bin/virtualhost: 20: [: test.lo: unexpected operator
/usr/local/bin/virtualhost: 26: [: unexpected operator
/usr/local/bin/virtualhost: 30: [: create: unexpected operator
-e This domain dont exists.
Please Try Another one
I suggest add to beginning of file
because in mint 17 the default shell is /bin/sh
problem: http://stackoverflow.com/questions/3411048/unexpected-operator-in-shell-programming
Is there an option to add ServerAlias into the new vhost?
For example:
$ sudo sh /path/to/virtualhost.sh [create | delete] [domain] [domain_alias1] [domain_alias1] [optional host_dir]
I think in order to make this script more flexible for future features, it'd be good to change the way this takes in settings so that it uses flags instead of a specific order of parameters.
For example, in order to implement #25, it'd be great to do something like virtualhost create localhost --user=www-data:www-data
.
Other possible options could be:
--alias=www.example.localhost
--ssl_cert=/path/to/certificate.crt
--ssl_key=/path/to/key.key
--skip-test-file
--skip-hosts
By default the owner and group are both set to root. Can you please add an option to set the owner and group?
Something like sudo virtualhost [create] [domain] [optional host_dir] [owner] [group]
I think would be great.
Thank you!
Is there a way to get a list of all virtual hosts? i.e, virtualhost list
Hi, just trying to use your script to save some time creating virtualhost on a mac.
I found that the line 40 is overwriting the rootDir variable passed as 3rd parameter and getting error when it tries to create the folder.
running: sudo virtualhost create cms.dev cms
got: mkdir: /Users/eze/Projects/Web//Users/eze/Projects/Web: No such file or directory
If I commented out that line all works, except that the nginx group on a mac is not www-data
, it works with staff
so I got chown: www-data: illegal group name
. and that the domain is without the .
(cms.dev -> cmsdev)
Maybe a top configurable variable like serverGroup or so will make it more flexible.
Thanks!
Hello,
Added the same check to add the new domain to windows host file , running on the windows bash with a small caveat. The window bash needs to be run as Administrator for it to have write access to the file.
if ! echo "127.0.0.1 $domain" >> /mnt/c/Windows/System32/drivers/etc/hosts
then
echo $"ERROR: Not able to write in windows hosts file"
exit;
else
echo -e $"Host added to windows hosts file \n"
fi
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.