Run Python on Apache to view InfluxDB data

Introduction

Since I had a lot of trouble running Python on Apache, this post also serves as a memorandum. I'm still a beginner, so if you have any points or advice, please feel free to point out.

Overview

--Procedures for running Python on Apache --Access InfluxDB with Python and display the acquired data in the browser

environment

Apache installation preparation

You can download the Apache installer from the following site.

Apache Lounge : https://www.apachelounge.com/download/

When you unzip the zip file, you will find a file called ʻApache24` in it, so place it in any directory (this time directly under the C drive).

Apache installation

Install Apache at the command prompt. ** That's right, I do it on that black screen. ** You need to open it as an administrator, so enter the following command when it starts.

powershell start-process cmd -verb runas

When executed, it can be opened as an administrator.

Also, if you say "It's a hassle to type a command!", Search as shown in the image below and press "Run as administrator" to get the same result. スクリーンショット (6).png

After executing, set the current directory to C: \ Apache24 \ bin as shown in the image below. スクリーンショット (9).png

Enter httpd -k install here to complete the Apache installation. From now on, enter commands on this screen to start, stop, and restart.

Start: httpd -k start Stop: httpd -k stop Reboot: httpd -k restart

Enter http: // localhost in your browser and it will be OK if ** It works! ** is displayed.

Process Python with Apache

Placing the .py file in Apache's htdocs and running it does not process Python by default. The script is returned as plain text as it is not processed. Therefore, it is necessary to rewrite the configuration file and modify the Python script itself.

Rewriting httpd.conf

There is a file called httpd.conf in C: \ Apache24 \ conf, so open it and rewrite it as follows.

httpd.conf



#LoadModule cgi_module modules/mod_cgi.so
 ↓
LoadModule cgi_module modules/mod_cgi.so

Options Indexes FollowSymLinks
 ↓
Options Indexes FollowSymLinks ExecCGI

#AddHandler cgi-script .cgi
 ↓
AddHandler cgi-script .cgi .py

Restart Apache when you are done.

Python script creation / execution

First of all, let's write a simple script and execute it to check if it can be displayed easily.

hello.py


#!C:/Users/"username"/AppData/Local/Programs/Python/Python37/python.exe
# -*- coding: utf-8 -*-

print("Content-Type: text/html;\n")
print("<h1>hello</h1>")

The first line, #! C: /Users/"username "/AppData/Local/Programs/Python/Python37/python.exe, specifies Python to run on Apache. Follow the path to Python installed on your PC. Be sure to write it as you can't process Python without it. This time, I added a <h1> </ h1> tag to make it easier to distinguish from the case where it is returned as just text. Run this script and if you see hello in bold, you're good to go.

スクリーンショット (17).png If you see something like this, you are successful.

Get and output InfluxDB data

Use the ʻinfluxdbmodule to get InfluxDB data in Python. It is not installed by default, so you need to install it withpip` from the terminal.

pip install influxdb

If you are using an on-campus or corporate network, you may need to specify a proxy server. In that case,

pip install influxdb --proxy="Proxy server:port"

It's okay if you do it.

Now, let's actually get the value of InfluxDB and display it on the browser. I rewrote the hello.py I used earlier.

hello.py


#!C:/Users/username/AppData/Local/Programs/Python/Python37/python.exe
# -*- coding: utf-8 -*-

from influxdb import InfluxDBClient

print("Content-Type: text/html;\n")

client = InfluxDBClient(
    host='hostname',
    port=8086,
    username='root',
    password='root',
    database='Database name'
)

dbq = client.query("select * from test_measurement")
for i in dbq:
    for n in i:
        print(n)
        print("<br>")

When I ran this, the browser displayed: スクリーンショット (22).png The line break <br> is also enabled properly.

Finally

I intend to write the procedure that was executed so that there are no mistakes, but if you have any suggestions or advice, please do not hesitate to tell me. This is the first time I've posted to Qiita, but it's quite difficult to write a sentence like this ...

reference

-Even beginners can do it! How to install Apache -Until you run python with apache

Recommended Posts

Run Python on Apache to view InfluxDB data
Put MicroPython on Windows to run ESP32 on Python
TensorFlow: Run data learned in Python on Android
How to embed mod_wsgi into Apache on Python Windows
Run Openpose on Python (Windows)
[Python] Notes on data analysis
Run Tensorflow 2.x on Python 3.7
Update python on Mac to 3.7-> 3.8
Run Python CGI on CORESERVER
Run unix command on python
How to run Notepad ++ Python
[Python] How to FFT mp3 data
Python 3.6 on Windows ... and to Xamarin.
Preparing to run Flask on EC2
Introduction to Python Hands On Part 1
Run Python on Schedule on AWS Lambda
Run TensorFlow Docker Image on Python3
How to run matplotlib on heroku
Until you run python with apache
Steps to install python3 on mac
Periodically run Python on Heroku Scheduler
Update Python on Mac from 2 to 3
How to install OpenCV on Cloud9 and run it in Python
[Python] View data on new coronavirus infections on a 7-day moving average
Memorandum of understanding when Python is run on EC2 with Apache
Migrate Django applications running on Python 2.7 to Python 3.5
Create a shortcut to run a Python file in VScode on your terminal
Run servo with Python on ESP32 (Windows)
How to read pydoc on python interpreter
[Python] Run Flask on Google App Engine
Data integration from Python app on Linux to Amazon Redshift with ODBC
Convert Excel data to JSON with python
Run AzureKinect in Python on Christmas Eve.
Steps to install Python environment on Ubuntu
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
How to erase Python 2.x on Mac.
Convert FX 1-minute data to 5-minute data with Python
Memorandum on how to use gremlin python
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
How to install Apache (httpd) on CentOS7
[Python] Run Headless Chrome on AWS Lambda
Install python on xserver to use pip
Run Python code on A2019 Community Edition
How to use "deque" for Python data
How to install Apache (httpd) on CentOS8
Compress python data and write to sqlite
Data integration from Python app on Windows to Amazon Redshift with ODBC
How to run Cython on OSX Memo
Run Python in C ++ on Visual Studio 2017
How to run a Maya Python script
[Introduction to Data Scientists] Basics of Python ♬
Books on data science to read in 2020
Run python wsgi server on NGINX Unit
How to run GUI programs such as tkinter in Python environment on WSL2
Periodically run a python program on AWS Lambda
Install and run Python3.5 + NumPy + SciPy on Windows 10
Python C / C ++ Extension Pattern-Pass data to Python as np.array
Run django applications on Windows + Apache + mod_wsgi + services.
[Python] 100 knocks on data science (structured data processing) 018 Explanation
[2020 version] How to install Python3 on AWS EC2
Easy way to use Python 2.7 on Cent OS 6