Coder Social home page Coder Social logo

virtualhost's Introduction

Virtualhost Manage Script

Bash Script to allow create or delete apache/nginx virtual hosts on Ubuntu on a quick way.

Installation

  1. Download the script
  2. Apply permission to execute:
$ chmod +x /path/to/virtualhost.sh
  1. Optional: if you want to use the script globally, then you need to copy the file to your /usr/local/bin directory, is better if you copy it without the .sh extension:
$ sudo cp /path/to/virtualhost.sh /usr/local/bin/virtualhost

For Global Shortcut

$ cd /usr/local/bin
$ wget -O virtualhost https://raw.githubusercontent.com/RoverWire/virtualhost/master/virtualhost.sh
$ chmod +x virtualhost
$ wget -O virtualhost-nginx https://raw.githubusercontent.com/RoverWire/virtualhost/master/virtualhost-nginx.sh
$ chmod +x virtualhost-nginx

Usage

Basic command line syntax:

$ sudo sh /path/to/virtualhost.sh [create | delete] [domain] [optional host_dir]

With script installed on /usr/local/bin

$ sudo virtualhost [create | delete] [domain] [optional host_dir]

Examples

to create a new virtual host:

$ sudo virtualhost create mysite.dev

to create a new virtual host with custom directory name:

$ sudo virtualhost create anothersite.dev my_dir

to delete a virtual host

$ sudo virtualhost delete mysite.dev

to delete a virtual host with custom directory name:

$ sudo virtualhost delete anothersite.dev my_dir

Localization

For Apache:

$ sudo cp /path/to/locale/<language>/virtualhost.mo /usr/share/locale/<language>/LC_MESSAGES/

For NGINX:

$ sudo cp /path/to/locale/<language>/virtualhost-nginx.mo /usr/share/locale/<language>/LC_MESSAGES/

virtualhost's People

Contributors

abedham avatar antleblanc avatar cpswsg avatar guille21 avatar hwaien avatar igormukhingmailcom avatar jacobdb avatar jonscottclark avatar manseka avatar mavisland avatar nzesalem avatar rafamalaga86 avatar roverwire avatar vinicius73 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

virtualhost's Issues

Add option for SSL-enabled virtualhosts

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.

Error when creating new virtualhost

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.

Change to a flag system rather than order of parameters so that more features can be added

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

Add IP Address to Virtual Server Details

Is it possible to add IP Address in Virtual Server Details? Currently I have to manually add in the IP Address to make sure the virtualhost working.

virtualhost

** I'm using Webmin to add the IP Address

Error when run from crontab

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

Adding the host to windows host file

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 

Both Virtualhost scripts do not work in 14.04

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.

I took some errors

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

There is an Error creating mysite.dev file

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

Feature request domain directory structure domain/public_html

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

it gives the error

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

ERROR: Not able to write in file /var/www/html/dev.local/public_html/phpinfo.php. Please check permissions.

Lets Encrypt

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?

"Please check permissions" in NGINX version

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.

Option to set set the owner and group

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!

Virtual host creation problem under ubuntu

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

Feature request: add entry to /etc/hosts

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.

empty var $sitesAvailabledomain

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

[: ... unexpected operator

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

!/bin/bash

because in mint 17 the default shell is /bin/sh

problem: http://stackoverflow.com/questions/3411048/unexpected-operator-in-shell-programming

chmod directory

Hi
it's have root right, instead of www-data for directory.

regards

Fails to create subdirectory

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.

There is an ERROR creating phyo.test file

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 ..

Unexpected operator & Command not found

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.

Unable to add ServerAlias

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]

Variable overwrite $userDir$rootDir

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!

Add user account

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)

Writing to hosts file fails on WSL

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.