Home > Backend Development > Python Tutorial > How does Apache support Python?

How does Apache support Python?

爱喝马黛茶的安东尼
Release: 2019-06-18 11:17:50
Original
10038 people have browsed it

How does Apache support Python?

How does Apache support Python?

1. Install apache

If apache is installed, make sure that the configuration has not been significantly modified, otherwise it may be affected. If apache is not installed, install it through apt-get:

$ sudo apt-get install apache2
Copy after login

Tips: If you compile and install it yourself, the configuration and directory mentioned below should be modified according to the actual situation.

Related recommendations: "python video tutorial"

2. Install the mod_python module

This module is embedded With the Python interpreter, Apache can run Python scripts through this module and then output the content to the browser. This module is like a bridge connecting apache and python. The installation is also very simple, apt-get installs directly:

$ sudo apt-get install libapache2-mod-python
Copy after login

After the installation is completed, check /etc/apache2/mods-enabled/python.load, you can see that the module has been loaded, there is no need to do it manually at all Add to.

$ less /etc/apache2/mods-enabled/python.load
LoadModule python_module /usr/lib/apache2/modules/mod_python.so
Copy after login

3. Tell apache to use python to execute when encountering a file with the py suffix

Modify /etc/apache2/sites-enabled/ 000-default configuration file, find the following configuration:

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
Copy after login

If your configuration has not been changed, what you see should be the same as above. Add three lines of configuration in Directory, the final result is as follows:

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
AddHandler mod_python .py
PythonHandler test
PythonDebug On
</Directory>
Copy after login

After saving, restart apache:

$ sudo /etc/init.d/apache2 restart
Copy after login

The environment is now complete, let’s test it below .

Test

Create a new hello.py file under the site root directory /var/www/ with the following content:

from mod_python import apache
def handler(req):
req.write("Hello World!")
return apache.OK
Copy after login

Make sure this The file has execution permission. For convenience, change it directly to 777:

$ chmod 777 hello.py
Use a browser to access the file:

http://localhost/ hello.py
If you see hello world!, it means success.

The above is the detailed content of How does Apache support Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Apache automatically stops
From 1970-01-01 08:00:00
0
0
0
apache restart failed
From 1970-01-01 08:00:00
0
0
0
apache error
From 1970-01-01 08:00:00
0
0
0
How apache logs
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template