How to run nginx.service after /vagrant is mounted
Using the mount.target unit seems to work.
Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/wily64"
config.vm.provision :shell, path: "bootstrap.sh"
# Disabling mounting of the /vagrant directory to make sure ngnix is blocked
# Comment this out to allow the shared folder
config.vm.synced_folder ".", "/vagrant", disabled: true
end
bootstrap.sh:
#!/usr/bin/env bash
apt-get update
apt-get install -y nginx
# Make vagrant.mount loosely dependent on nginx.service
sed -i 's/WantedBy=multi-user.target/WantedBy=vagrant.mount/' /lib/systemd/system/nginx.service
systemctl daemon-reload
# Update service symlinks
systemctl disable nginx.service
systemctl enable nginx.service
Test this by running vagrant reload
to cause the VM to boot without the /vagrant directory mounted. Log into the VM and see that nginx isn't running:
vagrant@vagrant-ubuntu-wily-64:~$ sudo systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: inactive (dead)
Then comment out the Vagrantfile line that disables the shared folder and vagrant reload
again. Log into the VM again and see that nginx is running:
vagrant@vagrant-ubuntu-wily-64:~$ systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2016-07-25 04:28:00 UTC; 42s ago
Process: 1152 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 1146 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 1156 (nginx)
Memory: 7.4M
CPU: 17ms
CGroup: /system.slice/nginx.service
├─1156 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─1157 nginx: worker process
edit /lib/systemd/system/nginx.service
and change WantedBy
directive under Install
section...
WantedBy=vagrant.mount
You can check if the vagrant.mount
event exists via: systemctl list-units vagrant.mount
. You should see this:
UNIT LOAD ACTIVE SUB DESCRIPTION
vagrant.mount loaded active mounted /vagrant
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.