Ubuntu 安装/vagrant后如何运行nginx.service 我想做什么?
我正在尝试从由vagrant自动装载的Ubuntu 安装/vagrant后如何运行nginx.service 我想做什么?,ubuntu,vagrant,systemd,Ubuntu,Vagrant,Systemd,我正在尝试从由vagrant自动装载的/vagrant进行nginx加载配置 所以我编辑了nginx.service,让它在共享文件夹挂载后启动,但它不起作用 当然,nginx是在virtualbox-guest-utils.service之后启动的,但是,它似乎是在vagrant.mount(/vagrant)之前启动的。 因为nginx无法从/vagrant加载配置,并且它在手动运行命令systemctl restart nginx.service后工作 如何在自动生成后运行服务单元。装载单
/vagrant
进行nginx加载配置
所以我编辑了nginx.service,让它在共享文件夹挂载后启动,但它不起作用
当然,nginx是在virtualbox-guest-utils.service之后启动的,但是,它似乎是在vagrant.mount(/vagrant)之前启动的。
因为nginx无法从/vagrant加载配置,并且它在手动运行命令systemctl restart nginx.service
后工作
如何在自动生成后运行服务单元。装载单元已启动
环境
- 流浪汉1.8.1
- Ubuntu服务器15.10(Ubuntu/wily64)
- VirtualBox 5.0.14
#/lib/systemd/system/nginx.service
#停止为nginx跳舞
# =======================
#
#ExecStop向nginx进程发送SIGSTOP(优雅停止)。
#如果在5s(--retry QUIT/5)之后nginx仍在运行,systemd将接管控制权
#并将SIGTERM(快速关机)发送到主进程。
#再过5秒(TimeoutStopSec=5),如果nginx处于活动状态,systemd将发送
#将kill添加到进程组中的所有剩余进程(KillMode=mixed)。
#
#nginx信号参考文件:
# http://nginx.org/en/docs/control.html
#
[单位]
Description=高性能web服务器和反向代理服务器
After=network.target
[服务]
类型=分叉
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx-t-q-g'daemon;主控_流程开启;'
ExecStart=/usr/sbin/nginx-g'守护进程打开;主控_流程开启;'
ExecReload=/usr/sbin/nginx-g'守护进程打开;主控_流程开启;'-s重新加载
ExecStop=-/sbin/start-stop守护进程--安静--停止--重试退出/5--pidfile/run/nginx.pid
TimeoutStopSec=5
基尔模式=混合
[安装]
WantedBy=multi-user.target
#/etc/systemd/system/nginx.service.d/override.conf
[单位]
Requires=virtualbox-guest-utils.service vagrant.mount
After=virtualbox-guest-utils.service vagrant.mount
模式2:不工作
#/lib/systemd/system/nginx.service
#停止为nginx跳舞
# =======================
#
#ExecStop向nginx进程发送SIGSTOP(优雅停止)。
#如果在5s(--retry QUIT/5)之后nginx仍在运行,systemd将接管控制权
#并将SIGTERM(快速关机)发送到主进程。
#再过5秒(TimeoutStopSec=5),如果nginx处于活动状态,systemd将发送
#将kill添加到进程组中的所有剩余进程(KillMode=mixed)。
#
#nginx信号参考文件:
# http://nginx.org/en/docs/control.html
#
[单位]
Description=高性能web服务器和反向代理服务器
After=network.target
[服务]
类型=分叉
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx-t-q-g'daemon;主控_流程开启;'
ExecStart=/usr/sbin/nginx-g'守护进程打开;主控_流程开启;'
ExecReload=/usr/sbin/nginx-g'守护进程打开;主控_流程开启;'-s重新加载
ExecStop=-/sbin/start-stop守护进程--安静--停止--重试退出/5--pidfile/run/nginx.pid
TimeoutStopSec=5
基尔模式=混合
[安装]
WantedBy=multi-user.target
#/etc/systemd/system/nginx.service.d/override.conf
[单位]
RequiresMountsFor=/vagrant
我写了一篇文章
简而言之,我正在添加/etc/udev/rules.d/50 vagrant mount.rules
和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
使用挂载。目标单元似乎有效 流浪汉档案:
# -*- 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
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"
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目录的情况下启动。登录到VM并查看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重载
。再次登录VM并查看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
编辑
/lib/systemd/system/nginx.service
并更改Install
部分下的WantedBy
指令
WantedBy=vagrant.mount
您可以通过以下方式检查vagrant.mount
事件是否存在:systemctl list units vagrant.mount
。您应该看到:
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'.
根据Ansible(如果用于提供)总结先前的回答和评论:
将
{{vagrant\u mount\u unit}}
替换为vagrant mount unit name。它取决于挂载点,例如,如果挂载点是/home/vagrant/app
,则单元名称将是home vagrant app。挂载我想在这里放弃我自己的答案,因为似乎每个答案都有正确答案的一部分,并且一些细节只能在注释中找到
在本例中,我在/var/www/mymount1和/var/www/mymount2中装载了2个文件系统
我的流浪汉档案的这一部分:
# -*- 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
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"
我将下面的脚本放在/docs/vagrant/init-vagrant.sh中作为我的provisioner文件
# Copy nginx systemd script and make edits in /etc/systemd/system, as updates might change the original in /lib/systemd/system/nginx.service
cp /lib/systemd/system/nginx.service /etc/systemd/system/
# These tell that nginx should not be started before file system mounts are available
echo WantedBy=var-www-mymount1.mount >> /etc/systemd/system/nginx.service
echo WantedBy=var-www-mymount2.mount >> /etc/systemd/system/nginx.service
# Update service symlinks
systemctl daemon-reload
systemctl disable nginx.service
systemctl enable nginx.service
非常感谢。干得好。然而,为什么.mount
或requiresmounts for
不起作用?对于Debian 8 guest来说,vagrant.mount
和requiresmounts for
模式也不适用。当您将WantedBy
更改为vagrant.mount
时,您必须再次启用该服务,然后它才能工作。无需编辑udev规则。请参阅下面的@jacob wan的答案,以获得更清洁的解决方案。我已经测试了这个问题,可以确认这是正确的答案。当您在[Install]
部分安装了WantedBy=vagrant.mount
,然后禁用并启用该服务时,它将在下次引导时正确启动。您不需要先禁用NFS装载。否则,这应该是公认的答案。不需要“黑客”,对我也有用。从语义上讲,它确实感觉像是一种黑客行为,因为WantedBy
依赖关系是错误的。尽管我尝试了各种其他解决方案(要求
,要求
,之后
,以及这些方案的组合),但它们都不起作用,因为systemd忽略了不存在的依赖项,如日志消息所示,“无法为单元xxx添加依赖项作业。服务,ignorin”