3

I am on Ubuntu 18.10, running under vmware and unable to start the nginx service - I get the error "nginx.service: Can't open PID file /run/nginx".

I have compiled nginx from source using passenger. It went ok on one machine, but trying it in my vm I cannot start the nginx service. It gives the above error as it cannot open the PID file (or create it?). I have done systemctl enable & start and it hangs.

I have used the /etc/sytemd/system/nginx.service script as follows:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -t
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

I have tried substituting /var/run for /run and it gives the same error.

There is no reference to the PID in the nginx.conf (and I don't think there needs to be?).

Could anyone give me any pointers on how to troubleshoot or fix this please?

3

Just stumbled across your question and thought to leave a short answer:

Systemd does NOT create PID files by itsef. It expects the executables to create it (usually within the /run, or specifically for Ubuntu within the /var/run folder). Systemd only checks, if it has been created after the service started. It also deletes the PID file when killing the service. Reference

Nginx server as forking process does support creating a PID file. To change its location, you may use the pid directive in its nginx.conf file, which you may find in the /etc/nginx folder for the standard package. This pid directive defaults to logs/nginx.pid. Reference

When manually creating the Nginx service, you need to make sure, that both locations within your Systemd nginx.service file and within your Nginx nginx.conf configuration file match.

1

Is /run available? Can you cd into /run and touch a file?

cd /run && touch hello.txt && ls -lash

If not that probably means that you want nginx to put the PID file in a location that is not available. You can do 2 things;

  1. Figure out why /run is not available (probably tmpfs configuration)
  2. Put the PID file somewhere else, for example /tmp
2
  • I can do this with sudo, yes, and I see "hello.txt". When try use the PID line "PIDFile=/tmp/nginx.pid" I get the same error: "Can't open PID file /tmp/nginx.pid (yet?) after start: No such file or directory". – tentimes Jan 27 '19 at 17:53
  • Interestingly, in a VPS version of this server I have been creating everything went ok. But going back to that setup I can find no nginx.service file in /etc/systemd/system or /lib/systemd/system. I am able to control the service though using systmctl. So maybe in the VPS server (also Ubuntu 18.10) I got the service working using some different method to my vm that I am currently on? It says .etc.init.d/nginx somewhere in the message for the working version, so is there a way to get this service running with init.d scripts? – tentimes Jan 27 '19 at 17:57
1

I have an answer in the sense of how to fix this, but I will leave it open for a bit so someone may comment on WHY this is so.

I changed the PID line so the PID file was written to the log folder of the installed nginx folder (/opt/nginx/logs). Anwhere else and it would REFUSE to write the PID file!!!

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -t
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

I have no idea what the reason for this behaviour is and it would be great if some Ubuntu Linux expert could tell me why it wouldn't write the PID anywhere else, including the normal run location /run and /var/run (or even tmp) for example, as is normal in all quoted startup scripts for the program. I think this only happens when it is created from source.

1
  • for some reasons mine told me no file or directory /opt/nginx/logs/nginx.pid when i did run nginx -s reload but i did run these lines and everthing went green. pkill -9 nginx && nginx -c /etc/nginx/nginx.conf && nginx -s reload – mwangaben Jan 12 at 7:45
0

Same symptoms on Ubuntu 20. Compiled the latest stable Nginx (1.17). Service command would timeout for Nginx.

I solved the problem by recompiling as root. The service needs port 80. Why fight it?

I also added the pid file options to ./configure

./configure --pid-path=/run/nginx.pid

Wherever it's setting the pid path to by default seems to be inaccessible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.