├── .gitattributes ├── LICENSE.md ├── README.md ├── install.sh └── vhost-creator.sh /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2010-2016 Matteo Merola http://matteomerola.me 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | vhost-creator 2 | ============ 3 | 4 | This is a useful bash script that will let you save time creating vhosts for you. 5 | 6 | It supports both ubuntu and centos (be careful to write `ubuntu` or `centos` in lowercase). 7 | 8 | Instructions: 9 | - `mkdir ~/scripts && cd $_` 10 | - `wget https://raw.githubusercontent.com/mattmezza/vhost-creator/master/vhost-creator.sh` 11 | - `chmod +x vhost-creator.sh` 12 | - `sudo ./vhost-creator.sh` 13 | - follow Instructions 14 | 15 | or for the most adventurous just run `curl -s https://raw.githubusercontent.com/mattmezza/vhost-creator/master/install.sh | sh` to install it globally for your user (you can use the script afterwards with `sudo vhost-creator`) 16 | 17 | Don't forget to add a A record from your DNS pointing to your web server IP address. 18 | 19 | You can also mv the script to a system wide bin folder or add it to your bash profile. 20 | 21 | The original idea was this [one](https://gist.github.com/mattmezza/2e326ba2f1352a4b42b8) 22 | 23 | 24 | ###### Thanks to: 25 | - `alexnogard` for his original version 26 | - [Fred Bradley](https://github.com/fredbradley) for [this](https://gist.github.com/fredbradley/296bce8eba544647f10f) gist. 27 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OLD=`pwd` 4 | INSTALL_DIR=/usr/local/bin 5 | cd $INSTALL_DIR 6 | wget https://raw.githubusercontent.com/mattmezza/vhost-creator/master/vhost-creator.sh 7 | mv vhost-creator.sh vhost-creator 8 | chmod +x vhost-creator 9 | echo "Installed successfully in $INSTALL_DIR" 10 | cd $OLD -------------------------------------------------------------------------------- /vhost-creator.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This script is used for create virtual hosts on CentOs. 3 | # Created by alexnogard from http://alexnogard.com 4 | # Improved by mattmezza from http://matteomerola.me 5 | # Feel free to modify it 6 | # PARAMETERS 7 | # 8 | # $usr - User 9 | # $dir - directory of web files 10 | # $servn - webserver address without www. 11 | # $cname - cname of webserver 12 | # EXAMPLE 13 | # Web directory = /var/www/ 14 | # ServerName = domain.com 15 | # cname = devel 16 | # 17 | # 18 | # Check if you execute the script as root user 19 | # 20 | # This will check if directory already exist then create it with path : /directory/you/choose/domain.com 21 | # Set the ownership, permissions and create a test index.php file 22 | # Create a vhost file domain in your /etc/httpd/conf.d/ directory. 23 | # And add the new vhost to the hosts. 24 | # 25 | # 26 | if [ "$(whoami)" != 'root' ]; then 27 | echo "Dude, you should execute this script as root user..." 28 | exit 1; 29 | fi 30 | echo "First of all, is this server an Ubuntu or is it a CentOS?" 31 | read -p "ubuntu or centos (lowercase, please) : " osname 32 | 33 | SERVICE_="apache2" 34 | VHOST_PATH="/etc/apache2/sites-available" 35 | CFG_TEST="apachectl -t" 36 | if [ "$osname" == "centos" ]; then 37 | SERVICE_="httpd" 38 | VHOST_PATH="/etc/httpd/conf.d" 39 | CFG_TEST="service httpd configtest" 40 | elif [ "$osname" != "ubuntu" ]; then 41 | echo "Sorry mate but I only support ubuntu or centos" 42 | echo " " 43 | echo "By the way, are you sure you have entered 'centos' or 'ubuntu' all lowercase???" 44 | exit 1; 45 | fi 46 | 47 | echo "Enter the server name you want" 48 | read -p "e.g. mydomain.tld (without www) : " servn 49 | echo "Enter a CNAME" 50 | read -p "e.g. www or dev for dev.website.com : " cname 51 | echo "Enter the path of directory you wanna use" 52 | read -p "e.g. /var/www/, dont forget the / : " dir 53 | echo "Enter the name of the document root folder" 54 | read -p "e.g. htdocs : " docroot 55 | echo "Enter the user you wanna use" 56 | read -p "e.g. apache/www-data : " usr 57 | echo "Enter the listened IP for the web server" 58 | read -p "e.g. * : " listen 59 | echo "Enter the port on which the web server should respond" 60 | read -p "e.g. 80 : " port 61 | 62 | if ! mkdir -p $dir$cname_$servn/$docroot; then 63 | echo "Web directory already Exist !" 64 | else 65 | echo "Web directory created with success !" 66 | fi 67 | echo "

