[Sakura rental server] Try using flask.

Introduction

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.

[Sakura rental server](for beginners) How to build an environment for Python, pyenv, and Flask. | For csh

procedure

-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.

cman.jp: What is 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)

The error that occurred and its response

When accessing from chrome

When accessing https: // (UserName) .sakura.ne.jp/hello/ from chrome, the following display is displayed. スクリーンショット 2019-11-02 17.07.59.png

When executing files directly

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. スクリーンショット 2019-11-02 17.09.49.png

Rewriting index.cgi

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)

Direct execution of index.cgi

This time I was able to run it without error. スクリーンショット 2019-11-02 17.22.19.png

However, an internal server error still occurs when accessing from chrome.

Rewriting file access privileges

When you execute $ ls -la, it is currently as follows. スクリーンショット 2019-11-02 17.27.06.png

Here, execute permission is given as chmod 744 index.cgi. スクリーンショット 2019-11-02 17.28.48.png

By doing this, I was able to confirm the hello world even from chrome!

スクリーンショット 2019-11-02 17.29.39.png

To conclude

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. スクリーンショット 2019-11-02 17.36.30.png

It's strange that app.py works even if it doesn't have any permissions.

References

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

[Sakura rental server] Try using flask.
Try using the web application framework Flask
Try using Tkinter
Try using docker-py
Try using cookiecutter
Try using PDFMiner
Try using geopandas
Try using scipy
Try using pandas.DataFrame
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try using PyODE
Try to separate Controllers using Blueprint in Flask
Try to create an HTTP server using Node.js
Try using virtualenv (virtualenvwrapper)
[Azure] Try using Azure Functions
Try using virtualenv now
Try using W & B
Try using Django templates.html
[Kaggle] Try using LGBM
Try using Python's feedparser.
Try using Python's Tkinter
[Python] I tried running a local server using flask
Try using Tweepy [Python2.7]
Try using Pytorch's collate_fn
[Sakura Rental Server] (For beginners) How to build an environment for Python, pyenv, and Flask. | For csh
Try using PythonTex with Texpad.
[Python] Try using Tkinter's canvas
Try using Jupyter's Docker image
Try function optimization using Hyperopt
Try using matplotlib with PyCharm
Try using Azure Logic Apps
Try using Kubernetes Client -Python-
[Kaggle] Try using xg boost
Try using the Twitter API
Try the Taxii server (1. Server settings)
Try using AWS SageMaker Studio
Try tweeting automatically using Selenium.
Try using SQLAlchemy + MySQL (Part 1)
Try using the Twitter API
Try using SQLAlchemy + MySQL (Part 2)
Try using Django's template feature
Try using the PeeringDB 2.0 API
Try using Pelican's draft feature
Try using pytest-Overview and Samples-
Try using folium with anaconda