2

I've recently got an Ubuntu 18 server and I'm a little confused to say the least.

First: If I run sudo service nginx start or sudo systemctl start nginx, everything appears to work. My site is loading, and both sudo service nginx status and sudo systemctl status nginx return an all clear message.

Now the issue: When I reboot my server, nginx isn't automatically being started as the website is not being loaded. I tried sudo systemmctl enable nginx and tried another reboot. Again, nginx is not running as the website isn't loading.

In fact, if I reboot my server sudo reboot and then sudo service nginx status or sudo systemctl status nginx I get an error message. Despite the error message, I can run sudo service nginx start or sudo systemctl start nginx and it'll all go back to working.

The error message I get is the following:

(venv) user@server:/data/project$ sudo systemctl status nginx
● 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 2019-02-26 17:50:18 CET; 1min 6s ago
     Docs: man:nginx(8)
  Process: 830 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)

Feb 26 17:50:18 md3.tudelft.nl systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 26 17:50:18 md3.tudelft.nl nginx[830]: nginx: [emerg] open() "/data/project/uwsgi_params" failed (2: No such file or directory) in /etc/nginx/sites-enabled/paintingViewer.conf
Feb 26 17:50:18 md3.tudelft.nl nginx[830]: nginx: configuration file /etc/nginx/nginx.conf test failed
Feb 26 17:50:18 md3.tudelft.nl systemd[1]: nginx.service: Control process exited, code=exited status=1
Feb 26 17:50:18 md3.tudelft.nl systemd[1]: nginx.service: Failed with result 'exit-code'.
Feb 26 17:50:18 md3.tudelft.nl systemd[1]: Failed to start A high performance web server and a reverse proxy server.

Well, that seems pretty simple: it can't find the uwsgi_params file. THe uwsgi_params file does exist though, and it is in the right location.

My nginx .conf file:

# the upstream component nginx needs to connect to
upstream django {
     server unix:///data/project/paintingViewer.sock; # for a file socket
    # server 127.0.0.1:8001; # for a web port socket -> works if we use Djano's build in webserver.
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name server;
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /data/project/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /data/project/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /data/project/uwsgi_params; # the uwsgi_params file you installed
    }
}

and the /data/project/uwsgi_params file is:

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

Now, there's a few things I don't understand:

1) How is it possible that nginx works if I run the start command, but that nginx doesn't work on boot?

2) How can the status read that nginx can not find the uwsgi file, while it works on a start?

3) How can I get nginx to run properly on a reboot?

  • 2
    Is `/data` some external filesystem or other mount? – Lenniey Feb 26 '19 at 17:03
  • @Lenniey Oh, wow. Yes,it is. Is it trying to run `nginx` before `/data` is being mounted? – Mitchell van Zuylen Feb 26 '19 at 17:06
  • 1
    Could be, what kind of mount is it? How is it mounted? Using sytemd or manually via script or...? – Lenniey Feb 26 '19 at 17:08
  • @Lenniey I honestly don't know, and I don't know how to check. IT delivers them mounted like that. I'll contact them for the specifics ASAP but that'll take a day at least. – Mitchell van Zuylen Feb 26 '19 at 17:10
  • 2
    You could try `systemctl edit nginx.service` and add `[Unit] RequiresMountsFor=/data`. – Lenniey Feb 26 '19 at 17:11
  • @Lenniey Doing it without `sudo` tells `Failed to create directories for "/etc/systemd/system/nginx.service.d/override.conf": Permission denied`. Running with `sudo` returns an empty file. Is that correct, or should there be a file here? – Mitchell van Zuylen Feb 26 '19 at 17:13
  • 1
    You're changing a systemd service, so sudo / root is necessary. The empty file is correct, as aoyu don't have any options atm. There should be a newline after `[Unit]`...is there any way to add a newline in a comment? – Lenniey Feb 26 '19 at 17:15
  • @Lenniey That did it! Thanks a lot, you've been super helpful. If you create and answer I'll accept it. – Mitchell van Zuylen Feb 26 '19 at 17:18
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/90269/discussion-between-lenniey-and-mitchell-van-zuylen). – Lenniey Feb 26 '19 at 17:57

1 Answers1

8

Nginx can't find the file because the mount / external filesystem is not ready at startup after boot. To tell nginx to wait for the mount, use systemctl edit nginx.service and add the following lines:

[Unit]
RequiresMountsFor=<mountpoint>

or in you specific case:

[Unit]
RequiresMountsFor=/data

nginx will now always wait for /data to be ready before starting.

Lenniey
  • 5,220
  • 2
  • 18
  • 29