Hello,
Problem: I cannot configure Nginx to serve my Flask app on a subdomain.
Let me explain the issue.
I have an OVH basic VPS. I can access it via SSH using the CLI, and I use Ubuntu 22.04. I have a working Yunohost installation. I installed some apps like JupyterLab, Yellow, YesWiki, etc., on the main domain and subdomains, all with SSL support, and everything works great.
For a personal learning project, I would like to use my VPS to serve a Flask app.
Step 1: Create a Dummy Flask App
I created a virtual environment, activated it, and installed the necessary modules:
First, I made a dummy Flask app:
-
Created a directory for the app:
mkdir /var/www/flask_app
-
Navigated to the directory:
cd /var/www/flask_app
-
Created a virtual environment:
python3 -m venv venv
-
Activated the virtual environment:
source /var/www/flask_app/venv/bin/activate
-
Installed Flask and Gunicorn:
pip install flask gunicorn
I then created a simple Flask app in /var/www/flask_app/app.py
:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
Step 2: Create a Systemd Service File
I created a systemd service file at /etc/systemd/system/flask_app.service
:
[Unit]
Description=Gunicorn instance to serve Flask application
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/flask_app
Environment=“PATH=/var/www/flask_app/venv/bin”
ExecStart=/var/www/flask_app/venv/bin/gunicorn --workers 3 --bind unix:/var/www/flask_app/flask_app.sock -m 007 app:app
[Install]
WantedBy=multi-user.target
I then started and enabled the Gunicorn service with the following commands:
sudo systemctl daemon-reload
sudo systemctl restart flask_app
sudo systemctl enable flask_app
I checked the status of the service with sudo systemctl status flask_app
, and everything worked great. The service is up and running. The Flask app responds locally as the command curl --unix-socket /var/www/flask_app/flask_app.sock http://localhost/
returns a nice “Hello, World!” response.
Step 3: Configure Nginx
I want this “Hello, World!” message to display in my Firefox browser on any distant computer.
I created a subdomain testraw.rogues.fr
through the online Yunohost administration panel. I own the domain rogues.fr
at OVH registrar. I ran YHdiagnostic, almost everything is green (just usual email issues), and then I set up an SSL certificate. DNS records are created with the automatic OVH binding system. Everything works fine.
I did not install any application on the subdomain.
In /etc/nginx/conf.d
, there is an empty testraw.rogues.fr.d
directory and a testraw.rogues.fr.conf
file.
That’s where the issue begins. I do not know much about Nginx configuration files.
Problem with Nginx Configuration
In Firefox, https://testraw.rogues.fr
keeps redirecting to the rogues.fr
Yunohost login page.
I have tried a million different configuration files with the help of ChatGPT. I have another subdomain, datascientist.rogues.fr
. I fed ChatGPT with the datascientist.rogues.fr.conf
file and /datascientist.rogues.fr.d/yellow__2.conf
file for it to get inspiration from, but it doesn’t work.
I spent hours on this, and I am desperate. I haven’t posted on a forum since 1997 asking for support to download MP3 songs with Napster. So, I hope that the format of my question is okay for forums. I am a French native, and I hope my English is understandable.
I would appreciate any clues or help. Thanks in advance!