简体   繁体   中英

deploying flask-restful app using apache mod_wsgi

I'm trying to create a flask-restful based app, but not able to host it properly. Here is my virtual host file FlaskApp.conf :

<VirtualHost *:80>
ServerName flaskapp

WSGIScriptAlias / /var/www/flaskapp/myopinion.wsgi

<Directory /var/www/flaskapp/flaskapp>
    WSGIProcessGroup myopinion
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

and my wsgi file:

#!/usr/bin/python

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/flaskapp/")
from server import app as application

Server is my python module stored in /var/www/flaskapp/flaskapp directory.

My virtual host file is located at /etc/apache2/sites_available/ and my .wsgi file is located in /var/www/flaskapp directory.

When I'm trying to access my urls from server.py file it is giving error code 404 Not found: requested url /ques/ was not found on this server .

I'm in desperate need of help here, I'm stuck with this problem since past two weeks. Please help me out here.

You might have a problem with your virtual host file. Try this:

<VirtualHost *:80>
ServerName flaskapp

WSGIScriptAlias / /var/www/flaskapp/myopinion.wsgi
WSGIProcessGroup myopinion

<Directory /var/www/flaskapp/flaskapp/>
    Order deny,allow
    Allow from all
</Directory>

Your wsgi file is also not correct. It should be:

#!/usr/bin/python

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/flaskapp/")

from flaskapp import server as application

In the wsgi file, you put the directory name after from , the object name after import . Aliasing it as application is not necessary, but a common practice.

I used a similar apache-flask redirection for my flask application. You can take a look at the tutorial which i followed.

Link : https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps

The mod_wsgi setup worked for me using the above link. I used it to redirect Flask port:5000 to Port 80 on apache2. It is possible to install and enable mod_wsgi using the following commands in a shell:

sudo apt-get install libapache2-mod-wsgi python-dev
sudo a2enmod wsgi

Once the installation is done and everything is configured correctly, Apache has to be restarted using the following command:

sudo service apache2 restart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM