我试图让两个应用程序可以在我的 LAN 服务器(树莓派)上访问。
- lab_app:这是一个非常简单的应用程序,由第三方的 Flask 制作,使用 sqlite3 数据库,结果是我的应用程序文件夹中的一个文件。它位于path
/var/www/lab_app/
,运行在内部端口8080,外部端口80。
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)
- aqi_luftdaten:一个更复杂的应用程序,由我用 django 制作,使用 postgresql 数据库。它位于路径
/var/www/aqi_luftdaten/
并在内部端口 8000 上运行 - 我已通过命令手动启动它:
python manage.py runserver 0.0.0.0:8000
目前,只有lab_app
:
它的Web服务器是nginx,应用程序服务器是uWSGI。
nginx是这样配置的:
有一个配置文件/var/www/lab_app/lab_app_nginx.conf
,由目录中的符号链接指向/etc/nginx/conf.d
ln -s /var/www/lab_app/lab_app_nginx.conf /etc/nginx/conf.d/
其内容为
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;
}
}
通过遵循教程,我删除了默认的启用 nginx 站点的配置文件,以便该目录/etc/nginx/sites-enabled
无效。
目录内/etc/nginx/sites-available/
只有默认文件/etc/nginx/sites-available/default
。
其内容为
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是这样配置的:
有一个文件/var/www/lab_app/lab_app_uwsgi.ini
内容为:
[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
我已经使用命令将其信息加载到uWSGI
bin/uwsgi --ini /var/www/lab_app/lab_app_uwsgi.ini
然后有一个/etc/systemd/system/emperor.uwsgi.service
包含内容的文件
[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
这个应用程序在我的 LAN 上运行良好,并且可以从外部设备浏览器访问我的服务器 IP。
现在我想aqi_luftdaten
在外部端口 80 上公开应用程序。
所以我想做的第一件事就是为这个应用程序配置 nginx。
所以我创建了一个 nginx 配置文件/var/www/aqi_luftdaten/nginx/aqi_luftdaten_nginx.conf
,其中包含内容
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;
}
}
/etc/nginx/conf.d/
并将其通过符号链接到目录
ln -s /var/www/aqi_luftdaten/nginx/aqi_luftdaten_nginx.conf /etc/nginx/conf.d/
然后重启nginx
/etc/init.d/nginx restart
但我收到此错误:
[....] 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!
通过跑步systemctl status nginx.service
我得到
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.
所以看来端口 8000 已经在使用中,但我不明白为什么,因为其他应用程序在端口 8080:80 上公开