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.
--Procedures for running Python on Apache --Access InfluxDB with Python and display the acquired data in the browser
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).
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.
After executing, set the current directory to C: \ Apache24 \ bin
as shown in the image below.
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.
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.
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.
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.
If you see something like this, you are successful.
Use the ʻinfluxdbmodule to get InfluxDB data in Python. It is not installed by default, so you need to install it with
pip` 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:
The line break <br>
is also enabled properly.
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 ...
-Even beginners can do it! How to install Apache -Until you run python with apache
Recommended Posts