Manage nginx virtual servers and hosts file entries
MIT License
This python3 script facilitates the creation and deletion of nginx virtual servers and hosts file entries.
It is annoying to set up new virtual servers and hosts file entries by hand every time I take on a new client.
sudo pip install vhostm
git clone [email protected]:eatonphil/vhostm
cd vhostm
pyvenv .env
. .env/bin/activate
pip install -e ./
Vhostm differs slightly from spinup and provides a much more useful interface for viewing existing vhosts.
The following defaults are used:
{
"nginx_conf_dir": "/etc/nginx/sites-enabled",
"hosts_file": "/etc/hosts",
"vhosts_file": "/etc/vhostm/vhosts.conf"
}
To override any of these settings per user, copy the json with the settings you wish to override into ~/.vhostm.conf and change the value of the key.
For instance, on FreeBSD, the config (~/.vhostm.conf) may look like this:
{
"nginx_conf_dir": "/usr/local/etc/nginx/conf.d",
"vhosts_file": "/usr/local/etc/vhostm/vhosts.conf"
}
You may also override either of these per command by using the flags (--nginx_conf_dir, --vhosts_file, --hosts_file).
sudo vhostm list
This one-liner creates a new vhost at /etc/nginx/sites-enabled/mysite.com
that forwards mysite.com to localhost:3000 and serves static files from
./static. Additionally, this adds an entry in your hosts file so mysite.com
points to localhost.
sudo vhostm add -d mysite.com -p 3000 -s ./static
This one-line deletes the previously created config file and removes the entry from the hosts file.
sudo vhostm del -d mysite.com
If you would like to provide an alternative template for the nginx config, you can override the default by using the --nginx_template_file flag or assigning the "nginx_template_file" in the ~/.vhostm.conf file.
Use the default template in vhostm/vhostm.py as an example.