$cname $servn

" > $dir$cname_$servn/$docroot/index.html 68 | chown -R $usr:$usr $dir$cname_$servn/$docroot 69 | chmod -R '775' $dir$cname_$servn/$docroot 70 | mkdir /var/log/$cname_$servn 71 | 72 | alias=$cname.$servn 73 | if [[ "${cname}" == "" ]]; then 74 | alias=$servn 75 | fi 76 | 77 | echo "#### $cname $servn 78 | 79 | ServerName $servn 80 | ServerAlias $alias 81 | DocumentRoot $dir$cname_$servn/$docroot 82 | 83 | Options Indexes FollowSymLinks MultiViews 84 | AllowOverride All 85 | Order allow,deny 86 | Allow from all 87 | Require all granted 88 | 89 | " > $VHOST_PATH/$cname_$servn.conf 90 | if ! echo -e $VHOST_PATH/$cname_$servn.conf; then 91 | echo "Virtual host wasn't created !" 92 | else 93 | echo "Virtual host created !" 94 | fi 95 | echo "Would you like me to create ssl virtual host [y/n]? " 96 | read q 97 | if [[ "${q}" == "yes" ]] || [[ "${q}" == "y" ]]; then 98 | openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout $VHOST_PATH/$cname_$servn.key -out $VHOST_PATH/$cname_$servn.crt 99 | if ! echo -e $VHOST_PATH/$cname_$servn.key; then 100 | echo "Certificate key wasn't created !" 101 | else 102 | echo "Certificate key created !" 103 | fi 104 | if ! echo -e $VHOST_PATH/$cname_$servn.crt; then 105 | echo "Certificate wasn't created !" 106 | else 107 | echo "Certificate created !" 108 | if [ "$osname" == "ubuntu" ]; then 109 | echo "Enabling Virtual host..." 110 | sudo a2ensite $cname_$servn.conf 111 | fi 112 | fi 113 | 114 | echo "#### ssl $cname $servn 115 | 116 | SSLEngine on 117 | SSLCertificateFile $VHOST_PATH/$cname_$servn.crt 118 | SSLCertificateKeyFile $VHOST_PATH/$cname_$servn.key 119 | ServerName $servn 120 | ServerAlias $alias 121 | DocumentRoot $dir$cname_$servn/$docroot 122 | 123 | Options Indexes FollowSymLinks MultiViews 124 | AllowOverride All 125 | Order allow,deny 126 | Allow from all 127 | Satisfy Any 128 | 129 | " > $VHOST_PATH/ssl.$cname_$servn.conf 130 | if ! echo -e $VHOST_PATH/ssl.$cname_$servn.conf; then 131 | echo "SSL Virtual host wasn't created !" 132 | else 133 | echo "SSL Virtual host created !" 134 | if [ "$osname" == "ubuntu" ]; then 135 | echo "Enabling SSL Virtual host..." 136 | sudo a2ensite ssl.$cname_$servn.conf 137 | fi 138 | fi 139 | fi 140 | 141 | echo "127.0.0.1 $servn" >> /etc/hosts 142 | if [ "$alias" != "$servn" ]; then 143 | echo "127.0.0.1 $alias" >> /etc/hosts 144 | fi 145 | echo "Testing configuration" 146 | sudo $CFG_TEST 147 | echo "Would you like me to restart the server [y/n]? " 148 | read q 149 | if [[ "${q}" == "yes" ]] || [[ "${q}" == "y" ]]; then 150 | service $SERVICE_ restart 151 | fi 152 | echo "======================================" 153 | echo "All works done! You should be able to see your website at http://$servn" 154 | echo "" 155 | echo "Share the love! <3" 156 | echo "======================================" 157 | echo "" 158 | echo "Wanna contribute to improve this script? Found a bug? https://github.com/mattmezza/vhost-creator" 159 | --------------------------------------------------------------------------------