Skip to content

Instantly share code, notes, and snippets.

@raflymln
Last active November 25, 2022 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raflymln/f5f83ad47728526242f0c071b2ca8dfc to your computer and use it in GitHub Desktop.
Save raflymln/f5f83ad47728526242f0c071b2ca8dfc to your computer and use it in GitHub Desktop.
Automatically create .conf files symlink from sites-available folder to sites-enabled folder on Nginx restart/start
# 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
Documentation=man:nginx(8)
After=network.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=sh -c 'cd /etc/nginx/sites-enabled; find ../sites-available -name \*.conf -exec ln -vsf "{}" . \';\''
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
@raflymln
Copy link
Author

With this modified nginx.service file, it allows you to organize .conf file in sites-available folder, and then only symlink all .conf file (not including the directories) into sites-enabled folder

Example sites-available

sites-available/
├─ domain.conf
├─ Company 1/
│  ├─ domain1.conf
├─ Company 2/
│  ├─ domain2.conf
│  ├─ domain3.conf
│  ├─ domain4.conf
├─ Company 3/
│  ├─ domain5.conf
│  ├─ domain6.conf

When nginx restarted, it will automatically create this below on sites-enabled directory

sites-enabled/
├─ domain.conf
├─ domain1.conf
├─ domain2.conf
├─ domain3.conf
├─ domain4.conf
├─ domain5.conf
├─ domain6.conf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment