Missing /etc/nginx folder
Not entirely sure how this might have happened, but I once SSHed into on of my servers to find out that the entire /etc/nginx folder has dissapeared. I tried looking elsewhere or using locate, but the configuration folder was nowhere to be found. Just like that the /var/log/nginx folder has dissapeared as well. It might have happened during an upgrade, but after some thorough searching, I still couldn't figure out when or why it happened.
Strangely, all services (websites) using Nginx were running fine and the entire service was active and running as well, still pointing to the non-existent config file:
$ systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/lib/systemd/system/nginx.service; disabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/nginx.service.d
└─override.conf
Active: active (running) since Wed 2021-09-15 21:40:43 CEST; 3 days ago
Docs: https://nginx.org/en/docs/
Main PID: 98 (nginx)
CPU: 20.643s
CGroup: /system.slice/nginx.service
├─ 98 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─101 nginx: worker process
Sep 15 21:40:43 hostname systemd[1]: Starting nginx - high performance web server...
Sep 15 21:40:43 hostname systemd[1]: Started nginx - high performance web server.
$ cd /etc/nginx
-bash: cd: /etc/nginx: No such file or directory
However, if the server or Nginx randomly restarted, it wouldn't be able to start again and all hell would break loose.
Fortunately, I have backed up my config file, which is really the only thing that matters, the rest can be rebuilt.
Stop Nginx
$ sudo systemctl stop nginx
Try to restart it, and just like I expected:
$ sudo systemctl restart nginx
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
$ sudo systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/lib/systemd/system/nginx.service; disabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/nginx.service.d
└─override.conf
Active: failed (Result: exit-code) since Sun 2021-09-19 16:25:01 CEST; 5s ago
Docs: https://nginx.org/en/docs/
Process: 22419 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
CPU: 11ms
Sep 19 16:25:01 hostname systemd[1]: Starting nginx - high performance web server...
Sep 19 16:25:01 hostname nginx[22419]: nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" fa>
Sep 19 16:25:01 hostname nginx[22419]: 2021/09/19 16:25:01 [emerg] 22419#22419: open() "/etc/nginx/nginx.conf" failed (2:>
Sep 19 16:25:01 hostname systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
Sep 19 16:25:01 hostname systemd[1]: nginx.service: Failed with result 'exit-code'.
Sep 19 16:25:01 hostname systemd[1]: Failed to start nginx - high performance web server.
Uninstall and reinstall Nginx
$ sudo apt purge nginx*
$ sudo apt install nginx
Fix configuration
$ cd /etc/nginx/conf.d
$ sudo mv default.conf proxy.conf
I'm copying the config through SSH session, so just $ sudo vi proxy.conf
and Shift+Ins
and Esc
and ZZ
to save and quit.
Test config, final fixes
Test the configuration
$ sudo nginx -t
Generate secure dhparam.pem. This will take a loooooong time, especially on VPSes with a single core like in my case.
$ openssl dhparam -out dhparam.pem 4096