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

How does Apache support Python?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-17 18:06:079762browse

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

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

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

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>

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>

After saving, restart apache:

$ sudo /etc/init.d/apache2 restart

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

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!

Statement:
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