Report this

What is the reason for this report?

Setting up a sub-domain to point to a flask application on same droplet (with uwsgi and nginx on Ubuntu 15.10)

Posted on January 17, 2016

I have a domain called abc.com which points to a static html page, in /var/www/abc.com/public_html.

I want to host a Flask application on same droplet (different port maybe?), which is accessible using a subdomain demo.abc.com.

I followed instructions here, and here, but they seem not to be working for me right now.

Here is a how my Zone file looks like currently -

$ORIGIN abc.com.
$TTL 1800
abc.com. IN SOA ns1.digitalocean.com. hostmaster.abc.com. 1451918078 10800 3600 604800 1800
abc.com. 1800 IN NS ns1.digitalocean.com.
abc.com. 1800 IN NS ns2.digitalocean.com.
abc.com. 1800 IN NS ns3.digitalocean.com.
abc.com. 1800 IN A 111.222.333.44
*.abc.com. 1800 IN CNAME abc.com.

I have created an app.py in /var/www/demo.abc.com/ which has

from flask import Flask
application = Flask(__name__)

@application.route("/")
def hello():
    return 'Hello World!'

if __name__ == "__main__":
    application.run(host='0.0.0.0')

I have 2 files in /etc/nginx/sites-available abc.com demo.abc.com

Here is what demo.abc.com looks like:

server {
    listen 80;
    server_tokens off;
    server_name demo.abc.com;

     location / {
         include uwsgi_params;
         uwsgi_pass unix:/tmp/abc.sock;
     }

     location /static {
         alias /var/www/demo.abc.com/static;
     }

     ## Only requests to our Host are allowed
     if ($host !~ ^(demo.abc.com.com|www.demo.abc.com)$ ) {
        return 444;
     }
}

Here is what /etc/uwsgi/apps-available/demo.abc.com.ini looks like

[uwsgi]
vhost = true
socket = /tmp/demo.sock
venv = /var/www/demo.abc.com/.env
chdir = /var/www/demo.abc.com
module = app
callable = application

master = true
processes = 5

chmod-socket = 660
vacuum = true

die-on-term = true

abc.com works fine, while demo.abc.com is not running. I don’t get any error while starting service nginx and uwsgi. I am however beginner in both of them. Please guide me on what I am doing wrong



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

When finding problems en debugging them its always usefull to use logs to see what is going on. Define your logs and check what nginx tells you once you access the website :

        error_log /var/log/demo.abc_error.log;
        access_log /var/log/demo.abc_access.log combined;

Heya,

Your configuration appears mostly correct at a glance. However, let’s troubleshoot step by step:

  1. DNS Settings:

    • Ensure your subdomain demo.abc.com is correctly pointing to your DigitalOcean droplet’s IP address. This seems correct as per the zone file you provided, but you can double-check using ping demo.abc.com and see if it resolves to the correct IP.
  2. Nginx Configuration:

  • Ensure the configuration for demo.abc.com is symlinked to the sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/demo.abc.com /etc/nginx/sites-enabled/
  • You’ve mentioned demo.abc.com.com in the server block (if condition). This seems like a typo. It should be demo.abc.com.
  1. uWSGI Configuration:

    • Ensure the uWSGI app configuration is also symlinked to the apps-enabled directory:
sudo ln -s /etc/uwsgi/apps-available/demo.abc.com.ini /etc/uwsgi/apps-enabled/
  • Ensure your Flask app has the necessary permissions to be executed by the user running the uWSGI process. The socket file should also be accessible by the Nginx user (usually www-data).
  1. Flask App:
  • Check if the Flask app runs correctly without uWSGI:
cd /var/www/demo.abc.com/
source .env/bin/activate
python app.py

If it runs correctly, you should be able to access the Flask app at http://demo.abc.com:5000. 5. Logs:

  • Check Nginx error logs for any issues related to the demo.abc.com domain:
sudo tail -f /var/log/nginx/error.log

Check uWSGI logs for any issues related to the Flask app:

sudo tail -f /var/log/uwsgi/app/demo.abc.com.log
  1. Restart Services:
  • After making changes, ensure you restart both Nginx and uWSGI:
sudo service nginx restart
sudo service uwsgi restart
  1. Firewall:
  • Ensure your firewall isn’t blocking the incoming requests. If you’re using ufw on Debian:
sudo ufw allow http
sudo ufw allow https

Try the above steps one by one and see if you can pinpoint the issue. The log files, in particular, will be very helpful in diagnosing the problem.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.