Software & AppsOperating SystemLinux

Fixing nginx.service PID file error on Ubuntu 18.10

Ubuntu 7

In this article, we will discuss how to fix the “nginx.service: Can’t open PID file /run/nginx” error on Ubuntu 18.10. This error often occurs when the nginx service is unable to open or create the PID file specified in the systemd configuration.

Quick Answer

To fix the "nginx.service: Can’t open PID file /run/nginx" error on Ubuntu 18.10, you can check and correct the PID file location in both the nginx.conf and nginx.service files, ensure that the nginx service has the necessary permissions to access or create the PID file, or change the PID file location to a different directory. If these solutions do not work, recompiling nginx may be necessary.

Understanding the Error

The PID (Process ID) file is used by nginx to store the process ID of the master process. It is specified in the nginx configuration file (nginx.conf) and the systemd service file (nginx.service). If the nginx service cannot access or create this file, it will not start correctly, leading to the error mentioned above.

Identifying the Cause

The most common causes for this error are:

  1. The PID file location specified in the nginx.service file does not match the location specified in the nginx.conf file.
  2. The nginx service does not have the necessary permissions to access or create the PID file.
  3. The directory where the PID file is supposed to be located does not exist.

Solutions

Checking and Correcting the PID File Location

First, check the PID file location in both the nginx.conf and nginx.service files. You can use the nano text editor to open these files:

sudo nano /etc/nginx/nginx.conf
sudo nano /lib/systemd/system/nginx.service

In the nginx.conf file, look for the pid directive, which should look something like this:

pid /run/nginx.pid;

In the nginx.service file, look for the PIDFile directive, which should look something like this:

PIDFile=/run/nginx.pid

If the locations do not match, correct them so they do. After making the changes, save the files and exit the text editor. Then, reload the systemd manager configuration and restart the nginx service:

sudo systemctl daemon-reload
sudo systemctl restart nginx

Checking and Correcting Permissions

If the PID file locations match but the error persists, check the permissions of the directory where the PID file is located. The nginx service needs write access to this directory to create the PID file. You can check the permissions with the ls -ld command:

ls -ld /run/nginx

If necessary, change the owner of the directory to the nginx user and set the correct permissions:

sudo chown nginx:nginx /run/nginx
sudo chmod 755 /run/nginx

Then, restart the nginx service:

sudo systemctl restart nginx

Changing the PID File Location

If the above solutions do not work, you can change the PID file location to a different directory, such as /tmp. To do this, modify the pid directive in the nginx.conf file and the PIDFile directive in the nginx.service file:

pid /tmp/nginx.pid;
PIDFile=/tmp/nginx.pid

After making the changes, save the files and exit the text editor. Then, reload the systemd manager configuration and restart the nginx service:

sudo systemctl daemon-reload
sudo systemctl restart nginx

Recompiling nginx

Another solution is to recompile nginx as root and specify the PID file path during the configuration process. However, this solution is more complex and should only be attempted if the above solutions do not work.

Conclusion

The “nginx.service: Can’t open PID file /run/nginx” error can be resolved by ensuring that the PID file location specified in the nginx.service file matches the location specified in the nginx.conf file, checking the permissions and accessibility of the specified PID file location, and changing the PID file location if necessary. If all else fails, recompiling nginx as root may be necessary.

What is the purpose of the PID file in nginx?

The PID (Process ID) file is used by nginx to store the process ID of the master process. It helps in managing and monitoring the nginx service.

Why am I getting the “nginx.service: Can’t open PID file /run/nginx” error?

This error occurs when the nginx service is unable to open or create the PID file specified in the systemd configuration. It can be caused by incorrect PID file locations, permission issues, or non-existing directories.

How can I check and correct the PID file location?

To check the PID file location, open the nginx.conf and nginx.service files using a text editor such as nano. Look for the pid directive in nginx.conf and the PIDFile directive in nginx.service. Make sure the locations match and correct them if necessary. Then, reload the systemd manager configuration and restart the nginx service.

What should I do if the PID file locations match but the error persists?

If the error persists, check the permissions of the directory where the PID file is located. The nginx service needs write access to this directory. Use the ls -ld command to check the permissions. If necessary, change the owner of the directory to the nginx user and set the correct permissions. Then, restart the nginx service.

Can I change the PID file location to a different directory?

Yes, you can change the PID file location to a different directory if needed. Modify the pid directive in nginx.conf and the PIDFile directive in nginx.service to specify the new location. After making the changes, reload the systemd manager configuration and restart the nginx service.

Is recompiling nginx an option to fix this error?

Yes, recompiling nginx as root and specifying the PID file path during the configuration process is another solution. However, this is a more complex solution and should only be attempted if the previous solutions do not work.

Leave a Comment

Your email address will not be published. Required fields are marked *