/vagrant挂载后如何运行nginx.service [英] How to run nginx.service after /vagrant is mounted

查看:30
本文介绍了/vagrant挂载后如何运行nginx.service的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从由 vagrant 自动挂载的 /vagrant 进行 nginx 加载配置.

I'm trying to make nginx load configurations from /vagrant mounted by vagrant automatically.

所以我编辑了 nginx.service 让它在安装共享文件夹后启动,但它不起作用.

So I edited nginx.service to make it boot after shared folder mounted, but it not works.

当然nginx是在virtualbox-guest-utils.service之后启动的,但是好像是在vagrant.mount(/vagrant)之前启动.因为 nginx 无法从/vagrant 加载配置,它在手动运行命令 systemctl restart nginx.service 后工作.

Certainly nginx has been booted after virtualbox-guest-utils.service, however, it seems to be booted before vagrant.mount (/vagrant). Because nginx couldn't load configurations from /vagrant and it works after running command systemctl restart nginx.service manually.

如何在自动生成的 .mount 单元启动后运行 .service 单元?

How to run .service unit after auto generated .mount unit started?

  • 流浪者 1.8.1
  • Ubuntu 服务器 15.10 (ubuntu/wily64)
  • VirtualBox 5.0.14
# /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

模式 2:不起作用

# /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

推荐答案

我写了一个发布我如何使用 udev 事件重新启动 nginx 和 php5-fpm.

I wrote a post how I'm using udev event to restart nginx and php5-fpm.

简而言之,我正在添加带有规则的 /etc/udev/rules.d/50-vagrant-mount.rules:

In short I'm adding /etc/udev/rules.d/50-vagrant-mount.rules with rule:

SUBSYSTEM=="bdi",ACTION=="add",RUN+="/bin/bash /root/.udev-mount-restart-services.sh"

脚本 /root/.udev-mount-restart-services.sh 是:

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

这篇关于/vagrant挂载后如何运行nginx.service的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