I tried to use flask to set up an API server on the rented Sakura rental server (standard plan). (** Note that the Sakura rental server does not allow resident daemon processes **) There were some points that were clogged, so I will also record how to deal with them.
--Preferences are set according to the following page.
Try # 019 – I tried to build an address search API using Flask on Sakura's rental server
--The application to be created follows the following page.
-Create three files under / home / (UserName) / www / hello: .htaccess
, ʻapp.py, ʻindex.cgi
--Access https: // (UserName) .sakura.ne.jp/hello/
from chrome
.htaccess
: One of Apache's control configuration files. Since httpd.conf
, which normally controls Apache, controls the entire WEB server, only the server administrator can change it. If you want to change the settings for yourself on the Sakura rental server, change .htaccess
.
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /hello/index.cgi/$1 [QSA,L]
<Files ~ "\.py$">
deny from all
</Files>
app.py
#!/home/miyablo/.pyenv/versions/flask_peewee_3.6.4/bin/python
# -*- coding: utf-8 -*-
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "Hello World!\n"
if __name__ == '__main__':
app.run()
index.cgi
#!/home/miyablo/.pyenv/versions/flask_peewee_3.6.4/bin/python
import cgitb
cgitb.enable()
from wsgiref.handlers import CGIHandler
from app import app
CGIHandler().run(app)
When accessing https: // (UserName) .sakura.ne.jp/hello/
from chrome, the following display is displayed.
When I try to execute ʻindex.cgi directly with the
$ python index.cgi` command on the command line of the Sakura server connected to ssh, the following error occurs.
This error was the same as the one on the following page, so edit the ʻindex.cgi` file as described on the following page.
Leave a problem trying to use Flask on Sakura's rental server.
index.cgi
#!/home/miyablo/.pyenv/versions/flask_peewee_3.6.4/bin/python
import cgitb
cgitb.enable()
from wsgiref.handlers import CGIHandler
from app import app
#-------------------Below, the rewritten part------------------------
from sys import path
path.insert(0, '/miyablo/www/hello/')
from app import app
class ProxyFix(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
#* Rewrite required
environ['SERVER_NAME'] = "miyablo.sakura.ne.jp"
environ['SERVER_PORT'] = "80"
environ['REQUEST_METHOD'] = "GET"
environ['SCRIPT_NAME'] = ""
environ['PATH_INFO'] = "/"
environ['QUERY_STRING'] = ""
environ['SERVER_PROTOCOL'] = "HTTP/1.1"
return self.app(environ, start_response)
if __name__ == '__main__':
app.wsgi_app = ProxyFix(app.wsgi_app)
CGIHandler().run(app)
This time I was able to run it without error.
However, an internal server error still occurs when accessing from chrome.
When you execute $ ls -la
, it is currently as follows.
Here, execute permission is given as chmod 744 index.cgi
.
By doing this, I was able to confirm the hello world even from chrome!
By the way, as a result of trying various things wondering how much authority should be, at least
--Owner read permission in .htaccess --Owner read / execute permission in index.cgi
It was found that if there was, the reaction would return normally.
It's strange that app.py works even if it doesn't have any permissions.
Try # 019 – I tried to build an address search API using Flask on Sakura's rental server I'm leaving a problem when trying to use Flask on Sakura's rental server. chmod? chown? File permission system summary for those who do not understand well
Recommended Posts