[PYTHON] CentOS 7: Enable pyenv with Apache (httpd) CGI

This is the first post. I'm a beginner in programming. I think that there are many points that cannot be reached, so we look forward to your suggestions.

1. Purpose

The subject of this article is an error and its solution that I encountered while configuring the CentOS 7 Apache (httpd) CGI to use pyenv in a virtual environment. Build a web server that can run Python for each virtual environment. (Section 5 describes a series of steps, such as installing Apache and pyenv)

2. Operating environment

・ CentOS 7

$ cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
$ uname -r
3.10.0-514.26.2.el7.x86_64

-Apache (httpd) ・ Pyenv ・ SELinux is disabled

3. Motivation

I wanted to create a web server that could run Python, which is getting a lot of attention in machine learning. Since we want to use Anaconda and Tensorflow, which are indispensable for machine learning, a server that runs in a virtual environment is desirable.   When I installed pyenv and httpd and ran the test .py file on the server, I got an error and I couldn't find a direct solution anywhere, so as a reminder, I'm going to use pyenv on CentOS. I will acknowledge it so that it will be helpful for those who want to make the server used.

4. Errors and solutions

4.1. The error that occurred

pyenv as root user

# git clone git://github.com/yyuu/pyenv.git ~/.pyenv

I installed it with. (Of course, pyenv-virtualenv.) Sample.py file with Shebang specified as follows in / var / www / cgi-bin /

/var/www/cgi-bin/sample.py


#!/root/.pyenv/versions/{VIRTUAL_ENV_NAME}/bin/python

Place and

# chmod 755 sample.py

I changed the permissions in, but the browser page displayed at http://127.0.0.1/cgi-bin/sample.py (127.0.0.1 is the IP address when browsing with the browser of the local machine) It was an Internal Server Error. The following is the content:

500 Internal Server Error Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at root@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Error log

# tail /var/log/httpd/error_log

According to

/var/log/httpd/error_log


[Fri Sep 15 18:39:12.890589 2017] [cgi:error] [pid 1302] [client 192.168.10.2:60025] AH01215: (13)Permission denied: exec of '/var/www/cgi-bin/sample.py' failed
[Fri Sep 15 18:39:12.890725 2017] [cgi:error] [pid 1302] [client 192.168.10.2:60025] End of script output before headers: sample.py

(The date and time, client IP address, and port number are edited)

Shebang worked with the default Python 2.7 Path, #! / Usr / local / bin / python. There seems to be a cause in the shebang.

4.2. Causes and solutions

When logging in as root, it seems that placing pyenv directly under / root (that is, /root/.pyenv) was the cause of the Internal Sever Error. (Isn't / root authorized? It seems that you need to install pyenv system-wide) Installing pyenv in / usr / local / pyenv solved the above problem. So, when installing pyenv from Github, run it with the command below.

# git clone git://github.com/yyuu/pyenv.git /usr/local/pyenv
# git clone git://github.com/yyuu/pyenv-virtualenv.git /usr/local/pyenv/plugins/pyenv-virtualenv

5. From installing Apache and pyenv to running a virtual environment on a web server

From here on, it's an extra section. This section describes a series of steps from installing Apache and pyenv to running a virtual environment of pyenv on a web server.

5.1. Apache (httpd) installation and config settings

Install Apache.

$ sudo yum -y install httpd
$ sudo systemctl start httpd
$ sudo systemctl enable httpd

Execute vim /etc/httpd/conf/httpd.conf and add the following.

/etc/httpd/conf/httpd.conf


<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options ExecCGI
    Require all granted
    AddHandler cgi-script .py .pyc
</Dicrectory>

(The command on the last line is for starting Apache when the OS starts.)

5.2. Disable SELinux

Be careful when editing SELinux config. In the worst case, it seems to cause a kernel panic.

$ getenforce
Disabled

Check if it becomes. If it is not Disabled, you need to edit SELinux.

$ sudo setenforce 0
$ sudo vim /etc/selinux/config

/etc/selinux/config


SELINUX=disabled

5.3. Installing pyenv

Install the prerequisite packages.

$ sudo yum -y install git
$ sudo yum -y group install "Development Tools"
$ sudo yum -y install readline-devel lib-devel bzip2-devel sqlite-devel openssl-devel

Install pyenv from Github.

# git clone git://github.com/yyuu/pyenv.git /usr/local/pyenv
# git clone git://github.com/yyuu/pyenv-virtualenv.git /usr/local/pyenv/plugins/pyenv-virtualenv

In Terminal.

Regarding the creation of virtual environment, let me omit it in this article.

5.4. Run pyenv virtual environment on Apache

later,

/var/www/cgi-bin/sample.py


#!/usr/local/pyenv/versions/{YOUR_VIRTUAL_ENV_NAME}/bin/python

print("Content-Type text/html")
print("")
print("<!DOCTYPE html>")
print("<html>")
print("<head>")
print("    <metacharset='utf-8'>")
print("    <title>SAMPLE</title>")
print("</head>")
print("<body>")
print("    <h1>Hello, world!</h1>")
print("</body>")
print("</html>")

In the directory / var / www / cgi-bin /. Apply {YOUR_VIRTUAL_ENV_NAME} to the name of your virtual environment.

chmod 755 /var/www/cgi-bin/sample.py

And don't forget to change the permissions.

Enter http: // {Server's IP Address} /cgi-bin/sample.py in your browser in the URL search field. When Hello, world! Is displayed, the process is complete.

6. Reference

This is a reference that I quoted and used as a reference when solving problems and contributing. ・ Until Python3 runs as CGI on CentOS7 --Qiita -Install pyenv and rbenv on CentOS system-wide-Qiita ・ "Pien v + er Tsuaen v (Sento S7) -Kuita" (http: // Quiita. Kom / Saito 1978 / Tems / E82421 E29 E118bd397 C)

Recommended Posts

CentOS 7: Enable pyenv with Apache (httpd) CGI
CentOS8 + Apache2.4 + pyenv + mod_wsgi + Django project deployment
CentOS 6.4 with Python 2.7.3 with Apache with mod_wsgi and Django
How to install Apache (httpd) on CentOS7
How to install Apache (httpd) on CentOS8
Play with CentOS 8
pyenv + pyenv-virtualenv (CentOS7)
Run with CentOS7 + Apache2.4 + Python3.6 for the time being
Getting started with apache2
Install python with pyenv
Build python3.x with pyenv
How to install Python2.7 python3.5 with pyenv (on RHEL5 CentOS5) (2016 Nov)
Set up a web server with CentOS7 + Anaconda + Django + Apache
How to start Apache by specifying httpd.conf with systemd (CentOS7, CentOS8)