A simple bash script to quickly create a virtual host config file for Apache and Nginx. Also, creates the main document root and a static document root (public sub directory) served by Nginx directly.
This script was created for a very specific purpose, but could probably be easily configured for other purposes. The setup it's intended for is a host computer serving multiple PHP websites through Apache running inside of a Docker container and using Nginx to route requests. It also automatically creates a static subdomain that can be used for bypassing Apache for static content.
SETUP
Simply edit the configuration directories at the top of the main .sh file and customize the templates in the template directory as needed.
EXAMPLE USAGE
./quick-vhost.sh itslennysfault.com
Creating apache vhost conf
Creating apache nginx conf
Creating apache document root
Creating static document root
Creating placeholder indexes
This would create the following files / directories in the default configuration:
- /var/www/vhosts/itslennysfault.com.conf
- Apache config file for the virtual host itslennysfault.com pointing to the document root of the same name
- /etc/nginx/sites-enabled/itslennysfault.com
- Nginx config file for the virtual host itslennysfault.com that proxies to apache running inside of a docker container AND static.itslennysfault.com pointing to the public sub folder of that same document root allowing nginx to serve static files from that subdomain
- /etc/httpd/vhosts/itslennysfault.com
- Apache document root
- /etc/httpd/vhosts/itslennysfault.com/public
- Nginx static document root
- also creates a placeholder index.html in both of the document roots.
CONFIGURATION
HOST_DOCUMENT_DIR | This is the path where the script will create the document root for this virtual host. This is also the path used in the Nginx config so it knows where to look for static files. |
CONTAINER_DOCUMENT_DIR | This is the path that will get set in the apache virtual host. If your host and container are using the same file paths OR if you're running Nginx and Apache on the same server/container this would be the same as the above. However, often you're mapping a volume on the container to a different path on the host. |
APACHE_VHOST_DIR | This is the directory on the host where the virtual host configurations will be stored. This should be mapped to a directory inside of the container that is included in the apache config. |
NGINX_VHOST_DIR | This is the directory where you want the virtual host configuration files for Nginx to be stored. This could be your sites-enabled directory, but it's probably better to create a separate directory for these files and set them to be included automatically. |
EXAMPLE SETUP
Below is everything needed to get up and running with a container-ized Apache instance running behind Nginx. There are more optimal ways to initiate this, but this is the quickest way to get up and running. It is assumed you already know how to create a docker container running Apache.
Quick-Vhost Configuration:
HOST_DOCUMENT_DIR="/ilf/docroots/"
CONTAINER_DOCUMENT_DIR="/var/www/vhosts/"
APACHE_VHOST_DIR="/ilf/apache-vhosts/"
NGINX_VHOST_DIR="/ilf/nginx-vhosts/"
Apache Configuration (within container):
#includes files generated by quick-vhost
Include vhost/*.conf
Nginx Configuration:
#includes files generated by quick-vhost
include /ilf/nginx-vhosts/*;
Map volumes on docker to appropriate paths on the host:
docker run -v /ilf/apache-vhosts:/etc/httpd/vhost -v /ilf/docroots:/var/www/vhosts -p 8080:80 ilf_php2 /usr/sbin/httpd -D FOREGROUND &