-
-
Save fideloper/2710970 to your computer and use it in GitHub Desktop.
| #! /usr/bin/python | |
| from sys import argv | |
| from os.path import exists | |
| from os import makedirs | |
| from os import symlink | |
| from os import system | |
| import getopt | |
| # | |
| # Show Usage, Output to STDERR | |
| # | |
| def show_usage(): | |
| print """ | |
| Create a new vHost in Ubuntu Server | |
| Assumes /etc/apache2/sites-available and /etc/apache2/sites-enabled setup used | |
| -d DocumentRoot - i.e. /var/www/yoursite | |
| -h Help - Show this menu. | |
| -s ServerName - i.e. example.com or sub.example.com | |
| """ | |
| exit(1) | |
| # | |
| # Output vHost skeleton, fill with userinput | |
| # To be outputted into new file | |
| # | |
| def create_vhost(documentroot, servername): | |
| out = """<VirtualHost *:80> | |
| ServerAdmin webmaster@localhost | |
| ServerName %s | |
| DocumentRoot %s | |
| <Directory %s> | |
| Options -Indexes +FollowSymLinks +MultiViews | |
| AllowOverride All | |
| Order allow,deny | |
| Allow from all | |
| Require all granted | |
| </Directory> | |
| ErrorLog ${APACHE_LOG_DIR}/%s-error.log | |
| # Possible values include: debug, info, notice, warn, error, crit, | |
| # alert, emerg. | |
| LogLevel warn | |
| CustomLog ${APACHE_LOG_DIR}/%s-access.log combined | |
| </VirtualHost>""" % (servername, documentroot, documentroot, servername, servername) | |
| return out | |
| #Parse flags, fancy python way. Long options also! | |
| try: | |
| opts, args = getopt.getopt(argv[1:], "hd:s:", ["help", "document-root=", 'server-name=']) | |
| except getopt.GetoptError, err: | |
| print str(err) | |
| show_usage() | |
| #Sanity check - make sure there are arguments | |
| if opts.__len__() == 0: | |
| show_usage() | |
| documentRoot = None | |
| serverName = None | |
| #Get values from flags | |
| for option, value in opts: | |
| if option in ('-h', '--help'): | |
| show_usage() | |
| elif option in ('-d', '--document-root'): | |
| documentRoot = value | |
| elif option in ('-s', '--server-name'): | |
| serverName = value | |
| else: | |
| print "Unknown parameter used" | |
| show_usage() | |
| if exists(documentRoot) == False: | |
| makedirs(documentRoot, 0755) | |
| #chown USER:USER $DocumentRoot #POSSIBLE IMPLEMENTATION, new flag -u ? | |
| #from pwd import getpwnam -> inspect: getpwnam('someuser') | |
| if exists('%s/%s.conf' % (documentRoot, serverName)): | |
| print 'vHost already exists. Aborting' | |
| show_usage() | |
| else: | |
| target = open('/etc/apache2/sites-available/%s.conf' % serverName, 'w') | |
| target.write(create_vhost(documentRoot, serverName)) | |
| target.close() | |
| srcLink = '/etc/apache2/sites-available/%s.conf' % serverName | |
| destLink = '/etc/apache2/sites-enabled/%s.conf' % serverName | |
| symlink(srcLink, destLink) | |
| system('service apache2 reload') |
| #! /bin/bash | |
| # Run this as sudo! | |
| # I move this file to /usr/local/bin/vhost and run command 'vhost' from anywhere, using sudo. | |
| # | |
| # Show Usage, Output to STDERR | |
| # | |
| function show_usage { | |
| cat <<- _EOF_ | |
| Create a new vHost in Ubuntu Server | |
| Assumes /etc/apache2/sites-available and /etc/apache2/sites-enabled setup used | |
| -d DocumentRoot - i.e. /var/www/yoursite | |
| -h Help - Show this menu. | |
| -s ServerName - i.e. example.com or sub.example.com | |
| -a ServerAlias - i.e. *.example.com or another domain altogether | |
| -p File path to the SSL certificate. Directories only, no file name. | |
| If using an SSL Certificate, also creates a port :443 vhost as well. | |
| This *ASSUMES* a .crt and a .key file exists | |
| at file path /provided-file-path/your-server-or-cert-name.[crt|key]. | |
| Otherwise you can except Apache errors when you reload Apache. | |
| Ensure Apache's mod_ssl is enabled via "sudo a2enmod ssl". | |
| -c Certificate filename. "xip.io" becomes "xip.io.key" and "xip.io.crt". | |
| Example Usage. Serve files from /var/www/xip.io at http(s)://192.168.33.10.xip.io | |
| using ssl files from /etc/ssl/xip.io/xip.io.[key|crt] | |
| sudo vhost -d /var/www/xip.io -s 192.168.33.10.xip.io -p /etc/ssl/xip.io -c xip.io | |
| _EOF_ | |
| exit 1 | |
| } | |
| # | |
| # Output vHost skeleton, fill with userinput | |
| # To be outputted into new file | |
| # | |
| function create_vhost { | |
| cat <<- _EOF_ | |
| <VirtualHost *:80> | |
| ServerAdmin webmaster@localhost | |
| ServerName $ServerName | |
| $ServerAlias | |
| DocumentRoot $DocumentRoot | |
| # Uncomment this to proxy pass to fastcgi | |
| # Assumes Apache 2.4 with mod_proxy_fcgi | |
| # ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000$DocumentRoot/$1 | |
| <Directory $DocumentRoot> | |
| Options -Indexes +FollowSymLinks +MultiViews | |
| AllowOverride All | |
| Require all granted | |
| </Directory> | |
| ErrorLog \${APACHE_LOG_DIR}/$ServerName-error.log | |
| # Possible values include: debug, info, notice, warn, error, crit, | |
| # alert, emerg. | |
| LogLevel warn | |
| CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined | |
| #ProxyPassMatch | |
| </VirtualHost> | |
| _EOF_ | |
| } | |
| function create_ssl_vhost { | |
| cat <<- _EOF_ | |
| <VirtualHost *:443> | |
| ServerAdmin webmaster@localhost | |
| ServerName $ServerName | |
| $ServerAlias | |
| DocumentRoot $DocumentRoot | |
| # Uncomment this to proxy pass to fastcgi | |
| # Assumes Apache 2.4 with mod_proxy_fcgi | |
| # ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000$DocumentRoot/$1 | |
| <Directory $DocumentRoot> | |
| Options -Indexes +FollowSymLinks +MultiViews | |
| AllowOverride All | |
| Require all granted | |
| </Directory> | |
| ErrorLog \${APACHE_LOG_DIR}/$ServerName-error.log | |
| # Possible values include: debug, info, notice, warn, error, crit, | |
| # alert, emerg. | |
| LogLevel warn | |
| CustomLog \${APACHE_LOG_DIR}/$ServerName-access.log combined | |
| #ProxyPassMatch | |
| SSLEngine on | |
| SSLCertificateFile $CertPath/$CertName.crt | |
| SSLCertificateKeyFile $CertPath/$CertName.key | |
| <FilesMatch "\.(cgi|shtml|phtml|php)$"> | |
| SSLOptions +StdEnvVars | |
| </FilesMatch> | |
| BrowserMatch "MSIE [2-6]" \\ | |
| nokeepalive ssl-unclean-shutdown \\ | |
| downgrade-1.0 force-response-1.0 | |
| # MSIE 7 and newer should be able to use keepalive | |
| BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown | |
| </VirtualHost> | |
| _EOF_ | |
| } | |
| #Sanity Check - are there two arguments with 2 values? | |
| if [ "$#" -lt 4 ]; then | |
| show_usage | |
| fi | |
| CertPath="" | |
| #Parse flags | |
| while getopts "d:s:a:p:c:h" OPTION; do | |
| case $OPTION in | |
| h) | |
| show_usage | |
| ;; | |
| d) | |
| DocumentRoot=$OPTARG | |
| ;; | |
| s) | |
| ServerName=$OPTARG | |
| ;; | |
| a) | |
| Alias=$OPTARG | |
| ;; | |
| p) | |
| CertPath=$OPTARG | |
| ;; | |
| c) | |
| CertName=$OPTARG | |
| ;; | |
| *) | |
| show_usage | |
| ;; | |
| esac | |
| done | |
| # If alias is set: | |
| if [ "$Alias" != "" ]; then | |
| ServerAlias="ServerAlias "$Alias | |
| else | |
| ServerAlias="" | |
| fi | |
| # If CertName doesn't get set, set it to ServerName | |
| if [ "$CertName" == "" ]; then | |
| CertName=$ServerName | |
| fi | |
| if [ ! -d $DocumentRoot ]; then | |
| mkdir -p $DocumentRoot | |
| #chown USER:USER $DocumentRoot #POSSIBLE IMPLEMENTATION, new flag -u ? | |
| fi | |
| if [ -f "$DocumentRoot/$ServerName.conf" ]; then | |
| echo 'vHost already exists. Aborting' | |
| show_usage | |
| else | |
| create_vhost > /etc/apache2/sites-available/${ServerName}.conf | |
| # Add :443 handling | |
| if [ "$CertPath" != "" ]; then | |
| create_ssl_vhost >> /etc/apache2/sites-available/${ServerName}.conf | |
| fi | |
| # Enable Site | |
| cd /etc/apache2/sites-available/ && a2ensite ${ServerName}.conf | |
| service apache2 reload | |
| fi |
Usage
Install:
curl https://gist.github.com/fideloper/2710970/raw/5d7efd74628a1e3261707056604c99d7747fe37d/vhost.sh > vhost
chmod guo+x vhost
sudo mv vhost /usr/local/bin
Use:
# See Help Menu
vhost -h
# Create virtual host for "myproject.local"
sudo vhost -d /var/www/myproject -s myproject.local
At line 97 of vhost.py is
system('service apache2 reload')
and not
os.system('service apache2 reload')
Good work anyway.
@kl4us - Thanks!
Under Install:
chmos guo+x vhost
should be
chmod guo+x vhost
Updated, ty
Could you by any chances add ServerAlias as a option? That way in Vaprobash could add the ability to use the Vhost config option and the xip.io option as well.
yes this script misses out a very important step which is to add the server alias to the /etc/hosts file
echo ${Alias} >> /etc/hosts
add this line to the end of the script (before the last 'fi')
this is for ubuntu
sorry i meant add this:
echo 127.0.1.1 ${Alias} >> /etc/hosts
This rocks. In order to add the localhost information to the beginning of /etc/hosts, add this instead:
echo 127.0.0.1 ${Alias} | cat - /etc/hosts > /tmp/out && mv /tmp/out /etc/hosts
This generates a file with the new localhost information added to the top. Great scripts.
Updates:
8.2.2013