gpt4 book ai didi

ubuntu -/vagrant 挂载后如何运行 nginx.service

转载 作者:太空宇宙 更新时间:2023-11-03 16:49:16 25 4
gpt4 key购买 nike

我想做什么?

我正在尝试让 nginx 从 /vagrant 自动加载配置,由 vagrant 自动挂载。

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

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

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

环境

  • Vagrant 1.8.1
  • Ubuntu 服务器 15.10 (ubuntu/wily64)
  • VirtualBox 5.0.14

systemctl cat nginx.service

模式 1:无效

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

最佳答案

使用 mount.target 单元似乎可行。

Vagrant 文件:

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

通过运行 vagrant reload 进行测试,使 VM 在没有挂载/vagrant 目录的情况下启动。登录虚拟机并查看 nginx 未运行:

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)

然后注释掉禁用共享文件夹的 Vagrantfile 行并再次 vagrant reload。再次登录虚拟机,可以看到nginx正在运行:

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

关于ubuntu -/vagrant 挂载后如何运行 nginx.service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35821926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com