我正在尝试由无业游民自动安装的/vagrant
进行nginx负载配置。
所以我编辑了nginx.service以使其在挂载共享文件夹后启动,但它不起作用。
当然,nginx已在virtualbox-guest-utils.service之后启动,但是似乎在vagrant.mount(/ vagrant)之前启动了。因为nginx无法从/ vagrant加载配置,并且在手动运行命令systemctl restart nginx.service
后可以正常工作。
自动生成的.mount单元启动后如何运行.service单元?
# /lib/systemd/system/nginx.service
# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
# /etc/systemd/system/nginx.service.d/override.conf
[Unit]
Requires=virtualbox-guest-utils.service vagrant.mount
After=virtualbox-guest-utils.service vagrant.mount
# /lib/systemd/system/nginx.service
# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
# /etc/systemd/system/nginx.service.d/override.conf
[Unit]
RequiresMountsFor=/vagrant
我写了post how I'm using udev
event to restart nginx and php5-fpm。
简而言之,我将udev
与规则相加:
/etc/udev/rules.d/50-vagrant-mount.rules
脚本SUBSYSTEM=="bdi",ACTION=="add",RUN+="/bin/bash /root/.udev-mount-restart-services.sh"
是:
/root/.udev-mount-restart-services.sh
使用mount.target单位似乎起作用。
Vagrantfile:
sleep 5 # wait for a bit for NFS to make sure resources are available
systemctl restart php5-fpm > /dev/null 2>&1
systemctl restart nginx > /dev/null 2>&1
bootstrap.sh:
# -*- 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
通过运行#!/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
进行测试,以使VM在未安装/ vagrant目录的情况下启动。登录到虚拟机,然后查看nginx没有运行:
vagrant reload
然后注释掉禁用共享文件夹的Vagrantfile行和再次单击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)
。再次登录到VM,然后查看nginx是否正在运行:
vagrant reload
编辑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
并在/lib/systemd/system/nginx.service
部分下更改WantedBy
指令...
Install
您可以通过以下方式检查WantedBy=vagrant.mount
事件是否存在:vagrant.mount
。您应该看到以下内容:
systemctl list-units vagrant.mount
根据Ansible总结先前的答案和评论(如果用于提供):
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'.
用流浪汉安装单元名称替换- name: Copy Nginx Systemd unit to /etc/
copy:
src: /lib/systemd/system/nginx.service
dest: /etc/systemd/system/nginx.service
remote_src: yes
- name: Change Nginx Systemd unit
lineinfile:
path: /etc/systemd/system/nginx.service
regexp: '^WantedBy='
line: 'WantedBy={{ vagrant_mount_unit }}'
state: present
- name: Systemd daemon reload
systemd:
daemon_reload: yes
- name: Disable Nginx
service:
name: nginx
enabled: no
- name: Enable Nginx
service:
name: nginx
enabled: yes
。它取决于安装点,例如如果安装点为{{ vagrant_mount_unit }}
,则单元名称为/home/vagrant/app
。
我想在这里放下自己的答案,因为似乎每个答案都包含正确答案的一部分,并且某些详细信息只能在注释中找到。
在此示例中,我在/ var / www / mymount1和/ var / www / mymount2中有2个文件系统挂载
我的Vagrantfile的这一部分:
home-vagrant-app.mount
我将下面的脚本作为我的供应商文件放在/docs/vagrant/init-vagrant.sh中。
config.vm.provision "shell", path: "docs/vagrant/init-vagrant.sh"
config.vm.synced_folder "mymount1", "/var/www/mymount1", type: "nfs"
config.vm.synced_folder "mymount2", "/var/www/mymount2", type: "nfs"