How to recreate nginix.pid when I deleted it from tmp folder? - nginx

So I tried to free up some space in nginx server and I deleted content of tmp files.
Then When i tried to run command:
sudo nginx -t -c /etc/nginx/nginx.conf
I got error:
sudo nginx -t -c /etc/nginx/nginx.conf
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (2: No such file or directory)
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2018/04/27 10:33:44 [emerg] 1580#1580: open() "/var/log/nginx/access.log" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
Is there anyway to recreate nginx.pid file?. I also created manually nginx/error.log file and gave them permissions chmod -R 777.

You could touch the files and then start again nginx, for example:
# touch /var/log/nginx/error.log
# touch /var/log/nginx/access.log
Test if your config just to verify and start again, the pid will be automatically created:
# nginx -t -c /etc/nginx/nginx.conf
From the command line example from the docs you could also do:
nginx -t -c /path/to/nginx.conf -g "pid /var/run/nginx.pid; worker_processes 2;"

Related

No nginx folders after installing on Amazon Linux 2 EC2 instance

I'm trying to install Nginx on an EC2 instance running Amazon Linux 2. After running the commands as listed here on: https://stackoverflow.com/a/61256478/11309912.
Checking the version shows that is installed.
nginx -v
nginx version: nginx/1.22.0
But either under /etc or /local there is no nginx folder being created.
nginx -t does run but fails:
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2022/11/13 13:33:46 [emerg] 16016#16016: open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
I tried running sudo yum remove nginx and sudo yum install nginx/sudo amazon-linux-extras install -y nginx1 but both to no avail.
Is there a way to (re)install nginx that places these folders and configs?

Install of openresty : nginx.pid not found

I try to install OpenResty 1.13.6.1 under CentOS 7. When I try to run openresty I get this error:
[root#flo ~]# openresty -s reload
nginx: [error] open() "/usr/local/openresty/nginx/logs/nginx.pid" failed (2: No such file or directory)
When I look at my logs, I only have 2 files:
[root#flo ~]# ll /usr/local/openresty/nginx/logs/
total 8
-rw-r--r--. 1 root root 0 1 mars 12:24 access.log
-rw-r--r--. 1 root root 4875 1 mars 16:03 error.log
I do not see how to find a solution.
///////////////////UPDATE//////////////////
I try to do this to folow the instructions of this page : https://openresty.org/en/getting-started.html
[root#flo ~]# PATH=/usr/local/openresty/nginx/sbin:$PATH
[root#flo ~]# export PATH
[root#flo ~]# nginx -p pwd/ -c conf/nginx.conf
And I have this error :
nginx: [alert] could not open error log file: open() "/root/logs/error.log" failed (2: No such file or directory)
2018/03/02 09:02:55 [emerg] 30824#0: open() "/root/conf/nginx.conf" failed (2: No such file or directory)
/////////////////UPDATE2//////////////:
[root#nexus-chat1 ~]# cd /root/
[root#nexus-chat1 ~]# ll
total 4
-rw-------. 1 root root 1512 1 mars 11:05 anaconda-ks.cfg
drwxr-xr-x. 3 root root 65 1 mars 11:36 openresty_compilation
Where do I need to create these folders ?
mkdir ~/work
cd ~/work
mkdir logs/ conf/
In /usr/local/openresty/ ?
Very likely nginx cannot open a log file because folder doesn't exists or permission issue.
You can see the reason within error.log file
openresty -s reload is used to tell nginx to reload the currently running instance. That's why it's complaining about the missing pid file.
Anyway, that's not the correct way to start openresty. Have a look at https://openresty.org/en/getting-started.html for instructions on how to get started.

nginx.conf and nginx.pid users and permissions

I'm embarking on watching my NGINX error.log files at level: warn... probably a silly idea and will cause me to crash my server as I work out any bugs happening, but hey, we're nerds and this is why we're here.
I'm noticing a [warn] and an [emerg] pop up every time I restart my server, which shows:
[warn] 8041#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
[emerg] 8041#0: open() "/run/nginx.pid" failed (13: Permission denied)
The top of my nginx.conf file reads:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
Which to me, shows me a few things.
I'm running NGINX with the user: www-data.
The number of worker processes that are allowed is automatically adjusted.
my PID file/information is being stored in /run/nginx.pid.
The error tells me that NGINX doesn't have permission to access /run/nginx.pid, which led me to see the user permissions for said file.
sudo ls -la /run/nginx.pid
reveals:
-rw-r--r-- 1 root root 5 Jun 18 05:34 /run/nginx.pid
Then trying:
ps -ef | grep nginx
produces:
root 5914 1 0 05:34 ? 00:00:00 nginx: master process /u
www-data 5917 5914 0 05:34 ? 00:00:00 nginx: worker process
scratches head
Now, can somebody out there tell me why, or how the hell NGINX has managed to create the master process with root ownership, and now the worker processes are owned by www-data?
Or more to the point, anybody have some suggestions on what to do about this [emerg] error I'm getting?
My first thought is to just try and change the ownership of the /run/nginx.pid file and see how NGINX likes it, but I kind of feel that even if I do that manually this time, when I restart the server, I'll run into the same problem.
My second thought is maybe there is somewhere else that I define my worker process initiation within NGINX..
Thanks.
EDIT
The contents of the /etc/systemd/system/multi-user.target.wants/nginx.service file are:
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
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=/usr/sbin/nginx -s quit
[Install]
WantedBy=multi-user.target
I got the same error on my Centos 7 server today.
nginx.pid" failed (13: Permission denied)
For me, it turned out to be a problem with SELinux. I did the following to make it work again:
systemctl stop nginx
touch /var/run/nginx.pid
chcon -u system_u -t httpd_var_run_t /var/run/nginx.pid
systemctl start nginx
running
ls -Z nginx.pid
should output
-rw-r--r--. root root system_u:object_r:httpd_var_run_t:s0 nginx.pid
In my case I got a
"/usr/local/var/run/nginx.pid" failed (13: Permission denied)
bind() to 0.0.0.0:80 failed (48: Address already in use)
and the working solution was made up of these steps:
stop root process
sudo nginx -s stop
check if process stopped
ps aux | grep nginx
restart process
sudo nginx -s reload
gave me the error
nginx: [error] open() “/usr/local/var/run/nginx.pid” failed (2: No such file or directory)
probabil .pid was started with the wrong root user as I uncommented the line with path to .pid in /usr/local/etc/nginx/nginx.conf and then I commented it back again
to start nginx as a user and not root
brew services start nginx
result at running command
ps aux | grep nginx
youruser 89212 0.0 0.0 4268280 644 s002 S+ 2:46PM 0:00.00 grep nginx
youruser 89179 0.0 0.0 4302204 1776 ?? S 2:45PM 0:00.00 nginx: worker process
youruser 89178 0.0 0.0 4275372 4368 ?? S 2:45PM 0:00.01 nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;
And as it can be seen, the nginx process started with the expected user and not as root and the conflict between processes was gone and I could access the PHP application local domain.
For Ubuntu 20.04+
I got the exact same error while I was using sudo systemctl reload nginx
Using sudo service nginx restart instead throws no error

/etc/init.d/nginx restart as root fails but nginx -t won't tell anything

So Nginx is started in daemon mode, but root, as confirms this command:
root#test:/home/vagrant# ps -edf | grep nginx
root 7331 1 0 13:42 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 7333 7331 0 13:42 ? 00:00:00 nginx: worker process
But if I:
root#test:/home/vagrant# /etc/init.d/nginx restart
* Restarting nginx nginx [fail]
But when I run:
root#test:/home/vagrant# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
It's only when I go back to user vagrant that I get:
[13:46:58] vagrant#test:/home/vagrant $ nginx -t
2016/04/21 13:47:01 [emerg] 7390#7390: open() "/run/nginx.pid" failed (13: Permission denied)
And if I:
root#test:/home/vagrant# ls -l /run/nginx.pid
-rw-r--r-- 1 root root 5 Apr 21 13:45 /run/nginx.pid
So why won't nginx restart? What am I doing wrong? I mean, root not having enough permission doesn't make sense, does it?
And how come sudo nginx -t doesn't display any error message?
Have you this directive in your configuration file user www www; ?
If yes, does the user www (or whom you set) have access to the pid file and to the site directories ?
extra: if you need you can move the pif file using the pid path; directive.

Restarting nginx: nginxnginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

When I try to restart nginx with sudo /etc/init.d/nginx restart I get the message from the subject.
I discovered that the reason is most likely that the script doesn't know how to stop the deamon because the pid file (/var/run/nginx.pid) is not created on start.
I have two installations on two different servers... one was compiled from source and the other came with phusion passenger.
I tried this command:
start-stop-daemon --start --quiet --pidfile /var/run/nginx.pid --exec /usr/sbin/nginx -- -c /etc/nginx/nginx.conf
on both machines and on one the pid file is created and on the other it is not - on that machine the paths are a bit different (but I don't think this is relevant):
start-stop-daemon --start --quiet --pidfile /var/run/nginx.pid --exec /opt/nginx/sbin/nginx -- -c /opt/nginx/conf/nginx.conf
The process starts and pid is not written...
I'm on Debian...
Any suggestions?
The solution is to uncomment this line in nginx.conf:
pid /var/run/nginx.pid;
It looks like different installations do it differently but the right thing is to uncomment it.
I was able to fix this by running the following fuser command
$ sudo fuser -k 80/tcp
which kills whatever process is using port 80..hopefully didn't screw anything else.
Credit for this goes to:
https://goo.gl/6oc0xD
$ sudo nginx -t
to see all processes, sometimes you do not have the full permission

Resources