I wanted to create a Flask environment on the Sakura rental server (standard), but since sudo cannot be used and the default shell is cshell, I had to make some changes from the reference article on the web, so I had a difficult memorandum. (For beginners)
--0. Confirmation of environment ―― 1. Build a python environment using pyenv ―― 2. Install flask using pip in the built python environment ―― 3. Set cgi and check the operation of Flask app.
The user directory on the Sakura rental server is / home /
python
$ cd /home/<username>/
$ cd $HOME
$ cd ~/
$ cd /home/fififactory/
After this, we will put the configuration under the user directory.
Check the location and version of Python installed by default
python
$ which python
/usr/local/bin/python
$ python -V
2.7.3
Checking the shell
python
$ echo $SHELL
csh
Here, bash and zsh people are outside the scope of this article.
Since git can be used on the Sakura rental server, I will download it.
After practicing, I will write commands with a shell script. (It will be useful when you look back at it later. You can execute it by hitting the command directly, so I think that is fine for those who are troublesome)
python
#file name: pyenv-download.sh
#!/bin/csh <-It is clearly stated that it will work with cshell.
git clone https://github.com/yyuu/pyenv.git ~/.pyenv
This command tells you to use git to save the pyenv repository in the ~ / .pyenv folder from https://github.com/yyuu/pyenv.git.
Then save the shell script (pyenv-download.sh) in your user directory and run the shell.
python
#Add execute permission.
$ chmod 755 pyenv-download.sh
#Run shell script
$ ~/pyenv-download.sh
Now that you have downloaded pyenv, it's time to install it.
.cshrc
python
# $FreeBSD: src/share/skel/dot.cshrc,v 1.14.6.1 2008/11/25 02:59:29 kensmith Exp $
#
# .cshrc - csh resource script, read at beginning of execution by each shell
#
# see also csh(1), environ(7).
#
.....
(Enter at the end)
# Setting for pyenv
# *****************
if ( -e $HOME/.pyenv/bin/pyenv ) then
echo '>> Exist pyenv'
#Set the root path of pyenv
setenv PYENV_ROOT $HOME/.pyenv
#Add pyenv directory to PATH
setenv PATH $PYENV_ROOT/shims:$PYENV_ROOT/bin:$PATH
#The default temporary directory~/Change to tmp
setenv TMPDIR $HOME/tmp
#Restart pyenv
pyenv rehash
else
echo '>> NO INSTALL pyenv'
endif
This command uses an if statement to condition if $ HOME / .pyenv / bin / pyenv exists. And when pyenv exists, it goes through the directory and path settings. (TMDIR may not need to be included in the if statement.)
I rewrote the shell, so let me read it again.
python
$ source $HOME/.cshrc
Now, let's build a python environment using pyenv.
First, download the version of python to use. The command is pyenv install.
python
$ pyenv install --list #Check the installation list
$ pyenv install 2.7.9 # python2.7.Install 9
However, this is not yet usable. Only that version of python is prepared in the pyenv directory.
Since pyenv manages the environment for each directory, create a directory in an appropriate location.
python
#Create a directory for the virtual environment(Location is arbitrary)
$ mkdir $HOME/tmp/python
$ cd $HOME/tmp/python
Check the path and version of python at that time.
python
$ which python #Check the current python
/usr/local/bin/python
$ pyton -V
2.7.3
It should come out for the above. Make sure you still keep Sakura's defaults.
python
$ pyenv local 2.7.9 #Build local python
$ pyenv local #Check if it is set
python
$ whitch python
/home/fififactory/.pyenv/shims/python
$ python -V
2.7.9
Now you can see that python is specified in / shims / python in pyenv. This is what I put in the path in .cshrc. So it's stuck if the .cshrc path setting isn't working. (I got stuck)
Python environment construction completed
Now that you can use pip thanks to pyenv, let's put flask in the local python environment.
First, create an environment for Flask with virtualenv.
python
$ git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
$ pyenv virtualenv 3.4.2 3.4.2-flask
As an environment based on python version 3.4.2, I created a virtual environment named 3.4.2-flask (name is arbitrary).
Install Flask with pip. As before, move to the directory where you want to set the environment and type the following command.
python
$ pyenv local 3.4.2-flask
$ pip install Flask
If you have Flask in your pip list, you're done.
Prepare a directory for access test under the www directory of the user directory.
python
mkdir ~/www/flask-cgi-test
mkdir ~/www/flask-cgi-test/hello
cd ~/www/flask-cgi-test/hello
Prepare three files under the / hello / directory.
(1) .htaccess
python
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /flask-cgi-test/hello/index.cgi/$1 [QSA,L]
(2) appFlask.py
python
# coding: utf-8
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "Hello!"
if __name__ == '__main__':
app.run()
(3) index.cgi
python
#!/home/fififactory/.pyenv/versions/3.4.2-flask/bin/python
import cgitb
cgitb.enable()
from wsgiref.handlers import CGIHandler
from appFlask import app
CGIHandler().run(app)
Access http: //<username>.sakura.ne.jp/flask-cgi-test/hello
If hello appears, it is successful.
When I tried to make a web application, I was enthusiastically registering with Sakura and trying to run Flask, but unlike the local environment, I could not use sudo or install it without administrator privileges. It took about 4 days just to move this article. The thing I was most addicted to was that I didn't really understand the shell script and it was too late to notice that the default shell was chs. This is a memorandum for beginners, but I posted it with the hope that it would be of some help to those who are also addicted to building an environment. If you make a mistake, please comment.
Thank you for visiting.
[.cshrc --csh, tcsh settings](http://technique.sonots.com/?UNIX%2F%E8%A8%AD%E5%AE%9A%E3%80%81%E8%A8%AD% E5% AE% 9A% E3% 83% 95% E3% 82% A1% E3% 82% A4% E3% 83% AB% 2F.cshrc)
Use pyenv and Flask on Sakura rental server
Recommended Posts