Skip to content

Instantly share code, notes, and snippets.

@djboris88
Last active January 15, 2019 12:47
Show Gist options
  • Save djboris88/ab0a882caf88c0a55dbe444b64de7943 to your computer and use it in GitHub Desktop.
Save djboris88/ab0a882caf88c0a55dbe444b64de7943 to your computer and use it in GitHub Desktop.
Quickly create Virtual Host for Apache2 on Ubuntu-like distros

Instructions

Download the add-vhost.sh file, make it executable and move it to /usr/bin/add-vhost:
sudo chmod +x add-vhost.sh && sudo mv add-vhost.sh /usr/bin/add-vhost

Run it as root from anywhere:
sudo add-vhost

This will create a configuration file for the Apache, both for non-SSL and SSL versions, and also allow you to set different PHP versions if you have installed them as PHP-fpm services.

If you don't have PHP-fpm versions installed, remove these parts:

while ! [[ "${php_version}" =~ ^(5.6|7.2|7.3)$ ]]
do
    read -p "PHP version (5.6, 7.2, 7.3): " php_version
done

... and ...

<FilesMatch \.php$>
    # Apache 2.4.10+ can proxy to unix socket
    SetHandler \"proxy:unix:/var/run/php/php${php_version}-fpm.sock|fcgi://localhost/\"
</FilesMatch>
#!/usr/bin/env bash
# exit if not run with sudo
if [ "$EUID" -ne 0 ]
then echo "Please run as root"
exit
fi
# get ServerNume
while [[ -z "${server_name}" ]] || [[ -f /etc/apache2/sites-available/${server_name}.conf ]]
do
if [ -f /etc/apache2/sites-available/${server_name}.conf ]
then
echo "Virtual host with this ServerName already exists!"
fi
read -p "ServerName: " server_name
done
# Check path
while [[ -z "${path}" ]] || [[ ! -d "${path}" ]]
do
if [[ ! -z "${path}" ]] && [[ ! -d "${path}" ]]
then
echo "Directory at ${path} does not exist."
fi
read -p "DocumentRoot: " path
done
while ! [[ "${php_version}" =~ ^(5.6|7.2|7.3)$ ]]
do
read -p "PHP version (5.6, 7.2, 7.3): " php_version
done
echo "<VirtualHost *:80>
ServerName ${server_name}
ServerAlias www.${server_name}
ErrorLog \${APACHE_LOG_DIR}/${server_name}-error.log
CustomLog \${APACHE_LOG_DIR}/${server_name}-access.log combined
DocumentRoot ${path}
<Directory ${path}>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
# Apache 2.4.10+ can proxy to unix socket
SetHandler \"proxy:unix:/var/run/php/php${php_version}-fpm.sock|fcgi://localhost/\"
</FilesMatch>
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName ${server_name}
ServerAlias www.${server_name}
ErrorLog \${APACHE_LOG_DIR}/${server_name}-error.log
CustomLog \${APACHE_LOG_DIR}/${server_name}-access.log combined
DocumentRoot ${path}
<Directory ${path}>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
# Apache 2.4.10+ can proxy to unix socket
SetHandler \"proxy:unix:/var/run/php/php${php_version}-fpm.sock|fcgi://localhost/\"
</FilesMatch>
</VirtualHost>
</IfModule>" >> /etc/apache2/sites-available/${server_name}.conf
a2ensite ${server_name}
systemctl reload apache2
echo "Apache reloaded"
echo "127.0.0.1 ${server_name}" >> /etc/hosts
echo "Added ${server_name} to /etc/hosts"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment