0

I am trying to make two apps be accessible on my LAN server (raspberry pi).

  • lab_app: this one is a very simple app, made with flask from a third party, using a sqlite3 database, which results to be a file inside my app folder. It is located at path /var/www/lab_app/ and runs on internal port 8080, external port 80.

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080)
  • aqi_luftdaten: a more complex app, made with django by me, using postgresql database. It is located at path /var/www/aqi_luftdaten/ and runs on internal port 8000 - I have manually started it via command:

python manage.py runserver 0.0.0.0:8000

For the moment, only lab_app is working and accessible:

Its web server is nginx and its application server is uWSGI.

nginx is configured in this way:

there is a configuration file /var/www/lab_app/lab_app_nginx.conf which is pointed by a simbolic link from directory /etc/nginx/conf.d

ln -s /var/www/lab_app/lab_app_nginx.conf /etc/nginx/conf.d/

Its content is

server {
    listen      80;
    server_name localhost;
    charset     utf-8;
    client_max_body_size 75M;

    location /static {
        root /var/www/lab_app/;
    }

    location / { try_files $uri @labapp; }
    location @labapp {
        include uwsgi_params;
        uwsgi_pass unix:/var/www/lab_app/lab_app_uwsgi.sock;
    }
}

by following a tutorial, I have removed the default nginx sites-enabled config file, so that the directory /etc/nginx/sites-enabled is void.

Inside the directory /etc/nginx/sites-available/ there is only the default file /etc/nginx/sites-available/default.

Its content is

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }

}

uWSGI is configured in this way:

there is a file /var/www/lab_app/lab_app_uwsgi.ini having content:

[uwsgi]
#application's base folder
base = /var/www/lab_app

#python module to import
app = lab_app
module = %(app)

home = %(base)
pythonpath = %(base)

#socket file's location
socket = /var/www/lab_app/%n.sock

#permissions for the socket file
chmod-socket    = 666

#the variable that holds a flask application inside the module imported at line #6
callable = app

#location of log files
logto = /var/log/uwsgi/%n.log

I have loaded its information to uWSGI by using the command

bin/uwsgi --ini /var/www/lab_app/lab_app_uwsgi.ini

and then there is a file /etc/systemd/system/emperor.uwsgi.service having content

[Unit]
Description=uWSGI Emperor
After=syslog.target

[Service]
ExecStart=/var/www/lab_app/bin/uwsgi --ini /var/www/lab_app/lab_app_uwsgi.ini
# Requires systemd version 211 or newer
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

This app runs fine on my LAN and is reachable at my server's ip from an external device browser.

Now I would like to expose app aqi_luftdaten on external port 80.

So the first thing I want to do is to configure nginx for this app.

So I have created an nginx config file /var/www/aqi_luftdaten/nginx/aqi_luftdaten_nginx.conf, having content

server {
    listen      3000;
    server_name localhost;
    charset     utf-8;
    client_max_body_size 75M;

    location /static/ {
        root /var/www/aqi_luftdaten/static/;
    }

    location / { 
    proxy_pass http://localhost:8000;
    }
}

and linked it simbollicaly to directory /etc/nginx/conf.d/ via

ln -s /var/www/aqi_luftdaten/nginx/aqi_luftdaten_nginx.conf /etc/nginx/conf.d/

then restart nginx

/etc/init.d/nginx restart

but I get this error:

[....] Restarting nginx (via systemctl): nginx.serviceJob for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
 failed!

and by running systemctl status nginx.service I get

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 Mon 2024-04-01 12:04:18 BST; 10s ago
     Docs: man:nginx(8)
  Process: 26704 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 26705 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)

Apr 01 12:04:15 Raspberry100 systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 01 12:04:15 Raspberry100 nginx[26705]: nginx: [emerg] bind() to 0.0.0.0:8000 failed (98: Address already in use)
Apr 01 12:04:16 Raspberry100 nginx[26705]: nginx: [emerg] bind() to 0.0.0.0:8000 failed (98: Address already in use)
Apr 01 12:04:16 Raspberry100 nginx[26705]: nginx: [emerg] bind() to 0.0.0.0:8000 failed (98: Address already in use)
Apr 01 12:04:17 Raspberry100 nginx[26705]: nginx: [emerg] bind() to 0.0.0.0:8000 failed (98: Address already in use)
Apr 01 12:04:17 Raspberry100 nginx[26705]: nginx: [emerg] bind() to 0.0.0.0:8000 failed (98: Address already in use)
Apr 01 12:04:18 Raspberry100 nginx[26705]: nginx: [emerg] still could not bind()
Apr 01 12:04:18 Raspberry100 systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
Apr 01 12:04:18 Raspberry100 systemd[1]: nginx.service: Failed with result 'exit-code'.
Apr 01 12:04:18 Raspberry100 systemd[1]: Failed to start A high performance web server and a reverse proxy server.

So it seems that port 8000 is already in use, but I don't understand why , as the other application is exposed on ports 8080:80

1 Answer 1

-1

The problem was actually the fact that the aqi_luftdaten application was manually started and running on port 8000, so nginx found that port already in use.

I fixed the problem by stopping the server running with ctrl+C and then running again

/etc/init.d/nginx restart

and this time I get

[ ok ] Restarting nginx (via systemctl): nginx.service.

If I try to reach the aqi_luftdaten application in the browser of another device http://<raspberrypi_IP>:3000/ I successfully get

<html><head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.14.2</center>
</body>
</html>

as the uWSGI is not configured yet.

2
  • 1
    But none of the nginx config you showed use is attempting to listen on port 8000
    – symcbean
    Commented Apr 1 at 14:13
  • @tms91 remind to accept your answer, else we will be reminded, to answer this question until the end of the universum
    – djdomi
    Commented Apr 5 at 5:33

Not the answer you're looking for? Browse other questions tagged .