Execute python3 system with PHP exec () on AWS EC2

Using Python 3 series on Amazon Linux I solved it by the method of this blog, but I will supplement and introduce the part that I thought was a little insufficient for myself.

Main premise: Install Pyenv in /usr/bin/ ..

In other blogs, I think that pyenv is installed in/home/ec2-user/..., but since the ec2-user directory has strict privileges, install python3 in it, for example. , Exec (/home/ec2-user/.../bin/python3.7)

permission denied

Will be. However, if you rewrite the permissions of the home directory and ec2-user directory, is it a security specification? Or something, the horrifying thing that scp or ssh can't get into the server happens. (If that happens, you can fix it by opening a terminal from the AWS ec2 instance page.)

1. Install the libraries required to use pyenv

$ sudo yum install gcc gcc-c++ make git openssl-devel bzip2-devel zlib-devel readline-devel sqlite-devel

2. Install pyenv, set permissions and paths

$ sudo git clone https://github.com/yyuu/pyenv.git /usr/bin/.pyenv
$ cd /usr/bin/.pyenv
$ sudo mkdir shims
$ sudo mkdir versions

3. Change the permissions of the installed folder so that it can be accessed from exec.

$ sudo chown -R ec2-user:apache /usr/bin/.pyenv

#Or if apache is not installed
$ sudo chown -R ec2-user:ec2-user /usr/bin/.pyenv

4. Let's set the path

There are other blogs that have detailed information on setting the path, so please check that out. Use the vim editor to open .bashrc.

vi ~/.bashrc

Add this.

export PYENV_ROOT="/usr/bin/.pyenv"
if [ -d "${PYENV_ROOT}" ]; then
    export PATH=${PYENV_ROOT}/bin:$PATH
    eval "$(pyenv init -)"
fi

Reload in the rewritten state.

$ source ~/.bashrc

5. Install python with pyenv normally

This time I will install python3.7.0 properly. Even if you install it, it remains python on the existing system

$ pyenv install 3.7.0
$ python -V
Python 2.7.12

In this way, the mode is changed to python 3.7.0.

$ pyenv global 3.7.0
$ python -V
Python 3.7.0

6. Run python3 in php.

If you go through the location where you just installed it, you will find the location where python3.7 is installed. 「/usr/bin/.pyenv/versions/3.7.0/bin/python3.7」 __ With this, you can use python3.7. __ The following two are the same, just looking the same through the __path. __

$ python
$ /usr/bin/.pyenv/versions/3.7.0/bin/python3.7

You can now use python3.7.0 with php. As a test, let's use exec () like this. You can use python3.7 properly.

$command = "/usr/bin/.pyenv/versions/3.7.0/bin/python3.7 -V";
exec($command, $output, $return_var);
$json_array = json_encode($output);
echo $json_array

Recommended Posts

Execute python3 system with PHP exec () on AWS EC2
Building an environment to execute python programs on AWS EC2
[2020 version] How to install Python3 on AWS EC2
Build python environment with pyenv on EC2 (ubuntu)
Python development on Ubuntu on AWS EC2 (using JupyterLab)
Periodically execute Python Script on AWS Data Pipeline
AWS CDK with Python
# 2 Build a Python environment on AWS EC2 instance (ubuntu18.04)
Deploy Python3 function with Serverless Framework on AWS Lambda
I want to AWS Lambda with Python on Mac!
Python --Install MySQLDB on EC2
Presentation Support System with Python3
[AWS] Install node.js on EC2 instance and execute sample program
Install AWS SDK for PHP on AWS EC2 (PHP7.2 + Apache2.4.41 + OPCashe + Composer)
Easily build network infrastructure and EC2 with AWS CDK Python
exec, eval to execute [python] statement
Dynamic proxy with python, ruby, PHP
Text extraction with AWS Textract (Python3.6)
# 3 Build a Python (Django) environment on AWS EC2 instance (ubuntu18.04) part2
Things to note when running Python on EC2 from AWS Lambda
Run Python on Schedule on AWS Lambda
Japanese file enumeration with Python2 system on Windows (5C problem countermeasure)
Make a recommender system with python
Notes on using rstrip with python.
Notify HipChat with AWS Lambda (Python)
Getting started with Python 3.8 on Windows
Handling regular expressions with PHP / Python
Memorandum of understanding when Python is run on EC2 with Apache
Send image with python, save with php
[Memo] Tweet on twitter with python
I easily built an operating environment for Python3 + Tornado on AWS EC2.
Modules cannot be imported in Python on EC2 run from AWS Lambda
Run a batch of Python 2.7 with nohup on Amazon Linux AMI on EC2
Prepare the environment of Chainer on EC2 spot instance with AWS Lambda
# 1 Until you deploy Django's web application (instance construction with EC2 on AWS)
System trading starting with Python3: long-term investment
[AWS] Using ini files with Lambda [Python]
Run servo with Python on ESP32 (Windows)
Execute Python code on C ++ (using Boost.Python)
Realize PHP / Python generator with Golang / Ruby
Execute Python script with cron of TS-220
"System trade starting with Python3" reading memo
A memo with Python2.7 and Python3 on CentOS
Map rent information on a map with python
Follow active applications on Mac with Python
Notes on doing Japanese OCR with Python
scipy stumbles with pip install on python 2.7.8
I want to play with aws with python
Download files on the web with Python
Build Python environment with Anaconda on Mac
[Python] Run Headless Chrome on AWS Lambda
Connect to s3 with AWS Lambda Python
Installing PIL with Python 3.x on macOS
Install Python 3.8, Pip 3.8 on EC2 (Amazon Linux 2)
Touch AWS with Serverless Framework and Python
Python + Selenium + Headless Chromium with aws lambda
Working with GPS on Raspberry Pi 3 Python
Getting started with Python with 100 knocks on language processing
Periodically run a python program on AWS Lambda
Try Tensorflow with a GPU instance on AWS
Build a python environment with ansible on centos6