Issue
Is is possible to run flask rest api in apache + ubuntu server where already PHP website is running with that domain??
in short: if I browse url www.example.com then normal php website should load. if I call www.example.com/services/api then flask api should give result....
Updated => Answer Finally I found a solution, posting here if it can help someone... (Ubuntu Server)
-->with root (Assuming Python and Pip already installed)
sudo apt-get install libapache2-mod-wsgi-py3 python3-dev
sudo apt-get install python3-pip
pip3 install virtualenv
-->with domain user navigate to folder where you want to create virtual environment... then type below command (you can change virtual environment name according to your need)
python3 -m virtualenv myenv
<-(any name)- activate environment with command
source myenv/bin/activate
- install flask with commend
pip install flask
- create file below data-->
flaskapp.wsgi
in project folder or anywhere you want... then following code
import logging
import sys
activate_this = '/your/envfolder/path/bin/activate_this.py'
exec(compile(open(activate_this).read(), activate_this, 'exec'), dict(__file__=activate_this))
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '/your/app/folder/path/where/you/can/import/app')
from App import app as application
#application.secret_key = 'anything-you-wish'
Finally with etc/apache2/sites_available
and your sitename.conf
add below code...
WSGIScriptAlias /your/wsgi/folderpath/flaskapp.wsgi
<Directory /your/wsgi/folderpath/>
# set permissions as per apache2.conf file
allow from all
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Note: This is how I made it to work... I am not a professional server developer, I am not sure if this is a right way to do it...
Solution
That is possible. You will need to specify a certain route for each app.
For example,
foo.com/ -> PHP foo.com/api -> Flask App
A sample apache config would be something like this:
<VirtualHost *:80>
ServerName foo.com
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
ProxyPass /api/ http://127.0.0.1:8081/
ProxyPassReverse /api/ http://127.0.0.1:8081/
</VirtualHost>
Answered By - Brian
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.