Score:0

Unable to restart nginx, I think something to do with port 80?

by flag

I am following Corey Schafer's flask tutorial.

I am trying to deploy to a Linux server, but am unable to get nginx working.

These are the commands I have run so far:

sudo apt install nginx

pip install gunicorn

sudo rm /etc/nginx/sites-enabled/default

I have then tried to create and edit a new file:

sudo nano /etc/nginx/sites-enabled/flaskblog

This is what I input into that file:

server {
    listen 80;
    server_name DOMAIN_IP;

    location /static {
        alias /Users/myname/VScode/Flask_Blog/flaskblog/static;
    }

    location / {
        proxy_pass http://localhost:8000;
        include /etc/nginx/proxy_params;
        proxy_redirect off;
    }
}

I then run

sudo nano ufw allow http/tcp

sudo ufw delete allow 5000

sudo ufw enable

Then when I try to restart nginx I get a failure

sudo systemctl restart nginx

Produces

Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.

systemctl status nginx.service

Produces:

    nginx.service - A high performance web server and a reverse proxy server
         Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
         Active: failed (Result: exit-code) since Tue 2022-05-31 06:33:22 UTC; 4min 6s ago
           Docs: man:nginx(8)
        Process: 62279 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCC>
        Process: 62280 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)

sudo systemctl status nginx

Produces:

    nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Wed 2022-06-01 09:41:07 UTC; 2min 5s ago
       Docs: man:nginx(8)
    Process: 66304 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
        CPU: 11ms

Jun 01 09:41:07 flask-server systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 01 09:41:07 flask-server nginx[66304]: nginx: [emerg] unknown directive "/Users/charleserrington/VScode/Flask_Blog/flaskblog" in /etc/nginx/sites-enabled/flaskblog:6
Jun 01 09:41:07 flask-server nginx[66304]: nginx: configuration file /etc/nginx/nginx.conf test failed
Jun 01 09:41:07 flask-server systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
Jun 01 09:41:07 flask-server systemd[1]: nginx.service: Failed with result 'exit-code'.
Jun 01 09:41:07 flask-server systemd[1]: Failed to start A high performance web server and a reverse proxy server.

sudo nano /var/log/nginx/error.log

Produces:

2022/05/31 05:54:02 [emerg] 61717#61717: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to [::]:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to [::]:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to [::]:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to [::]:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: bind() to [::]:80 failed (98: Unknown error)
2022/05/31 05:54:02 [emerg] 61717#61717: still could not bind()
2022/05/31 06:06:31 [emerg] 62174#62174: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:06:31 [emerg] 62174#62174: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:06:31 [emerg] 62174#62174: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:06:31 [emerg] 62174#62174: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:06:31 [emerg] 62174#62174: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:06:31 [emerg] 62174#62174: still could not bind()
2022/05/31 06:10:15 [emerg] 62189#62189: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:10:15 [emerg] 62189#62189: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:10:15 [emerg] 62189#62189: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:10:15 [emerg] 62189#62189: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:10:15 [emerg] 62189#62189: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:10:15 [emerg] 62189#62189: still could not bind()
2022/05/31 06:23:31 [emerg] 62221#62221: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:23:31 [emerg] 62221#62221: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:23:31 [emerg] 62221#62221: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:23:31 [emerg] 62221#62221: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:23:31 [emerg] 62221#62221: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:23:31 [emerg] 62221#62221: still could not bind()
2022/05/31 06:33:20 [emerg] 62280#62280: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:33:20 [emerg] 62280#62280: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:33:20 [emerg] 62280#62280: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:33:20 [emerg] 62280#62280: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:33:20 [emerg] 62280#62280: bind() to 0.0.0.0:80 failed (98: Unknown error)
2022/05/31 06:33:20 [emerg] 62280#62280: still could not bind()
2022/06/01 05:54:41 [emerg] 65391#65391: bind() to 0.0.0.0:80 failed (98: Unknown error)

All errors seem to be unknown, so I'm not really sure where to go from here.

I read many serverfault and stackoverflow posts and it seemed like it was something to do with port 80, I ran many of the commands recommended, including:

netstat -tnlp | grep -w 80
netstat -ano|grep 80|grep LISTEN
sudo netstat -lnp
sudo lsof -i :80
kill -9 65734 (i didn't have permission to run this)
netstat -tulpn
grep -rnw /etc/nginx/ -e '80'

And one of these commands got it working and allowed me to restart nginx and access the website, but I don't know which and now it's broken again, but last time it looked like there was something at port 80, but now when I run checks there is nothing at port 80 it seems so I'm even more lost than before.

EDIT:

As Nick Andriopoulos pointed out the problem seems to be caused by

location /static {
    alias /Users/myname/VScode/Flask_Blog/flaskblog/static;
}

I changed this to

location /static {
    root /Users/myname/VScode/Flask_Blog/flaskblog/;
}

Once I did this I was able to restart nginx and the site now runs, but now nginx is unable to find the static file.

I checked the error log and I see multiple lines of this sort of thing:

   2022/06/03 06:09:42 [error] 71064#71064: *1 open() "/Users/myname/VScode/Flask_Blog/flaskblog/static/main.css" failed (2: No such file or directory), client: 217.146.83.247, server: IP, request: "GET /static/main.css HTTP/1.1", host: "IP", referrer: "http://IP/"
2022/06/03 06:09:42 [error] 71064#71064: *1 open() "/Users/myname/VScode/Flask_Blog/flaskblog/static/profile_pics/271fcf0ffe2a92aa.jpg" failed (2: No such file or directory), client: 217.146.83.247, server: IP, request: "GET /static/profile_pics/271fcf0ffe2a92aa.jpg HTTP/1.1", host: "IP", referrer: "http://IP/"
2022/06/03 06:09:42 [error] 71064#71064: *4 open() "/Users/myname/VScode/Flask_Blog/flaskblog/static/profile_pics/default.jpg" failed (2: No such file or directory), client: 217.146.83.247, server: IP, request: "GET /static/profile_pics/default.jpg HTTP/1.1", host: "IP", referrer: "http://IP/"
2022/06/03 06:09:45 [error] 71064#71064: *4 open() "/Users/myname/VScode/Flask_Blog/flaskblog/static/main.css" failed (2: No such file or directory), client: 217.146.83.247, server: IP, request: "GET /static/main.css HTTP/1.1", host: "IP", referrer: "http://IP/home"

So nginx is unable to find/access the path I have set.

It is the right path though, if I do "Go to Folder" on my Mac and paste in in all of the files and folders are there. Thank you for any help.

Score:0
in flag

From your output, it seems that the issue is related to your custom configuration:

sudo systemctl status nginx:

Jun 01 09:41:07 flask-server nginx[66304]: nginx: [emerg] unknown directive "/Users/charleserrington/VScode/Flask_Blog/flaskblog" in /etc/nginx/sites-enabled/flaskblog:6
Jun 01 09:41:07 flask-server nginx[66304]: nginx: configuration file /etc/nginx/nginx.conf test failed
Jun 01 09:41:07 flask-server systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE

These errors mean that the line

    location /static {
        alias /Users/myname/VScode/Flask_Blog/flaskblog/static;
    }

Is most likely responsible for the server not starting. The bind logs come after that.

Please confirm that the above path ( /Users/myname/VScode/Flask_Blog/flaskblog/static exists and is accessible by the nginx process.

by flag
I think you are right. Thank you. I have edited the bottom of the question above because the formatting in the comment section is horrible. Could you please check what I have written below EDIT:?
Score:-1
in flag

I think the port 80 is running under an apache web server. You can't able to run two web servers at one time.

First, you check that port 80 is already running,

sudo netstat -plunt | grep 80

tcp6   0   0    :::80    :::*   LISTEN      1573456/apache2

If it is running, then disable the apache:

sudo service apache2 stop

Now start the Nginx:

sudo service nginx start
in flag
The output of that command is already in the op and shows that nothing is listening on that port.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.